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 = 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 string[] |
||
| 77 | */ |
||
| 78 | protected $interfaces = []; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @var InterfaceDefinition[] |
||
| 82 | */ |
||
| 83 | protected $interfacesDefinitions = []; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * @param string $name |
||
| 87 | * @param Node\Stmt\Class_ $statement |
||
| 88 | * @param integer $type |
||
| 89 | */ |
||
| 90 | 858 | public function __construct($name, Node\Stmt\Class_ $statement = null, $type = 0) |
|
| 96 | |||
| 97 | /** |
||
| 98 | * @param ClassMethod $methodDefintion |
||
| 99 | */ |
||
| 100 | 25 | public function addMethod(ClassMethod $methodDefintion) |
|
| 104 | |||
| 105 | /** |
||
| 106 | * @param Node\Stmt\Property $property |
||
| 107 | */ |
||
| 108 | 4 | public function addProperty(Node\Stmt\Property $property) |
|
| 116 | |||
| 117 | /** |
||
| 118 | * @param Node\Stmt\ClassConst $const |
||
| 119 | */ |
||
| 120 | 3 | public function addConst(Node\Stmt\ClassConst $const) |
|
| 124 | |||
| 125 | /** |
||
| 126 | * @param Context $context |
||
| 127 | * @return $this |
||
| 128 | */ |
||
| 129 | 25 | public function compile(Context $context) |
|
| 216 | |||
| 217 | /** |
||
| 218 | * @param string $name |
||
| 219 | * @param boolean|false $inherit |
||
| 220 | * @return bool |
||
| 221 | */ |
||
| 222 | 1 | public function hasMethod($name, $inherit = false) |
|
| 235 | |||
| 236 | /** |
||
| 237 | * @param string $name |
||
| 238 | * @param bool $inherit |
||
| 239 | * @return bool |
||
| 240 | */ |
||
| 241 | 2 | public function hasConst($name, $inherit = false) |
|
| 258 | |||
| 259 | /** |
||
| 260 | * @param $name |
||
| 261 | * @param boolean|false $inherit |
||
| 262 | * @return ClassMethod|null |
||
| 263 | */ |
||
| 264 | 1 | public function getMethod($name, $inherit = false) |
|
| 276 | |||
| 277 | /** |
||
| 278 | * @param $name |
||
| 279 | * @param bool $inherit |
||
| 280 | * @return bool |
||
| 281 | */ |
||
| 282 | 3 | public function hasProperty($name, $inherit = false) |
|
| 290 | |||
| 291 | /** |
||
| 292 | * @param string $name |
||
| 293 | * @param bool $inherit |
||
| 294 | * @return Node\Stmt\Property |
||
| 295 | */ |
||
| 296 | public function getProperty($name, $inherit = false) |
||
| 310 | |||
| 311 | /** |
||
| 312 | * @return string |
||
| 313 | */ |
||
| 314 | 25 | public function getFilepath() |
|
| 318 | |||
| 319 | /** |
||
| 320 | * @param string $filepath |
||
| 321 | */ |
||
| 322 | 25 | public function setFilepath($filepath) |
|
| 326 | |||
| 327 | /** |
||
| 328 | * @return bool |
||
| 329 | */ |
||
| 330 | public function isAbstract() |
||
| 334 | |||
| 335 | /** |
||
| 336 | * @param null|string $extendsClass |
||
| 337 | */ |
||
| 338 | public function setExtendsClass($extendsClass) |
||
| 342 | |||
| 343 | /** |
||
| 344 | * @return null|ClassDefinition |
||
| 345 | */ |
||
| 346 | public function getExtendsClassDefinition() |
||
| 350 | |||
| 351 | /** |
||
| 352 | * @param ClassDefinition $extendsClassDefinition |
||
| 353 | */ |
||
| 354 | 1 | public function setExtendsClassDefinition(ClassDefinition $extendsClassDefinition) |
|
| 358 | |||
| 359 | /** |
||
| 360 | * @param string $interface |
||
| 361 | */ |
||
| 362 | public function addInterface($interface) |
||
| 366 | |||
| 367 | /** |
||
| 368 | * @return string[] |
||
| 369 | */ |
||
| 370 | 25 | public function getInterfaces() |
|
| 374 | |||
| 375 | /** |
||
| 376 | * @param InterfaceDefinition $interfaceDefinition |
||
| 377 | */ |
||
| 378 | public function addInterfaceDefinition(InterfaceDefinition $interfaceDefinition) |
||
| 382 | |||
| 383 | /** |
||
| 384 | * @return null|string |
||
| 385 | */ |
||
| 386 | 25 | public function getExtendsClass() |
|
| 390 | |||
| 391 | /** |
||
| 392 | * @param TraitDefinition $definition |
||
| 393 | */ |
||
| 394 | public function mergeTrait(TraitDefinition $definition) |
||
| 403 | } |
||
| 404 |
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.