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 ReflectedClass 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 ReflectedClass, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
11 | class ReflectedClass |
||
12 | { |
||
13 | /** |
||
14 | * @var bool |
||
15 | */ |
||
16 | protected $abstract; |
||
17 | |||
18 | protected $fileName; |
||
19 | |||
20 | protected $className; |
||
21 | protected $shortClassName; |
||
22 | |||
23 | protected $namespace; |
||
24 | protected $uses = []; |
||
25 | protected $methods = []; |
||
26 | protected $magicGetSetMethods = []; |
||
27 | protected $docBlockLines = []; |
||
28 | |||
29 | protected $constructorArgs = []; |
||
30 | |||
31 | public function __construct($className) |
||
39 | |||
40 | public function addMagicGetSetMethod( |
||
69 | |||
70 | public function addMagicMethod( |
||
98 | |||
99 | public function hasMethod($methodName) |
||
103 | |||
104 | public function hasMagicGetSetMethod($methodName) |
||
108 | |||
109 | public function getMethod($methodName) |
||
116 | |||
117 | public function getMagicGetSetMethod($methodName) |
||
124 | |||
125 | /** |
||
126 | * @return array |
||
127 | */ |
||
128 | public function getConstructorArgs() |
||
132 | |||
133 | /** |
||
134 | * @param $className |
||
135 | * @param $alias |
||
136 | */ |
||
137 | public function addUse($className, $alias = null) |
||
152 | |||
153 | protected function reflectClass() |
||
167 | |||
168 | protected function reflectUseStatements() |
||
201 | |||
202 | protected function reflectDocBlock() |
||
222 | |||
223 | protected function reflectConstructorArgs() |
||
243 | |||
244 | /** |
||
245 | * @param $type |
||
246 | * @param $fieldName |
||
247 | * @return string |
||
248 | */ |
||
249 | protected function getMethodName($type, $fieldName) |
||
253 | |||
254 | protected function reflectMagicMethods($matches) |
||
276 | |||
277 | protected function skipDocBlockLine($line) |
||
284 | |||
285 | /** |
||
286 | * @return string |
||
287 | */ |
||
288 | protected function getMethodPattern() |
||
293 | |||
294 | /** |
||
295 | * @return mixed |
||
296 | */ |
||
297 | public function getClassName() |
||
301 | |||
302 | /** |
||
303 | * @return mixed |
||
304 | */ |
||
305 | public function getNamespace() |
||
309 | |||
310 | /** |
||
311 | * @return mixed |
||
312 | */ |
||
313 | public function getUses() |
||
317 | |||
318 | /** |
||
319 | * @return mixed |
||
320 | */ |
||
321 | public function getMethods() |
||
325 | |||
326 | /** |
||
327 | * @return mixed |
||
328 | */ |
||
329 | public function getMagicGetSetMethods() |
||
333 | |||
334 | /** |
||
335 | * @return array |
||
336 | */ |
||
337 | public function getDocBlockLines() |
||
341 | |||
342 | /** |
||
343 | * @return mixed |
||
344 | */ |
||
345 | public function getFileName() |
||
349 | |||
350 | /** |
||
351 | * @return bool |
||
352 | */ |
||
353 | public function isAbstract() |
||
357 | |||
358 | /** |
||
359 | * @return mixed |
||
360 | */ |
||
361 | public function getShortClassName() |
||
365 | } |
||
366 |
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.