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 ClassAnnotator 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 ClassAnnotator, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
11 | class ClassAnnotator |
||
12 | { |
||
13 | /** |
||
14 | * @var ReflectedClass |
||
15 | */ |
||
16 | protected $class; |
||
17 | |||
18 | protected $fields; |
||
19 | |||
20 | public function __construct($className) |
||
21 | { |
||
22 | $this->class = new ReflectedClass($className); |
||
23 | } |
||
24 | |||
25 | /** |
||
26 | * @return array |
||
27 | */ |
||
28 | protected function reflectFields() |
||
29 | { |
||
30 | $reflectionClass = new \ReflectionClass($this->class->getClassName()); |
||
31 | if (!$reflectionClass->hasMethod('fieldDefinitions')) { |
||
32 | return; |
||
33 | } |
||
34 | $reflectionMethod = $reflectionClass->getMethod('fieldDefinitions'); |
||
35 | |||
36 | $classObject = $reflectionClass->newInstanceWithoutConstructor(); |
||
37 | $this->fields = $reflectionMethod->invoke($classObject); |
||
38 | |||
39 | foreach ($this->fields as $fieldName => $field) { |
||
40 | $fieldType = ''; |
||
41 | if (isset($field[JsonObject::TYPE])) { |
||
42 | $fieldType = $field[JsonObject::TYPE]; |
||
43 | } |
||
44 | if (isset($field[JsonObject::DECORATOR])) { |
||
45 | $getReturnType = $field[JsonObject::DECORATOR]; |
||
46 | $this->class->addUse($field[JsonObject::DECORATOR]); |
||
47 | } else { |
||
48 | $getReturnType = $fieldType; |
||
49 | } |
||
50 | $getReturnTypeParts = explode('\\', trim($getReturnType, '\\')); |
||
51 | if (!$this->isPrimitive($fieldType) && count($getReturnTypeParts) > 1) { |
||
52 | $getReturnClassName = array_pop($getReturnTypeParts); |
||
53 | } else { |
||
54 | $getReturnClassName = $getReturnType; |
||
55 | } |
||
56 | if ($this->isOptional($field)) { |
||
57 | $optional = ' = null'; |
||
58 | } else { |
||
59 | $optional = ''; |
||
60 | } |
||
61 | |||
62 | $fieldTypeParts = explode('\\', trim($fieldType, '\\')); |
||
63 | if (!$this->isPrimitive($fieldType) && count($fieldTypeParts) > 1) { |
||
64 | $this->class->addUse($fieldType); |
||
65 | $fieldType = array_pop($fieldTypeParts); |
||
66 | } |
||
67 | |||
68 | $args = [trim($fieldType . ' $' . $fieldName . $optional)]; |
||
69 | |||
70 | $this->class->addMagicGetSetMethod('get', $fieldName, [], $getReturnClassName); |
||
71 | $this->class->addMagicGetSetMethod('set', $fieldName, $args, $this->class->getShortClassName()); |
||
72 | } |
||
73 | } |
||
74 | |||
75 | protected function reflectReference() |
||
96 | |||
97 | protected function reflectElementType() |
||
98 | { |
||
99 | $reflectionClass = new \ReflectionClass($this->class->getClassName()); |
||
100 | if (!$reflectionClass->hasMethod('getType')) { |
||
101 | return; |
||
102 | } |
||
103 | $reflectionMethod = $reflectionClass->getMethod('getType'); |
||
104 | |||
105 | $classObject = $reflectionClass->newInstanceWithoutConstructor(); |
||
106 | $elementType = $reflectionMethod->invoke($classObject); |
||
107 | |||
108 | if ($elementType && !$this->isPrimitive($elementType)) { |
||
109 | $elementTypeClass = new \ReflectionClass($elementType); |
||
110 | $this->class->addUse($elementType); |
||
111 | $getAtMethod = $reflectionClass->getMethod('getAt'); |
||
112 | View Code Duplication | if ($getAtMethod->getDeclaringClass()->getName() != $this->class->getClassName()) { |
|
|
|||
113 | $this->class->addMagicMethod( |
||
114 | 'getAt', |
||
115 | ['$offset'], |
||
116 | $elementTypeClass->getShortName(), |
||
117 | null, |
||
118 | null, |
||
119 | false, |
||
120 | true |
||
121 | ); |
||
122 | } |
||
123 | $addMethod = $reflectionClass->getMethod('add'); |
||
124 | View Code Duplication | if ($addMethod->getDeclaringClass()->getName() != $this->class->getClassName()) { |
|
125 | $this->class->addMagicMethod( |
||
126 | 'add', |
||
127 | [$elementTypeClass->getShortName() . ' $element'], |
||
128 | $reflectionClass->getShortName(), |
||
129 | null, |
||
130 | null, |
||
131 | false, |
||
132 | true |
||
133 | ); |
||
134 | } |
||
135 | $current = $reflectionClass->getMethod('current'); |
||
136 | View Code Duplication | if ($current->getDeclaringClass()->getName() != $this->class->getClassName()) { |
|
137 | $this->class->addMagicMethod( |
||
138 | 'current', |
||
139 | [], |
||
140 | $elementTypeClass->getShortName(), |
||
141 | null, |
||
142 | null, |
||
143 | false, |
||
144 | true |
||
145 | ); |
||
146 | } |
||
147 | } |
||
148 | } |
||
149 | |||
150 | protected function reflectResultClass() |
||
151 | { |
||
152 | $reflectionClass = new \ReflectionClass($this->class->getClassName()); |
||
153 | if (!$reflectionClass->hasMethod('getResultClass')) { |
||
154 | return; |
||
155 | } |
||
156 | $reflectionMethod = $reflectionClass->getMethod('getResultClass'); |
||
157 | |||
158 | $classObject = $reflectionClass->newInstanceWithoutConstructor(); |
||
159 | $resultClass = $reflectionMethod->invoke($classObject); |
||
160 | |||
161 | $resultClassReflection = new \ReflectionClass($resultClass); |
||
162 | $this->class->addUse($resultClass); |
||
163 | $mapResponseMethod = $reflectionClass->getMethod('mapResponse'); |
||
164 | View Code Duplication | if ($mapResponseMethod->getDeclaringClass()->getName() != $this->class->getClassName()) { |
|
165 | $this->class->addUse('\Commercetools\Core\Response\ApiResponseInterface'); |
||
166 | $this->class->addMagicMethod( |
||
167 | 'mapResponse', |
||
168 | ['ApiResponseInterface $response'], |
||
169 | $resultClassReflection->getShortName(), |
||
170 | null, |
||
171 | null, |
||
172 | false, |
||
173 | true |
||
174 | ); |
||
175 | } |
||
176 | } |
||
177 | |||
178 | /** |
||
179 | * |
||
180 | */ |
||
181 | public function generate() |
||
191 | |||
192 | public function generateCurrentMethod() |
||
193 | { |
||
194 | if ($this->class->isAbstract()) { |
||
195 | return; |
||
201 | |||
202 | public function generateMapResponseMethod() |
||
211 | |||
212 | /** |
||
213 | * @param $field |
||
214 | * @return bool |
||
215 | */ |
||
216 | protected function isOptional($field) |
||
226 | |||
227 | |||
228 | protected function annotate() |
||
279 | |||
280 | protected function ignoreDocBlockLine($lineNr, $lines) |
||
297 | |||
298 | protected function isPrimitive($type) |
||
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.