Skip to content

Revert wrong typehint for getSession return type #324

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

Merged
merged 1 commit into from
Jan 15, 2023

Conversation

VincentLanglet
Copy link
Contributor

TLDR: This would be one way to closes #323, and IMHO it's the best one because

  • it's correct in 100% of the time for Symfony users and promotes to right correct code.
  • People who doesn't care about the Session/SessionInterface can ignore the error.
  • People who care about the Session/SessionInterface are not dealing with wrong errors.

This type hint was introduced by
012305d
which was done to replace the original PR
#233 cc @ruudk

As explained in symfony/symfony#39222 (comment)
or symfony/symfony#41765 (comment):

It was said by @ruudk

For a small set of users it could mean that phpstan now falsely allows getFlashBag on the request session, because they have a custom session implementation. They prob didn't call the flashbag anyway, as it doesn't work.

But this is not the real problem of the stub.
The real issue is that if you use service decoration, or write correctly your calls to support multiple sessions, like

$session = $request->getSession();
if ($session instanceof FlashBagAwareSessionInterface) {
     $session->getFlashBag()->addMessage('Foo');
} elseif ($session instanceof MyCustomSessionInterface) {
     $session->doThings();
} else {
     // ...
}

You ended up with false positive from phpstan about always true instanceof or always false instanceof.
This is way better to have a "false negative" (`getFlashBag() doesn't exist in SessionInterface) than introducing false positive.

If people doesn't want to write correct code about Symfony session, they should ignore the error

ignoreErros:
     - '#Call to an undefined method Symfony\\Component\\HttpFoundation\\Session\\SessionInterface::getFlashBag\(\)#'

rather than introducing a wrong stub.

Closes #323

@ruudk
Copy link
Contributor

ruudk commented Jan 15, 2023

I looked at it again, I think that retrieving it from the Request is coming from the early days. Now with ArgumentValueResolvers this can be done simpler:

public function myAction(Session $session) : array
{
    $session->getFlashBag()->add('success', 'Hi!');
}

or

public function myAction(FlashBagAwareSessionInterface $session) : array
{
    $session->getFlashBag()->add('success', 'Hi!');
}

This code doesn't require any instanceof checks.

So I'm 👍 with shipping this PR.

@ondrejmirtes ondrejmirtes merged commit d89a521 into phpstan:1.2.x Jan 15, 2023
@ondrejmirtes
Copy link
Member

Thank you both!

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.

Session/SessionInterface inconsistency
3 participants