Skip to content

SessionInterface::getFlashBag() error reported by PHP Stan #39222

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

Closed
DavidGarciaCat opened this issue Nov 29, 2020 · 6 comments
Closed

SessionInterface::getFlashBag() error reported by PHP Stan #39222

DavidGarciaCat opened this issue Nov 29, 2020 · 6 comments

Comments

@DavidGarciaCat
Copy link

DavidGarciaCat commented Nov 29, 2020

Symfony version(s) affected:

Both 4.x and 5.x

Description

Symfony cannot inject by default the Session service because it's not mapped as a service, so we need to inject the SessionInterface. However, PHP Stan reports an error when calling getFlashBag()

Call to an undefined method Symfony\Component\HttpFoundation\Session\SessionInterface::getFlashBag().

How to reproduce

Inject the SessionInterface and then call the getFlashBag() method. Finally, run vendor/bin/phpstan analyse src -l 2 for a level 2 analysis, where we should get that error message.

Possible Solution

Define the getFlashBag() method into the SessionInterface.

Alternatively, maybe you can give another option to pass this error reported by PHP Stan without adding extra annotations to ignore these lines once after another when they are in use in several services.

@jderusse
Copy link
Member

If you call getFlashBag on a SessionInterface the issue is in your code. Not all implementation of SessionInterface provides this method.

duplicate of #20258, #11279, #10036 , #5568
and probably many others

@jderusse
Copy link
Member

note: You can fix it by adding if ($session instance of Session) in your code.

@DavidGarciaCat
Copy link
Author

Isn't a redundancy checking if the injected interface is an instance of the only service that (by default) implements it and is defined in the DI mapping?

@wouterj
Copy link
Member

wouterj commented Nov 29, 2020

Nobody promises that your class will be instantiated by the Symfony DI mapping. Of course, that is the case in the normal path (and it's very likely to always be the case), but the point of static analysis is exactly to avoid issues if this unexpected situation does occur. If you don't like that, I recommend tweaking your PHPstan configuration to no longer complain about this issue (or add PHPdocs to indicate that you trust that it's always a Session instance).

@grogy
Copy link

grogy commented Aug 10, 2022

As write doc we can use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface as dependency. It works great 👍

@DavidGarciaCat
Copy link
Author

Thanks, @grogy - funny thing no one mentioned it until now 😄 and I was not aware of this interface

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

No branches or pull requests

5 participants