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 | 776 | public function __construct($name, Node\Stmt\Class_ $statement = null, $type = 0) |
|
| 91 | |||
| 92 | /** |
||
| 93 | * @param ClassMethod $methodDefintion |
||
| 94 | */ |
||
| 95 | 25 | public function addMethod(ClassMethod $methodDefintion) |
|
| 99 | |||
| 100 | /** |
||
| 101 | * @param Node\Stmt\Property $property |
||
| 102 | */ |
||
| 103 | 4 | public function addProperty(Node\Stmt\Property $property) |
|
| 110 | |||
| 111 | /** |
||
| 112 | * @param Node\Stmt\ClassConst $const |
||
| 113 | */ |
||
| 114 | 3 | public function addConst(Node\Stmt\ClassConst $const) |
|
| 118 | |||
| 119 | /** |
||
| 120 | * @param Context $context |
||
| 121 | * @return $this |
||
| 122 | */ |
||
| 123 | 25 | public function compile(Context $context) |
|
| 210 | |||
| 211 | /** |
||
| 212 | * @param string $name |
||
| 213 | * @param boolean|false $inherit |
||
| 214 | * @return bool |
||
| 215 | */ |
||
| 216 | 1 | public function hasMethod($name, $inherit = false) |
|
| 229 | |||
| 230 | /** |
||
| 231 | * @param string $name |
||
| 232 | * @param bool $inherit |
||
| 233 | * @return bool |
||
| 234 | */ |
||
| 235 | 2 | public function hasConst($name, $inherit = false) |
|
| 243 | |||
| 244 | /** |
||
| 245 | * @param $name |
||
| 246 | * @param boolean|false $inherit |
||
| 247 | * @return ClassMethod|null |
||
| 248 | */ |
||
| 249 | 1 | public function getMethod($name, $inherit = false) |
|
| 261 | |||
| 262 | /** |
||
| 263 | * @param $name |
||
| 264 | * @param bool $inherit |
||
| 265 | * @return bool |
||
| 266 | */ |
||
| 267 | 2 | public function hasProperty($name, $inherit = false) |
|
| 275 | |||
| 276 | /** |
||
| 277 | * @param string $name |
||
| 278 | * @param bool $inherit |
||
| 279 | * @return Node\Stmt\Property |
||
| 280 | */ |
||
| 281 | public function getProperty($name, $inherit = false) |
||
| 295 | |||
| 296 | /** |
||
| 297 | * @return string |
||
| 298 | */ |
||
| 299 | 25 | public function getFilepath() |
|
| 303 | |||
| 304 | /** |
||
| 305 | * @param string $filepath |
||
| 306 | */ |
||
| 307 | 25 | public function setFilepath($filepath) |
|
| 311 | |||
| 312 | /** |
||
| 313 | * @return bool |
||
| 314 | */ |
||
| 315 | public function isAbstract() |
||
| 319 | |||
| 320 | /** |
||
| 321 | * @param null|string $extendsClass |
||
| 322 | */ |
||
| 323 | public function setExtendsClass($extendsClass) |
||
| 327 | |||
| 328 | /** |
||
| 329 | * @return null|ClassDefinition |
||
| 330 | */ |
||
| 331 | public function getExtendsClassDefinition() |
||
| 335 | |||
| 336 | /** |
||
| 337 | * @param ClassDefinition $extendsClassDefinition |
||
| 338 | */ |
||
| 339 | 1 | public function setExtendsClassDefinition(ClassDefinition $extendsClassDefinition) |
|
| 343 | |||
| 344 | /** |
||
| 345 | * @param array $interface |
||
| 346 | */ |
||
| 347 | public function addInterface($interface) |
||
| 351 | |||
| 352 | /** |
||
| 353 | * @return null|string |
||
| 354 | */ |
||
| 355 | 25 | public function getExtendsClass() |
|
| 359 | } |
||
| 360 |
Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.