1: | <?php declare(strict_types=1); |
2: | |
3: | namespace PhpParser; |
4: | |
5: | use PhpParser\NodeVisitor\FindingVisitor; |
6: | use PhpParser\NodeVisitor\FirstFindingVisitor; |
7: | |
8: | class NodeFinder |
9: | { |
10: | |
11: | |
12: | |
13: | |
14: | |
15: | |
16: | |
17: | |
18: | public function find($nodes, callable $filter) : array { |
19: | if (!is_array($nodes)) { |
20: | $nodes = [$nodes]; |
21: | } |
22: | |
23: | $visitor = new FindingVisitor($filter); |
24: | |
25: | $traverser = new NodeTraverser; |
26: | $traverser->addVisitor($visitor); |
27: | $traverser->traverse($nodes); |
28: | |
29: | return $visitor->getFoundNodes(); |
30: | } |
31: | |
32: | |
33: | |
34: | |
35: | |
36: | |
37: | |
38: | |
39: | |
40: | public function findInstanceOf($nodes, string $class) : array { |
41: | return $this->find($nodes, function ($node) use ($class) { |
42: | return $node instanceof $class; |
43: | }); |
44: | } |
45: | |
46: | |
47: | |
48: | |
49: | |
50: | |
51: | |
52: | |
53: | |
54: | public function findFirst($nodes, callable $filter) { |
55: | if (!is_array($nodes)) { |
56: | $nodes = [$nodes]; |
57: | } |
58: | |
59: | $visitor = new FirstFindingVisitor($filter); |
60: | |
61: | $traverser = new NodeTraverser; |
62: | $traverser->addVisitor($visitor); |
63: | $traverser->traverse($nodes); |
64: | |
65: | return $visitor->getFoundNode(); |
66: | } |
67: | |
68: | |
69: | |
70: | |
71: | |
72: | |
73: | |
74: | |
75: | |
76: | public function findFirstInstanceOf($nodes, string $class) { |
77: | return $this->findFirst($nodes, function ($node) use ($class) { |
78: | return $node instanceof $class; |
79: | }); |
80: | } |
81: | } |
82: | |