1: | <?php declare(strict_types=1); |
2: | |
3: | namespace PhpParser\NodeVisitor; |
4: | |
5: | use PhpParser\Node; |
6: | use PhpParser\NodeVisitorAbstract; |
7: | |
8: | |
9: | |
10: | |
11: | |
12: | class FindingVisitor extends NodeVisitorAbstract { |
13: | |
14: | protected $filterCallback; |
15: | |
16: | protected array $foundNodes; |
17: | |
18: | public function __construct(callable $filterCallback) { |
19: | $this->filterCallback = $filterCallback; |
20: | } |
21: | |
22: | |
23: | |
24: | |
25: | |
26: | |
27: | |
28: | |
29: | public function getFoundNodes(): array { |
30: | return $this->foundNodes; |
31: | } |
32: | |
33: | public function beforeTraverse(array $nodes): ?array { |
34: | $this->foundNodes = []; |
35: | |
36: | return null; |
37: | } |
38: | |
39: | public function enterNode(Node $node) { |
40: | $filterCallback = $this->filterCallback; |
41: | if ($filterCallback($node)) { |
42: | $this->foundNodes[] = $node; |
43: | } |
44: | |
45: | return null; |
46: | } |
47: | } |
48: | |