rector/src/PhpParser/Node/BetterNodeFinder.php

176 lines
4.5 KiB
PHP
Raw Normal View History

<?php declare(strict_types=1);
2018-11-08 11:56:06 +00:00
namespace Rector\PhpParser\Node;
use PhpParser\Node;
2019-01-14 18:21:23 +00:00
use PhpParser\Node\Stmt;
2018-10-09 10:18:42 +00:00
use PhpParser\Node\Stmt\Expression;
use PhpParser\NodeFinder;
use Rector\NodeTypeResolver\Node\Attribute;
use Rector\PhpParser\Printer\BetterStandardPrinter;
final class BetterNodeFinder
{
/**
* @var NodeFinder
*/
private $nodeFinder;
/**
* @var BetterStandardPrinter
*/
private $betterStandardPrinter;
public function __construct(NodeFinder $nodeFinder, BetterStandardPrinter $betterStandardPrinter)
{
$this->nodeFinder = $nodeFinder;
$this->betterStandardPrinter = $betterStandardPrinter;
}
/**
* @param string|string[] $type
*/
public function findFirstParentInstanceOf(Node $node, $type): ?Node
{
if (! is_array($type)) {
$type = [$type];
}
/** @var Node|null $parentNode */
$parentNode = $node->getAttribute(Attribute::PARENT_NODE);
if ($parentNode === null) {
return null;
}
do {
if ($this->isTypes($parentNode, $type)) {
return $parentNode;
}
if ($parentNode === null) {
return null;
}
} while ($parentNode = $parentNode->getAttribute(Attribute::PARENT_NODE));
return null;
}
public function findFirstAncestorInstanceOf(Node $node, string $type): ?Node
{
$currentNode = $node->getAttribute(Attribute::PARENT_NODE);
while ($currentNode !== null) {
if ($currentNode instanceof $type) {
return $currentNode;
}
$currentNode = $currentNode->getAttribute(Attribute::PARENT_NODE);
}
2017-10-26 19:06:11 +00:00
return null;
}
/**
2019-01-14 18:21:23 +00:00
* @param Node|Node[]|Stmt[] $nodes
* @return Node[]
*/
public function findInstanceOf($nodes, string $type): array
{
return $this->nodeFinder->findInstanceOf($nodes, $type);
}
/**
* @param Node|Node[] $nodes
*/
public function findFirstInstanceOf($nodes, string $type): ?Node
{
return $this->nodeFinder->findFirstInstanceOf($nodes, $type);
}
/**
* @param Node|Node[] $nodes
*/
public function findLastInstanceOf($nodes, string $type): ?Node
{
$foundInstances = $this->nodeFinder->findInstanceOf($nodes, $type);
2019-02-17 14:12:47 +00:00
if ($foundInstances === []) {
return null;
}
return array_pop($foundInstances);
}
/**
* @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
/**
* @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
public function findFirstPrevious(Node $node, callable $filter): ?Node
{
2018-10-11 04:49:49 +00:00
$node = $node instanceof Expression ? $node : $node->getAttribute(Attribute::CURRENT_EXPRESSION);
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 next expression
2018-10-11 04:49:49 +00:00
$previousExpression = $node->getAttribute(Attribute::PREVIOUS_EXPRESSION);
2018-10-09 10:18:42 +00:00
if ($previousExpression === null) {
return null;
}
return $this->findFirstPrevious($previousExpression, $filter);
}
/**
* @return Node\Expr\Assign[]
*/
public function findAssignsOfVariable(Node $node, Node\Expr\Variable $variable): array
{
$assignNodes = $this->findInstanceOf($node, Node\Expr\Assign::class);
return array_filter($assignNodes, function (Node\Expr\Assign $assign) use ($variable) {
if ($this->betterStandardPrinter->areNodesEqual($assign->var, $variable)) {
return true;
}
if ($assign->var instanceof Node\Expr\ArrayDimFetch) {
return $this->betterStandardPrinter->areNodesEqual($assign->var->var, $variable);
}
return false;
});
}
/**
* @param string[] $types
*/
private function isTypes(Node $node, array $types): bool
{
foreach ($types as $type) {
if (is_a($node, $type, true)) {
return true;
}
}
return false;
}
}