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 |
||
| 17 | class ClassDefinition extends ParentDefinition |
||
| 18 | { |
||
| 19 | /** |
||
| 20 | * @var int |
||
| 21 | */ |
||
| 22 | protected $type; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Class methods |
||
| 26 | * |
||
| 27 | * @var ClassMethod[] |
||
| 28 | */ |
||
| 29 | protected $methods = array(); |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Class properties |
||
| 33 | * |
||
| 34 | * @var Node\Stmt\Property[] |
||
| 35 | */ |
||
| 36 | protected $properties = array(); |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Class constants |
||
| 40 | * |
||
| 41 | * @var Node\Stmt\Const_[] |
||
| 42 | */ |
||
| 43 | protected $constants = array(); |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @todo Use Finder |
||
| 47 | * |
||
| 48 | * @var string |
||
| 49 | */ |
||
| 50 | protected $filepath; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var string|null |
||
| 54 | */ |
||
| 55 | protected $extendsClass; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @var ClassDefinition|null |
||
| 59 | */ |
||
| 60 | protected $extendsClassDefinition; |
||
|
|
|||
| 61 | |||
| 62 | /** |
||
| 63 | * @var array |
||
| 64 | */ |
||
| 65 | protected $interfaces = array(); |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @param string $name |
||
| 69 | * @param integer $type |
||
| 70 | */ |
||
| 71 | 388 | public function __construct($name, $type) |
|
| 76 | |||
| 77 | /** |
||
| 78 | * @param ClassMethod $methodDefintion |
||
| 79 | */ |
||
| 80 | 10 | public function addMethod(ClassMethod $methodDefintion) |
|
| 84 | |||
| 85 | /** |
||
| 86 | * @param Node\Stmt\Property $property |
||
| 87 | */ |
||
| 88 | 1 | public function addProperty(Node\Stmt\Property $property) |
|
| 92 | |||
| 93 | /** |
||
| 94 | * @param Node\Stmt\ClassConst $const |
||
| 95 | */ |
||
| 96 | 2 | public function addConst(Node\Stmt\ClassConst $const) |
|
| 97 | { |
||
| 98 | 2 | $this->constants[$const->consts[0]->name] = $const; |
|
| 99 | 2 | } |
|
| 100 | |||
| 101 | /** |
||
| 102 | * @param Context $context |
||
| 103 | * @return $this |
||
| 104 | */ |
||
| 105 | 9 | public function compile(Context $context) |
|
| 145 | |||
| 146 | /** |
||
| 147 | * @param string $name |
||
| 148 | * @param boolean|false $inherit |
||
| 149 | * @return bool |
||
| 150 | */ |
||
| 151 | 1 | public function hasMethod($name, $inherit = false) |
|
| 164 | |||
| 165 | /** |
||
| 166 | * @param string $name |
||
| 167 | * @param bool $inherit |
||
| 168 | * @return bool |
||
| 169 | */ |
||
| 170 | 2 | public function hasConst($name, $inherit = false) |
|
| 178 | |||
| 179 | /** |
||
| 180 | * @param $name |
||
| 181 | * @param boolean|false $inherit |
||
| 182 | * @return ClassMethod|null |
||
| 183 | */ |
||
| 184 | 1 | public function getMethod($name, $inherit = false) |
|
| 196 | |||
| 197 | /** |
||
| 198 | * @param $name |
||
| 199 | * @param bool $inherit |
||
| 200 | * @return bool |
||
| 201 | */ |
||
| 202 | 1 | public function hasProperty($name, $inherit = false) |
|
| 210 | |||
| 211 | /** |
||
| 212 | * @param string $name |
||
| 213 | * @param bool $inherit |
||
| 214 | * @return Node\Stmt\Property |
||
| 215 | */ |
||
| 216 | public function getProperty($name, $inherit = false) |
||
| 230 | |||
| 231 | /** |
||
| 232 | * @return string |
||
| 233 | */ |
||
| 234 | 9 | public function getFilepath() |
|
| 238 | |||
| 239 | /** |
||
| 240 | * @param string $filepath |
||
| 241 | */ |
||
| 242 | 9 | public function setFilepath($filepath) |
|
| 246 | |||
| 247 | /** |
||
| 248 | * @return bool |
||
| 249 | */ |
||
| 250 | public function isAbstract() |
||
| 254 | |||
| 255 | /** |
||
| 256 | * @param null|string $extendsClass |
||
| 257 | */ |
||
| 258 | public function setExtendsClass($extendsClass) |
||
| 262 | |||
| 263 | /** |
||
| 264 | * @return null|ClassDefinition |
||
| 265 | */ |
||
| 266 | public function getExtendsClassDefinition() |
||
| 270 | |||
| 271 | /** |
||
| 272 | * @param ClassDefinition $extendsClassDefinition |
||
| 273 | */ |
||
| 274 | 1 | public function setExtendsClassDefinition(ClassDefinition $extendsClassDefinition) |
|
| 278 | |||
| 279 | /** |
||
| 280 | * @param array $interface |
||
| 281 | */ |
||
| 282 | public function addInterface($interface) |
||
| 286 | |||
| 287 | /** |
||
| 288 | * @return null|string |
||
| 289 | */ |
||
| 290 | 9 | public function getExtendsClass() |
|
| 294 | } |
||
| 295 |
Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.