Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Problem/Motivation
The method Drupal\Core\Routing\RequestContext::fromRequest() should be return $this
. The class is overriding the Symfony class Symfony\Component\Routing\RequestContext. See: https://github.com/symfony/symfony/blob/4.4/src/Symfony/Component/Routin....
Steps to reproduce
Proposed resolution
Add the statement return $this;
to the method.
Remaining tasks
User interface changes
API changes
Data model changes
Release notes snippet
Comment | File | Size | Author |
---|---|---|---|
#18 | 3233047-18.patch | 1.44 KB | longwave |
#18 | 3233047-18-test-only.patch | 733 bytes | longwave |
Comments
Comment #2
daffie CreditAttribution: daffie commentedThe fix.
Comment #3
longwaveI was going to suggest adding a test, but #3232892: [Symfony 6] Add "static" type hint to the method Drupal\Core\Routing\RequestContext::fromRequest() will ensure that the return type is correct without adding a test (albeit not until PHP 8), so I think this is OK as is.
Comment #4
larowlanWow, this is an odd method.
It is named like a factory (from[Something]) but actually mutates the objects rather than returning a new instance.
My first thought was, well this must be unused, but it is used fairly heavily in core, just that we don't chain it.
Instead of adding a test, we could update one of the existing usages to chain, e.g
\Drupal\Core\Routing\UrlMatcher::finalMatch
would be a good candidateComment #5
daffie CreditAttribution: daffie commentedAdd the change to the method
\Drupal\Core\Routing\UrlMatcher::finalMatch()
. The change is the method only works whenDrupal\Core\Routing\RequestContext::fromRequest()
returns$this
. @larowlan: Good idea.Comment #6
longwaveNeeds
()
around thenew RequestContext()
.edit; and this doesn't test this method return value anyway, you want something like
->fromRequest($request)->setContext($context)
Comment #7
dwwComment #8
Kristen PolThank you for the updated patch.
1. Read through the issue and comments.
2. Looked at the interdiff and see that the changes in #7 address the feedback in #6.
3. Tests pass and no new tests are to be added based on the discussion above.
4. Reviewed the patch itself and it seems clear and addresses the issue in the issue summary.
5. No obvious way to test this manually so relying on automated tests.
Marking RTBC. Thanks, all!
Comment #9
larowlanIssue credits
Comment #10
larowlanCommitted ed5384a and pushed to 9.3.x. Thanks!
Comment #12
daffie CreditAttribution: daffie commentedMaybe I am missing the point, but is this change not the wrong one? Where does the parameter value for the method
setContext()
come from? The value of$context
only exists after this line. Should it not be:The method
fromRequest()
returns itself. This is an instance of Drupal\Core\Routing\RequestContext. In the call$this->setContext($context);
is$this
an instance of Drupal\Core\Routing\UrlMatcher.Comment #13
larowlanYeah, you're right @daffie
Comment #15
daffie CreditAttribution: daffie commentedComment #16
longwaveSorry, that was me misreading the code in #6.
But this begs the question: why does #7 not fail? And it seems the answer is that this is dead code.
finalMatch
is not part of the interface, and this is the only instance of this string in core.I think we need a followup to remove this method, and to find another suitable candidate to test this change in this issue.
Comment #17
longwaveFiled #3238199: \Drupal\Core\Routing\UrlMatcher is dead code
Comment #18
longwavePathValidator uses
RequestContext::fromRequest()
so we can rework that instead.Also refactored
RequestContext::fromRequest()
to just return the result of the parent call.Comment #19
daffie CreditAttribution: daffie commentedLooks good to me.
Comment #21
catchCommitted 4c0d316 and pushed to 9.3.x. Thanks!