123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- <?php declare(strict_types=1);
- namespace PhpParser;
- use PhpParser\NodeVisitor\FindingVisitor;
- use PhpParser\NodeVisitor\FirstFindingVisitor;
- class NodeFinder {
-
- public function find($nodes, callable $filter): array {
- if ($nodes === []) {
- return [];
- }
- if (!is_array($nodes)) {
- $nodes = [$nodes];
- }
- $visitor = new FindingVisitor($filter);
- $traverser = new NodeTraverser($visitor);
- $traverser->traverse($nodes);
- return $visitor->getFoundNodes();
- }
-
- public function findInstanceOf($nodes, string $class): array {
- return $this->find($nodes, function ($node) use ($class) {
- return $node instanceof $class;
- });
- }
-
- public function findFirst($nodes, callable $filter): ?Node {
- if ($nodes === []) {
- return null;
- }
- if (!is_array($nodes)) {
- $nodes = [$nodes];
- }
- $visitor = new FirstFindingVisitor($filter);
- $traverser = new NodeTraverser($visitor);
- $traverser->traverse($nodes);
- return $visitor->getFoundNode();
- }
-
- public function findFirstInstanceOf($nodes, string $class): ?Node {
- return $this->findFirst($nodes, function ($node) use ($class) {
- return $node instanceof $class;
- });
- }
- }
|