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 Node\Stmt\ClassConst[] |
||
| 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 | public function __construct($name, Node\Stmt\Class_ $statement = null, $type = 0) |
||
| 87 | { |
||
| 88 | $this->name = $name; |
||
| 89 | $this->statement = $statement; |
||
| 90 | $this->type = $type; |
||
| 91 | } |
||
| 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 | public function addMethod(ClassMethod $classMethod, $overwrite = true) |
||
| 99 | { |
||
| 100 | if ($overwrite) { |
||
| 101 | $this->methods[$classMethod->getName()] = $classMethod; |
||
| 102 | } 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 | return true; |
||
| 112 | } |
||
| 113 | |||
| 114 | /** |
||
| 115 | * @param Node\Stmt\Property $property |
||
| 116 | */ |
||
| 117 | public function addProperty(Node\Stmt\Property $property) |
||
| 118 | { |
||
| 119 | foreach ($property->props as $propertyDefinition) { |
||
| 120 | $this->properties[$propertyDefinition->name] = $propertyDefinition; |
||
| 121 | $this->propertyStatements[$propertyDefinition->name] = $property; |
||
| 122 | } |
||
| 123 | } |
||
| 124 | |||
| 125 | /** |
||
| 126 | * @param Node\Stmt\ClassConst $const |
||
| 127 | */ |
||
| 128 | public function addConst(Node\Stmt\ClassConst $const) |
||
| 129 | { |
||
| 130 | $this->constants[$const->consts[0]->name] = $const; |
||
| 131 | } |
||
| 132 | |||
| 133 | /** |
||
| 134 | * @param Context $context |
||
| 135 | * @return $this |
||
| 136 | */ |
||
| 137 | public function compile(Context $context) |
||
| 138 | { |
||
| 139 | if ($this->compiled) { |
||
| 140 | return $this; |
||
| 141 | } |
||
| 142 | |||
| 143 | $this->compiled = true; |
||
| 144 | $context->setFilepath($this->filepath); |
||
| 145 | $context->setScope($this); |
||
| 146 | |||
| 147 | $context->getEventManager()->fire( |
||
| 148 | Event\StatementBeforeCompile::EVENT_NAME, |
||
| 149 | new Event\StatementBeforeCompile( |
||
| 150 | $this->statement, |
||
| 151 | $context |
||
| 152 | ) |
||
| 153 | ); |
||
| 154 | |||
| 155 | // Compile event for properties |
||
| 156 | foreach ($this->properties as $property) { |
||
| 157 | if (!$property->default) { |
||
| 158 | continue; |
||
| 159 | } |
||
| 160 | |||
| 161 | // fire expression event for property default |
||
| 162 | $context->getEventManager()->fire( |
||
| 163 | Event\ExpressionBeforeCompile::EVENT_NAME, |
||
| 164 | new Event\ExpressionBeforeCompile( |
||
| 165 | $property->default, |
||
| 166 | $context |
||
| 167 | ) |
||
| 168 | ); |
||
| 169 | } |
||
| 170 | |||
| 171 | // Compile event for PropertyProperty |
||
| 172 | foreach ($this->properties as $property) { |
||
| 173 | $context->getEventManager()->fire( |
||
| 174 | Event\StatementBeforeCompile::EVENT_NAME, |
||
| 175 | new Event\StatementBeforeCompile( |
||
| 176 | $property, |
||
| 177 | $context |
||
| 178 | ) |
||
| 179 | ); |
||
| 180 | } |
||
| 181 | |||
| 182 | // Compile event for constants |
||
| 183 | foreach ($this->constants as $const) { |
||
| 184 | $context->getEventManager()->fire( |
||
| 185 | Event\StatementBeforeCompile::EVENT_NAME, |
||
| 186 | new Event\StatementBeforeCompile( |
||
| 187 | $const, |
||
| 188 | $context |
||
| 189 | ) |
||
| 190 | ); |
||
| 191 | } |
||
| 192 | |||
| 193 | // Compiler event for property statements |
||
| 194 | foreach ($this->propertyStatements as $prop) { |
||
| 195 | $context->getEventManager()->fire( |
||
| 196 | Event\StatementBeforeCompile::EVENT_NAME, |
||
| 197 | new Event\StatementBeforeCompile( |
||
| 198 | $prop, |
||
| 199 | $context |
||
| 200 | ) |
||
| 201 | ); |
||
| 202 | } |
||
| 203 | |||
| 204 | // Compile each method |
||
| 205 | foreach ($this->methods as $method) { |
||
| 206 | $context->clearSymbols(); |
||
| 207 | |||
| 208 | if (!$method->isStatic()) { |
||
| 209 | $thisPtr = new Variable('this', $this, CompiledExpression::OBJECT); |
||
| 210 | $thisPtr->incGets(); |
||
| 211 | $context->addVariable($thisPtr); |
||
| 212 | } |
||
| 213 | |||
| 214 | $method->compile($context); |
||
| 215 | |||
| 216 | $symbols = $context->getSymbols(); |
||
| 217 | if (count($symbols) > 0) { |
||
| 218 | foreach ($symbols as $name => $variable) { |
||
| 219 | if (!$variable->isGlobal() && $variable->isUnused()) { |
||
| 220 | $context->warning( |
||
| 221 | 'unused-' . $variable->getSymbolType(), |
||
| 222 | sprintf( |
||
| 223 | 'Unused ' . $variable->getSymbolType() . ' $%s in method %s()', |
||
| 224 | $variable->getName(), |
||
| 225 | $method->getName() |
||
| 226 | ) |
||
| 227 | ); |
||
| 228 | } |
||
| 229 | } |
||
| 230 | } |
||
| 231 | } |
||
| 232 | |||
| 233 | return $this; |
||
| 234 | } |
||
| 235 | |||
| 236 | /** |
||
| 237 | * @param string $name |
||
| 238 | * @param boolean|false $inherit |
||
| 239 | * @return bool |
||
| 240 | */ |
||
| 241 | public function hasMethod($name, $inherit = false) |
||
| 242 | { |
||
| 243 | if (isset($this->methods[$name])) { |
||
| 244 | return true; |
||
| 245 | } |
||
| 246 | |||
| 247 | if ($inherit && $this->extendsClassDefinition && $this->extendsClassDefinition->hasMethod($name, $inherit)) { |
||
| 248 | $method = $this->extendsClassDefinition->getMethod($name, $inherit); |
||
| 249 | return $method && ($method->isPublic() || $method->isProtected()); |
||
| 250 | } |
||
| 251 | |||
| 252 | return false; |
||
| 253 | } |
||
| 254 | |||
| 255 | /** |
||
| 256 | * @param string $name |
||
| 257 | * @param bool $inherit |
||
| 258 | * @return bool |
||
| 259 | */ |
||
| 260 | public function hasConst($name, $inherit = false) |
||
| 261 | { |
||
| 262 | if ($inherit && $this->extendsClassDefinition && $this->extendsClassDefinition->hasConst($name, $inherit)) { |
||
| 263 | return true; |
||
| 264 | } |
||
| 265 | |||
| 266 | return isset($this->constants[$name]); |
||
| 267 | } |
||
| 268 | |||
| 269 | /** |
||
| 270 | * @param $name |
||
| 271 | * @param boolean|false $inherit |
||
| 272 | * @return ClassMethod|null |
||
| 273 | */ |
||
| 274 | public function getMethod($name, $inherit = false) |
||
| 275 | { |
||
| 276 | if (isset($this->methods[$name])) { |
||
| 277 | return $this->methods[$name]; |
||
| 278 | } |
||
| 279 | |||
| 280 | if ($inherit && $this->extendsClassDefinition) { |
||
| 281 | return $this->extendsClassDefinition->getMethod($name, $inherit); |
||
| 282 | } |
||
| 283 | |||
| 284 | return null; |
||
| 285 | } |
||
| 286 | |||
| 287 | /** |
||
| 288 | * @param $name |
||
| 289 | * @param bool $inherit |
||
| 290 | * @param bool $isParent It's an internal parameter |
||
| 291 | * @return bool |
||
| 292 | */ |
||
| 293 | public function hasProperty($name, $inherit = false, $isParent = true) |
||
| 294 | { |
||
| 295 | if (isset($this->properties[$name])) { |
||
| 296 | if (!$isParent) { |
||
| 297 | $property = $this->propertyStatements[$name]; |
||
| 298 | if ($property->isPrivate()) { |
||
| 299 | return false; |
||
| 300 | } |
||
| 301 | } |
||
| 302 | |||
| 303 | return true; |
||
| 304 | } |
||
| 305 | |||
| 306 | if ($inherit && $this->extendsClassDefinition) { |
||
| 307 | return $this->extendsClassDefinition->hasProperty($name, true, false); |
||
| 308 | } |
||
| 309 | |||
| 310 | return false; |
||
| 311 | } |
||
| 312 | |||
| 313 | /** |
||
| 314 | * @param string $name |
||
| 315 | * @param bool $inherit |
||
| 316 | * @return Node\Stmt\PropertyProperty|null |
||
| 317 | */ |
||
| 318 | public function getProperty($name, $inherit = false) |
||
| 330 | |||
| 331 | /** |
||
| 332 | * @param string $name |
||
| 333 | * @param bool $inherit |
||
| 334 | * @return Node\Stmt\Property|null |
||
| 335 | */ |
||
| 336 | public function getPropertyStatement($name, $inherit = false) |
||
| 348 | |||
| 349 | /** |
||
| 350 | * @return string |
||
| 351 | */ |
||
| 352 | public function getFilepath() |
||
| 353 | { |
||
| 354 | return $this->filepath; |
||
| 355 | } |
||
| 356 | |||
| 357 | /** |
||
| 358 | * @param string $filepath |
||
| 359 | */ |
||
| 360 | public function setFilepath($filepath) |
||
| 361 | { |
||
| 362 | $this->filepath = $filepath; |
||
| 363 | } |
||
| 364 | |||
| 365 | /** |
||
| 366 | * @return bool |
||
| 367 | */ |
||
| 368 | public function isAbstract() |
||
| 369 | { |
||
| 370 | return (bool) ($this->type & Node\Stmt\Class_::MODIFIER_ABSTRACT); |
||
| 371 | } |
||
| 372 | |||
| 373 | /** |
||
| 374 | * @return bool |
||
| 375 | */ |
||
| 376 | public function isFinal() |
||
| 377 | { |
||
| 378 | return (bool) ($this->type & Node\Stmt\Class_::MODIFIER_FINAL); |
||
| 379 | } |
||
| 380 | |||
| 381 | /** |
||
| 382 | * @param null|string $extendsClass |
||
| 383 | */ |
||
| 384 | public function setExtendsClass($extendsClass) |
||
| 385 | { |
||
| 386 | $this->extendsClass = $extendsClass; |
||
| 387 | } |
||
| 388 | |||
| 389 | /** |
||
| 390 | * @return null|ClassDefinition |
||
| 391 | */ |
||
| 392 | public function getExtendsClassDefinition() |
||
| 396 | |||
| 397 | /** |
||
| 398 | * @param ClassDefinition $extendsClassDefinition |
||
| 399 | */ |
||
| 400 | public function setExtendsClassDefinition(ClassDefinition $extendsClassDefinition) |
||
| 401 | { |
||
| 402 | $this->extendsClassDefinition = $extendsClassDefinition; |
||
| 403 | } |
||
| 404 | |||
| 405 | /** |
||
| 406 | * @param string $interface |
||
| 407 | */ |
||
| 408 | public function addInterface($interface) |
||
| 412 | |||
| 413 | /** |
||
| 414 | * @return null|string |
||
| 415 | */ |
||
| 416 | public function getExtendsClass() |
||
| 417 | { |
||
| 418 | return $this->extendsClass; |
||
| 419 | } |
||
| 420 | |||
| 421 | /** |
||
| 422 | * @param TraitDefinition $definition |
||
| 423 | * @param Node\Stmt\TraitUseAdaptation\Alias[] $adaptations |
||
| 424 | */ |
||
| 425 | public function mergeTrait(TraitDefinition $definition, array $adaptations) |
||
| 454 | |||
| 455 | /** |
||
| 456 | * @return ClassMethod[] |
||
| 457 | */ |
||
| 458 | public function getMethods() |
||
| 462 | } |
||
| 463 |
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.