|
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 licence along with the code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Lechimp\Dicto\Indexer; |
|
12
|
|
|
|
|
13
|
|
|
use Lechimp\Dicto\Indexer as I; |
|
14
|
|
|
use Lechimp\Dicto\Variables\Variable; |
|
15
|
|
|
use PhpParser\Node as N; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Implementation of Indexer with PhpParser. |
|
19
|
|
|
*/ |
|
20
|
|
|
class Indexer implements Location, ListenerRegistry, \PhpParser\NodeVisitor { |
|
21
|
|
|
/** |
|
22
|
|
|
* @var string |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $project_root_path; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var I\Insert|null |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $insert; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var \PhpParser\Parser |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $parser; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var array string => array() |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $listeners_enter_entity; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @var array string => array() |
|
43
|
|
|
*/ |
|
44
|
|
|
protected $listeners_leave_entity; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @var array string => array() |
|
48
|
|
|
*/ |
|
49
|
|
|
protected $listeners_enter_misc; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @var array string => array() |
|
53
|
|
|
*/ |
|
54
|
|
|
protected $listeners_leave_misc; |
|
55
|
|
|
|
|
56
|
|
|
// state for parsing a file |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @var string|null |
|
60
|
|
|
*/ |
|
61
|
|
|
protected $file_path = null; |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @var string[]|null |
|
65
|
|
|
*/ |
|
66
|
|
|
protected $file_content = null; |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* This contains the stack of ids were currently in, i.e. the nesting of |
|
70
|
|
|
* known code blocks we are in. |
|
71
|
|
|
* |
|
72
|
|
|
* @var array|null contain ($entity_type, $entity_id) |
|
73
|
|
|
*/ |
|
74
|
|
|
protected $entity_stack = null; |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @param string $project_root_path |
|
78
|
|
|
* @param Schema[] $rule_schemas |
|
|
|
|
|
|
79
|
|
|
*/ |
|
80
|
23 |
|
public function __construct(\PhpParser\Parser $parser, $project_root_path, Insert $insert) { |
|
81
|
23 |
|
$this->parser = $parser; |
|
82
|
23 |
|
assert('is_string($project_root_path)'); |
|
83
|
23 |
|
$this->project_root_path = $project_root_path; |
|
84
|
23 |
|
$this->insert = $insert; |
|
85
|
23 |
|
$this->listeners_enter_entity = array |
|
86
|
23 |
|
( 0 => array() |
|
87
|
23 |
|
); |
|
88
|
23 |
|
$this->listeners_leave_entity = array |
|
89
|
23 |
|
( 0 => array() |
|
90
|
23 |
|
); |
|
91
|
23 |
|
$this->listeners_enter_misc = array |
|
92
|
23 |
|
( 0 => array() |
|
93
|
23 |
|
); |
|
94
|
23 |
|
$this->listeners_leave_misc = array |
|
95
|
23 |
|
( 0 => array() |
|
96
|
23 |
|
); |
|
97
|
23 |
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @param string $path |
|
101
|
|
|
*/ |
|
102
|
22 |
|
public function index_file($path) { |
|
103
|
22 |
|
if ($this->insert === null) { |
|
104
|
|
|
throw new \RuntimeException( |
|
105
|
|
|
"Set an inserter to be used before starting to index files."); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
22 |
|
$content = file_get_contents($this->project_root_path."/$path"); |
|
109
|
22 |
|
if ($content === false) { |
|
110
|
|
|
throw \InvalidArgumentException("Can't read file $path."); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
22 |
|
$stmts = $this->parser->parse($content); |
|
114
|
22 |
|
if ($stmts === null) { |
|
115
|
|
|
throw new \RuntimeException("Can't parse file $path."); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
22 |
|
$traverser = new \PhpParser\NodeTraverser; |
|
119
|
22 |
|
$traverser->addVisitor($this); |
|
120
|
|
|
|
|
121
|
22 |
|
$this->entity_stack = array(); |
|
122
|
22 |
|
$this->file_path = $path; |
|
123
|
22 |
|
$this->file_content = explode("\n", $content); |
|
124
|
22 |
|
$traverser->traverse($stmts); |
|
125
|
22 |
|
$this->entity_stack = null; |
|
126
|
22 |
|
$this->file_path = null; |
|
127
|
22 |
|
$this->file_content = null; |
|
128
|
22 |
|
} |
|
129
|
|
|
|
|
130
|
|
|
// helper |
|
131
|
|
|
|
|
132
|
22 |
|
private function lines_from_to($start, $end) { |
|
133
|
22 |
|
assert('is_int($start)'); |
|
134
|
22 |
|
assert('is_int($end)'); |
|
135
|
22 |
|
return implode("\n", array_slice($this->file_content, $start-1, $end-$start+1)); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
// from ListenerRegistry |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* @inheritdoc |
|
142
|
|
|
*/ |
|
143
|
1 |
|
public function on_enter_entity($types, \Closure $listener) { |
|
144
|
1 |
|
$this->on_enter_or_leave_something("listeners_enter_entity", $types, $listener); |
|
145
|
1 |
|
return $this; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* @inheritdoc |
|
150
|
|
|
*/ |
|
151
|
1 |
|
public function on_leave_entity($types, \Closure $listener) { |
|
152
|
1 |
|
$this->on_enter_or_leave_something("listeners_leave_entity", $types, $listener); |
|
153
|
1 |
|
return $this; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* @inheritdoc |
|
158
|
|
|
*/ |
|
159
|
22 |
|
public function on_enter_misc($classes, \Closure $listener) { |
|
160
|
22 |
|
$this->on_enter_or_leave_something("listeners_enter_misc", $classes, $listener); |
|
161
|
22 |
|
return $this; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* @inheritdoc |
|
166
|
|
|
*/ |
|
167
|
1 |
|
public function on_leave_misc($classes, \Closure $listener) { |
|
168
|
1 |
|
$this->on_enter_or_leave_something("listeners_leave_misc", $classes, $listener); |
|
169
|
1 |
|
return $this; |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
// generalizes over over on_enter/leave_xx |
|
173
|
22 |
|
protected function on_enter_or_leave_something($what, $things, \Closure $listener) { |
|
174
|
22 |
|
$loc = &$this->$what; |
|
175
|
22 |
|
if ($things === null) { |
|
176
|
1 |
|
$loc[0][] = $listener; |
|
177
|
1 |
|
} |
|
178
|
|
|
else { |
|
179
|
22 |
|
foreach ($things as $thing) { |
|
180
|
22 |
|
assert('is_string($thing)'); |
|
181
|
22 |
|
if (!array_key_exists($thing, $loc)) { |
|
182
|
22 |
|
$loc[$thing] = array(); |
|
183
|
22 |
|
} |
|
184
|
22 |
|
$loc[$thing][] = $listener; |
|
185
|
22 |
|
} |
|
186
|
|
|
} |
|
187
|
22 |
|
} |
|
188
|
|
|
|
|
189
|
|
|
// generalizes over calls to misc listeners |
|
190
|
22 |
View Code Duplication |
protected function call_misc_listener($which, $node) { |
|
|
|
|
|
|
191
|
22 |
|
$listeners = &$this->$which; |
|
192
|
22 |
|
foreach ($listeners[0] as $listener) { |
|
193
|
|
|
$listener($this->insert, $this, $node); |
|
194
|
22 |
|
} |
|
195
|
22 |
|
$cls = get_class($node); |
|
196
|
22 |
|
if (array_key_exists($cls, $listeners)) { |
|
197
|
20 |
|
foreach ($listeners[$cls] as $listener) { |
|
198
|
20 |
|
$listener($this->insert, $this, $node); |
|
199
|
20 |
|
} |
|
200
|
20 |
|
} |
|
201
|
22 |
|
} |
|
202
|
|
|
|
|
203
|
22 |
View Code Duplication |
protected function call_entity_listener($which, $type, $id, $node) { |
|
|
|
|
|
|
204
|
22 |
|
$listeners = &$this->$which; |
|
205
|
22 |
|
foreach ($listeners[0] as $listener) { |
|
206
|
1 |
|
$listener($this->insert, $this, $type, $id, $node); |
|
207
|
22 |
|
} |
|
208
|
22 |
|
if (array_key_exists($type, $listeners)) { |
|
209
|
|
|
foreach ($listeners[$type] as $listener) { |
|
210
|
|
|
$listener($this->insert, $this, $type, $id, $node); |
|
211
|
|
|
} |
|
212
|
|
|
} |
|
213
|
22 |
|
} |
|
214
|
|
|
|
|
215
|
|
|
// from Location |
|
216
|
|
|
|
|
217
|
|
|
/** |
|
218
|
|
|
* @inheritdoc |
|
219
|
|
|
*/ |
|
220
|
16 |
|
public function file_path() { |
|
221
|
16 |
|
return $this->file_path; |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
/** |
|
225
|
|
|
* @inheritdoc |
|
226
|
|
|
*/ |
|
227
|
16 |
|
public function file_content($from_line = null, $to_line = null) { |
|
228
|
16 |
|
if ($from_line !== null) { |
|
229
|
16 |
|
assert('$to_line !== null'); |
|
230
|
16 |
|
return $this->lines_from_to($from_line, $to_line); |
|
231
|
|
|
} |
|
232
|
|
|
else { |
|
233
|
|
|
assert('$to_line === null'); |
|
234
|
|
|
return implode("\n", $this->file_content); |
|
235
|
|
|
} |
|
236
|
|
|
} |
|
237
|
|
|
|
|
238
|
|
|
/** |
|
239
|
|
|
* @inheritdoc |
|
240
|
|
|
*/ |
|
241
|
16 |
|
public function in_entities() { |
|
242
|
16 |
|
return $this->entity_stack; |
|
243
|
|
|
} |
|
244
|
|
|
|
|
245
|
|
|
// from \PhpParser\NodeVisitor |
|
246
|
|
|
|
|
247
|
|
|
/** |
|
248
|
|
|
* @inheritdoc |
|
249
|
|
|
*/ |
|
250
|
22 |
|
public function beforeTraverse(array $nodes) { |
|
251
|
|
|
// for sure found a file |
|
252
|
22 |
|
$id = $this->insert->entity |
|
253
|
22 |
|
( Variable::FILE_TYPE |
|
254
|
22 |
|
, $this->file_path |
|
255
|
22 |
|
, $this->file_path |
|
256
|
22 |
|
, 1 |
|
257
|
22 |
|
, count($this->file_content) |
|
258
|
22 |
|
, implode("\n", $this->file_content) |
|
259
|
22 |
|
); |
|
260
|
|
|
|
|
261
|
22 |
|
$this->entity_stack[] = array(Variable::FILE_TYPE, $id); |
|
262
|
|
|
|
|
263
|
22 |
|
$this->call_entity_listener("listeners_enter_entity", Variable::FILE_TYPE, $id, null); |
|
264
|
|
|
|
|
265
|
22 |
|
return null; |
|
266
|
|
|
} |
|
267
|
|
|
|
|
268
|
|
|
/** |
|
269
|
|
|
* @inheritdoc |
|
270
|
|
|
*/ |
|
271
|
22 |
|
public function afterTraverse(array $nodes) { |
|
272
|
22 |
|
list($type, $id) = array_pop($this->entity_stack); |
|
273
|
|
|
|
|
274
|
22 |
|
$this->call_entity_listener("listeners_leave_entity", $type, $id, null); |
|
275
|
|
|
|
|
276
|
22 |
|
return null; |
|
277
|
|
|
} |
|
278
|
|
|
|
|
279
|
|
|
/** |
|
280
|
|
|
* @inheritdoc |
|
281
|
|
|
*/ |
|
282
|
22 |
|
public function enterNode(\PhpParser\Node $node) { |
|
283
|
22 |
|
$start_line = $node->getAttribute("startLine"); |
|
284
|
22 |
|
$end_line = $node->getAttribute("endLine"); |
|
285
|
22 |
|
$source = $this->lines_from_to($start_line, $end_line); |
|
286
|
|
|
|
|
287
|
22 |
|
$id = null; |
|
288
|
22 |
|
$type = null; |
|
289
|
|
|
|
|
290
|
|
|
// Class |
|
291
|
22 |
|
if ($node instanceof N\Stmt\Class_) { |
|
292
|
21 |
|
$type = Variable::CLASS_TYPE; |
|
293
|
21 |
|
$id = $this->insert->entity |
|
294
|
21 |
|
( $type |
|
295
|
21 |
|
, $node->name |
|
296
|
21 |
|
, $this->file_path |
|
297
|
21 |
|
, $start_line |
|
298
|
21 |
|
, $end_line |
|
299
|
21 |
|
, $source |
|
300
|
21 |
|
); |
|
301
|
21 |
|
} |
|
302
|
|
|
// Method or Function |
|
303
|
22 |
View Code Duplication |
elseif ($node instanceof N\Stmt\ClassMethod) { |
|
|
|
|
|
|
304
|
21 |
|
$type = Variable::METHOD_TYPE; |
|
305
|
21 |
|
$id = $this->insert->entity |
|
306
|
21 |
|
( $type |
|
307
|
21 |
|
, $node->name |
|
308
|
21 |
|
, $this->file_path |
|
309
|
21 |
|
, $start_line |
|
310
|
21 |
|
, $end_line |
|
311
|
21 |
|
, $source |
|
312
|
21 |
|
); |
|
313
|
21 |
|
} |
|
314
|
22 |
View Code Duplication |
elseif ($node instanceof N\Stmt\Function_) { |
|
|
|
|
|
|
315
|
|
|
$type = Variable::FUNCTION_TYPE; |
|
316
|
|
|
$id = $this->insert->entity |
|
317
|
|
|
( $type |
|
318
|
|
|
, $node->name |
|
319
|
|
|
, $this->file_path |
|
320
|
|
|
, $start_line |
|
321
|
|
|
, $end_line |
|
322
|
|
|
, $source |
|
323
|
|
|
); |
|
324
|
|
|
} |
|
325
|
|
|
|
|
326
|
22 |
|
if ($id !== null) { |
|
327
|
21 |
|
$this->call_entity_listener("listeners_enter_entity", $type, $id, $node); |
|
328
|
21 |
|
$this->entity_stack[] = array($type, $id); |
|
329
|
21 |
|
} |
|
330
|
|
|
else { |
|
331
|
22 |
|
$this->call_misc_listener("listeners_enter_misc", $node); |
|
332
|
|
|
} |
|
333
|
22 |
|
} |
|
334
|
|
|
|
|
335
|
|
|
/** |
|
336
|
|
|
* @inheritdoc |
|
337
|
|
|
*/ |
|
338
|
22 |
|
public function leaveNode(\PhpParser\Node $node) { |
|
339
|
|
|
// Class |
|
340
|
|
|
if($node instanceof N\Stmt\Class_ |
|
341
|
22 |
|
or $node instanceof N\Stmt\ClassMethod |
|
|
|
|
|
|
342
|
22 |
|
or $node instanceof N\Stmt\Function_) { |
|
|
|
|
|
|
343
|
21 |
|
list($type, $id) = array_pop($this->entity_stack); |
|
344
|
21 |
|
$this->call_entity_listener("listeners_leave_entity", $type, $id, $node); |
|
345
|
21 |
|
} |
|
346
|
|
|
else { |
|
347
|
22 |
|
$this->call_misc_listener("listeners_leave_misc", $node); |
|
348
|
|
|
} |
|
349
|
22 |
|
} |
|
350
|
|
|
} |
|
351
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.