decouple hasInstanceOfName() to betterNodeFinder

This commit is contained in:
TomasVotruba 2020-07-19 13:11:20 +02:00
parent 990dab6196
commit dcbfb005a0
3 changed files with 29 additions and 37 deletions

View File

@ -14,7 +14,6 @@ use PhpParser\Node\Param;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\If_;
use PhpParser\Node\Stmt\Property;
use PhpParser\NodeTraverser;
use PHPStan\Analyser\Scope;
use PHPStan\Type\Type;
use PHPStan\Type\TypeWithClassName;
@ -243,28 +242,10 @@ final class BreakingVariableRenameGuard
return false;
}
return (bool) $this->hasVariableOfName((array) $functionLike->uses, $expectedName);
}
/**
* @param Node[] $nodes
*/
private function hasVariableOfName(array $nodes, string $name): bool
{
$contains = false;
$this->callableNodeTraverser->traverseNodesWithCallable($nodes, function (Node $node) use (&$contains, $name) {
if (! $node instanceof Variable) {
return null;
}
if (! $this->nodeNameResolver->isName($node, $name)) {
return null;
}
$contains = true;
return NodeTraverser::STOP_TRAVERSAL;
});
return $contains;
return (bool) $this->betterNodeFinder->hasInstanceOfName(
(array) $functionLike->uses,
Variable::class,
$expectedName
);
}
}

View File

@ -166,18 +166,6 @@ PHP
return $parentNode->getParams() ?? [];
}
// /**
// * @todo decouple to standalone service
// */
// private function shouldSkipForNameConflict(Assign $assign, string $newName): bool
// {
// if ($this->skipOnConflictParamName($assign, $newName)) {
// return true;
// }
//
// return $this->skipOnConflictOtherVariable($assign, $newName);
// }
/**
* @return ClassMethod|Function_|Closure|null
*/

View File

@ -10,6 +10,7 @@ use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassLike;
use PhpParser\Node\Stmt\Expression;
use PhpParser\NodeFinder;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\NodeTypeResolver\Node\AttributeKey;
/**
@ -22,9 +23,15 @@ final class BetterNodeFinder
*/
private $nodeFinder;
public function __construct(NodeFinder $nodeFinder)
/**
* @var NodeNameResolver
*/
private $nodeNameResolver;
public function __construct(NodeFinder $nodeFinder, NodeNameResolver $nodeNameResolver)
{
$this->nodeFinder = $nodeFinder;
$this->nodeNameResolver = $nodeNameResolver;
}
/**
@ -106,6 +113,22 @@ final class BetterNodeFinder
return $this->nodeFinder->findFirstInstanceOf($nodes, $type);
}
/**
* @param Node|Node[] $nodes
*/
public function hasInstanceOfName($nodes, string $type, string $name): bool
{
$foundInstances = $this->nodeFinder->findInstanceOf($nodes, $type);
foreach ($foundInstances as $foundInstance) {
if ($this->nodeNameResolver->isName($foundInstance, $name)) {
return true;
}
}
return false;
}
/**
* @param Node|Node[] $nodes
* @param string[] $types