diff --git a/rules/symfony/src/Rector/MethodCall/SimplifyWebTestCaseAssertionsRector.php b/rules/symfony/src/Rector/MethodCall/SimplifyWebTestCaseAssertionsRector.php index 226eb71a9828..1b60f3f5540a 100644 --- a/rules/symfony/src/Rector/MethodCall/SimplifyWebTestCaseAssertionsRector.php +++ b/rules/symfony/src/Rector/MethodCall/SimplifyWebTestCaseAssertionsRector.php @@ -8,7 +8,6 @@ use PhpParser\Node\Arg; use PhpParser\Node\Expr\MethodCall; use PhpParser\Node\Expr\PropertyFetch; -use PhpParser\Node\Expr\Variable; use PhpParser\Node\Scalar\LNumber; use PhpParser\Node\Scalar\String_; use PhpParser\Node\Stmt\Expression; @@ -25,11 +24,6 @@ */ final class SimplifyWebTestCaseAssertionsRector extends AbstractRector { - /** - * @var string - */ - private const THIS = 'this'; - /** * @var string */ @@ -40,12 +34,6 @@ final class SimplifyWebTestCaseAssertionsRector extends AbstractRector */ private $getStatusCodeMethodCall; - public function __construct() - { - $clientGetResponse = new MethodCall(new Variable('client'), 'getResponse'); - $this->getStatusCodeMethodCall = new MethodCall($clientGetResponse, 'getStatusCode'); - } - public function getDefinition(): RectorDefinition { return new RectorDefinition('Simplify use of assertions in WebTestCase', [ @@ -111,6 +99,9 @@ public function getNodeTypes(): array */ public function refactor(Node $node): ?Node { + $clientGetResponse = $this->createMethodCall('client', 'getResponse'); + $this->getStatusCodeMethodCall = $this->createMethodCall($clientGetResponse, 'getStatusCode'); + if (! $this->isInWebTestCase($node)) { return null; } @@ -119,9 +110,9 @@ public function refactor(Node $node): ?Node $args = []; $args[] = new Arg(new LNumber(200)); $args[] = new Arg($this->getStatusCodeMethodCall); - $match = new MethodCall(new Variable(self::THIS), self::ASSERT_SAME, $args); + $match = $this->createLocalMethodCall(self::ASSERT_SAME, $args); if ($this->areNodesEqual($node, $match)) { - return new MethodCall(new Variable(self::THIS), 'assertResponseIsSuccessful'); + return $this->createLocalMethodCall('assertResponseIsSuccessful'); } // assertResponseStatusCodeSame @@ -133,7 +124,7 @@ public function refactor(Node $node): ?Node // assertSelectorTextContains $args = $this->matchAssertContainsCrawlerArg($node); if ($args !== null) { - return new MethodCall(new Variable(self::THIS), 'assertSelectorTextContains', $args); + return $this->createLocalMethodCall('assertSelectorTextContains', $args); } // 3. assertResponseRedirects @@ -171,7 +162,7 @@ private function processAssertResponseStatusCodeSame(Node $node): ?MethodCall return null; } - return new MethodCall(new Variable(self::THIS), 'assertResponseStatusCodeSame', [$node->args[0]]); + return $this->createLocalMethodCall('assertResponseStatusCodeSame', [$node->args[0]]); } /** @@ -224,13 +215,12 @@ private function processAssertResponseRedirects(MethodCall $methodCall): ?Node $args[] = new Arg(new LNumber(301)); $args[] = new Arg($this->getStatusCodeMethodCall); - $match = new MethodCall(new Variable(self::THIS), self::ASSERT_SAME, $args); + $match = $this->createLocalMethodCall(self::ASSERT_SAME, $args); if ($this->areNodesEqual($previousNode, $match)) { - $clientGetLocation = new MethodCall(new PropertyFetch(new MethodCall( - new Variable('client'), - 'getResponse' - ), 'headers'), 'get', [new Arg(new String_('Location'))]); + $getResponseMethodCall = $this->createMethodCall('client', 'getResponse'); + $propertyFetch = new PropertyFetch($getResponseMethodCall, 'headers'); + $clientGetLocation = $this->createMethodCall($propertyFetch, 'get', [new Arg(new String_('Location'))]); if (! isset($methodCall->args[1])) { return null; @@ -243,7 +233,7 @@ private function processAssertResponseRedirects(MethodCall $methodCall): ?Node $this->removeNode($previousNode); - return new MethodCall(new Variable(self::THIS), 'assertResponseRedirects', $args); + return $this->createLocalMethodCall('assertResponseRedirects', $args); } } diff --git a/src/Rector/AbstractRector/NodeFactoryTrait.php b/src/Rector/AbstractRector/NodeFactoryTrait.php index 5a63d7305a69..52b21d882399 100644 --- a/src/Rector/AbstractRector/NodeFactoryTrait.php +++ b/src/Rector/AbstractRector/NodeFactoryTrait.php @@ -132,7 +132,7 @@ protected function createMethodCall($variable, string $method, array $arguments protected function createLocalMethodCall(string $method, array $arguments = []): MethodCall { $thisVariable = new Variable('this'); - return $this->createMethodCall($thisVariable, $method, $arguments); + return $this->nodeFactory->createMethodCall($thisVariable, $method, $arguments); } /**