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 | 739 | public function __construct($name, Node\Stmt\Class_ $statement = null, $type = 0) |
|
91 | |||
92 | /** |
||
93 | * @param ClassMethod $methodDefintion |
||
94 | */ |
||
95 | 20 | public function addMethod(ClassMethod $methodDefintion) |
|
99 | |||
100 | /** |
||
101 | * @param Node\Stmt\Property $property |
||
102 | */ |
||
103 | 3 | public function addProperty(Node\Stmt\Property $property) |
|
110 | |||
111 | /** |
||
112 | * @param Node\Stmt\ClassConst $const |
||
113 | */ |
||
114 | 2 | public function addConst(Node\Stmt\ClassConst $const) |
|
118 | |||
119 | /** |
||
120 | * @param Context $context |
||
121 | * @return $this |
||
122 | */ |
||
123 | 20 | public function compile(Context $context) |
|
181 | |||
182 | /** |
||
183 | * @param string $name |
||
184 | * @param boolean|false $inherit |
||
185 | * @return bool |
||
186 | */ |
||
187 | 1 | public function hasMethod($name, $inherit = false) |
|
200 | |||
201 | /** |
||
202 | * @param string $name |
||
203 | * @param bool $inherit |
||
204 | * @return bool |
||
205 | */ |
||
206 | 2 | public function hasConst($name, $inherit = false) |
|
214 | |||
215 | /** |
||
216 | * @param $name |
||
217 | * @param boolean|false $inherit |
||
218 | * @return ClassMethod|null |
||
219 | */ |
||
220 | 1 | public function getMethod($name, $inherit = false) |
|
232 | |||
233 | /** |
||
234 | * @param $name |
||
235 | * @param bool $inherit |
||
236 | * @return bool |
||
237 | */ |
||
238 | 1 | public function hasProperty($name, $inherit = false) |
|
246 | |||
247 | /** |
||
248 | * @param string $name |
||
249 | * @param bool $inherit |
||
250 | * @return Node\Stmt\Property |
||
251 | */ |
||
252 | public function getProperty($name, $inherit = false) |
||
266 | |||
267 | /** |
||
268 | * @return string |
||
269 | */ |
||
270 | 20 | public function getFilepath() |
|
274 | |||
275 | /** |
||
276 | * @param string $filepath |
||
277 | */ |
||
278 | 20 | public function setFilepath($filepath) |
|
282 | |||
283 | /** |
||
284 | * @return bool |
||
285 | */ |
||
286 | public function isAbstract() |
||
290 | |||
291 | /** |
||
292 | * @param null|string $extendsClass |
||
293 | */ |
||
294 | public function setExtendsClass($extendsClass) |
||
298 | |||
299 | /** |
||
300 | * @return null|ClassDefinition |
||
301 | */ |
||
302 | public function getExtendsClassDefinition() |
||
306 | |||
307 | /** |
||
308 | * @param ClassDefinition $extendsClassDefinition |
||
309 | */ |
||
310 | 1 | public function setExtendsClassDefinition(ClassDefinition $extendsClassDefinition) |
|
314 | |||
315 | /** |
||
316 | * @param array $interface |
||
317 | */ |
||
318 | public function addInterface($interface) |
||
322 | |||
323 | /** |
||
324 | * @return null|string |
||
325 | */ |
||
326 | 20 | public function getExtendsClass() |
|
330 | } |
||
331 |
Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.