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) |
|
| 111 | |||
| 112 | /** |
||
| 113 | * @param Node\Stmt\ClassConst $const |
||
| 114 | */ |
||
| 115 | 3 | public function addConst(Node\Stmt\ClassConst $const) |
|
| 119 | |||
| 120 | /** |
||
| 121 | * @param Context $context |
||
| 122 | * @return $this |
||
| 123 | */ |
||
| 124 | 25 | public function compile(Context $context) |
|
| 211 | |||
| 212 | /** |
||
| 213 | * @param string $name |
||
| 214 | * @param boolean|false $inherit |
||
| 215 | * @return bool |
||
| 216 | */ |
||
| 217 | 1 | public function hasMethod($name, $inherit = false) |
|
| 230 | |||
| 231 | /** |
||
| 232 | * @param string $name |
||
| 233 | * @param bool $inherit |
||
| 234 | * @return bool |
||
| 235 | */ |
||
| 236 | 2 | public function hasConst($name, $inherit = false) |
|
| 244 | |||
| 245 | /** |
||
| 246 | * @param $name |
||
| 247 | * @param boolean|false $inherit |
||
| 248 | * @return ClassMethod|null |
||
| 249 | */ |
||
| 250 | 1 | public function getMethod($name, $inherit = false) |
|
| 262 | |||
| 263 | /** |
||
| 264 | * @param $name |
||
| 265 | * @param bool $inherit |
||
| 266 | * @return bool |
||
| 267 | */ |
||
| 268 | 3 | public function hasProperty($name, $inherit = false) |
|
| 276 | |||
| 277 | /** |
||
| 278 | * @param string $name |
||
| 279 | * @param bool $inherit |
||
| 280 | * @return Node\Stmt\Property |
||
| 281 | */ |
||
| 282 | public function getProperty($name, $inherit = false) |
||
| 296 | |||
| 297 | /** |
||
| 298 | * @return string |
||
| 299 | */ |
||
| 300 | 25 | public function getFilepath() |
|
| 304 | |||
| 305 | /** |
||
| 306 | * @param string $filepath |
||
| 307 | */ |
||
| 308 | 25 | public function setFilepath($filepath) |
|
| 312 | |||
| 313 | /** |
||
| 314 | * @return bool |
||
| 315 | */ |
||
| 316 | public function isAbstract() |
||
| 320 | |||
| 321 | /** |
||
| 322 | * @param null|string $extendsClass |
||
| 323 | */ |
||
| 324 | public function setExtendsClass($extendsClass) |
||
| 328 | |||
| 329 | /** |
||
| 330 | * @return null|ClassDefinition |
||
| 331 | */ |
||
| 332 | public function getExtendsClassDefinition() |
||
| 336 | |||
| 337 | /** |
||
| 338 | * @param ClassDefinition $extendsClassDefinition |
||
| 339 | */ |
||
| 340 | 1 | public function setExtendsClassDefinition(ClassDefinition $extendsClassDefinition) |
|
| 344 | |||
| 345 | /** |
||
| 346 | * @param array $interface |
||
| 347 | */ |
||
| 348 | public function addInterface($interface) |
||
| 352 | |||
| 353 | /** |
||
| 354 | * @return null|string |
||
| 355 | */ |
||
| 356 | 25 | public function getExtendsClass() |
|
| 360 | } |
||
| 361 |
Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.