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
Merged

Phpstan fixes #204

merged 8 commits into from
Oct 14, 2019

Conversation

alister
Copy link
Contributor

@alister alister commented Oct 5, 2019

A group of individual commits each fixing one distinct issue.

They are:

  • Ignoring one class of error that is harder to check (DI config)
  • Checking that a service exists before using it
  • Asserting particular types are being dealt with
  • Returning the proper type - strings, not bool
  • Docblock additions & tweaks to suggest more generally, what is actually being returned

@alister alister marked this pull request as ready for review October 5, 2019 19:21
@alister alister mentioned this pull request Oct 5, 2019
Copy link
Member

@weaverryan weaverryan left a comment

Choose a reason for hiding this comment

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

Just a few questions/tweaks!

src/Client/ClientRegistry.php Show resolved Hide resolved
src/Client/OAuth2Client.php Outdated Show resolved Hide resolved
@@ -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.

src/DependencyInjection/Configuration.php Outdated Show resolved Hide resolved
src/DependencyInjection/KnpUOAuth2ClientExtension.php Outdated Show resolved Hide resolved
Rordering the condition allows for falling through to throw the
exception on error.
`fetchUserFromToken()` wants the hard class, not the interface.
Leave those changes for later, by using a docblock @var notation
to tweak the interface down to the hard-class it describes
'guard.finish_registration.user_information',
$e->getUserInformation()
);
}
Copy link
Member

Choose a reason for hiding this comment

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

What if not? Probably better to throw an exception else? And the same in a few other places. Wdyt?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

indeed - OAuth2Client::getSession() will throw a \LogicException if there is no session - and it's 'stateless''. Should a session be required everywhere - and how much stateless-ness is needed?

Copy link
Member

Choose a reason for hiding this comment

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

Hmm, in this case, because the user would be calling this method directly (and the method is called saveUserInfoToSession()), if we're unable to actually save to the session because there is none, I think an exception is a good idea

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Taking a look in the Request class, Symfony 4.1 deprecated calling getSession() if there one didn't exist - it's been triggering a notice since then, and 5.0 will throw a BadMethodCallException. Till 4.4 or so though, it was however, still marked as potentially returning null.

I'll make a friendlier error message instead for all the checks with a if (!$request->hasSession()) { throw .... }. ThehasSession() method was in for our minimum-supported version of 3.4.

Check with `hasSession()` (since 3.4), to provide a friendlier message.

Until Symfony v4.4, `getSession()` is also marked as @return ...|null,
so confirm with an additional check of `instanceof SessionInterface`.
@weaverryan weaverryan merged commit 1ecb9f9 into knpuniversity:master Oct 14, 2019
@weaverryan
Copy link
Member

Thank you @alister! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants