From 053d5d55bb591db02220c60f6f4b90318a548748 Mon Sep 17 00:00:00 2001 From: TomasVotruba Date: Tue, 19 Dec 2017 13:43:37 +0100 Subject: [PATCH] [PHPUnit] SpecificMethodRector - add support for empty --- .../Contrib/PHPUnit/SpecificMethodRector.php | 37 ++++++++++++++----- 1 file changed, 27 insertions(+), 10 deletions(-) diff --git a/src/Rector/Contrib/PHPUnit/SpecificMethodRector.php b/src/Rector/Contrib/PHPUnit/SpecificMethodRector.php index 26f5a4ea160..af745bb7478 100644 --- a/src/Rector/Contrib/PHPUnit/SpecificMethodRector.php +++ b/src/Rector/Contrib/PHPUnit/SpecificMethodRector.php @@ -3,6 +3,7 @@ namespace Rector\Rector\Contrib\PHPUnit; use PhpParser\Node; +use PhpParser\Node\Expr\Empty_; use PhpParser\Node\Expr\FuncCall; use PhpParser\Node\Expr\MethodCall; use PhpParser\Node\Identifier; @@ -22,7 +23,7 @@ final class SpecificMethodRector extends AbstractRector /** * @var string[][]|false[][] */ - private $oldToNewMethods = [ + private static $oldToNewMethods = [ 'is_readable' => ['assertIsReadable', 'assertNotIsReadable'], 'array_key_exists' => ['assertArrayHasKey', 'assertArrayNotHasKey'], 'empty' => ['assertEmpty', 'assertNotEmpty'], @@ -67,15 +68,13 @@ final class SpecificMethodRector extends AbstractRector } $firstArgumentValue = $node->args[0]->value; - if (! $firstArgumentValue instanceof FuncCall) { + + $funcCallName = $this->resolveFunctionName($firstArgumentValue); + if ($funcCallName === null) { return false; } - /** @var Name $nameNode */ - $nameNode = $firstArgumentValue->name; - - $funcCallName = $nameNode->toString(); - if (! isset($this->oldToNewMethods[$funcCallName])) { + if (! isset(self::$oldToNewMethods[$funcCallName])) { return false; } @@ -101,12 +100,14 @@ final class SpecificMethodRector extends AbstractRector $identifierNode = $methodCallNode->name; $oldMethodName = $identifierNode->toString(); - [$trueMethodName, $falseMethodName] = $this->oldToNewMethods[$this->activeFuncCallName]; + [$trueMethodName, $falseMethodName] = self::$oldToNewMethods[$this->activeFuncCallName]; if ($oldMethodName === 'assertTrue' && $trueMethodName) { - $methodCallNode->name = $trueMethodName; + /** @var string $trueMethodName */ + $methodCallNode->name = new Identifier($trueMethodName); } elseif ($oldMethodName === 'assertFalse' && $falseMethodName) { - $methodCallNode->name = $falseMethodName; + /** @var string $falseMethodName */ + $methodCallNode->name = new Identifier($falseMethodName); } } @@ -116,4 +117,20 @@ final class SpecificMethodRector extends AbstractRector $funcCall = $methodCallNode->args[0]->value; $methodCallNode->args[0] = $funcCall->args[0]; } + + private function resolveFunctionName(Node $node): ?string + { + if ($node instanceof FuncCall) { + /** @var Name $nameNode */ + $nameNode = $node->name; + + return $nameNode->toString(); + } + + if ($node instanceof Empty_) { + return 'empty'; + } + + return null; + } }