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 ObjectFacade 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 ObjectFacade, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
15 | class ObjectFacade |
||
16 | { |
||
17 | /** |
||
18 | * @var object |
||
19 | */ |
||
20 | private $object; |
||
21 | /** |
||
22 | * @var ReflectionClass |
||
23 | */ |
||
24 | private $reflection; |
||
25 | |||
26 | /** |
||
27 | * ObjectFacade constructor. |
||
28 | * |
||
29 | * @param object $object |
||
30 | */ |
||
31 | public function __construct($object) |
||
37 | |||
38 | /** |
||
39 | * @return object |
||
40 | */ |
||
41 | final public function getObject() |
||
45 | |||
46 | /** |
||
47 | * @return ReflectionClass |
||
48 | */ |
||
49 | final public function getReflection(): ReflectionClass |
||
57 | |||
58 | /** |
||
59 | * @param string $name |
||
60 | * |
||
61 | * @return array |
||
62 | */ |
||
63 | private function getNameVariations(string $name): array |
||
71 | |||
72 | /** |
||
73 | * @param string $name |
||
74 | * @param $value |
||
75 | * |
||
76 | * @return bool |
||
77 | */ |
||
78 | final public function setValue(string $name, $value): bool |
||
88 | |||
89 | /** |
||
90 | * @param string $name |
||
91 | * |
||
92 | * @return mixed|null |
||
93 | */ |
||
94 | final public function getValue(string $name) |
||
110 | |||
111 | /** |
||
112 | * @param string $name |
||
113 | * @param $value |
||
114 | * |
||
115 | * @return bool |
||
116 | */ |
||
117 | View Code Duplication | final public function setValueByProperty(string $name, $value): bool |
|
128 | |||
129 | /** |
||
130 | * @param string $name |
||
131 | * @param $value |
||
132 | * |
||
133 | * @return bool |
||
134 | */ |
||
135 | View Code Duplication | final public function setValueByMethod(string $name, $value): bool |
|
146 | |||
147 | /** |
||
148 | * @param string $name |
||
149 | * |
||
150 | * @return mixed|null |
||
151 | */ |
||
152 | View Code Duplication | final public function getValueByMethod(string $name) |
|
161 | |||
162 | /** |
||
163 | * @param string $name |
||
164 | * |
||
165 | * @return mixed|null |
||
166 | */ |
||
167 | View Code Duplication | final public function getValueByProperty(string $name) |
|
176 | |||
177 | /** |
||
178 | * @param string $postfix |
||
179 | * |
||
180 | * @return null|ReflectionMethod |
||
181 | */ |
||
182 | final public function getSetterMethod(string $postfix) |
||
186 | |||
187 | /** |
||
188 | * @param string $postfix |
||
189 | * |
||
190 | * @return null|ReflectionMethod |
||
191 | */ |
||
192 | final public function getGetterMethod(string $postfix) |
||
196 | |||
197 | /** |
||
198 | * @param string $postfix |
||
199 | * @param array $prefixe |
||
200 | * |
||
201 | * @return null|ReflectionMethod |
||
202 | */ |
||
203 | final public function getMethod(string $postfix, array $prefixe) |
||
214 | |||
215 | /** |
||
216 | * @param string $name |
||
217 | * @param array ...$args |
||
218 | * |
||
219 | * @return mixed|null |
||
220 | */ |
||
221 | final public function invokeMethod(string $name, ...$args) |
||
230 | |||
231 | /** |
||
232 | * @param string $name |
||
233 | * |
||
234 | * @return null|ReflectionMethod |
||
235 | */ |
||
236 | final public function getMethodByName(string $name) |
||
240 | |||
241 | /** |
||
242 | * @param string $name |
||
243 | * |
||
244 | * @return null|ReflectionProperty |
||
245 | */ |
||
246 | final public function getPropertyByName(string $name) |
||
250 | |||
251 | /** |
||
252 | * @param string $name |
||
253 | * |
||
254 | * @return bool |
||
255 | */ |
||
256 | final public function hasProperty(string $name): bool |
||
260 | |||
261 | /** |
||
262 | * @param string $name |
||
263 | * |
||
264 | * @return bool |
||
265 | */ |
||
266 | final public function hasMethod(string $name): bool |
||
270 | } |
||
271 |
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.