[PHPUnit] SpecificMethodRector - add support for empty

This commit is contained in:
TomasVotruba 2017-12-19 13:43:37 +01:00
parent 408d883874
commit 053d5d55bb

View File

@ -3,6 +3,7 @@
namespace Rector\Rector\Contrib\PHPUnit; namespace Rector\Rector\Contrib\PHPUnit;
use PhpParser\Node; use PhpParser\Node;
use PhpParser\Node\Expr\Empty_;
use PhpParser\Node\Expr\FuncCall; use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\MethodCall; use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Identifier; use PhpParser\Node\Identifier;
@ -22,7 +23,7 @@ final class SpecificMethodRector extends AbstractRector
/** /**
* @var string[][]|false[][] * @var string[][]|false[][]
*/ */
private $oldToNewMethods = [ private static $oldToNewMethods = [
'is_readable' => ['assertIsReadable', 'assertNotIsReadable'], 'is_readable' => ['assertIsReadable', 'assertNotIsReadable'],
'array_key_exists' => ['assertArrayHasKey', 'assertArrayNotHasKey'], 'array_key_exists' => ['assertArrayHasKey', 'assertArrayNotHasKey'],
'empty' => ['assertEmpty', 'assertNotEmpty'], 'empty' => ['assertEmpty', 'assertNotEmpty'],
@ -67,15 +68,13 @@ final class SpecificMethodRector extends AbstractRector
} }
$firstArgumentValue = $node->args[0]->value; $firstArgumentValue = $node->args[0]->value;
if (! $firstArgumentValue instanceof FuncCall) {
$funcCallName = $this->resolveFunctionName($firstArgumentValue);
if ($funcCallName === null) {
return false; return false;
} }
/** @var Name $nameNode */ if (! isset(self::$oldToNewMethods[$funcCallName])) {
$nameNode = $firstArgumentValue->name;
$funcCallName = $nameNode->toString();
if (! isset($this->oldToNewMethods[$funcCallName])) {
return false; return false;
} }
@ -101,12 +100,14 @@ final class SpecificMethodRector extends AbstractRector
$identifierNode = $methodCallNode->name; $identifierNode = $methodCallNode->name;
$oldMethodName = $identifierNode->toString(); $oldMethodName = $identifierNode->toString();
[$trueMethodName, $falseMethodName] = $this->oldToNewMethods[$this->activeFuncCallName]; [$trueMethodName, $falseMethodName] = self::$oldToNewMethods[$this->activeFuncCallName];
if ($oldMethodName === 'assertTrue' && $trueMethodName) { if ($oldMethodName === 'assertTrue' && $trueMethodName) {
$methodCallNode->name = $trueMethodName; /** @var string $trueMethodName */
$methodCallNode->name = new Identifier($trueMethodName);
} elseif ($oldMethodName === 'assertFalse' && $falseMethodName) { } 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; $funcCall = $methodCallNode->args[0]->value;
$methodCallNode->args[0] = $funcCall->args[0]; $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;
}
} }