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) |
|
| 287 | |||
| 288 | /** |
||
| 289 | * @param $name |
||
| 290 | * @param bool $inherit |
||
| 291 | * @param bool $isParent It's an internal parameter |
||
| 292 | * @return bool |
||
| 293 | 1 | */ |
|
| 294 | public function hasProperty($name, $inherit = false, $isParent = true) |
||
| 313 | |||
| 314 | /** |
||
| 315 | * @param string $name |
||
| 316 | * @param bool $inherit |
||
| 317 | * @return Node\Stmt\PropertyProperty |
||
| 318 | */ |
||
| 319 | public function getProperty($name, $inherit = false) |
||
| 333 | |||
| 334 | /** |
||
| 335 | * @param string $name |
||
| 336 | * @param bool $inherit |
||
| 337 | * @return Node\Stmt\Property |
||
| 338 | */ |
||
| 339 | public function getPropertyStatement($name, $inherit = false) |
||
| 351 | 50 | ||
| 352 | /** |
||
| 353 | 50 | * @return string |
|
| 354 | 50 | */ |
|
| 355 | public function getFilepath() |
||
| 359 | 1 | ||
| 360 | /** |
||
| 361 | 1 | * @param string $filepath |
|
| 362 | */ |
||
| 363 | public function setFilepath($filepath) |
||
| 367 | 1 | ||
| 368 | /** |
||
| 369 | 1 | * @return bool |
|
| 370 | */ |
||
| 371 | public function isAbstract() |
||
| 375 | 2 | ||
| 376 | /** |
||
| 377 | 2 | * @return bool |
|
| 378 | 2 | */ |
|
| 379 | public function isFinal() |
||
| 383 | |||
| 384 | /** |
||
| 385 | * @param null|string $extendsClass |
||
| 386 | */ |
||
| 387 | public function setExtendsClass($extendsClass) |
||
| 391 | 3 | ||
| 392 | /** |
||
| 393 | 3 | * @return null|ClassDefinition |
|
| 394 | 3 | */ |
|
| 395 | public function getExtendsClassDefinition() |
||
| 399 | |||
| 400 | /** |
||
| 401 | * @param ClassDefinition $extendsClassDefinition |
||
| 402 | */ |
||
| 403 | public function setExtendsClassDefinition(ClassDefinition $extendsClassDefinition) |
||
| 407 | 50 | ||
| 408 | /** |
||
| 409 | 50 | * @param string $interface |
|
| 410 | */ |
||
| 411 | public function addInterface($interface) |
||
| 415 | |||
| 416 | /** |
||
| 417 | * @return null|string |
||
| 418 | */ |
||
| 419 | public function getExtendsClass() |
||
| 423 | |||
| 424 | /** |
||
| 425 | * @param TraitDefinition $definition |
||
| 426 | * @param Node\Stmt\TraitUseAdaptation\Alias[] $adaptations |
||
| 427 | */ |
||
| 428 | public function mergeTrait(TraitDefinition $definition, array $adaptations) |
||
| 457 | } |
||
| 458 |
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.