Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Phpstan fixes #204

Merged
merged 8 commits into from
Oct 14, 2019
14 changes: 13 additions & 1 deletion phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
parameters:
level: 1
level: max
inferPrivatePropertyTypeFromConstructor: true
paths:
- ./src/
Expand All @@ -17,3 +17,15 @@ parameters:
-
message: '#Class [a-zA-Z0-9\\_]+ not found#'
path: ./src/DependencyInjection/Providers

# The DependencyInjection returns are very complex to deal with
-
message: '#.*NodeParentInterface|null.*#'
path: ./src/DependencyInjection/Providers
-
message: '#.*NodeDefinition::children.*#'
path: ./src/DependencyInjection
# Searches for root(), for backward compatibility with Symfony <4.2. Removed in 5.0
-
message: '#Call to an undefined method Symfony\\Component\\Config\\Definition\\Builder\\TreeBuilder::root\(\)\.#'
path: ./src/DependencyInjection
22 changes: 15 additions & 7 deletions src/Client/ClientRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,23 @@ public function __construct(ContainerInterface $container, array $serviceMap)
*/
public function getClient($key)
{
if (!isset($this->serviceMap[$key])) {
throw new \InvalidArgumentException(sprintf(
'There is no OAuth2 client called "%s". Available are: %s',
$key,
implode(', ', array_keys($this->serviceMap))
));
if (isset($this->serviceMap[$key])) {
$client = $this->container->get($this->serviceMap[$key]);
if (!$client instanceof OAuth2Client) {
throw new \InvalidArgumentException(sprintf(
'Somehow the "%s" client is not an instance of OAuth2Client.',
$key
));
}
alister marked this conversation as resolved.
Show resolved Hide resolved

return $client;
}

return $this->container->get($this->serviceMap[$key]);
throw new \InvalidArgumentException(sprintf(
'There is no OAuth2 client called "%s". Available are: %s',
$key,
implode(', ', array_keys($this->serviceMap))
));
}

/**
Expand Down
12 changes: 6 additions & 6 deletions src/Client/OAuth2Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use League\OAuth2\Client\Token\AccessToken;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Session\SessionInterface;

class OAuth2Client implements OAuth2ClientInterface
{
Expand Down Expand Up @@ -82,7 +83,7 @@ public function redirect(array $scopes = [], array $options = [])
/**
* Call this after the user is redirected back to get the access token.
*
* @return \League\OAuth2\Client\Token\AccessToken
* @return AccessToken|\League\OAuth2\Client\Token\AccessTokenInterface
*
* @throws InvalidStateException
* @throws MissingAuthorizationCodeException
Expand Down Expand Up @@ -131,6 +132,7 @@ public function fetchUserFromToken(AccessToken $accessToken)
*/
public function fetchUser()
{
/** @var AccessToken $token */
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this needed if the method has @return on it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even with the |AccessToken, the AbstractProvider::getAccessToken that OAuth2Client::getAccessToken calls still only returns an AccessTokenInterface. However, OAuth2Client::fetchUserFromToken as well as the AbstractProvider::getResourceOwner that it calls, take the hard classes.

In 7.4 the types can be wider/narrower where it makes sense, but in the meantime, we have to suggest to PHPStan that it's it's also the hard class that fetchUserFromToken expects.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea, that's a mess. I guess checking if it's not an instance of AccessToken and throwing an exception is more honest?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It could only be one of those two, depending on the version and so if the interface was available (and described only in the docblock return types). The tree of code it runs from that point should only be methods described by the Interface, which is perfectly handled by the AccessToken class anyway.

So, the comment does for PHPStan, what PHP 7.4 would be able to do for itself - widening the acceptable type.

$token = $this->getAccessToken();

return $this->fetchUserFromToken($token);
Expand Down Expand Up @@ -161,16 +163,14 @@ private function getCurrentRequest()
}

/**
* @return \Symfony\Component\HttpFoundation\Session\SessionInterface|null
* @return SessionInterface
*/
private function getSession()
{
$session = $this->getCurrentRequest()->getSession();

if (!$session) {
if (!$this->getCurrentRequest()->hasSession()) {
throw new \LogicException('In order to use "state", you must have a session. Set the OAuth2Client to stateless to avoid state');
}

return $session;
return $this->getCurrentRequest()->getSession();
}
}
4 changes: 2 additions & 2 deletions src/Client/Provider/AmazonClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ class AmazonClient extends OAuth2Client
/**
* @param AccessToken $accessToken
*
* @return AmazonResourceOwner
* @return AmazonResourceOwner|\League\OAuth2\Client\Provider\ResourceOwnerInterface
*/
public function fetchUserFromToken(AccessToken $accessToken)
{
return parent::fetchUserFromToken($accessToken);
}

/**
* @return AmazonResourceOwner
* @return AmazonResourceOwner|\League\OAuth2\Client\Provider\ResourceOwnerInterface
*/
public function fetchUser()
{
Expand Down
4 changes: 2 additions & 2 deletions src/Client/Provider/Auth0Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ class Auth0Client extends OAuth2Client
/**
* @param AccessToken $accessToken
*
* @return Auth0ResourceOwner
* @return Auth0ResourceOwner|\League\OAuth2\Client\Provider\ResourceOwnerInterface
*/
public function fetchUserFromToken(AccessToken $accessToken)
{
return parent::fetchUserFromToken($accessToken);
}

/**
* @return Auth0ResourceOwner
* @return Auth0ResourceOwner|\League\OAuth2\Client\Provider\ResourceOwnerInterface
*/
public function fetchUser()
{
Expand Down
4 changes: 2 additions & 2 deletions src/Client/Provider/AzureClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ class AzureClient extends OAuth2Client
/**
* @param AccessToken $accessToken
*
* @return AzureResourceOwner
* @return AzureResourceOwner|\League\OAuth2\Client\Provider\ResourceOwnerInterface
*/
public function fetchUserFromToken(AccessToken $accessToken)
{
return parent::fetchUserFromToken($accessToken);
}

/**
* @return AzureResourceOwner
* @return AzureResourceOwner|\League\OAuth2\Client\Provider\ResourceOwnerInterface
*/
public function fetchUser()
{
Expand Down
4 changes: 2 additions & 2 deletions src/Client/Provider/BitbucketClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ class BitbucketClient extends OAuth2Client
/**
* @param AccessToken $accessToken
*
* @return BitbucketResourceOwner
* @return BitbucketResourceOwner|\League\OAuth2\Client\Provider\ResourceOwnerInterface
*/
public function fetchUserFromToken(AccessToken $accessToken)
{
return parent::fetchUserFromToken($accessToken);
}

/**
* @return BitbucketResourceOwner
* @return BitbucketResourceOwner|\League\OAuth2\Client\Provider\ResourceOwnerInterface
*/
public function fetchUser()
{
Expand Down
4 changes: 2 additions & 2 deletions src/Client/Provider/BoxClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ class BoxClient extends OAuth2Client
/**
* @param AccessToken $accessToken
*
* @return BoxResourceOwner
* @return BoxResourceOwner|\League\OAuth2\Client\Provider\ResourceOwnerInterface
*/
public function fetchUserFromToken(AccessToken $accessToken)
{
return parent::fetchUserFromToken($accessToken);
}

/**
* @return BoxResourceOwner
* @return BoxResourceOwner|\League\OAuth2\Client\Provider\ResourceOwnerInterface
*/
public function fetchUser()
{
Expand Down
4 changes: 2 additions & 2 deletions src/Client/Provider/BufferClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ class BufferClient extends OAuth2Client
/**
* @param AccessToken $accessToken
*
* @return BufferUser
* @return BufferUser|\League\OAuth2\Client\Provider\ResourceOwnerInterface
*/
public function fetchUserFromToken(AccessToken $accessToken)
{
return parent::fetchUserFromToken($accessToken);
}

/**
* @return BufferUser
* @return BufferUser|\League\OAuth2\Client\Provider\ResourceOwnerInterface
*/
public function fetchUser()
{
Expand Down
4 changes: 2 additions & 2 deletions src/Client/Provider/CanvasLMSClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ class CanvasLMSClient extends OAuth2Client
/**
* @param AccessToken $accessToken
*
* @return CanvasLMSResourceOwner
* @return CanvasLMSResourceOwner|\League\OAuth2\Client\Provider\ResourceOwnerInterface
*/
public function fetchUserFromToken(AccessToken $accessToken)
{
return parent::fetchUserFromToken($accessToken);
}

/**
* @return CanvasLMSResourceOwner
* @return CanvasLMSResourceOwner|\League\OAuth2\Client\Provider\ResourceOwnerInterface
*/
public function fetchUser()
{
Expand Down
4 changes: 2 additions & 2 deletions src/Client/Provider/CleverClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ class CleverClient extends OAuth2Client
/**
* @param AccessToken $accessToken
*
* @return CleverUser
* @return CleverUser|\League\OAuth2\Client\Provider\ResourceOwnerInterface
*/
public function fetchUserFromToken(AccessToken $accessToken)
{
return parent::fetchUserFromToken($accessToken);
}

/**
* @return CleverUser
* @return CleverUser|\League\OAuth2\Client\Provider\ResourceOwnerInterface
*/
public function fetchUser()
{
Expand Down
4 changes: 2 additions & 2 deletions src/Client/Provider/DevianArtClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ class DevianArtClient extends OAuth2Client
/**
* @param AccessToken $accessToken
*
* @return DeviantArtResourceOwner
* @return DeviantArtResourceOwner|\League\OAuth2\Client\Provider\ResourceOwnerInterface
*/
public function fetchUserFromToken(AccessToken $accessToken)
{
return parent::fetchUserFromToken($accessToken);
}

/**
* @return DeviantArtResourceOwner
* @return DeviantArtResourceOwner|\League\OAuth2\Client\Provider\ResourceOwnerInterface
*/
public function fetchUser()
{
Expand Down
4 changes: 2 additions & 2 deletions src/Client/Provider/DigitalOceanClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ class DigitalOceanClient extends OAuth2Client
/**
* @param AccessToken $accessToken
*
* @return DigitalOceanResourceOwner
* @return DigitalOceanResourceOwner|\League\OAuth2\Client\Provider\ResourceOwnerInterface
*/
public function fetchUserFromToken(AccessToken $accessToken)
{
return parent::fetchUserFromToken($accessToken);
}

/**
* @return DigitalOceanResourceOwner
* @return DigitalOceanResourceOwner|\League\OAuth2\Client\Provider\ResourceOwnerInterface
*/
public function fetchUser()
{
Expand Down
4 changes: 2 additions & 2 deletions src/Client/Provider/DiscordClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ class DiscordClient extends OAuth2Client
/**
* @param AccessToken $accessToken
*
* @return DiscordResourceOwner
* @return DiscordResourceOwner|\League\OAuth2\Client\Provider\ResourceOwnerInterface
*/
public function fetchUserFromToken(AccessToken $accessToken)
{
return parent::fetchUserFromToken($accessToken);
}

/**
* @return DiscordResourceOwner
* @return DiscordResourceOwner|\League\OAuth2\Client\Provider\ResourceOwnerInterface
*/
public function fetchUser()
{
Expand Down
4 changes: 2 additions & 2 deletions src/Client/Provider/DribbbleClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ class DribbbleClient extends OAuth2Client
/**
* @param AccessToken $accessToken
*
* @return DribbbleResourceOwner
* @return DribbbleResourceOwner|\League\OAuth2\Client\Provider\ResourceOwnerInterface
*/
public function fetchUserFromToken(AccessToken $accessToken)
{
return parent::fetchUserFromToken($accessToken);
}

/**
* @return DribbbleResourceOwner
* @return DribbbleResourceOwner|\League\OAuth2\Client\Provider\ResourceOwnerInterface
*/
public function fetchUser()
{
Expand Down
4 changes: 2 additions & 2 deletions src/Client/Provider/DropboxClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ class DropboxClient extends OAuth2Client
/**
* @param AccessToken $accessToken
*
* @return DropboxResourceOwner
* @return DropboxResourceOwner|\League\OAuth2\Client\Provider\ResourceOwnerInterface
*/
public function fetchUserFromToken(AccessToken $accessToken)
{
return parent::fetchUserFromToken($accessToken);
}

/**
* @return DropboxResourceOwner
* @return DropboxResourceOwner|\League\OAuth2\Client\Provider\ResourceOwnerInterface
*/
public function fetchUser()
{
Expand Down
4 changes: 2 additions & 2 deletions src/Client/Provider/DrupalClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ class DrupalClient extends OAuth2Client
/**
* @param AccessToken $accessToken
*
* @return DrupalResourceOwner
* @return DrupalResourceOwner|\League\OAuth2\Client\Provider\ResourceOwnerInterface
*/
public function fetchUserFromToken(AccessToken $accessToken)
{
return parent::fetchUserFromToken($accessToken);
}

/**
* @return DrupalResourceOwner
* @return DrupalResourceOwner|\League\OAuth2\Client\Provider\ResourceOwnerInterface
*/
public function fetchUser()
{
Expand Down
4 changes: 2 additions & 2 deletions src/Client/Provider/ElanceClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ class ElanceClient extends OAuth2Client
/**
* @param AccessToken $accessToken
*
* @return ElanceResourceOwner
* @return ElanceResourceOwner|\League\OAuth2\Client\Provider\ResourceOwnerInterface
*/
public function fetchUserFromToken(AccessToken $accessToken)
{
return parent::fetchUserFromToken($accessToken);
}

/**
* @return ElanceResourceOwner
* @return ElanceResourceOwner|\League\OAuth2\Client\Provider\ResourceOwnerInterface
*/
public function fetchUser()
{
Expand Down
4 changes: 2 additions & 2 deletions src/Client/Provider/EveOnlineClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ class EveOnlineClient extends OAuth2Client
/**
* @param AccessToken $accessToken
*
* @return EveOnlineResourceOwner
* @return EveOnlineResourceOwner|\League\OAuth2\Client\Provider\ResourceOwnerInterface
*/
public function fetchUserFromToken(AccessToken $accessToken)
{
return parent::fetchUserFromToken($accessToken);
}

/**
* @return EveOnlineResourceOwner
* @return EveOnlineResourceOwner|\League\OAuth2\Client\Provider\ResourceOwnerInterface
*/
public function fetchUser()
{
Expand Down
Loading