Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 27 additions & 7 deletions packages/Symfony/src/Rector/HttpKernel/GetRequestRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,11 @@ private function isGetRequestInAction(Node $node): bool
return false;
}

// must be $this->getRequest() in controller
if (! $this->isName($node->var, 'this')) {
return false;
}

if (! $this->isName($node, 'getRequest') && ! $this->isGetMethodCallWithRequestParameters($node)) {
return false;
}
Expand All @@ -146,13 +151,8 @@ private function isActionWithGetRequestInBody(Node $node): bool
if (! $this->controllerMethodAnalyzer->isAction($node)) {
return false;
}

// "$this->getRequest()"
$isGetRequestMethod = (bool) $this->betterNodeFinder->find($node, function (Node $node): bool {
return $this->isName($node, 'getRequest');
});

if ($isGetRequestMethod) {
$containsGetRequestMethod = $this->containsGetRequestMethod($node);
if ($containsGetRequestMethod) {
return true;
}

Expand All @@ -163,6 +163,10 @@ private function isActionWithGetRequestInBody(Node $node): bool
return false;
}

if (! $this->isName($node->var, 'this')) {
return false;
}

return $this->isName($node, 'get');
});

Expand Down Expand Up @@ -194,4 +198,20 @@ private function isGetMethodCallWithRequestParameters(MethodCall $methodCall): b

return $stringValue->value === 'request';
}

private function containsGetRequestMethod(Node $node): bool
{
// "$this->getRequest()"
return (bool) $this->betterNodeFinder->find($node, function (Node $node): bool {
if (! $node instanceof MethodCall) {
return false;
}

if (! $this->isName($node->var, 'this')) {
return false;
}

return $this->isName($node, 'getRequest');
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php declare (strict_types=1);

namespace Rector\Symfony\Tests\Rector\HttpKernel\GetRequestRector\Fixture;

use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;

final class SkipEventSubscriber
{
public function onKernelException(GetResponseForExceptionEvent $event)
{
$request = $event->getRequest();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public function test(): void
__DIR__ . '/Fixture/fixture.php.inc',
__DIR__ . '/Fixture/fixture2.php.inc',
__DIR__ . '/Fixture/fixture3.php.inc',
__DIR__ . '/Fixture/skip_event_subscriber.php.inc',
]);
}

Expand Down