Skip to content

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
wants to merge 1 commit into from

Conversation

ruudk
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.

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.
@ruudk
Copy link
Contributor Author

ruudk commented Jan 14, 2022

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

@lookyman
Copy link
Collaborator

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

@ruudk
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.

@lookyman
Copy link
Collaborator

lookyman commented Jan 14, 2022

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.

@ruudk
Copy link
Contributor Author

ruudk commented Jan 14, 2022

@ruudk
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.

@lookyman
Copy link
Collaborator

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

ondrejmirtes added a commit that referenced this pull request Jan 16, 2022
@ondrejmirtes
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 ruudk deleted the request-get-session branch January 16, 2022 09:00
@ruudk
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

@ondrejmirtes
Copy link
Member

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

@VincentLanglet
Copy link
Contributor

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

@ondrejmirtes
Copy link
Member

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

@VincentLanglet
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.

@ondrejmirtes
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
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants