Merge pull request #200 from rectorphp/fix-array-key-exists

[PHPUnit] Fix array_key_exists() argument up-lifting
This commit is contained in:
Tomáš Votruba 2017-12-19 22:29:27 +01:00 committed by GitHub
commit c4dc955661
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

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\Arg;
use PhpParser\Node\Expr\Empty_; 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;
@ -52,8 +53,6 @@ final class SpecificMethodRector extends AbstractRector
public function isCandidate(Node $node): bool public function isCandidate(Node $node): bool
{ {
$this->activeFuncCallName = null;
if (! $this->methodCallAnalyzer->isTypesAndMethods( if (! $this->methodCallAnalyzer->isTypesAndMethods(
$node, $node,
['PHPUnit\Framework\TestCase', 'PHPUnit_Framework_TestCase'], ['PHPUnit\Framework\TestCase', 'PHPUnit_Framework_TestCase'],
@ -89,7 +88,7 @@ final class SpecificMethodRector extends AbstractRector
public function refactor(Node $methodCallNode): ?Node public function refactor(Node $methodCallNode): ?Node
{ {
$this->renameMethod($methodCallNode); $this->renameMethod($methodCallNode);
$this->moveArgumentUp($methodCallNode); $this->moveFunctionArgumentsUp($methodCallNode);
return $methodCallNode; return $methodCallNode;
} }
@ -111,15 +110,26 @@ final class SpecificMethodRector extends AbstractRector
} }
} }
private function moveArgumentUp(MethodCall $methodCallNode): void /**
* Before:
* - $this->assertTrue(array_key_exists('...', ['...']), 'second argument');
*
* After:
* - $this->assertArrayHasKey('...', ['...'], 'second argument');
*/
private function moveFunctionArgumentsUp(MethodCall $methodCallNode): void
{ {
$funcCallOrEmptyNode = $methodCallNode->args[0]->value; $funcCallOrEmptyNode = $methodCallNode->args[0]->value;
if ($funcCallOrEmptyNode instanceof FuncCall) { if ($funcCallOrEmptyNode instanceof FuncCall) {
$methodCallNode->args[0] = $funcCallOrEmptyNode->args[0]; $oldArguments = $methodCallNode->args;
unset($oldArguments[0]);
$newArguments = array_merge($funcCallOrEmptyNode->args, $oldArguments);
$methodCallNode->args = $newArguments;
} }
if ($funcCallOrEmptyNode instanceof Empty_) { if ($funcCallOrEmptyNode instanceof Empty_) {
$methodCallNode->args[0] = $funcCallOrEmptyNode->expr; $methodCallNode->args[0] = new Arg($funcCallOrEmptyNode->expr);
} }
} }