|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @author Patsura Dmitry https://github.com/ovr <[email protected]> |
|
4
|
|
|
*/ |
|
5
|
|
|
|
|
6
|
|
|
namespace PHPSA\Definition; |
|
7
|
|
|
|
|
8
|
|
|
use PHPSA\CompiledExpression; |
|
9
|
|
|
use PHPSA\Context; |
|
10
|
|
|
use PhpParser\Node; |
|
11
|
|
|
use PHPSA\Variable; |
|
12
|
|
|
use PHPSA\Compiler\Event; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Class ClassDefinition |
|
16
|
|
|
* @package PHPSA\Definition |
|
17
|
|
|
*/ |
|
18
|
|
|
class ClassDefinition extends ParentDefinition |
|
|
|
|
|
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @var int |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $type; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Class methods |
|
27
|
|
|
* |
|
28
|
|
|
* @var ClassMethod[] |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $methods = array(); |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Class properties |
|
34
|
|
|
* |
|
35
|
|
|
* @var Node\Stmt\PropertyProperty[] |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $properties = array(); |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Property Statements |
|
41
|
|
|
* |
|
42
|
|
|
* @var Node\Stmt\Property[] |
|
43
|
|
|
*/ |
|
44
|
|
|
protected $propertyStatements = array(); |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Class constants |
|
48
|
|
|
* |
|
49
|
|
|
* @var Node\Stmt\Const_[] |
|
50
|
|
|
*/ |
|
51
|
|
|
protected $constants = array(); |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @todo Use Finder |
|
55
|
|
|
* |
|
56
|
|
|
* @var string |
|
57
|
|
|
*/ |
|
58
|
|
|
protected $filepath; |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @var Node\Stmt\Class_|null |
|
62
|
|
|
*/ |
|
63
|
|
|
protected $statement; |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @var string|null |
|
67
|
|
|
*/ |
|
68
|
|
|
protected $extendsClass; |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @var ClassDefinition|null |
|
72
|
|
|
*/ |
|
73
|
|
|
protected $extendsClassDefinition; |
|
|
|
|
|
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @var array |
|
77
|
|
|
*/ |
|
78
|
|
|
protected $interfaces = array(); |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @param string $name |
|
82
|
|
|
* @param Node\Stmt\Class_ $statement |
|
83
|
|
|
* @param integer $type |
|
84
|
|
|
*/ |
|
85
|
874 |
|
public function __construct($name, Node\Stmt\Class_ $statement = null, $type = 0) |
|
86
|
|
|
{ |
|
87
|
874 |
|
$this->name = $name; |
|
88
|
874 |
|
$this->statement = $statement; |
|
89
|
874 |
|
$this->type = $type; |
|
90
|
874 |
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @param ClassMethod $classMethod |
|
94
|
|
|
* @param bool $overwrite Should we overwrite method if it already exists |
|
95
|
|
|
* @return bool Did we overwrite method? |
|
96
|
|
|
*/ |
|
97
|
41 |
|
public function addMethod(ClassMethod $classMethod, $overwrite = true) |
|
98
|
|
|
{ |
|
99
|
41 |
|
if ($overwrite) { |
|
100
|
41 |
|
$this->methods[$classMethod->getName()] = $classMethod; |
|
101
|
41 |
|
} else { |
|
102
|
1 |
|
$name = $classMethod->getName(); |
|
103
|
|
|
if (isset($this->methods[$name])) { |
|
104
|
|
|
return false; |
|
105
|
|
|
} else { |
|
106
|
|
|
$this->methods[$name] = $classMethod; |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
41 |
|
return true; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* @param Node\Stmt\Property $property |
|
115
|
|
|
*/ |
|
116
|
8 |
|
public function addProperty(Node\Stmt\Property $property) |
|
117
|
|
|
{ |
|
118
|
8 |
|
foreach ($property->props as $propertyDefinition) { |
|
119
|
8 |
|
$this->properties[$propertyDefinition->name] = $propertyDefinition; |
|
120
|
8 |
|
} |
|
121
|
|
|
|
|
122
|
8 |
|
$this->propertyStatements[$propertyDefinition->name] = $property; |
|
|
|
|
|
|
123
|
8 |
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* @param Node\Stmt\ClassConst $const |
|
127
|
|
|
*/ |
|
128
|
4 |
|
public function addConst(Node\Stmt\ClassConst $const) |
|
129
|
|
|
{ |
|
130
|
4 |
|
$this->constants[$const->consts[0]->name] = $const; |
|
131
|
4 |
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* @param Context $context |
|
135
|
|
|
* @return $this |
|
136
|
|
|
*/ |
|
137
|
43 |
|
public function compile(Context $context) |
|
|
|
|
|
|
138
|
|
|
{ |
|
139
|
43 |
|
if ($this->compiled) { |
|
140
|
|
|
return true; |
|
|
|
|
|
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
43 |
|
$context->getEventManager()->fire( |
|
144
|
43 |
|
Event\StatementBeforeCompile::EVENT_NAME, |
|
145
|
43 |
|
new Event\StatementBeforeCompile( |
|
146
|
43 |
|
$this->statement, |
|
|
|
|
|
|
147
|
|
|
$context |
|
148
|
43 |
|
) |
|
149
|
43 |
|
); |
|
150
|
|
|
|
|
151
|
43 |
|
$this->compiled = true; |
|
152
|
43 |
|
$context->setFilepath($this->filepath); |
|
153
|
43 |
|
$context->setScope($this); |
|
154
|
|
|
|
|
155
|
|
|
// Compile event for properties |
|
156
|
43 |
|
foreach ($this->properties as $property) { |
|
157
|
7 |
|
if (!$property->default) { |
|
158
|
3 |
|
continue; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
// fire expression event for property default |
|
162
|
6 |
|
$context->getEventManager()->fire( |
|
163
|
6 |
|
Event\ExpressionBeforeCompile::EVENT_NAME, |
|
164
|
6 |
|
new Event\ExpressionBeforeCompile( |
|
165
|
6 |
|
$property->default, |
|
166
|
|
|
$context |
|
167
|
6 |
|
) |
|
168
|
6 |
|
); |
|
169
|
43 |
|
} |
|
170
|
|
|
|
|
171
|
|
|
// Compile event for PropertyProperty |
|
172
|
43 |
|
foreach ($this->properties as $property) { |
|
173
|
7 |
|
$context->getEventManager()->fire( |
|
174
|
7 |
|
Event\StatementBeforeCompile::EVENT_NAME, |
|
175
|
7 |
|
new Event\StatementBeforeCompile( |
|
176
|
7 |
|
$property, |
|
177
|
|
|
$context |
|
178
|
7 |
|
) |
|
179
|
7 |
|
); |
|
180
|
43 |
|
} |
|
181
|
|
|
|
|
182
|
|
|
// Compile event for constants |
|
183
|
43 |
|
foreach ($this->constants as $const) { |
|
184
|
2 |
|
$context->getEventManager()->fire( |
|
185
|
2 |
|
Event\StatementBeforeCompile::EVENT_NAME, |
|
186
|
2 |
|
new Event\StatementBeforeCompile( |
|
187
|
2 |
|
$const, |
|
188
|
|
|
$context |
|
189
|
2 |
|
) |
|
190
|
2 |
|
); |
|
191
|
43 |
|
} |
|
192
|
|
|
|
|
193
|
|
|
// Compiler event for property statements |
|
194
|
43 |
|
foreach ($this->propertyStatements as $prop) { |
|
195
|
7 |
|
$context->getEventManager()->fire( |
|
196
|
7 |
|
Event\StatementBeforeCompile::EVENT_NAME, |
|
197
|
7 |
|
new Event\StatementBeforeCompile( |
|
198
|
7 |
|
$prop, |
|
199
|
|
|
$context |
|
200
|
7 |
|
) |
|
201
|
7 |
|
); |
|
202
|
43 |
|
} |
|
203
|
|
|
|
|
204
|
|
|
// Compile each method |
|
205
|
43 |
|
foreach ($this->methods as $method) { |
|
206
|
40 |
|
$context->clearSymbols(); |
|
207
|
|
|
|
|
208
|
40 |
|
if (!$method->isStatic()) { |
|
209
|
39 |
|
$thisPtr = new Variable('this', $this, CompiledExpression::OBJECT); |
|
210
|
39 |
|
$thisPtr->incGets(); |
|
211
|
39 |
|
$context->addVariable($thisPtr); |
|
212
|
39 |
|
} |
|
213
|
|
|
|
|
214
|
40 |
|
$method->compile($context); |
|
215
|
|
|
|
|
216
|
40 |
|
$symbols = $context->getSymbols(); |
|
217
|
40 |
|
if (count($symbols) > 0) { |
|
218
|
39 |
|
foreach ($symbols as $name => $variable) { |
|
219
|
39 |
|
if ($variable->isUnused()) { |
|
220
|
10 |
|
$context->warning( |
|
221
|
10 |
|
'unused-' . $variable->getSymbolType(), |
|
222
|
10 |
|
sprintf( |
|
223
|
10 |
|
'Unused ' . $variable->getSymbolType() . ' $%s in method %s()', |
|
224
|
10 |
|
$variable->getName(), |
|
225
|
10 |
|
$method->getName() |
|
226
|
10 |
|
) |
|
227
|
10 |
|
); |
|
228
|
10 |
|
} |
|
229
|
39 |
|
} |
|
230
|
39 |
|
} |
|
231
|
43 |
|
} |
|
232
|
|
|
|
|
233
|
43 |
|
return $this; |
|
|
|
|
|
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
/** |
|
237
|
|
|
* @param string $name |
|
238
|
|
|
* @param boolean|false $inherit |
|
239
|
|
|
* @return bool |
|
240
|
|
|
*/ |
|
241
|
2 |
|
public function hasMethod($name, $inherit = false) |
|
242
|
|
|
{ |
|
243
|
2 |
|
if (isset($this->methods[$name])) { |
|
244
|
2 |
|
return true; |
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
1 |
|
if ($inherit && $this->extendsClassDefinition && $this->extendsClassDefinition->hasMethod($name, $inherit)) { |
|
248
|
|
|
$method = $this->extendsClassDefinition->getMethod($name, $inherit); |
|
249
|
|
|
return $method && ($method->isPublic() || $method->isProtected()); |
|
250
|
|
|
} |
|
251
|
|
|
|
|
252
|
1 |
|
return false; |
|
253
|
|
|
} |
|
254
|
|
|
|
|
255
|
|
|
/** |
|
256
|
|
|
* @param string $name |
|
257
|
|
|
* @param bool $inherit |
|
258
|
|
|
* @return bool |
|
259
|
|
|
*/ |
|
260
|
2 |
|
public function hasConst($name, $inherit = false) |
|
261
|
|
|
{ |
|
262
|
2 |
|
if ($inherit && $this->extendsClassDefinition && $this->extendsClassDefinition->hasConst($name, $inherit)) { |
|
263
|
1 |
|
return true; |
|
264
|
|
|
} |
|
265
|
|
|
|
|
266
|
2 |
|
return isset($this->constants[$name]); |
|
267
|
|
|
} |
|
268
|
|
|
|
|
269
|
|
|
/** |
|
270
|
|
|
* @param $name |
|
271
|
|
|
* @param boolean|false $inherit |
|
272
|
|
|
* @return ClassMethod|null |
|
273
|
|
|
*/ |
|
274
|
2 |
|
public function getMethod($name, $inherit = false) |
|
275
|
|
|
{ |
|
276
|
2 |
|
if (isset($this->methods[$name])) { |
|
277
|
2 |
|
return $this->methods[$name]; |
|
278
|
|
|
} |
|
279
|
|
|
|
|
280
|
|
|
if ($inherit && $this->extendsClassDefinition) { |
|
281
|
|
|
return $this->extendsClassDefinition->getMethod($name, $inherit); |
|
282
|
|
|
} |
|
283
|
|
|
|
|
284
|
|
|
return null; |
|
285
|
|
|
} |
|
286
|
|
|
|
|
287
|
|
|
/** |
|
288
|
|
|
* @param $name |
|
289
|
|
|
* @param bool $inherit |
|
290
|
|
|
* @return bool |
|
291
|
|
|
*/ |
|
292
|
1 |
|
public function hasProperty($name, $inherit = false) |
|
293
|
|
|
{ |
|
294
|
1 |
|
if (isset($this->properties[$name])) { |
|
295
|
1 |
|
return isset($this->properties[$name]); |
|
296
|
|
|
} |
|
297
|
|
|
|
|
298
|
1 |
|
return $inherit && $this->extendsClassDefinition && $this->extendsClassDefinition->hasProperty($name, true); |
|
299
|
|
|
} |
|
300
|
|
|
|
|
301
|
|
|
/** |
|
302
|
|
|
* @param string $name |
|
303
|
|
|
* @param bool $inherit |
|
304
|
|
|
* @return Node\Stmt\PropertyProperty |
|
305
|
|
|
*/ |
|
306
|
|
|
public function getProperty($name, $inherit = false) |
|
307
|
|
|
{ |
|
308
|
|
|
assert($this->hasProperty($name, $inherit)); |
|
309
|
|
|
|
|
310
|
|
|
if (isset($this->properties[$name])) { |
|
311
|
|
|
return $this->properties[$name]; |
|
312
|
|
|
} |
|
313
|
|
|
|
|
314
|
|
|
if ($inherit && $this->extendsClassDefinition) { |
|
315
|
|
|
return $this->extendsClassDefinition->getProperty($name, true); |
|
316
|
|
|
} |
|
317
|
|
|
|
|
318
|
|
|
return null; |
|
319
|
|
|
} |
|
320
|
|
|
|
|
321
|
|
|
/** |
|
322
|
|
|
* @param string $name |
|
323
|
|
|
* @param bool $inherit |
|
324
|
|
|
* @return Node\Stmt\Property |
|
325
|
|
|
*/ |
|
326
|
|
|
public function getPropertyStatement($name, $inherit = false) |
|
327
|
|
|
{ |
|
328
|
|
|
if (isset($this->propertyStatements[$name])) { |
|
329
|
|
|
return $this->propertyStatements[$name]; |
|
330
|
|
|
} |
|
331
|
|
|
|
|
332
|
|
|
if ($inherit && $this->extendsClassDefinition) { |
|
333
|
|
|
return $this->extendsClassDefinition->getPropertyStatement($name, true); |
|
334
|
|
|
} |
|
335
|
|
|
|
|
336
|
|
|
return null; |
|
337
|
|
|
} |
|
338
|
|
|
|
|
339
|
|
|
/** |
|
340
|
|
|
* @return string |
|
341
|
|
|
*/ |
|
342
|
43 |
|
public function getFilepath() |
|
343
|
|
|
{ |
|
344
|
43 |
|
return $this->filepath; |
|
345
|
|
|
} |
|
346
|
|
|
|
|
347
|
|
|
/** |
|
348
|
|
|
* @param string $filepath |
|
349
|
|
|
*/ |
|
350
|
43 |
|
public function setFilepath($filepath) |
|
351
|
|
|
{ |
|
352
|
43 |
|
$this->filepath = $filepath; |
|
353
|
43 |
|
} |
|
354
|
|
|
|
|
355
|
|
|
/** |
|
356
|
|
|
* @return bool |
|
357
|
|
|
*/ |
|
358
|
1 |
|
public function isAbstract() |
|
359
|
|
|
{ |
|
360
|
1 |
|
return (bool) ($this->type & Node\Stmt\Class_::MODIFIER_ABSTRACT); |
|
361
|
|
|
} |
|
362
|
|
|
|
|
363
|
|
|
/** |
|
364
|
|
|
* @return bool |
|
365
|
|
|
*/ |
|
366
|
1 |
|
public function isFinal() |
|
367
|
|
|
{ |
|
368
|
1 |
|
return (bool) ($this->type & Node\Stmt\Class_::MODIFIER_FINAL); |
|
369
|
|
|
} |
|
370
|
|
|
|
|
371
|
|
|
/** |
|
372
|
|
|
* @param null|string $extendsClass |
|
373
|
|
|
*/ |
|
374
|
|
|
public function setExtendsClass($extendsClass) |
|
375
|
|
|
{ |
|
376
|
|
|
$this->extendsClass = $extendsClass; |
|
377
|
|
|
} |
|
378
|
|
|
|
|
379
|
|
|
/** |
|
380
|
|
|
* @return null|ClassDefinition |
|
381
|
|
|
*/ |
|
382
|
|
|
public function getExtendsClassDefinition() |
|
383
|
|
|
{ |
|
384
|
|
|
return $this->extendsClassDefinition; |
|
385
|
|
|
} |
|
386
|
|
|
|
|
387
|
|
|
/** |
|
388
|
|
|
* @param ClassDefinition $extendsClassDefinition |
|
389
|
|
|
*/ |
|
390
|
1 |
|
public function setExtendsClassDefinition(ClassDefinition $extendsClassDefinition) |
|
|
|
|
|
|
391
|
|
|
{ |
|
392
|
1 |
|
$this->extendsClassDefinition = $extendsClassDefinition; |
|
393
|
1 |
|
} |
|
394
|
|
|
|
|
395
|
|
|
/** |
|
396
|
|
|
* @param string $interface |
|
397
|
|
|
*/ |
|
398
|
|
|
public function addInterface($interface) |
|
399
|
|
|
{ |
|
400
|
|
|
$this->interfaces[] = $interface; |
|
401
|
|
|
} |
|
402
|
|
|
|
|
403
|
|
|
/** |
|
404
|
|
|
* @return null|string |
|
405
|
|
|
*/ |
|
406
|
43 |
|
public function getExtendsClass() |
|
407
|
|
|
{ |
|
408
|
43 |
|
return $this->extendsClass; |
|
409
|
|
|
} |
|
410
|
|
|
|
|
411
|
|
|
/** |
|
412
|
|
|
* @param TraitDefinition $definition |
|
413
|
|
|
* @param Node\Stmt\TraitUseAdaptation\Alias[] $adaptations |
|
414
|
|
|
*/ |
|
415
|
|
|
public function mergeTrait(TraitDefinition $definition, array $adaptations) |
|
416
|
|
|
{ |
|
417
|
|
|
$methods = $definition->getMethods(); |
|
418
|
|
|
if ($methods) { |
|
|
|
|
|
|
419
|
|
|
foreach ($adaptations as $adaptation) { |
|
420
|
|
|
// We don't support Trait name for now |
|
421
|
|
|
if (!$adaptation->trait) { |
|
422
|
|
|
$methodNameFromTrait = $adaptation->method; |
|
423
|
|
|
if (isset($methods[$methodNameFromTrait])) { |
|
424
|
|
|
/** @var ClassMethod $method Method from Trait */ |
|
425
|
|
|
$method = $methods[$methodNameFromTrait]; |
|
426
|
|
|
if ($adaptation->newName |
|
|
|
|
|
|
427
|
|
|
|| ($adaptation->newModifier && $method->getModifier() != $adaptation->newModifier)) { |
|
|
|
|
|
|
428
|
|
|
// Don't modify original method from Trait |
|
429
|
|
|
$method = clone $method; |
|
430
|
|
|
$method->setName($adaptation->newName); |
|
431
|
|
|
$method->setModifier($adaptation->newModifier); |
|
432
|
|
|
|
|
433
|
|
|
$methods[$methodNameFromTrait] = $method; |
|
434
|
|
|
} |
|
435
|
|
|
} |
|
436
|
|
|
} |
|
437
|
|
|
} |
|
438
|
|
|
|
|
439
|
|
|
foreach ($methods as $method) { |
|
440
|
|
|
$this->addMethod($method, false); |
|
441
|
|
|
} |
|
442
|
|
|
} |
|
443
|
|
|
} |
|
444
|
|
|
} |
|
445
|
|
|
|
The class complexity is the sum of the complexity of all methods. A very high value is usually an indication that your class does not follow the single reponsibility principle and does more than one job.
Some resources for further reading:
You can also find more detailed suggestions for refactoring in the “Code” section of your repository.