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
|
40 |
|
public function addMethod(ClassMethod $classMethod, $overwrite = true) |
98
|
|
|
{ |
99
|
40 |
|
if ($overwrite) { |
100
|
40 |
|
$this->methods[$classMethod->getName()] = $classMethod; |
101
|
40 |
|
} 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
|
40 |
|
return true; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @param Node\Stmt\Property $property |
115
|
|
|
*/ |
116
|
7 |
|
public function addProperty(Node\Stmt\Property $property) |
117
|
|
|
{ |
118
|
7 |
|
foreach ($property->props as $propertyDefinition) { |
119
|
7 |
|
$this->properties[$propertyDefinition->name] = $propertyDefinition; |
120
|
7 |
|
} |
121
|
|
|
|
122
|
7 |
|
$this->propertyStatements[$propertyDefinition->name] = $property; |
|
|
|
|
123
|
7 |
|
} |
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
|
41 |
|
public function compile(Context $context) |
138
|
|
|
{ |
139
|
41 |
|
if ($this->compiled) { |
140
|
|
|
return $this; |
|
|
|
|
141
|
|
|
} |
142
|
|
|
|
143
|
41 |
|
$context->getEventManager()->fire( |
144
|
41 |
|
Event\StatementBeforeCompile::EVENT_NAME, |
145
|
41 |
|
new Event\StatementBeforeCompile( |
146
|
41 |
|
$this->statement, |
|
|
|
|
147
|
|
|
$context |
148
|
41 |
|
) |
149
|
41 |
|
); |
150
|
|
|
|
151
|
41 |
|
$this->compiled = true; |
152
|
41 |
|
$context->setFilepath($this->filepath); |
153
|
41 |
|
$context->setScope($this); |
154
|
|
|
|
155
|
|
|
// Compile event for properties |
156
|
41 |
|
foreach ($this->properties as $property) { |
157
|
6 |
|
if (!$property->default) { |
158
|
2 |
|
continue; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
// fire expression event for property default |
162
|
5 |
|
$context->getEventManager()->fire( |
163
|
5 |
|
Event\ExpressionBeforeCompile::EVENT_NAME, |
164
|
5 |
|
new Event\ExpressionBeforeCompile( |
165
|
5 |
|
$property->default, |
166
|
|
|
$context |
167
|
5 |
|
) |
168
|
5 |
|
); |
169
|
41 |
|
} |
170
|
|
|
|
171
|
|
|
// Compile event for constants |
172
|
41 |
|
foreach ($this->constants as $const) { |
173
|
2 |
|
$context->getEventManager()->fire( |
174
|
2 |
|
Event\StatementBeforeCompile::EVENT_NAME, |
175
|
2 |
|
new Event\StatementBeforeCompile( |
176
|
2 |
|
$const, |
177
|
|
|
$context |
178
|
2 |
|
) |
179
|
2 |
|
); |
180
|
41 |
|
} |
181
|
|
|
|
182
|
|
|
// Compiler event for property statements |
183
|
41 |
|
foreach ($this->propertyStatements as $prop) { |
184
|
6 |
|
$context->getEventManager()->fire( |
185
|
6 |
|
Event\StatementBeforeCompile::EVENT_NAME, |
186
|
6 |
|
new Event\StatementBeforeCompile( |
187
|
6 |
|
$prop, |
188
|
|
|
$context |
189
|
6 |
|
) |
190
|
6 |
|
); |
191
|
41 |
|
} |
192
|
|
|
|
193
|
|
|
// Compile each method |
194
|
41 |
|
foreach ($this->methods as $method) { |
195
|
39 |
|
$context->clearSymbols(); |
196
|
|
|
|
197
|
39 |
|
if (!$method->isStatic()) { |
198
|
38 |
|
$thisPtr = new Variable('this', $this, CompiledExpression::OBJECT); |
199
|
38 |
|
$thisPtr->incGets(); |
200
|
38 |
|
$context->addVariable($thisPtr); |
201
|
38 |
|
} |
202
|
|
|
|
203
|
39 |
|
$method->compile($context); |
204
|
|
|
|
205
|
39 |
|
foreach ($context->getSymbols() as $name => $variable) { |
206
|
38 |
|
if (!$variable->isUnused()) { |
207
|
38 |
|
continue; |
208
|
|
|
} |
209
|
|
|
|
210
|
9 |
|
$context->notice( |
211
|
9 |
|
'unused-' . $variable->getSymbolType(), |
212
|
9 |
|
sprintf( |
213
|
9 |
|
'Unused ' . $variable->getSymbolType() . ' $%s in method %s()', |
214
|
9 |
|
$variable->getName(), |
215
|
9 |
|
$method->getName() |
216
|
9 |
|
), |
217
|
9 |
|
$variable->getDeclarationStatement() |
|
|
|
|
218
|
9 |
|
); |
219
|
39 |
|
} |
220
|
41 |
|
} |
221
|
|
|
|
222
|
41 |
|
return $this; |
|
|
|
|
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* @param string $name |
227
|
|
|
* @param boolean|false $inherit |
228
|
|
|
* @return bool |
229
|
|
|
*/ |
230
|
2 |
|
public function hasMethod($name, $inherit = false) |
231
|
|
|
{ |
232
|
2 |
|
if (isset($this->methods[$name])) { |
233
|
2 |
|
return true; |
234
|
|
|
} |
235
|
|
|
|
236
|
1 |
|
if ($inherit && $this->extendsClassDefinition && $this->extendsClassDefinition->hasMethod($name, $inherit)) { |
237
|
|
|
$method = $this->extendsClassDefinition->getMethod($name, $inherit); |
238
|
|
|
return $method && ($method->isPublic() || $method->isProtected()); |
239
|
|
|
} |
240
|
|
|
|
241
|
1 |
|
return false; |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* @param string $name |
246
|
|
|
* @param bool $inherit |
247
|
|
|
* @return bool |
248
|
|
|
*/ |
249
|
2 |
|
public function hasConst($name, $inherit = false) |
250
|
|
|
{ |
251
|
2 |
|
if ($inherit && $this->extendsClassDefinition && $this->extendsClassDefinition->hasConst($name, $inherit)) { |
252
|
1 |
|
return true; |
253
|
|
|
} |
254
|
|
|
|
255
|
2 |
|
return isset($this->constants[$name]); |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* @param $name |
260
|
|
|
* @param boolean|false $inherit |
261
|
|
|
* @return ClassMethod|null |
262
|
|
|
*/ |
263
|
2 |
|
public function getMethod($name, $inherit = false) |
264
|
|
|
{ |
265
|
2 |
|
if (isset($this->methods[$name])) { |
266
|
2 |
|
return $this->methods[$name]; |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
if ($inherit && $this->extendsClassDefinition) { |
270
|
|
|
return $this->extendsClassDefinition->getMethod($name, $inherit); |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
return null; |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
/** |
277
|
|
|
* @param $name |
278
|
|
|
* @param bool $inherit |
279
|
|
|
* @return bool |
280
|
|
|
*/ |
281
|
3 |
|
public function hasProperty($name, $inherit = false) |
282
|
|
|
{ |
283
|
3 |
|
if (isset($this->properties[$name])) { |
284
|
1 |
|
return isset($this->properties[$name]); |
285
|
|
|
} |
286
|
|
|
|
287
|
3 |
|
return $inherit && $this->extendsClassDefinition && $this->extendsClassDefinition->hasProperty($name, true); |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
/** |
291
|
|
|
* @param string $name |
292
|
|
|
* @param bool $inherit |
293
|
|
|
* @return Node\Stmt\PropertyProperty |
294
|
|
|
*/ |
295
|
|
|
public function getProperty($name, $inherit = false) |
296
|
|
|
{ |
297
|
|
|
assert($this->hasProperty($name, $inherit)); |
298
|
|
|
|
299
|
|
|
if (isset($this->properties[$name])) { |
300
|
|
|
return $this->properties[$name]; |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
if ($inherit && $this->extendsClassDefinition) { |
304
|
|
|
return $this->extendsClassDefinition->getProperty($name, true); |
305
|
|
|
} |
306
|
|
|
|
307
|
|
|
return null; |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
/** |
311
|
|
|
* @param string $name |
312
|
|
|
* @param bool $inherit |
313
|
|
|
* @return Node\Stmt\Property |
314
|
|
|
*/ |
315
|
|
|
public function getPropertyStatement($name, $inherit = false) |
316
|
|
|
{ |
317
|
|
|
if (isset($this->propertyStatements[$name])) { |
318
|
|
|
return $this->propertyStatements[$name]; |
319
|
|
|
} |
320
|
|
|
|
321
|
|
|
if ($inherit && $this->extendsClassDefinition) { |
322
|
|
|
return $this->extendsClassDefinition->getPropertyStatement($name, true); |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
return null; |
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
/** |
329
|
|
|
* @return string |
330
|
|
|
*/ |
331
|
41 |
|
public function getFilepath() |
332
|
|
|
{ |
333
|
41 |
|
return $this->filepath; |
334
|
|
|
} |
335
|
|
|
|
336
|
|
|
/** |
337
|
|
|
* @param string $filepath |
338
|
|
|
*/ |
339
|
41 |
|
public function setFilepath($filepath) |
340
|
|
|
{ |
341
|
41 |
|
$this->filepath = $filepath; |
342
|
41 |
|
} |
343
|
|
|
|
344
|
|
|
/** |
345
|
|
|
* @return bool |
346
|
|
|
*/ |
347
|
|
|
public function isAbstract() |
348
|
|
|
{ |
349
|
|
|
return (bool) ($this->type & Node\Stmt\Class_::MODIFIER_ABSTRACT); |
350
|
|
|
} |
351
|
|
|
|
352
|
|
|
/** |
353
|
|
|
* @return bool |
354
|
|
|
*/ |
355
|
1 |
|
public function isFinal() |
356
|
|
|
{ |
357
|
1 |
|
return (bool) ($this->type & Node\Stmt\Class_::MODIFIER_FINAL); |
358
|
|
|
} |
359
|
|
|
|
360
|
|
|
/** |
361
|
|
|
* @param null|string $extendsClass |
362
|
|
|
*/ |
363
|
|
|
public function setExtendsClass($extendsClass) |
364
|
|
|
{ |
365
|
|
|
$this->extendsClass = $extendsClass; |
366
|
|
|
} |
367
|
|
|
|
368
|
|
|
/** |
369
|
|
|
* @return null|ClassDefinition |
370
|
|
|
*/ |
371
|
|
|
public function getExtendsClassDefinition() |
372
|
|
|
{ |
373
|
|
|
return $this->extendsClassDefinition; |
374
|
|
|
} |
375
|
|
|
|
376
|
|
|
/** |
377
|
|
|
* @param ClassDefinition $extendsClassDefinition |
378
|
|
|
*/ |
379
|
1 |
|
public function setExtendsClassDefinition(ClassDefinition $extendsClassDefinition) |
|
|
|
|
380
|
|
|
{ |
381
|
1 |
|
$this->extendsClassDefinition = $extendsClassDefinition; |
382
|
1 |
|
} |
383
|
|
|
|
384
|
|
|
/** |
385
|
|
|
* @param string $interface |
386
|
|
|
*/ |
387
|
|
|
public function addInterface($interface) |
388
|
|
|
{ |
389
|
|
|
$this->interfaces[] = $interface; |
390
|
|
|
} |
391
|
|
|
|
392
|
|
|
/** |
393
|
|
|
* @return null|string |
394
|
|
|
*/ |
395
|
41 |
|
public function getExtendsClass() |
396
|
|
|
{ |
397
|
41 |
|
return $this->extendsClass; |
398
|
|
|
} |
399
|
|
|
|
400
|
|
|
/** |
401
|
|
|
* @param TraitDefinition $definition |
402
|
|
|
* @param Node\Stmt\TraitUseAdaptation\Alias[] $adaptations |
403
|
|
|
*/ |
404
|
|
|
public function mergeTrait(TraitDefinition $definition, array $adaptations) |
405
|
|
|
{ |
406
|
|
|
$methods = $definition->getMethods(); |
407
|
|
|
if ($methods) { |
|
|
|
|
408
|
|
|
foreach ($adaptations as $adaptation) { |
409
|
|
|
// We don't support Trait name for now |
410
|
|
|
if (!$adaptation->trait) { |
411
|
|
|
$methodNameFromTrait = $adaptation->method; |
412
|
|
|
if (isset($methods[$methodNameFromTrait])) { |
413
|
|
|
/** @var ClassMethod $method Method from Trait */ |
414
|
|
|
$method = $methods[$methodNameFromTrait]; |
415
|
|
|
if ($adaptation->newName |
|
|
|
|
416
|
|
|
|| ($adaptation->newModifier && $method->getModifier() != $adaptation->newModifier)) { |
|
|
|
|
417
|
|
|
// Don't modify original method from Trait |
418
|
|
|
$method = clone $method; |
419
|
|
|
$method->setName($adaptation->newName); |
420
|
|
|
$method->setModifier($adaptation->newModifier); |
421
|
|
|
|
422
|
|
|
$methods[$methodNameFromTrait] = $method; |
423
|
|
|
} |
424
|
|
|
} |
425
|
|
|
} |
426
|
|
|
} |
427
|
|
|
|
428
|
|
|
foreach ($methods as $method) { |
429
|
|
|
$this->addMethod($method, false); |
430
|
|
|
} |
431
|
|
|
} |
432
|
|
|
} |
433
|
|
|
} |
434
|
|
|
|
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.