-
-
Notifications
You must be signed in to change notification settings - Fork 168
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
Use requestStack
to access current request when needed.
#3132
Conversation
Hi @vlad-ghita, At first I though there might be danger of infinite recursion here? public function getRequest(): Request
{
// always try to use current request
if ($this->request === null) {
$this->request = $this->requestStack->getCurrentRequest() ?? Request::createFromGlobals();
$this->init();
}
return $this->request;
}
public function init(): void
{
// Ensure in request cycle (even for override).
if ($this->getRequest() === null || $this->getRequest()->getHost() === '') {
return;
} But, that's not true, because PHPstan is complaining about it, though.. About it "will always evaluate to false". And i think it's correct. It looks like we can simplify it like this, by removing the bit that's "always false" public function init(): void
{
// Ensure in request cycle (even for override).
if ($this->getRequest()->getHost() === '') {
return;
} Would that work for you? |
src/Canonical.php
Outdated
} | ||
|
||
public function init(): void | ||
{ | ||
// Ensure in request cycle (even for override). | ||
if ($this->request === null || $this->request->getHost() === '') { | ||
if ($this->getRequest() === null || $this->getRequest()->getHost() === '') { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix PHPStan's remark:
Strict comparison using === between Symfony\Component\HttpFoundation\Request and null will always evaluate to false.
if ($this->getRequest() === null || $this->getRequest()->getHost() === '') { | |
if ($this->getRequest()->getHost() === '') { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed with possible infinite loop.
I aimed to keep init()
for backwards compatibility.
But maybe we should simply ditch it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See latest changes.
I added a request setter.
Please re-run checks.
My tests passed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good now, thanks @vlad-ghita 👍
I'll merge in after tests are done!
requestStack
to access current request when needed.
Storing current request in constructor isn't a good idea.
In general, it works (eg: when request from browser or most tests).
But not always.
EG: when test extends WebTestCase and login is executed through
$this->loginUser()
, then container is built without an active request.Thus, request is null in constructors ...
Proper fix implies storing
$requestStack
and accessing current request when required.This PR fixes Canonical and FrontendMenu (these failed in my use-case, so far).