2017-08-18 22:57:27 +02:00
|
|
|
<?php declare(strict_types=1);
|
2017-01-21 15:41:24 +01:00
|
|
|
|
|
|
|
namespace PhpParser\NodeVisitor;
|
|
|
|
|
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\NodeVisitorAbstract;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This visitor can be used to find and collect all nodes satisfying some criterion determined by
|
|
|
|
* a filter callback.
|
|
|
|
*/
|
2022-08-28 22:57:06 +02:00
|
|
|
class FindingVisitor extends NodeVisitorAbstract {
|
2017-01-21 15:41:24 +01:00
|
|
|
/** @var callable Filter callback */
|
|
|
|
protected $filterCallback;
|
|
|
|
/** @var Node[] Found nodes */
|
2023-08-16 21:18:30 +02:00
|
|
|
protected array $foundNodes;
|
2017-01-21 15:41:24 +01:00
|
|
|
|
|
|
|
public function __construct(callable $filterCallback) {
|
|
|
|
$this->filterCallback = $filterCallback;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get found nodes satisfying the filter callback.
|
|
|
|
*
|
|
|
|
* Nodes are returned in pre-order.
|
|
|
|
*
|
|
|
|
* @return Node[] Found nodes
|
|
|
|
*/
|
2022-08-28 22:57:06 +02:00
|
|
|
public function getFoundNodes(): array {
|
2017-01-21 15:41:24 +01:00
|
|
|
return $this->foundNodes;
|
|
|
|
}
|
|
|
|
|
2022-06-04 12:48:12 +02:00
|
|
|
public function beforeTraverse(array $nodes): ?array {
|
2017-01-21 15:41:24 +01:00
|
|
|
$this->foundNodes = [];
|
2017-02-01 19:56:15 -05:00
|
|
|
|
|
|
|
return null;
|
2017-01-21 15:41:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function enterNode(Node $node) {
|
|
|
|
$filterCallback = $this->filterCallback;
|
|
|
|
if ($filterCallback($node)) {
|
|
|
|
$this->foundNodes[] = $node;
|
|
|
|
}
|
2017-02-01 19:56:15 -05:00
|
|
|
|
|
|
|
return null;
|
2017-01-21 15:41:24 +01:00
|
|
|
}
|
2018-01-10 15:04:06 -02:00
|
|
|
}
|