diff --git a/src/NodeAnalyzer/ClassMethodAnalyzer.php b/src/NodeAnalyzer/ClassMethodAnalyzer.php new file mode 100644 index 00000000000..41ccf7c404e --- /dev/null +++ b/src/NodeAnalyzer/ClassMethodAnalyzer.php @@ -0,0 +1,39 @@ +isType($node, $type)) { + return false; + } + + /** @var MethodCall $node */ + return in_array($node->name->toString(), $methods, true); + } + + private function isType(Node $node, string $type): bool + { + if (! $node instanceof ClassMethod) { + return false; + } + + $nodeTypes = (array) $node->getAttributes(Attribute::TYPES); + + return in_array($type, $nodeTypes, true); + } + +} diff --git a/src/Rector/Dynamic/ArgumentReplacerRector.php b/src/Rector/Dynamic/ArgumentReplacerRector.php index 3c4dd9cbdcf..d994cac7323 100644 --- a/src/Rector/Dynamic/ArgumentReplacerRector.php +++ b/src/Rector/Dynamic/ArgumentReplacerRector.php @@ -7,6 +7,7 @@ use PhpParser\Node; use PhpParser\Node\Expr\MethodCall; use PhpParser\Node\Expr\StaticCall; use PhpParser\Node\Stmt\ClassMethod; +use Rector\NodeAnalyzer\ClassMethodAnalyzer; use Rector\NodeAnalyzer\MethodCallAnalyzer; use Rector\Rector\AbstractRector; @@ -26,14 +27,22 @@ final class ArgumentReplacerRector extends AbstractRector * @var mixed[]|null */ private $activeArgumentChangesByPosition; + /** + * @var ClassMethodAnalyzer + */ + private $classMethodAnalyzer; /** * @param mixed[] $argumentChangesByMethodAndType */ - public function __construct(array $argumentChangesByMethodAndType, MethodCallAnalyzer $methodCallAnalyzer) - { + public function __construct( + array $argumentChangesByMethodAndType, + MethodCallAnalyzer $methodCallAnalyzer, + ClassMethodAnalyzer $classMethodAnalyzer + ) { $this->argumentChangesMethodAndClass = $argumentChangesByMethodAndType; $this->methodCallAnalyzer = $methodCallAnalyzer; + $this->classMethodAnalyzer = $classMethodAnalyzer; } public function isCandidate(Node $node): bool @@ -84,10 +93,6 @@ final class ArgumentReplacerRector extends AbstractRector */ private function matchArgumentChanges(Node $node): ?array { -// if (! $node instanceof MethodCall) { -// return null; -// } - if (! $node instanceof ClassMethod && ! $node instanceof MethodCall && ! $node instanceof StaticCall) { return null; } @@ -97,6 +102,10 @@ final class ArgumentReplacerRector extends AbstractRector if ($this->methodCallAnalyzer->isTypeAndMethods($node, $type, $methods)) { return $argumentChangesByMethod[$node->name->toString()]; } + + if ($this->classMethodAnalyzer->isTypeAndMethods($node, $type, $methods)) { + return $argumentChangesByMethod[$node->name->toString()]; + } } return null;