Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Request > Return Session instead of SessionInterface #233

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
ruudk wants to merge 1 commit into phpstan:master from ruudk:request-get-session

Conversation

Copy link
Contributor

@ruudk ruudk commented Jan 11, 2022

Symfony 6 removed the Session and FlashBagInterface services.

That means that the only way to get the FlashBag (to add flashes) it to use $request->getSession()->getFlashBag().

But the problem is, getFlashBag is not defined on the SessionInterface:

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

To make everyone's life easier, let's change the return type to Sesssion instead.
This is what's returned in a default Symfony application.

lookyman reacted with thumbs up emoji
Symfony 6 removed the `Session` and `FlashBagInterface` services.
That means that the only way to get the FlashBag (to add flashes) it to use `$request->getSession()->getFlashBag()`.
But the problem is, `getFlashBag` is not defined on the `SessionInterface`:
```
Call to an undefined method Symfony\Component\HttpFoundation\Session\SessionInterface::getFlashBag().
```
To make everyone's life easier, let's change the return type to `Sesssion` instead.
This is what's returned in a default Symfony application.
Copy link
Contributor Author

ruudk commented Jan 14, 2022

@ondrejmirtes Does this make sense? Or should it be done differently?

Copy link
Collaborator

It makes sense, but still, it's technically straight up lying about the return type. I don't really like it.

Copy link
Contributor Author

ruudk commented Jan 14, 2022

But it means that all code needs to do an assertion check. For example, right now I have 40 errors because getFlashBag does not exist on session on request.

I thought these extensions should solve friction like this.

Copy link
Collaborator

lookyman commented Jan 14, 2022
edited
Loading

Can we somehow find out about the real return type from the service map? EDIT Right, we can't, because they removed the service. Hmm.

Copy link
Contributor Author

ruudk commented Jan 14, 2022

Copy link
Contributor Author

ruudk commented Jan 14, 2022

I think the suggested change makes a lot of sense because:

  • this is a symfony plugin;
  • almost all symfony users have the normal session configuration, meaning request session returns the session object.

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. And if they implemented it, great. If needed, and those complain, we can add a toggle to disable it for them.

Copy link
Collaborator

You know what, after some digging in Symfony's code, I think you are right. Lets merge this.

ruudk reacted with hooray emoji

Copy link
Member

Hi, I reused your test. Since the return type wasn't really dynamic, the same result can be achieved with stub files: 012305d

Thank you.

ruudk reacted with thumbs up emoji

@ruudk ruudk deleted the request-get-session branch January 16, 2022 09:00
Copy link
Contributor Author

ruudk commented Jan 16, 2022

Interesting, that was my first try but it didn't seem to pick it up. I read the code as : if request class does not exist then use stub

Copy link
Member

Nope, that's not how stub files work. They're purely for overriding PHPDocs, nothing else.

ruudk reacted with thumbs up emoji

Copy link
Contributor

@ondrejmirtes Hi, I'm curious ; is it possible to make this return type a Benevolent<Session|SessionInterface> with phpdoc ?

Copy link
Member

If Session implements SessionInterface, then Session|SessionInterface is just SessionInterface. Thanks to https://phpstan.org/developing-extensions/type-system#type-normalization.

Copy link
Contributor

If Session implements SessionInterface, then Session|SessionInterface is just SessionInterface. Thanks to https://phpstan.org/developing-extensions/type-system#type-normalization.

Oh I see...

So there is no way to add phpdoc in a way that

  • $session->getFlashbag() is not reported because it exists in Session
  • $session instanceof Session is not reported as always true because it can be a SessionInterface.

Copy link
Member

Both can't be true at the same time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Reviewers
No reviews
Assignees
No one assigned
Labels
None yet
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

AltStyle によって変換されたページ (->オリジナル) /