1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
/* |
4
|
|
|
* Go! AOP framework |
5
|
|
|
* |
6
|
|
|
* @copyright Copyright 2017, Lisachenko Alexander <[email protected]> |
7
|
|
|
* |
8
|
|
|
* This source file is subject to the license that is bundled |
9
|
|
|
* with this source code in the file LICENSE. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Go\Instrument\Transformer; |
13
|
|
|
|
14
|
|
|
use PhpParser\Node; |
15
|
|
|
use PhpParser\NodeVisitorAbstract; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Node finder visitor for compatibility with PHP-Parser < 4.0 |
19
|
|
|
*/ |
20
|
|
|
class NodeFinderVisitor extends NodeVisitorAbstract |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* List of node class names to search |
24
|
|
|
* |
25
|
|
|
* @var string[] |
26
|
|
|
*/ |
27
|
|
|
protected $searchNodes; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* List of found nodes |
31
|
|
|
* |
32
|
|
|
* @var Node[] Found nodes |
33
|
|
|
*/ |
34
|
|
|
protected $foundNodes; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param array $searchNodes List of node names to search in AST |
38
|
|
|
*/ |
39
|
25 |
|
public function __construct(array $searchNodes) |
40
|
|
|
{ |
41
|
25 |
|
$this->searchNodes = $searchNodes; |
42
|
25 |
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Get found nodes. |
46
|
|
|
* |
47
|
|
|
* Nodes are returned in pre-order. |
48
|
|
|
* |
49
|
|
|
* @return Node[] Found nodes |
50
|
|
|
*/ |
51
|
25 |
|
public function getFoundNodes(): array |
52
|
|
|
{ |
53
|
25 |
|
return $this->foundNodes; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Called once before traversal. |
58
|
|
|
* |
59
|
|
|
* Return value semantics: |
60
|
|
|
* * null: $nodes stays as-is |
61
|
|
|
* * otherwise: $nodes is set to the return value |
62
|
|
|
* |
63
|
|
|
* @param Node[] $nodes Array of nodes |
64
|
|
|
* |
65
|
|
|
* @return null|Node[] Array of nodes |
66
|
|
|
*/ |
67
|
25 |
|
public function beforeTraverse(array $nodes) |
68
|
|
|
{ |
69
|
25 |
|
$this->foundNodes = []; |
70
|
|
|
|
71
|
25 |
|
return null; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Called when entering a node. |
76
|
|
|
* |
77
|
|
|
* Return value semantics: |
78
|
|
|
* * null |
79
|
|
|
* => $node stays as-is |
80
|
|
|
* * NodeTraverser::DONT_TRAVERSE_CHILDREN |
81
|
|
|
* => Children of $node are not traversed. $node stays as-is |
82
|
|
|
* * NodeTraverser::STOP_TRAVERSAL |
83
|
|
|
* => Traversal is aborted. $node stays as-is |
84
|
|
|
* * otherwise |
85
|
|
|
* => $node is set to the return value |
86
|
|
|
* |
87
|
|
|
* @param Node $node Node |
88
|
|
|
* |
89
|
|
|
* @return null|int|Node Node |
90
|
|
|
*/ |
91
|
25 |
|
public function enterNode(Node $node) |
92
|
|
|
{ |
93
|
25 |
|
foreach ($this->searchNodes as $nodeClassName) { |
94
|
25 |
|
if ($node instanceof $nodeClassName) { |
95
|
25 |
|
$this->foundNodes[] = $node; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
25 |
|
return null; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|