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 |
||
19 | class ClassDefinition extends ParentDefinition |
||
|
|||
20 | { |
||
21 | /** |
||
22 | * @var int |
||
23 | */ |
||
24 | protected $type; |
||
25 | |||
26 | /** |
||
27 | * Class methods |
||
28 | * |
||
29 | * @var ClassMethod[] |
||
30 | */ |
||
31 | protected $methods = []; |
||
32 | |||
33 | /** |
||
34 | * Class properties |
||
35 | * |
||
36 | * @var Node\Stmt\PropertyProperty[] |
||
37 | */ |
||
38 | protected $properties = []; |
||
39 | |||
40 | /** |
||
41 | * Property Statements |
||
42 | * |
||
43 | * @var Node\Stmt\Property[] |
||
44 | */ |
||
45 | protected $propertyStatements = []; |
||
46 | |||
47 | /** |
||
48 | * Class constants |
||
49 | * |
||
50 | * @var string[] |
||
51 | */ |
||
52 | protected $constants = []; |
||
53 | |||
54 | /** |
||
55 | * @todo Use Finder |
||
56 | * |
||
57 | * @var string |
||
58 | */ |
||
59 | protected $filepath; |
||
60 | |||
61 | /** |
||
62 | * @var Node\Stmt\Class_|null |
||
63 | */ |
||
64 | protected $statement; |
||
65 | |||
66 | /** |
||
67 | * @var string|null |
||
68 | */ |
||
69 | protected $extendsClass; |
||
70 | |||
71 | /** |
||
72 | * @var ClassDefinition|null |
||
73 | */ |
||
74 | protected $extendsClassDefinition; |
||
75 | |||
76 | /** |
||
77 | * @var array |
||
78 | */ |
||
79 | protected $interfaces = []; |
||
80 | |||
81 | /** |
||
82 | * @param string $name |
||
83 | * @param Node\Stmt\Class_ $statement |
||
84 | * @param integer $type |
||
85 | */ |
||
86 | 56 | public function __construct($name, Node\Stmt\Class_ $statement = null, $type = 0) |
|
92 | |||
93 | /** |
||
94 | * @param ClassMethod $classMethod |
||
95 | * @param bool $overwrite Should we overwrite method if it already exists |
||
96 | * @return bool Did we overwrite method? |
||
97 | */ |
||
98 | 48 | public function addMethod(ClassMethod $classMethod, $overwrite = true) |
|
113 | |||
114 | /** |
||
115 | * @param Node\Stmt\Property $property |
||
116 | */ |
||
117 | 8 | public function addProperty(Node\Stmt\Property $property) |
|
125 | |||
126 | /** |
||
127 | * @param Node\Stmt\ClassConst $const |
||
128 | */ |
||
129 | 4 | public function addConst(Node\Stmt\ClassConst $const) |
|
133 | |||
134 | /** |
||
135 | * @param Context $context |
||
136 | * @return $this |
||
137 | */ |
||
138 | 50 | public function compile(Context $context) |
|
236 | |||
237 | /** |
||
238 | * @param string $name |
||
239 | * @param boolean|false $inherit |
||
240 | * @return bool |
||
241 | */ |
||
242 | 4 | public function hasMethod($name, $inherit = false) |
|
243 | { |
||
244 | 4 | if (isset($this->methods[$name])) { |
|
245 | 2 | return true; |
|
246 | } |
||
247 | |||
248 | 3 | if ($inherit && $this->extendsClassDefinition && $this->extendsClassDefinition->hasMethod($name, $inherit)) { |
|
249 | 2 | $method = $this->extendsClassDefinition->getMethod($name, $inherit); |
|
250 | 2 | return $method && ($method->isPublic() || $method->isProtected()); |
|
251 | } |
||
252 | |||
253 | 1 | return false; |
|
254 | } |
||
255 | |||
256 | /** |
||
257 | * @param string $name |
||
258 | * @param bool $inherit |
||
259 | * @return bool |
||
260 | */ |
||
261 | 2 | public function hasConst($name, $inherit = false) |
|
269 | |||
270 | /** |
||
271 | * @param $name |
||
272 | * @param boolean|false $inherit |
||
273 | * @return ClassMethod|null |
||
274 | */ |
||
275 | 4 | public function getMethod($name, $inherit = false) |
|
276 | { |
||
277 | 4 | if (isset($this->methods[$name])) { |
|
278 | 2 | return $this->methods[$name]; |
|
279 | } |
||
280 | |||
281 | 2 | if ($inherit && $this->extendsClassDefinition) { |
|
282 | 2 | return $this->extendsClassDefinition->getMethod($name, $inherit); |
|
283 | } |
||
284 | |||
285 | return null; |
||
286 | } |
||
287 | |||
288 | /** |
||
289 | * @param $name |
||
290 | * @param bool $inherit |
||
291 | * @return bool |
||
292 | */ |
||
293 | 1 | public function hasProperty($name, $inherit = false) |
|
301 | |||
302 | /** |
||
303 | * @param string $name |
||
304 | * @param bool $inherit |
||
305 | * @return Node\Stmt\PropertyProperty |
||
306 | */ |
||
307 | public function getProperty($name, $inherit = false) |
||
321 | |||
322 | /** |
||
323 | * @param string $name |
||
324 | * @param bool $inherit |
||
325 | * @return Node\Stmt\Property |
||
326 | */ |
||
327 | public function getPropertyStatement($name, $inherit = false) |
||
339 | |||
340 | /** |
||
341 | * @return string |
||
342 | */ |
||
343 | 50 | public function getFilepath() |
|
347 | |||
348 | /** |
||
349 | * @param string $filepath |
||
350 | */ |
||
351 | 50 | public function setFilepath($filepath) |
|
355 | |||
356 | /** |
||
357 | * @return bool |
||
358 | */ |
||
359 | 1 | public function isAbstract() |
|
363 | |||
364 | /** |
||
365 | * @return bool |
||
366 | */ |
||
367 | 1 | public function isFinal() |
|
371 | |||
372 | /** |
||
373 | * @param null|string $extendsClass |
||
374 | */ |
||
375 | 2 | public function setExtendsClass($extendsClass) |
|
376 | { |
||
377 | 2 | $this->extendsClass = $extendsClass; |
|
378 | 2 | } |
|
379 | |||
380 | /** |
||
381 | * @return null|ClassDefinition |
||
382 | */ |
||
383 | public function getExtendsClassDefinition() |
||
387 | |||
388 | /** |
||
389 | * @param ClassDefinition $extendsClassDefinition |
||
390 | */ |
||
391 | 3 | public function setExtendsClassDefinition(ClassDefinition $extendsClassDefinition) |
|
395 | |||
396 | /** |
||
397 | * @param string $interface |
||
398 | */ |
||
399 | public function addInterface($interface) |
||
403 | |||
404 | /** |
||
405 | * @return null|string |
||
406 | */ |
||
407 | 50 | public function getExtendsClass() |
|
411 | |||
412 | /** |
||
413 | * @param TraitDefinition $definition |
||
414 | * @param Node\Stmt\TraitUseAdaptation\Alias[] $adaptations |
||
415 | */ |
||
416 | public function mergeTrait(TraitDefinition $definition, array $adaptations) |
||
445 | } |
||
446 |
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.