rector/src/PhpParser/Node/BetterNodeFinder.php

386 lines
10 KiB
PHP
Raw Normal View History

2019-10-13 05:59:52 +00:00
<?php
declare(strict_types=1);
namespace Rector\Core\PhpParser\Node;
use PhpParser\Node;
2020-07-25 20:02:44 +00:00
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\FunctionLike;
2019-01-14 18:21:23 +00:00
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassLike;
2018-10-09 10:18:42 +00:00
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\Return_;
use PhpParser\NodeFinder;
use Rector\Core\PhpParser\Comparing\NodeComparator;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Symplify\PackageBuilder\Php\TypeChecker;
use Webmozart\Assert\Assert;
2019-09-03 09:11:45 +00:00
/**
* @see \Rector\Core\Tests\PhpParser\Node\BetterNodeFinder\BetterNodeFinderTest
2019-09-03 09:11:45 +00:00
*/
final class BetterNodeFinder
{
/**
* @var NodeFinder
*/
private $nodeFinder;
/**
* @var NodeNameResolver
*/
private $nodeNameResolver;
2020-07-25 20:02:44 +00:00
/**
* @var TypeChecker
2020-07-25 20:02:44 +00:00
*/
private $typeChecker;
2020-07-25 20:02:44 +00:00
/**
* @var NodeComparator
*/
private $nodeComparator;
2020-07-25 20:02:44 +00:00
public function __construct(
NodeFinder $nodeFinder,
NodeNameResolver $nodeNameResolver,
TypeChecker $typeChecker,
NodeComparator $nodeComparator
2020-07-25 20:02:44 +00:00
) {
$this->nodeFinder = $nodeFinder;
$this->nodeNameResolver = $nodeNameResolver;
$this->typeChecker = $typeChecker;
$this->nodeComparator = $nodeComparator;
}
/**
2021-03-18 11:21:20 +00:00
* @template T of Node
2021-01-08 22:30:33 +00:00
* @param class-string<T> $type
* @return T|null
*/
2021-01-08 22:30:33 +00:00
public function findParentType(Node $node, string $type): ?Node
{
Assert::isAOf($type, Node::class);
$parent = $node->getAttribute(AttributeKey::PARENT_NODE);
if (! $parent instanceof Node) {
2021-01-08 22:30:33 +00:00
return null;
}
do {
if (is_a($parent, $type, true)) {
return $parent;
}
if (! $parent instanceof Node) {
2021-01-08 22:30:33 +00:00
return null;
}
} while ($parent = $parent->getAttribute(AttributeKey::PARENT_NODE));
return null;
}
/**
2021-03-18 11:27:34 +00:00
* @template T of \PhpParser\Node
2021-03-17 22:30:39 +00:00
* @param array<class-string<T>> $types
2021-01-08 22:30:33 +00:00
* @return T|null
*/
public function findParentTypes(Node $node, array $types): ?Node
{
Assert::allIsAOf($types, Node::class);
2021-01-08 22:30:33 +00:00
$parent = $node->getAttribute(AttributeKey::PARENT_NODE);
if (! $parent instanceof Node) {
return null;
}
do {
2021-04-15 18:55:45 +00:00
foreach ($types as $type) {
2021-04-15 21:07:07 +00:00
if (is_a($parent, $type, true)) {
2021-04-15 18:55:45 +00:00
return $parent;
}
}
2021-01-08 22:30:33 +00:00
if ($parent === null) {
return null;
}
2021-01-08 22:30:33 +00:00
} while ($parent = $parent->getAttribute(AttributeKey::PARENT_NODE));
return null;
}
/**
2021-03-18 11:21:20 +00:00
* @template T of Node
2021-01-08 22:30:33 +00:00
* @param class-string<T> $type
2019-01-14 18:21:23 +00:00
* @param Node|Node[]|Stmt[] $nodes
2021-01-08 22:30:33 +00:00
* @return T[]
*/
public function findInstanceOf($nodes, string $type): array
{
Assert::isAOf($type, Node::class);
return $this->nodeFinder->findInstanceOf($nodes, $type);
}
/**
2021-03-18 11:21:20 +00:00
* @template T of Node
2021-01-08 22:30:33 +00:00
* @param class-string<T> $type
* @param Node|Node[] $nodes
2021-01-08 22:30:33 +00:00
* @return T|null
*/
public function findFirstInstanceOf($nodes, string $type): ?Node
{
Assert::isAOf($type, Node::class);
return $this->nodeFinder->findFirstInstanceOf($nodes, $type);
}
/**
2021-03-18 11:21:20 +00:00
* @param class-string<Node> $type
* @param Node|Node[] $nodes
*/
public function hasInstanceOfName($nodes, string $type, string $name): bool
2020-07-19 13:39:13 +00:00
{
Assert::isAOf($type, Node::class);
2020-07-19 13:39:13 +00:00
return (bool) $this->findInstanceOfName($nodes, $type, $name);
}
/**
* @param Node|Node[] $nodes
*/
2020-07-19 15:20:00 +00:00
public function hasVariableOfName($nodes, string $name): bool
2020-07-19 13:39:13 +00:00
{
2020-07-19 15:20:00 +00:00
return (bool) $this->findVariableOfName($nodes, $name);
2020-07-19 13:39:13 +00:00
}
/**
* @param Node|Node[] $nodes
2021-01-08 22:30:33 +00:00
* @return Variable|null
2020-07-19 13:39:13 +00:00
*/
2020-07-19 15:20:00 +00:00
public function findVariableOfName($nodes, string $name): ?Node
{
2020-07-19 15:20:00 +00:00
return $this->findInstanceOfName($nodes, Variable::class, $name);
}
/**
* @param Node|Node[] $nodes
2021-03-18 11:21:20 +00:00
* @param array<class-string<Node>> $types
*/
public function hasInstancesOf($nodes, array $types): bool
{
Assert::allIsAOf($types, Node::class);
foreach ($types as $type) {
2021-03-18 11:21:20 +00:00
$foundNode = $this->nodeFinder->findFirstInstanceOf($nodes, $type);
if (! $foundNode instanceof Node) {
continue;
}
return true;
}
return false;
}
/**
2021-03-18 11:21:20 +00:00
* @template T of Node
2021-01-08 22:30:33 +00:00
* @param class-string<T> $type
* @param Node|Node[] $nodes
2021-01-08 22:30:33 +00:00
* @return T|null
*/
public function findLastInstanceOf($nodes, string $type): ?Node
{
Assert::isAOf($type, Node::class);
$foundInstances = $this->nodeFinder->findInstanceOf($nodes, $type);
2019-02-17 14:12:47 +00:00
if ($foundInstances === []) {
return null;
}
2021-01-08 22:30:33 +00:00
$lastItemKey = array_key_last($foundInstances);
return $foundInstances[$lastItemKey];
}
/**
* @param Node|Node[] $nodes
* @return Node[]
*/
public function find($nodes, callable $filter): array
{
return $this->nodeFinder->find($nodes, $filter);
}
2017-11-01 21:37:57 +00:00
/**
* Excludes anonymous classes!
*
* @param Node[]|Node $nodes
* @return ClassLike[]
*/
public function findClassLikes($nodes): array
{
return $this->find($nodes, function (Node $node): bool {
if (! $node instanceof ClassLike) {
return false;
}
2019-08-18 12:15:39 +00:00
// skip anonymous classes
2019-10-30 14:38:30 +00:00
return ! ($node instanceof Class_ && $node->isAnonymous());
});
}
2020-07-05 11:07:20 +00:00
/**
* @param Node[] $nodes
2021-01-08 22:30:33 +00:00
* @return ClassLike|null
2020-07-05 11:07:20 +00:00
*/
public function findFirstNonAnonymousClass(array $nodes): ?Node
2020-07-05 11:07:20 +00:00
{
return $this->findFirst($nodes, function (Node $node): bool {
if (! $node instanceof ClassLike) {
return false;
}
// skip anonymous classes
return ! ($node instanceof Class_ && $node->isAnonymous());
});
}
2017-11-01 21:37:57 +00:00
/**
* @param Node|Node[] $nodes
*/
public function findFirst($nodes, callable $filter): ?Node
{
return $this->nodeFinder->findFirst($nodes, $filter);
}
2018-10-09 10:18:42 +00:00
2021-01-08 22:30:33 +00:00
/**
* @return Assign|null
*/
public function findPreviousAssignToExpr(Expr $expr): ?Node
2020-07-25 20:02:44 +00:00
{
return $this->findFirstPrevious($expr, function (Node $node) use ($expr): bool {
2020-07-25 20:02:44 +00:00
if (! $node instanceof Assign) {
return false;
}
return $this->nodeComparator->areNodesEqual($node->var, $expr);
2020-07-25 20:02:44 +00:00
});
}
public function findFirstPreviousOfNode(Node $node, callable $filter): ?Node
{
// move to previous expression
$previousStatement = $node->getAttribute(AttributeKey::PREVIOUS_NODE);
if ($previousStatement !== null) {
$foundNode = $this->findFirst([$previousStatement], $filter);
// we found what we need
if ($foundNode !== null) {
return $foundNode;
}
return $this->findFirstPreviousOfNode($previousStatement, $filter);
}
$parent = $node->getAttribute(AttributeKey::PARENT_NODE);
if ($parent instanceof FunctionLike) {
return null;
}
if ($parent instanceof Node) {
return $this->findFirstPreviousOfNode($parent, $filter);
}
return null;
}
2018-10-09 10:18:42 +00:00
public function findFirstPrevious(Node $node, callable $filter): ?Node
{
$node = $node instanceof Expression ? $node : $node->getAttribute(AttributeKey::CURRENT_STATEMENT);
2019-01-25 00:49:26 +00:00
if ($node === null) {
return null;
}
2018-10-09 10:18:42 +00:00
$foundNode = $this->findFirst([$node], $filter);
// we found what we need
2019-02-17 14:12:47 +00:00
if ($foundNode !== null) {
2018-10-09 10:18:42 +00:00
return $foundNode;
}
// move to previous expression
2019-10-27 17:40:59 +00:00
$previousStatement = $node->getAttribute(AttributeKey::PREVIOUS_STATEMENT);
if ($previousStatement !== null) {
return $this->findFirstPrevious($previousStatement, $filter);
}
$parent = $node->getAttribute(AttributeKey::PARENT_NODE);
if ($parent === null) {
2018-10-09 10:18:42 +00:00
return null;
}
return $this->findFirstPrevious($parent, $filter);
2018-10-09 10:18:42 +00:00
}
2020-03-23 16:13:04 +00:00
/**
2021-03-18 11:21:20 +00:00
* @template T of Node
* @param array<class-string<T>> $types
2021-01-08 22:30:33 +00:00
* @return T|null
2020-03-23 16:13:04 +00:00
*/
public function findFirstPreviousOfTypes(Node $mainNode, array $types): ?Node
{
return $this->findFirstPrevious($mainNode, function (Node $node) use ($types): bool {
return $this->typeChecker->isInstanceOf($node, $types);
2020-03-23 16:13:04 +00:00
});
}
public function findFirstNext(Node $node, callable $filter): ?Node
{
$next = $node->getAttribute(AttributeKey::NEXT_NODE);
if ($next instanceof Node) {
if ($next instanceof Return_ && $next->expr === null) {
return null;
}
$found = $this->findFirst($next, $filter);
if ($found instanceof Node) {
return $found;
}
return $this->findFirstNext($next, $filter);
}
$parent = $node->getAttribute(AttributeKey::PARENT_NODE);
if ($parent instanceof Return_ || $parent instanceof FunctionLike) {
return null;
}
if ($parent instanceof Node) {
return $this->findFirstNext($parent, $filter);
}
return null;
}
/**
2021-03-18 11:21:20 +00:00
* @template T of Node
2021-03-18 11:27:34 +00:00
* @param Node|Node[] $nodes
2021-01-08 22:30:33 +00:00
* @param class-string<T> $type
* @return T|null
*/
private function findInstanceOfName($nodes, string $type, string $name): ?Node
{
Assert::isAOf($type, Node::class);
$foundInstances = $this->nodeFinder->findInstanceOf($nodes, $type);
foreach ($foundInstances as $foundInstance) {
2021-01-08 22:30:33 +00:00
if (! $this->nodeNameResolver->isName($foundInstance, $name)) {
continue;
}
2021-01-08 22:30:33 +00:00
return $foundInstance;
}
return null;
}
}