Complex classes like ClassDefinition often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ClassDefinition, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
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 = []; |
||
31 | |||
32 | /** |
||
33 | * Class properties |
||
34 | * |
||
35 | * @var Node\Stmt\PropertyProperty[] |
||
36 | */ |
||
37 | protected $properties = []; |
||
38 | |||
39 | /** |
||
40 | * Property Statements |
||
41 | * |
||
42 | * @var Node\Stmt\Property[] |
||
43 | */ |
||
44 | protected $propertyStatements = []; |
||
45 | |||
46 | /** |
||
47 | * Class constants |
||
48 | * |
||
49 | * @var Node\Stmt\Const_[] |
||
50 | */ |
||
51 | protected $constants = []; |
||
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 = []; |
||
79 | |||
80 | /** |
||
81 | * @param string $name |
||
82 | * @param Node\Stmt\Class_ $statement |
||
83 | * @param integer $type |
||
84 | */ |
||
85 | 878 | public function __construct($name, Node\Stmt\Class_ $statement = null, $type = 0) |
|
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 | 44 | public function addMethod(ClassMethod $classMethod, $overwrite = true) |
|
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) |
|
132 | |||
133 | /** |
||
134 | * @param Context $context |
||
135 | * @return $this |
||
136 | */ |
||
137 | 46 | public function compile(Context $context) |
|
235 | |||
236 | /** |
||
237 | * @param string $name |
||
238 | * @param boolean|false $inherit |
||
239 | * @return bool |
||
240 | */ |
||
241 | 2 | public function hasMethod($name, $inherit = false) |
|
254 | |||
255 | /** |
||
256 | * @param string $name |
||
257 | * @param bool $inherit |
||
258 | * @return bool |
||
259 | */ |
||
260 | 2 | public function hasConst($name, $inherit = false) |
|
268 | |||
269 | /** |
||
270 | * @param $name |
||
271 | * @param boolean|false $inherit |
||
272 | * @return ClassMethod|null |
||
273 | */ |
||
274 | 2 | public function getMethod($name, $inherit = false) |
|
286 | |||
287 | /** |
||
288 | * @param $name |
||
289 | * @param bool $inherit |
||
290 | * @return bool |
||
291 | */ |
||
292 | 1 | public function hasProperty($name, $inherit = false) |
|
300 | |||
301 | /** |
||
302 | * @param string $name |
||
303 | * @param bool $inherit |
||
304 | * @return Node\Stmt\PropertyProperty |
||
305 | */ |
||
306 | public function getProperty($name, $inherit = false) |
||
320 | |||
321 | /** |
||
322 | * @param string $name |
||
323 | * @param bool $inherit |
||
324 | * @return Node\Stmt\Property |
||
325 | */ |
||
326 | public function getPropertyStatement($name, $inherit = false) |
||
338 | |||
339 | /** |
||
340 | * @return string |
||
341 | */ |
||
342 | 46 | public function getFilepath() |
|
346 | |||
347 | /** |
||
348 | * @param string $filepath |
||
349 | */ |
||
350 | 46 | public function setFilepath($filepath) |
|
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() |
|
370 | |||
371 | /** |
||
372 | * @param null|string $extendsClass |
||
373 | */ |
||
374 | public function setExtendsClass($extendsClass) |
||
378 | |||
379 | /** |
||
380 | * @return null|ClassDefinition |
||
381 | */ |
||
382 | public function getExtendsClassDefinition() |
||
386 | |||
387 | /** |
||
388 | * @param ClassDefinition $extendsClassDefinition |
||
389 | */ |
||
390 | 1 | public function setExtendsClassDefinition(ClassDefinition $extendsClassDefinition) |
|
394 | |||
395 | /** |
||
396 | * @param string $interface |
||
397 | */ |
||
398 | public function addInterface($interface) |
||
402 | |||
403 | /** |
||
404 | * @return null|string |
||
405 | */ |
||
406 | 46 | public function getExtendsClass() |
|
410 | |||
411 | /** |
||
412 | * @param TraitDefinition $definition |
||
413 | * @param Node\Stmt\TraitUseAdaptation\Alias[] $adaptations |
||
414 | */ |
||
415 | public function mergeTrait(TraitDefinition $definition, array $adaptations) |
||
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.