1
|
|
|
<?php |
2
|
|
|
/****************************************************************************** |
3
|
|
|
* An implementation of dicto (scg.unibe.ch/dicto) in and for PHP. |
4
|
|
|
* |
5
|
|
|
* Copyright (c) 2016, 2015 Richard Klees <[email protected]> |
6
|
|
|
* |
7
|
|
|
* This software is licensed under The MIT License. You should have received |
8
|
|
|
* a copy of the license along with the code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Lechimp\Dicto\Indexer; |
12
|
|
|
|
13
|
|
|
use Lechimp\Dicto\Variables\Variable; |
14
|
|
|
use PhpParser\Node as N; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Visitor for the AST used by Indexer. |
18
|
|
|
*/ |
19
|
|
|
class ASTVisitor implements \PhpParser\NodeVisitor { |
20
|
|
|
/** |
21
|
|
|
* @var LocationImpl |
22
|
|
|
*/ |
23
|
|
|
protected $location; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var Insert |
27
|
|
|
*/ |
28
|
|
|
protected $insert; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var array string => array() |
32
|
|
|
*/ |
33
|
|
|
protected $listeners_enter_definition; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var array string => array() |
37
|
|
|
*/ |
38
|
|
|
protected $listeners_enter_misc; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* This is used to exit early in enterNode and to break enterNode apart |
42
|
|
|
* into many methods. |
43
|
|
|
* |
44
|
|
|
* @var array<string,string> |
45
|
|
|
*/ |
46
|
|
|
protected $jump_labels; |
47
|
|
|
|
48
|
53 |
|
public function __construct(LocationImpl $location, Insert $insert, array &$listeners_enter_definition, array &$listeners_enter_misc) { |
49
|
53 |
|
$this->location = $location; |
50
|
53 |
|
$this->insert = $insert; |
51
|
53 |
|
$this->listeners_enter_definition = $listeners_enter_definition; |
52
|
53 |
|
$this->listeners_enter_misc = $listeners_enter_misc; |
53
|
53 |
|
$this->jump_labels = |
54
|
|
|
[ N\Stmt\Namespace_::class => "enterNamespace" |
|
|
|
|
55
|
53 |
|
, N\Stmt\Class_::class => "enterClass" |
56
|
53 |
|
, N\Stmt\Interface_::class => "enterInterface" |
57
|
53 |
|
, N\Stmt\Trait_::class => "enterTrait" |
58
|
53 |
|
, N\Stmt\ClassMethod::class => "enterMethod" |
59
|
53 |
|
, N\Stmt\Function_::class => "enterFunction" |
60
|
53 |
|
]; |
61
|
53 |
|
} |
62
|
|
|
|
63
|
|
|
// generalizes over calls to misc listeners |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param string $which |
67
|
|
|
*/ |
68
|
38 |
View Code Duplication |
protected function call_misc_listener($which) { |
|
|
|
|
69
|
38 |
|
$listeners = &$this->$which; |
70
|
38 |
|
$current_node = $this->location->current_node(); |
71
|
38 |
|
foreach ($listeners[0] as $listener) { |
72
|
|
|
$listener($this->insert, $this->location, $current_node); |
73
|
38 |
|
} |
74
|
38 |
|
$cls = get_class($current_node); |
75
|
38 |
|
if (array_key_exists($cls, $listeners)) { |
76
|
23 |
|
foreach ($listeners[$cls] as $listener) { |
77
|
23 |
|
$listener($this->insert, $this->location, $current_node); |
78
|
23 |
|
} |
79
|
23 |
|
} |
80
|
38 |
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param string $which |
84
|
|
|
* @param string $type |
85
|
|
|
* @param int $type |
86
|
|
|
*/ |
87
|
51 |
View Code Duplication |
protected function call_definition_listener($which, $type, $id) { |
|
|
|
|
88
|
51 |
|
$listeners = &$this->$which; |
89
|
51 |
|
$current_node = $this->location->current_node(); |
90
|
51 |
|
foreach ($listeners[0] as $listener) { |
91
|
|
|
$listener($this->insert, $this->location, $type, $id, $current_node); |
92
|
51 |
|
} |
93
|
51 |
|
if (array_key_exists($type, $listeners)) { |
94
|
|
|
foreach ($listeners[$type] as $listener) { |
95
|
|
|
$listener($this->insert, $this->location, $type, $id, $current_node); |
96
|
|
|
} |
97
|
|
|
} |
98
|
51 |
|
} |
99
|
|
|
|
100
|
|
|
// from \PhpParser\NodeVisitor |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @inheritdoc |
104
|
|
|
*/ |
105
|
53 |
|
public function beforeTraverse(array $nodes) { |
106
|
53 |
|
$handle = $this->insert->_file($this->location->_file_name(), $this->location->_file_content()); |
107
|
53 |
|
$this->location->push_entity(Variable::FILE_TYPE, $handle); |
108
|
53 |
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @inheritdoc |
112
|
|
|
*/ |
113
|
53 |
|
public function afterTraverse(array $nodes) { |
114
|
53 |
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @inheritdoc |
118
|
|
|
*/ |
119
|
53 |
|
public function enterNode(\PhpParser\Node $node) { |
120
|
53 |
|
$this->location->set_current_node($node); |
121
|
|
|
|
122
|
53 |
|
$cls = get_class($node); |
123
|
53 |
|
if (array_key_exists($cls, $this->jump_labels)) { |
124
|
51 |
|
$start_line = $node->getAttribute("startLine"); |
125
|
51 |
|
$end_line = $node->getAttribute("endLine"); |
126
|
51 |
|
list($type, $handle) = $this->{$this->jump_labels[$cls]} |
127
|
51 |
|
($node, $start_line, $end_line); |
128
|
|
|
|
129
|
51 |
|
if ($type !== Variable::NAMESPACE_TYPE) { |
130
|
51 |
|
$this->call_definition_listener("listeners_enter_definition", $type, $handle); |
131
|
51 |
|
} |
132
|
51 |
|
$this->location->push_entity($type, $handle); |
133
|
51 |
|
} |
134
|
|
|
else { |
135
|
38 |
|
$this->call_misc_listener("listeners_enter_misc"); |
136
|
|
|
} |
137
|
|
|
|
138
|
53 |
|
$this->location->flush_current_node(); |
139
|
53 |
|
} |
140
|
|
|
|
141
|
7 |
|
public function enterNamespace(N\Stmt\Namespace_ $node, $start_line, $end_line) { |
|
|
|
|
142
|
7 |
|
$handle = $this->insert->_namespace |
143
|
7 |
|
( "".$node->name // force string representation |
144
|
7 |
|
); |
145
|
7 |
|
return [Variable::NAMESPACE_TYPE, $handle]; |
146
|
|
|
} |
147
|
|
|
|
148
|
44 |
View Code Duplication |
public function enterClass(N\Stmt\Class_ $node, $start_line, $end_line) { |
|
|
|
|
149
|
44 |
|
$handle = $this->insert->_class |
150
|
44 |
|
( $node->name |
151
|
44 |
|
, $this->location->_file() |
152
|
44 |
|
, $start_line |
153
|
44 |
|
, $end_line |
154
|
44 |
|
, $this->location->_namespace() |
155
|
44 |
|
); |
156
|
44 |
|
return [Variable::CLASS_TYPE, $handle]; |
157
|
|
|
} |
158
|
|
|
|
159
|
3 |
View Code Duplication |
public function enterInterface(N\Stmt\Interface_ $node, $start_line, $end_line) { |
|
|
|
|
160
|
3 |
|
$handle = $this->insert->_interface |
161
|
3 |
|
( $node->name |
162
|
3 |
|
, $this->location->_file() |
163
|
3 |
|
, $start_line |
164
|
3 |
|
, $end_line |
165
|
3 |
|
, $this->location->_namespace() |
166
|
3 |
|
); |
167
|
3 |
|
return [Variable::INTERFACE_TYPE, $handle]; |
168
|
|
|
} |
169
|
|
|
|
170
|
3 |
View Code Duplication |
public function enterTrait(N\Stmt\Trait_ $node, $start_line, $end_line) { |
|
|
|
|
171
|
3 |
|
$handle = $this->insert->_trait |
172
|
3 |
|
( $node->name |
173
|
3 |
|
, $this->location->_file() |
174
|
3 |
|
, $start_line |
175
|
3 |
|
, $end_line |
176
|
3 |
|
, $this->location->_namespace() |
177
|
3 |
|
); |
178
|
3 |
|
return [Variable::INTERFACE_TYPE, $handle]; |
179
|
|
|
} |
180
|
|
|
|
181
|
38 |
|
public function enterMethod(N\Stmt\ClassMethod $node, $start_line, $end_line) { |
182
|
38 |
|
$handle = $this->insert->_method |
183
|
38 |
|
( $node->name |
184
|
38 |
|
, $this->location->_class_interface_trait() |
185
|
38 |
|
, $this->location->_file() |
186
|
38 |
|
, $start_line |
187
|
38 |
|
, $end_line |
188
|
38 |
|
); |
189
|
38 |
|
return [Variable::METHOD_TYPE, $handle]; |
190
|
|
|
} |
191
|
|
|
|
192
|
4 |
|
public function enterFunction(N\Stmt\Function_ $node, $start_line, $end_line) { |
193
|
4 |
|
$handle = $this->insert->_function |
194
|
4 |
|
( $node->name |
195
|
4 |
|
, $this->location->_file() |
196
|
4 |
|
, $start_line |
197
|
4 |
|
, $end_line |
198
|
4 |
|
); |
199
|
4 |
|
return [Variable::FUNCTION_TYPE, $handle]; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* @inheritdoc |
204
|
|
|
*/ |
205
|
53 |
|
public function leaveNode(\PhpParser\Node $node) { |
206
|
|
|
if ( $node instanceof N\Stmt\Namespace_ |
207
|
53 |
|
|| $node instanceof N\Stmt\Class_ |
208
|
53 |
|
|| $node instanceof N\Stmt\Interface_ |
209
|
49 |
|
|| $node instanceof N\Stmt\ClassMethod |
210
|
53 |
|
|| $node instanceof N\Stmt\Function_) { |
211
|
|
|
|
212
|
50 |
|
$this->location->pop_entity(); |
213
|
50 |
|
} |
214
|
53 |
|
} |
215
|
|
|
} |
216
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..