ArgumentRemoverMerge WIP

This commit is contained in:
TomasVotruba 2017-11-01 18:23:50 +01:00
parent 4cb9e310bb
commit 7c35e8a93b
2 changed files with 54 additions and 6 deletions

View 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);
}
}

View File

@ -7,6 +7,7 @@ use PhpParser\Node;
use PhpParser\Node\Expr\MethodCall; use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\StaticCall; use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Stmt\ClassMethod; use PhpParser\Node\Stmt\ClassMethod;
use Rector\NodeAnalyzer\ClassMethodAnalyzer;
use Rector\NodeAnalyzer\MethodCallAnalyzer; use Rector\NodeAnalyzer\MethodCallAnalyzer;
use Rector\Rector\AbstractRector; use Rector\Rector\AbstractRector;
@ -26,14 +27,22 @@ final class ArgumentReplacerRector extends AbstractRector
* @var mixed[]|null * @var mixed[]|null
*/ */
private $activeArgumentChangesByPosition; private $activeArgumentChangesByPosition;
/**
* @var ClassMethodAnalyzer
*/
private $classMethodAnalyzer;
/** /**
* @param mixed[] $argumentChangesByMethodAndType * @param mixed[] $argumentChangesByMethodAndType
*/ */
public function __construct(array $argumentChangesByMethodAndType, MethodCallAnalyzer $methodCallAnalyzer) public function __construct(
{ array $argumentChangesByMethodAndType,
MethodCallAnalyzer $methodCallAnalyzer,
ClassMethodAnalyzer $classMethodAnalyzer
) {
$this->argumentChangesMethodAndClass = $argumentChangesByMethodAndType; $this->argumentChangesMethodAndClass = $argumentChangesByMethodAndType;
$this->methodCallAnalyzer = $methodCallAnalyzer; $this->methodCallAnalyzer = $methodCallAnalyzer;
$this->classMethodAnalyzer = $classMethodAnalyzer;
} }
public function isCandidate(Node $node): bool public function isCandidate(Node $node): bool
@ -84,10 +93,6 @@ final class ArgumentReplacerRector extends AbstractRector
*/ */
private function matchArgumentChanges(Node $node): ?array private function matchArgumentChanges(Node $node): ?array
{ {
// if (! $node instanceof MethodCall) {
// return null;
// }
if (! $node instanceof ClassMethod && ! $node instanceof MethodCall && ! $node instanceof StaticCall) { if (! $node instanceof ClassMethod && ! $node instanceof MethodCall && ! $node instanceof StaticCall) {
return null; return null;
} }
@ -97,6 +102,10 @@ final class ArgumentReplacerRector extends AbstractRector
if ($this->methodCallAnalyzer->isTypeAndMethods($node, $type, $methods)) { if ($this->methodCallAnalyzer->isTypeAndMethods($node, $type, $methods)) {
return $argumentChangesByMethod[$node->name->toString()]; return $argumentChangesByMethod[$node->name->toString()];
} }
if ($this->classMethodAnalyzer->isTypeAndMethods($node, $type, $methods)) {
return $argumentChangesByMethod[$node->name->toString()];
}
} }
return null; return null;