Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like ClassMetadata 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 ClassMetadata, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
22 | class ClassMetadata |
||
23 | { |
||
24 | /** |
||
25 | * @var string |
||
26 | */ |
||
27 | private $name; |
||
28 | |||
29 | /** |
||
30 | * @var ClassMetadata |
||
31 | */ |
||
32 | private $parent; |
||
33 | |||
34 | /** |
||
35 | * @var TraitMetadata[] |
||
36 | */ |
||
37 | private $traits; |
||
38 | |||
39 | /** |
||
40 | * @var bool |
||
41 | */ |
||
42 | private $final; |
||
43 | |||
44 | /** |
||
45 | * @var bool |
||
46 | */ |
||
47 | private $abstract; |
||
48 | |||
49 | /** |
||
50 | * @var MethodMetadata[] |
||
51 | */ |
||
52 | private $methods; |
||
53 | |||
54 | /** |
||
55 | * @var Class_ |
||
56 | */ |
||
57 | private $ast; |
||
58 | |||
59 | /** |
||
60 | * ClassMetadata constructor. |
||
61 | * |
||
62 | * @param string $name |
||
63 | * @param ClassMetadata|null $parent |
||
64 | * @param bool $final |
||
65 | * @param bool $abstract |
||
66 | * @param MethodMetadata[] $methods |
||
67 | * @param Class_ $ast |
||
68 | */ |
||
69 | public function __construct($name, ClassMetadata $parent = null, array $traits = [], $final = false, $abstract = false, array $methods = [], Class_ $ast = null) |
||
84 | |||
85 | /** |
||
86 | * @return string |
||
87 | */ |
||
88 | public function getName() |
||
92 | |||
93 | /** |
||
94 | * @return string |
||
95 | */ |
||
96 | public function getFqcn() |
||
100 | |||
101 | /** |
||
102 | * @return string |
||
103 | */ |
||
104 | public function getShortName() |
||
108 | |||
109 | /** |
||
110 | * @return bool |
||
111 | */ |
||
112 | public function isAutoloadable() |
||
116 | |||
117 | /** |
||
118 | * Check if class inherits some other class. |
||
119 | * |
||
120 | * @return bool |
||
121 | */ |
||
122 | public function hasParent() |
||
126 | |||
127 | /** |
||
128 | * @return ClassMetadata|null |
||
129 | */ |
||
130 | public function getParent() |
||
134 | |||
135 | /** |
||
136 | * @return bool |
||
137 | */ |
||
138 | public function hasTraits() |
||
142 | |||
143 | /** |
||
144 | * @return TraitMetadata[] |
||
145 | */ |
||
146 | public function getTraits() |
||
150 | |||
151 | /** |
||
152 | * @return bool |
||
153 | */ |
||
154 | public function isFinal() |
||
158 | |||
159 | /** |
||
160 | * @return bool |
||
161 | */ |
||
162 | public function isAbstract() |
||
166 | |||
167 | /** |
||
168 | * @return MethodMetadata[] |
||
169 | */ |
||
170 | public function getMethods() |
||
174 | |||
175 | /** |
||
176 | * Check if class has method, with optional inheritance tree and trait traverse. |
||
177 | * |
||
178 | * @param string $name |
||
179 | * @param bool $traverse |
||
180 | * |
||
181 | * @return bool |
||
182 | */ |
||
183 | View Code Duplication | public function hasMethod($name, $traverse = true) |
|
212 | |||
213 | /** |
||
214 | * Check if class has public method, with optional inheritance tree and trait traverse. |
||
215 | * |
||
216 | * @param string $name |
||
217 | * @param bool $traverse |
||
218 | * |
||
219 | * @return bool |
||
220 | */ |
||
221 | View Code Duplication | public function hasPublicMethod($name, $traverse = true) |
|
249 | |||
250 | /** |
||
251 | * Get public method for class, with optional inheritance tree and trait traverse. |
||
252 | * |
||
253 | * @param string $name |
||
254 | * @param bool $traverse |
||
255 | * |
||
256 | * @return MethodMetadata |
||
257 | * |
||
258 | * @throws \RunOpenCode\AbstractBuilder\Exception\RuntimeException |
||
259 | */ |
||
260 | public function getPublicMethod($name, $traverse = true) |
||
293 | |||
294 | /** |
||
295 | * @return Class_ |
||
296 | */ |
||
297 | public function getAst() |
||
301 | |||
302 | /** |
||
303 | * {@inheritdoc} |
||
304 | */ |
||
305 | public function __toString() |
||
309 | } |
||
310 |