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 | 391 | public function __construct($name, $type) |
|
76 | |||
77 | /** |
||
78 | * @param ClassMethod $methodDefintion |
||
79 | */ |
||
80 | 15 | public function addMethod(ClassMethod $methodDefintion) |
|
84 | |||
85 | /** |
||
86 | * @param Node\Stmt\Property $property |
||
87 | */ |
||
88 | 2 | public function addProperty(Node\Stmt\Property $property) |
|
94 | |||
95 | /** |
||
96 | * @param Node\Stmt\ClassConst $const |
||
97 | */ |
||
98 | public function addConst(Node\Stmt\ClassConst $const) |
||
102 | |||
103 | /** |
||
104 | * @param Context $context |
||
105 | * @return $this |
||
106 | */ |
||
107 | 14 | public function compile(Context $context) |
|
147 | |||
148 | /** |
||
149 | * @param string $name |
||
150 | * @param boolean|false $inherit |
||
151 | * @return bool |
||
152 | */ |
||
153 | 1 | public function hasMethod($name, $inherit = false) |
|
166 | |||
167 | /** |
||
168 | * @param $name |
||
169 | * @return bool |
||
170 | */ |
||
171 | public function hasConst($name) |
||
175 | |||
176 | /** |
||
177 | * @param $name |
||
178 | * @param boolean|false $inherit |
||
179 | * @return ClassMethod|null |
||
180 | */ |
||
181 | 1 | public function getMethod($name, $inherit = false) |
|
193 | |||
194 | /** |
||
195 | * @param $name |
||
196 | * @param bool $inherit |
||
197 | * @return bool |
||
198 | */ |
||
199 | 1 | public function hasProperty($name, $inherit = false) |
|
207 | |||
208 | /** |
||
209 | * @param string $name |
||
210 | * @param bool $inherit |
||
211 | * @return Node\Stmt\Property |
||
212 | */ |
||
213 | public function getProperty($name, $inherit = false) |
||
227 | |||
228 | /** |
||
229 | * @return string |
||
230 | */ |
||
231 | 14 | public function getFilepath() |
|
235 | |||
236 | /** |
||
237 | * @param string $filepath |
||
238 | */ |
||
239 | 14 | public function setFilepath($filepath) |
|
243 | |||
244 | /** |
||
245 | * @return bool |
||
246 | */ |
||
247 | public function isAbstract() |
||
251 | |||
252 | /** |
||
253 | * @param null|string $extendsClass |
||
254 | */ |
||
255 | public function setExtendsClass($extendsClass) |
||
259 | |||
260 | /** |
||
261 | * @return null|ClassDefinition |
||
262 | */ |
||
263 | public function getExtendsClassDefinition() |
||
267 | |||
268 | /** |
||
269 | * @param ClassDefinition $extendsClassDefinition |
||
270 | */ |
||
271 | public function setExtendsClassDefinition(ClassDefinition $extendsClassDefinition) |
||
275 | |||
276 | /** |
||
277 | * @param array $interface |
||
278 | */ |
||
279 | public function addInterface($interface) |
||
283 | |||
284 | /** |
||
285 | * @return null|string |
||
286 | */ |
||
287 | 14 | public function getExtendsClass() |
|
291 | } |
||
292 |
Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.