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 | 393 | 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 | 2 | public function addConst(Node\Stmt\ClassConst $const) |
|
99 | { |
||
100 | 2 | $this->constants[$const->consts[0]->name] = $const; |
|
101 | 2 | } |
|
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 string $name |
||
169 | * @param bool $inherit |
||
170 | * @return bool |
||
171 | */ |
||
172 | 2 | public function hasConst($name, $inherit = false) |
|
180 | |||
181 | /** |
||
182 | * @param $name |
||
183 | * @param boolean|false $inherit |
||
184 | * @return ClassMethod|null |
||
185 | */ |
||
186 | 1 | public function getMethod($name, $inherit = false) |
|
198 | |||
199 | /** |
||
200 | * @param $name |
||
201 | * @param bool $inherit |
||
202 | * @return bool |
||
203 | */ |
||
204 | 1 | public function hasProperty($name, $inherit = false) |
|
212 | |||
213 | /** |
||
214 | * @param string $name |
||
215 | * @param bool $inherit |
||
216 | * @return Node\Stmt\Property |
||
217 | */ |
||
218 | public function getProperty($name, $inherit = false) |
||
232 | |||
233 | /** |
||
234 | * @return string |
||
235 | */ |
||
236 | 14 | public function getFilepath() |
|
240 | |||
241 | /** |
||
242 | * @param string $filepath |
||
243 | */ |
||
244 | 14 | public function setFilepath($filepath) |
|
248 | |||
249 | /** |
||
250 | * @return bool |
||
251 | */ |
||
252 | public function isAbstract() |
||
256 | |||
257 | /** |
||
258 | * @param null|string $extendsClass |
||
259 | */ |
||
260 | public function setExtendsClass($extendsClass) |
||
264 | |||
265 | /** |
||
266 | * @return null|ClassDefinition |
||
267 | */ |
||
268 | public function getExtendsClassDefinition() |
||
272 | |||
273 | /** |
||
274 | * @param ClassDefinition $extendsClassDefinition |
||
275 | */ |
||
276 | 1 | public function setExtendsClassDefinition(ClassDefinition $extendsClassDefinition) |
|
280 | |||
281 | /** |
||
282 | * @param array $interface |
||
283 | */ |
||
284 | public function addInterface($interface) |
||
288 | |||
289 | /** |
||
290 | * @return null|string |
||
291 | */ |
||
292 | 14 | public function getExtendsClass() |
|
296 | } |
||
297 |
Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.