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 | 4 | 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 | 2 | public function getName() |
|
92 | |||
93 | /** |
||
94 | * @return string |
||
95 | */ |
||
96 | public function getFqcn() |
||
100 | |||
101 | /** |
||
102 | * @return string |
||
103 | */ |
||
104 | 1 | public function getShortName() |
|
109 | |||
110 | /** |
||
111 | * @return bool |
||
112 | */ |
||
113 | public function isAutoloadable() |
||
117 | |||
118 | /** |
||
119 | * Check if class inherits some other class. |
||
120 | * |
||
121 | * @return bool |
||
122 | */ |
||
123 | 1 | public function hasParent() |
|
127 | |||
128 | /** |
||
129 | * @return ClassMetadata|null |
||
130 | */ |
||
131 | 1 | public function getParent() |
|
135 | |||
136 | /** |
||
137 | * @return bool |
||
138 | */ |
||
139 | 1 | public function hasTraits() |
|
143 | |||
144 | /** |
||
145 | * @return TraitMetadata[] |
||
146 | */ |
||
147 | public function getTraits() |
||
151 | |||
152 | /** |
||
153 | * @return bool |
||
154 | */ |
||
155 | public function isFinal() |
||
159 | |||
160 | /** |
||
161 | * @return bool |
||
162 | */ |
||
163 | public function isAbstract() |
||
167 | |||
168 | /** |
||
169 | * @return MethodMetadata[] |
||
170 | */ |
||
171 | public function getMethods() |
||
175 | |||
176 | /** |
||
177 | * Check if class has method, with optional inheritance tree and trait traverse. |
||
178 | * |
||
179 | * @param string $name |
||
180 | * @param bool $traverse |
||
181 | * |
||
182 | * @return bool |
||
183 | */ |
||
184 | View Code Duplication | public function hasMethod($name, $traverse = true) |
|
213 | |||
214 | /** |
||
215 | * Check if class has public method, with optional inheritance tree and trait traverse. |
||
216 | * |
||
217 | * @param string $name |
||
218 | * @param bool $traverse |
||
219 | * |
||
220 | * @return bool |
||
221 | */ |
||
222 | 1 | View Code Duplication | public function hasPublicMethod($name, $traverse = true) |
250 | |||
251 | /** |
||
252 | * Get public method for class, with optional inheritance tree and trait traverse. |
||
253 | * |
||
254 | * @param string $name |
||
255 | * @param bool $traverse |
||
256 | * |
||
257 | * @return MethodMetadata |
||
258 | * |
||
259 | * @throws \RunOpenCode\AbstractBuilder\Exception\RuntimeException |
||
260 | */ |
||
261 | 1 | public function getPublicMethod($name, $traverse = true) |
|
294 | |||
295 | /** |
||
296 | * @return Class_ |
||
297 | */ |
||
298 | public function getAst() |
||
302 | |||
303 | /** |
||
304 | * {@inheritdoc} |
||
305 | */ |
||
306 | public function __toString() |
||
310 | } |
||
311 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.