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 ClassAnnotations 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 ClassAnnotations, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
9 | class ClassAnnotations extends Annotations |
||
10 | { |
||
11 | protected $classRefl; |
||
12 | protected $className; |
||
13 | protected $classGetter; |
||
14 | protected $classPropertyPattern; |
||
15 | protected $classWriteEmpty; |
||
16 | protected $classDocType; |
||
17 | protected $classRdfType; |
||
18 | protected $classRdfAbout; |
||
19 | protected $classDefaultPrefix; |
||
20 | protected $classIsRDF; |
||
21 | protected $classIgnoreAllClass; |
||
22 | protected $classNamespace; |
||
23 | protected $classDontCreateClassNode; |
||
24 | |||
25 | protected $propertyList; |
||
26 | protected $propertyAnnotations = []; |
||
27 | |||
28 | protected $model; |
||
29 | protected $forcePropName; |
||
30 | |||
31 | public function __construct($model, $config = "object", $forcePropName = "") |
||
59 | |||
60 | public function getPropertyList() |
||
77 | |||
78 | /** |
||
79 | * |
||
80 | * @param ReflectionProperty $property |
||
81 | * @param string $keyProp |
||
82 | * @return PropertyAnnotations |
||
83 | */ |
||
84 | public function getPropertyAnnotation($property, $keyProp = null) |
||
93 | |||
94 | public function getClassInstance() |
||
98 | |||
99 | public function getClassRefl() |
||
103 | |||
104 | public function getClassName() |
||
114 | |||
115 | public function getClassGetter() |
||
122 | |||
123 | public function getClassPropertyPattern($id = null) |
||
135 | |||
136 | public function getClassWriteEmpty() |
||
143 | |||
144 | public function getClassDocType() |
||
151 | |||
152 | View Code Duplication | public function getClassRdfType() |
|
160 | |||
161 | View Code Duplication | public function getClassRdfAbout() |
|
169 | |||
170 | public function getClassDefaultPrefix() |
||
177 | |||
178 | public function getClassIsRDF() |
||
185 | |||
186 | public function getClassIgnoreAllClass() |
||
193 | |||
194 | public function getClassNamespace() |
||
215 | |||
216 | public function getClassDontCreateClassNode() |
||
223 | |||
224 | |||
225 | public function setClassRefl($classRefl) |
||
229 | |||
230 | } |
||
231 |
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.