From 27a824d9c2cf34eb4f277ac9b0906edcfde2ddd3 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Sat, 15 Jun 2019 15:37:53 +0200 Subject: [PATCH] [Symfony] Fix GetRequestRector overlap to non-controllers --- .../Rector/HttpKernel/GetRequestRector.php | 34 +++++++++++++++---- .../Fixture/skip_event_subscriber.php.inc | 13 +++++++ .../GetRequestRector/GetRequestRectorTest.php | 1 + 3 files changed, 41 insertions(+), 7 deletions(-) create mode 100644 packages/Symfony/tests/Rector/HttpKernel/GetRequestRector/Fixture/skip_event_subscriber.php.inc diff --git a/packages/Symfony/src/Rector/HttpKernel/GetRequestRector.php b/packages/Symfony/src/Rector/HttpKernel/GetRequestRector.php index c50c3d85a7c2..bd2848ae5f08 100644 --- a/packages/Symfony/src/Rector/HttpKernel/GetRequestRector.php +++ b/packages/Symfony/src/Rector/HttpKernel/GetRequestRector.php @@ -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; } @@ -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; } @@ -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'); }); @@ -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'); + }); + } } diff --git a/packages/Symfony/tests/Rector/HttpKernel/GetRequestRector/Fixture/skip_event_subscriber.php.inc b/packages/Symfony/tests/Rector/HttpKernel/GetRequestRector/Fixture/skip_event_subscriber.php.inc new file mode 100644 index 000000000000..24df7b97effe --- /dev/null +++ b/packages/Symfony/tests/Rector/HttpKernel/GetRequestRector/Fixture/skip_event_subscriber.php.inc @@ -0,0 +1,13 @@ +getRequest(); + } +} diff --git a/packages/Symfony/tests/Rector/HttpKernel/GetRequestRector/GetRequestRectorTest.php b/packages/Symfony/tests/Rector/HttpKernel/GetRequestRector/GetRequestRectorTest.php index 81a1a5e91a36..76037616faa3 100644 --- a/packages/Symfony/tests/Rector/HttpKernel/GetRequestRector/GetRequestRectorTest.php +++ b/packages/Symfony/tests/Rector/HttpKernel/GetRequestRector/GetRequestRectorTest.php @@ -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', ]); }