mirror of
https://github.com/rectorphp/rector.git
synced 2025-02-24 19:53:14 +01:00
ArgumentRemoverMerge WIP
This commit is contained in:
parent
4cb9e310bb
commit
7c35e8a93b
39
src/NodeAnalyzer/ClassMethodAnalyzer.php
Normal file
39
src/NodeAnalyzer/ClassMethodAnalyzer.php
Normal file
@ -0,0 +1,39 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Rector\NodeAnalyzer;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr\MethodCall;
|
||||
use PhpParser\Node\Stmt\ClassMethod;
|
||||
use Rector\Node\Attribute;
|
||||
|
||||
/**
|
||||
* Checks "public function methodCall()"
|
||||
*/
|
||||
final class ClassMethodAnalyzer
|
||||
{
|
||||
/**
|
||||
* @param string[] $methods
|
||||
*/
|
||||
public function isTypeAndMethods(Node $node, string $type, array $methods): bool
|
||||
{
|
||||
if (! $this->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);
|
||||
}
|
||||
|
||||
}
|
@ -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;
|
||||
|
Loading…
x
Reference in New Issue
Block a user