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 array |
||
| 77 | */ |
||
| 78 | protected $interfaces = array(); |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @param string $name |
||
| 82 | * @param Node\Stmt\Class_ $statement |
||
| 83 | * @param integer $type |
||
| 84 | */ |
||
| 85 | 866 | 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 | 32 | public function addMethod(ClassMethod $classMethod, $overwrite = true) |
|
| 112 | |||
| 113 | /** |
||
| 114 | * @param Node\Stmt\Property $property |
||
| 115 | */ |
||
| 116 | 6 | public function addProperty(Node\Stmt\Property $property) |
|
| 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 | 33 | public function compile(Context $context) |
|
| 224 | |||
| 225 | /** |
||
| 226 | * @param string $name |
||
| 227 | * @param boolean|false $inherit |
||
| 228 | * @return bool |
||
| 229 | */ |
||
| 230 | 2 | public function hasMethod($name, $inherit = false) |
|
| 243 | |||
| 244 | /** |
||
| 245 | * @param string $name |
||
| 246 | * @param bool $inherit |
||
| 247 | * @return bool |
||
| 248 | */ |
||
| 249 | 2 | public function hasConst($name, $inherit = false) |
|
| 257 | |||
| 258 | /** |
||
| 259 | * @param $name |
||
| 260 | * @param boolean|false $inherit |
||
| 261 | * @return ClassMethod|null |
||
| 262 | */ |
||
| 263 | 2 | public function getMethod($name, $inherit = false) |
|
| 275 | |||
| 276 | /** |
||
| 277 | * @param $name |
||
| 278 | * @param bool $inherit |
||
| 279 | * @return bool |
||
| 280 | */ |
||
| 281 | 1 | public function hasProperty($name, $inherit = false) |
|
| 289 | |||
| 290 | /** |
||
| 291 | * @param string $name |
||
| 292 | * @param bool $inherit |
||
| 293 | * @return Node\Stmt\PropertyProperty |
||
| 294 | */ |
||
| 295 | public function getProperty($name, $inherit = false) |
||
| 309 | |||
| 310 | /** |
||
| 311 | * @param string $name |
||
| 312 | * @param bool $inherit |
||
| 313 | 33 | * @return Node\Stmt\Property |
|
| 314 | */ |
||
| 315 | 33 | public function getPropertyStatement($name, $inherit = false) |
|
| 327 | |||
| 328 | /** |
||
| 329 | * @return string |
||
| 330 | */ |
||
| 331 | public function getFilepath() |
||
| 335 | |||
| 336 | /** |
||
| 337 | 1 | * @param string $filepath |
|
| 338 | */ |
||
| 339 | 1 | public function setFilepath($filepath) |
|
| 343 | |||
| 344 | /** |
||
| 345 | * @return bool |
||
| 346 | */ |
||
| 347 | public function isAbstract() |
||
| 351 | |||
| 352 | /** |
||
| 353 | * @return bool |
||
| 354 | */ |
||
| 355 | public function isFinal() |
||
| 359 | |||
| 360 | /** |
||
| 361 | 1 | * @param null|string $extendsClass |
|
| 362 | */ |
||
| 363 | 1 | public function setExtendsClass($extendsClass) |
|
| 367 | |||
| 368 | /** |
||
| 369 | * @return null|ClassDefinition |
||
| 370 | */ |
||
| 371 | public function getExtendsClassDefinition() |
||
| 375 | |||
| 376 | /** |
||
| 377 | 33 | * @param ClassDefinition $extendsClassDefinition |
|
| 378 | */ |
||
| 379 | 33 | public function setExtendsClassDefinition(ClassDefinition $extendsClassDefinition) |
|
| 383 | |||
| 384 | /** |
||
| 385 | * @param string $interface |
||
| 386 | */ |
||
| 387 | public function addInterface($interface) |
||
| 391 | |||
| 392 | /** |
||
| 393 | * @return null|string |
||
| 394 | */ |
||
| 395 | public function getExtendsClass() |
||
| 399 | |||
| 400 | /** |
||
| 401 | * @param TraitDefinition $definition |
||
| 402 | * @param Node\Stmt\TraitUseAdaptation\Alias[] $adaptations |
||
| 403 | */ |
||
| 404 | public function mergeTrait(TraitDefinition $definition, array $adaptations) |
||
| 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.