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) |
|
| 87 | { |
||
| 88 | 56 | $this->name = $name; |
|
| 89 | 56 | $this->statement = $statement; |
|
| 90 | 56 | $this->type = $type; |
|
| 91 | 56 | } |
|
| 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) |
|
| 99 | { |
||
| 100 | 48 | if ($overwrite) { |
|
| 101 | 48 | $this->methods[$classMethod->getName()] = $classMethod; |
|
| 102 | 48 | } else { |
|
| 103 | $name = $classMethod->getName(); |
||
| 104 | if (isset($this->methods[$name])) { |
||
| 105 | return false; |
||
| 106 | } else { |
||
| 107 | $this->methods[$name] = $classMethod; |
||
| 108 | } |
||
| 109 | } |
||
| 110 | |||
| 111 | 48 | return true; |
|
| 112 | } |
||
| 113 | |||
| 114 | /** |
||
| 115 | * @param Node\Stmt\Property $property |
||
| 116 | */ |
||
| 117 | 8 | public function addProperty(Node\Stmt\Property $property) |
|
| 118 | { |
||
| 119 | 8 | foreach ($property->props as $propertyDefinition) { |
|
| 120 | 8 | $this->properties[$propertyDefinition->name] = $propertyDefinition; |
|
| 121 | 8 | $this->propertyStatements[$propertyDefinition->name] = $property; |
|
| 122 | 8 | } |
|
| 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 | 50 | public function compile(Context $context) |
|
| 235 | |||
| 236 | /** |
||
| 237 | * @param string $name |
||
| 238 | * @param boolean|false $inherit |
||
| 239 | * @return bool |
||
| 240 | */ |
||
| 241 | 4 | public function hasMethod($name, $inherit = false) |
|
| 242 | { |
||
| 243 | 4 | if (isset($this->methods[$name])) { |
|
| 244 | 2 | return true; |
|
| 245 | } |
||
| 246 | |||
| 247 | 3 | if ($inherit && $this->extendsClassDefinition && $this->extendsClassDefinition->hasMethod($name, $inherit)) { |
|
| 248 | 2 | $method = $this->extendsClassDefinition->getMethod($name, $inherit); |
|
| 249 | 2 | return $method && ($method->isPublic() || $method->isProtected()); |
|
| 250 | } |
||
| 251 | |||
| 252 | 1 | return false; |
|
| 253 | } |
||
| 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 | 4 | public function getMethod($name, $inherit = false) |
|
| 286 | |||
| 287 | /** |
||
| 288 | * @param $name |
||
| 289 | * @param bool $inherit |
||
| 290 | * @param bool $isParent It's an internal parameter |
||
| 291 | * @return bool |
||
| 292 | */ |
||
| 293 | 1 | public function hasProperty($name, $inherit = false, $isParent = true) |
|
| 312 | |||
| 313 | /** |
||
| 314 | * @param string $name |
||
| 315 | * @param bool $inherit |
||
| 316 | * @return Node\Stmt\PropertyProperty |
||
| 317 | */ |
||
| 318 | public function getProperty($name, $inherit = false) |
||
| 332 | |||
| 333 | /** |
||
| 334 | * @param string $name |
||
| 335 | * @param bool $inherit |
||
| 336 | * @return Node\Stmt\Property |
||
| 337 | */ |
||
| 338 | public function getPropertyStatement($name, $inherit = false) |
||
| 350 | |||
| 351 | /** |
||
| 352 | * @return string |
||
| 353 | */ |
||
| 354 | 50 | public function getFilepath() |
|
| 358 | |||
| 359 | /** |
||
| 360 | * @param string $filepath |
||
| 361 | */ |
||
| 362 | 50 | public function setFilepath($filepath) |
|
| 366 | |||
| 367 | /** |
||
| 368 | * @return bool |
||
| 369 | */ |
||
| 370 | 1 | public function isAbstract() |
|
| 374 | |||
| 375 | /** |
||
| 376 | * @return bool |
||
| 377 | */ |
||
| 378 | 1 | public function isFinal() |
|
| 382 | |||
| 383 | /** |
||
| 384 | * @param null|string $extendsClass |
||
| 385 | */ |
||
| 386 | 2 | public function setExtendsClass($extendsClass) |
|
| 390 | |||
| 391 | /** |
||
| 392 | * @return null|ClassDefinition |
||
| 393 | */ |
||
| 394 | public function getExtendsClassDefinition() |
||
| 398 | |||
| 399 | /** |
||
| 400 | * @param ClassDefinition $extendsClassDefinition |
||
| 401 | */ |
||
| 402 | 3 | public function setExtendsClassDefinition(ClassDefinition $extendsClassDefinition) |
|
| 406 | |||
| 407 | /** |
||
| 408 | * @param string $interface |
||
| 409 | */ |
||
| 410 | public function addInterface($interface) |
||
| 414 | |||
| 415 | /** |
||
| 416 | * @return null|string |
||
| 417 | */ |
||
| 418 | 50 | public function getExtendsClass() |
|
| 422 | |||
| 423 | /** |
||
| 424 | * @param TraitDefinition $definition |
||
| 425 | * @param Node\Stmt\TraitUseAdaptation\Alias[] $adaptations |
||
| 426 | */ |
||
| 427 | public function mergeTrait(TraitDefinition $definition, array $adaptations) |
||
| 456 | } |
||
| 457 |
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.