make MethodNameReplacerRector work only with method related nodes

This commit is contained in:
Tomas Votruba 2018-12-10 15:45:35 +01:00
parent 262e2e7cc6
commit f1f2473519

View File

@ -3,7 +3,10 @@
namespace Rector\Rector\MethodCall;
use PhpParser\Node;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Identifier;
use PhpParser\Node\Stmt\ClassMethod;
use Rector\NodeTypeResolver\Node\Attribute;
use Rector\Rector\AbstractRector;
use Rector\RectorDefinition\ConfiguredCodeSample;
@ -65,6 +68,9 @@ CODE_SAMPLE
public function refactor(Node $node): ?Node
{
$parentNode = $node->getAttribute(Attribute::PARENT_NODE);
if (! $this->isInstanceOf($parentNode, [MethodCall::class, StaticCall::class, ClassMethod::class])) {
return null;
}
foreach ($this->oldToNewMethodsByClass as $type => $oldToNewMethods) {
if (! $this->isType($parentNode, $type)) {
@ -83,4 +89,18 @@ CODE_SAMPLE
return $node;
}
/**
* @param string[] $nodeClasses
*/
private function isInstanceOf(Node $node, array $nodeClasses): bool
{
foreach ($nodeClasses as $nodeClass) {
if (is_a($node, $nodeClass, true)) {
return true;
}
}
return false;
}
}