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\Property[] |
||
36 | */ |
||
37 | protected $properties = array(); |
||
38 | |||
39 | /** |
||
40 | * Class constants |
||
41 | * |
||
42 | * @var Node\Stmt\Const_[] |
||
43 | */ |
||
44 | protected $constants = array(); |
||
45 | |||
46 | /** |
||
47 | * @todo Use Finder |
||
48 | * |
||
49 | * @var string |
||
50 | */ |
||
51 | protected $filepath; |
||
52 | |||
53 | /** |
||
54 | * @var Node\Stmt\Class_|null |
||
55 | */ |
||
56 | protected $statement; |
||
57 | |||
58 | /** |
||
59 | * @var string|null |
||
60 | */ |
||
61 | protected $extendsClass; |
||
62 | |||
63 | /** |
||
64 | * @var ClassDefinition|null |
||
65 | */ |
||
66 | protected $extendsClassDefinition; |
||
|
|||
67 | |||
68 | /** |
||
69 | * @var array |
||
70 | */ |
||
71 | protected $interfaces = array(); |
||
72 | |||
73 | /** |
||
74 | * @param string $name |
||
75 | * @param Node\Stmt\Class_ $statement |
||
76 | * @param integer $type |
||
77 | */ |
||
78 | 738 | public function __construct($name, Node\Stmt\Class_ $statement = null, $type = 0) |
|
79 | { |
||
80 | 738 | $this->name = $name; |
|
81 | 738 | $this->statement = $statement; |
|
82 | 738 | $this->type = $type; |
|
83 | 738 | } |
|
84 | |||
85 | /** |
||
86 | * @param ClassMethod $methodDefintion |
||
87 | */ |
||
88 | 20 | public function addMethod(ClassMethod $methodDefintion) |
|
92 | |||
93 | /** |
||
94 | * @param Node\Stmt\Property $property |
||
95 | */ |
||
96 | 3 | public function addProperty(Node\Stmt\Property $property) |
|
102 | |||
103 | /** |
||
104 | * @param Node\Stmt\ClassConst $const |
||
105 | */ |
||
106 | 2 | public function addConst(Node\Stmt\ClassConst $const) |
|
110 | |||
111 | /** |
||
112 | * @param Context $context |
||
113 | * @return $this |
||
114 | */ |
||
115 | 19 | public function compile(Context $context) |
|
178 | |||
179 | /** |
||
180 | * @param string $name |
||
181 | * @param boolean|false $inherit |
||
182 | * @return bool |
||
183 | */ |
||
184 | 1 | public function hasMethod($name, $inherit = false) |
|
197 | |||
198 | /** |
||
199 | * @param string $name |
||
200 | * @param bool $inherit |
||
201 | * @return bool |
||
202 | */ |
||
203 | 2 | public function hasConst($name, $inherit = false) |
|
211 | |||
212 | /** |
||
213 | * @param $name |
||
214 | * @param boolean|false $inherit |
||
215 | * @return ClassMethod|null |
||
216 | */ |
||
217 | 1 | public function getMethod($name, $inherit = false) |
|
229 | |||
230 | /** |
||
231 | * @param $name |
||
232 | * @param bool $inherit |
||
233 | * @return bool |
||
234 | */ |
||
235 | 1 | public function hasProperty($name, $inherit = false) |
|
243 | |||
244 | /** |
||
245 | * @param string $name |
||
246 | * @param bool $inherit |
||
247 | * @return Node\Stmt\Property |
||
248 | */ |
||
249 | public function getProperty($name, $inherit = false) |
||
263 | |||
264 | /** |
||
265 | * @return string |
||
266 | */ |
||
267 | 19 | public function getFilepath() |
|
271 | |||
272 | /** |
||
273 | * @param string $filepath |
||
274 | */ |
||
275 | 19 | public function setFilepath($filepath) |
|
279 | |||
280 | /** |
||
281 | * @return bool |
||
282 | */ |
||
283 | public function isAbstract() |
||
287 | |||
288 | /** |
||
289 | * @param null|string $extendsClass |
||
290 | */ |
||
291 | public function setExtendsClass($extendsClass) |
||
295 | |||
296 | /** |
||
297 | * @return null|ClassDefinition |
||
298 | */ |
||
299 | public function getExtendsClassDefinition() |
||
303 | |||
304 | /** |
||
305 | * @param ClassDefinition $extendsClassDefinition |
||
306 | */ |
||
307 | 1 | public function setExtendsClassDefinition(ClassDefinition $extendsClassDefinition) |
|
311 | |||
312 | /** |
||
313 | * @param array $interface |
||
314 | */ |
||
315 | public function addInterface($interface) |
||
319 | |||
320 | /** |
||
321 | * @return null|string |
||
322 | */ |
||
323 | 19 | public function getExtendsClass() |
|
327 | } |
||
328 |
Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.