|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Inspector\Analysis; |
|
4
|
|
|
|
|
5
|
|
|
use PhpParser\Node; |
|
6
|
|
|
use Inspector\Analysis\Exception\AnalysisException; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Walks/Traverses through each Node in a Abstract Syntax Tree recursively |
|
10
|
|
|
* triggering a callback for each node. |
|
11
|
|
|
*/ |
|
12
|
|
|
class NodeWalker |
|
13
|
|
|
{ |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @param Node[]|null $ast |
|
17
|
|
|
* @param callable $callback |
|
18
|
|
|
* @return array |
|
19
|
|
|
*/ |
|
20
|
|
|
public function walk($ast, callable $callback) |
|
21
|
|
|
{ |
|
22
|
|
|
if (empty($ast)) { |
|
23
|
|
|
return []; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
// Ensure that it is an array of nodes |
|
27
|
|
|
$ast = $this->harmonizeNodes($ast); |
|
28
|
|
|
|
|
29
|
|
|
$errors = []; |
|
30
|
|
|
|
|
31
|
|
|
$this->foreachNode($ast, function (Node $node) use (&$errors, $callback) { |
|
32
|
|
|
|
|
33
|
|
|
$errors = array_merge($errors, $this->triggerCallback($callback, $node)); |
|
34
|
|
|
|
|
35
|
|
|
$this->foreachSubNodes($node, function (array $subNodes) use (&$errors, $callback) { |
|
36
|
|
|
$moreErrors = $this->walk($subNodes, $callback); |
|
37
|
|
|
$errors = array_merge($errors, $moreErrors); |
|
38
|
|
|
}); |
|
39
|
|
|
}); |
|
40
|
|
|
|
|
41
|
|
|
return $errors; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Triggers the callback for the walker and catches errors |
|
46
|
|
|
* |
|
47
|
|
|
* @param callable $callback |
|
48
|
|
|
* @param $node |
|
49
|
|
|
* @return array |
|
50
|
|
|
*/ |
|
51
|
|
|
protected function triggerCallback(callable $callback, Node $node) |
|
52
|
|
|
{ |
|
53
|
|
|
$errors = []; |
|
54
|
|
|
try { |
|
55
|
|
|
$callback($node); |
|
56
|
|
|
} catch (AnalysisException $e) { |
|
57
|
|
|
$errors[] = $e; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
return $errors; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Makes it a consistent array of node even if it is just a single instance of Node. |
|
65
|
|
|
* |
|
66
|
|
|
* @param mixed $ast |
|
67
|
|
|
* @return array |
|
68
|
|
|
*/ |
|
69
|
|
|
protected function harmonizeNodes($ast) |
|
70
|
|
|
{ |
|
71
|
|
|
return ($ast instanceof Node) ? [$ast] : $ast; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Loops through the AST for each Node (linearly) and triggers the callback for it. |
|
76
|
|
|
* |
|
77
|
|
|
* @param Node[]|null $ast |
|
78
|
|
|
* @param callable $callback |
|
79
|
|
|
*/ |
|
80
|
|
|
protected function foreachNode($ast, callable $callback) |
|
81
|
|
|
{ |
|
82
|
|
|
foreach ($ast as $node) { |
|
|
|
|
|
|
83
|
|
|
if (!($node instanceof Node)) { |
|
84
|
|
|
continue; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
$callback($node); |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Checks and goes through each list of sub nodes a given Node has, |
|
93
|
|
|
* and triggers the callback for each of those sub nodes (Node[]) |
|
94
|
|
|
* |
|
95
|
|
|
* @param Node $node |
|
96
|
|
|
* @param callable $callback |
|
97
|
|
|
*/ |
|
98
|
|
|
protected function foreachSubNodes(Node $node, callable $callback) |
|
99
|
|
|
{ |
|
100
|
|
|
foreach ($node->getSubNodeNames() as $subNodeName) { |
|
101
|
|
|
$subNodeList = $node->{$subNodeName}; |
|
102
|
|
|
|
|
103
|
|
|
$subNodeList = $this->harmonizeNodes($subNodeList); |
|
104
|
|
|
|
|
105
|
|
|
if (!is_array($subNodeList)) { |
|
106
|
|
|
continue; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
// Node[] $subNodeList |
|
|
|
|
|
|
110
|
|
|
$callback($subNodeList); |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.