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 AnnotationLoader 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 AnnotationLoader, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
37 | class AnnotationLoader implements LoaderInterface |
||
38 | { |
||
39 | /** |
||
40 | * @var Reader |
||
41 | */ |
||
42 | protected $reader; |
||
43 | |||
44 | /** |
||
45 | * Constructor |
||
46 | * |
||
47 | * @param Reader $reader |
||
48 | */ |
||
49 | 19 | public function __construct(Reader $reader) |
|
53 | |||
54 | /** |
||
55 | * @inheritdoc |
||
56 | */ |
||
57 | 13 | public function loadClassMetadata(\ReflectionClass $class) |
|
69 | |||
70 | /** |
||
71 | * Parse JSON API resource metadata |
||
72 | * |
||
73 | * @param ApiResource $resource |
||
74 | * @param \ReflectionClass $class |
||
75 | * @return ResourceMetadata |
||
76 | */ |
||
77 | 6 | private function loadResourceMetadata(ApiResource $resource, \ReflectionClass $class) |
|
78 | { |
||
79 | 6 | $metadata = new ResourceMetadata($class->name); |
|
80 | 6 | $metadata->setName($resource->name); |
|
81 | |||
82 | 6 | $properties = $class->getProperties(); |
|
83 | 6 | foreach ($properties as $property) { |
|
84 | 6 | if ($property->getDeclaringClass()->name !== $class->name) { |
|
85 | 4 | continue; |
|
86 | } |
||
87 | |||
88 | 6 | foreach ($this->reader->getPropertyAnnotations($property) as $annotation) { |
|
89 | 6 | if ($annotation instanceof Attribute) { |
|
90 | 6 | $metadata->addAttribute($this->loadPropertyMetadata($annotation, $property)); |
|
91 | } elseif ($annotation instanceof Relationship) { |
||
92 | 6 | $metadata->addRelationship($this->loadPropertyMetadata($annotation, $property)); |
|
93 | } elseif ($annotation instanceof Id) { |
||
94 | 6 | $metadata->setIdMetadata($this->loadPropertyMetadata($annotation, $property)); |
|
95 | } |
||
96 | } |
||
97 | } |
||
98 | |||
99 | 6 | $this->loadDiscriminatorMetadata($resource, $metadata); |
|
100 | |||
101 | 6 | return $metadata; |
|
102 | } |
||
103 | |||
104 | /** |
||
105 | * @param \ReflectionClass $class |
||
106 | * @param ApiObject|null $object |
||
107 | * @return ObjectMetadata |
||
108 | */ |
||
109 | 6 | private function loadObjectMetadata(\ReflectionClass $class, ApiObject $object = null) |
|
110 | { |
||
111 | 6 | $metadata = new ObjectMetadata($class->name); |
|
112 | |||
113 | 6 | $properties = $class->getProperties(); |
|
114 | 6 | View Code Duplication | foreach ($properties as $property) { |
1 ignored issue
–
show
|
|||
115 | 6 | if ($property->getDeclaringClass()->name !== $class->name) { |
|
116 | 3 | continue; |
|
117 | } |
||
118 | |||
119 | 6 | $annotation = $this->reader->getPropertyAnnotation($property, Property::class); |
|
120 | 6 | if (null !== $annotation) { |
|
121 | 6 | $metadata->addProperty($this->loadPropertyMetadata($annotation, $property)); |
|
122 | } |
||
123 | } |
||
124 | |||
125 | 4 | if (null !== $object) { |
|
126 | 2 | $this->loadDiscriminatorMetadata($object, $metadata); |
|
127 | } |
||
128 | |||
129 | 4 | return $metadata; |
|
130 | } |
||
131 | |||
132 | /** |
||
133 | * Parse JSON API document metadata |
||
134 | * |
||
135 | * @param ApiDocument $document |
||
136 | * @param \ReflectionClass $class |
||
137 | * @return DocumentMetadata |
||
138 | */ |
||
139 | 3 | private function loadDocumentMetadata(ApiDocument $document, \ReflectionClass $class) |
|
140 | { |
||
141 | 3 | $metadata = new DocumentMetadata($class->name); |
|
142 | 3 | $metadata->setAllowEmpty($document->allowEmpty); |
|
143 | |||
144 | 3 | $properties = $class->getProperties(); |
|
145 | 3 | View Code Duplication | foreach ($properties as $property) { |
1 ignored issue
–
show
|
|||
146 | 3 | if ($property->getDeclaringClass()->name !== $class->name) { |
|
147 | continue; |
||
148 | } |
||
149 | |||
150 | 3 | $annotation = $this->reader->getPropertyAnnotation($property, ApiContent::class); |
|
151 | 3 | if (null !== $annotation) { |
|
152 | 3 | $metadata->setContentMetadata($this->loadPropertyMetadata($annotation, $property)); |
|
153 | |||
154 | 3 | break; |
|
155 | } |
||
156 | } |
||
157 | |||
158 | 3 | return $metadata; |
|
159 | } |
||
160 | |||
161 | /** |
||
162 | * Parse property metadata |
||
163 | * |
||
164 | * @param Property $annotation |
||
165 | * @param \ReflectionProperty $property |
||
166 | * @return PropertyMetadata |
||
167 | */ |
||
168 | 13 | private function loadPropertyMetadata(Property $annotation, \ReflectionProperty $property) |
|
169 | { |
||
170 | 13 | $metadata = new PropertyMetadata($property->name, $property->class); |
|
171 | |||
172 | 13 | list($dataType, $dataTypeParams) = $this->parseDataType($annotation, $property); |
|
173 | |||
174 | $metadata |
||
175 | 12 | ->setDataType($dataType) |
|
176 | 12 | ->setDataTypeParams($dataTypeParams) |
|
177 | 12 | ->setDataPath($this->getDataPath($annotation, $property)); |
|
178 | |||
179 | 12 | if ($annotation->setter) { |
|
180 | $metadata->setSetter($annotation->setter); |
||
181 | 12 | } elseif (false === $property->isPublic()) { |
|
182 | 7 | $setter = 'set' . ucfirst($property->name); |
|
183 | 7 | if (false === $property->getDeclaringClass()->hasMethod($setter)) { |
|
184 | 1 | throw new \RuntimeException(sprintf( |
|
185 | 1 | "Couldn't find setter for non public property: %s:%s", |
|
186 | 1 | $property->class, |
|
187 | 1 | $property->name |
|
188 | )); |
||
189 | } |
||
190 | |||
191 | 6 | $metadata->setSetter($setter); |
|
192 | } |
||
193 | |||
194 | 11 | return $metadata; |
|
195 | } |
||
196 | |||
197 | /** |
||
198 | * Parse property data type |
||
199 | * |
||
200 | * @param Property $annotation |
||
201 | * @param \ReflectionProperty $property |
||
202 | * @return array |
||
203 | */ |
||
204 | 13 | private function parseDataType(Property $annotation, \ReflectionProperty $property) |
|
205 | { |
||
206 | 13 | if (!empty($annotation->parser)) { |
|
207 | 4 | if (!$property->getDeclaringClass()->hasMethod($annotation->parser)) { |
|
208 | 1 | throw new \InvalidArgumentException(sprintf( |
|
209 | 1 | "Custom parser function %s:%s() for property '%s' does not exist", |
|
210 | 1 | $property->class, |
|
211 | 1 | $annotation->parser, |
|
212 | 1 | $property->name |
|
213 | )); |
||
214 | } |
||
215 | 3 | return ['custom', $annotation->parser]; |
|
216 | 12 | } elseif (!empty($annotation->type)) { |
|
217 | 10 | return $this->parseDataTypeString($annotation->type); |
|
218 | 10 | } elseif (preg_match('~@var\s(.*?)\s~si', $property->getDocComment(), $matches)) { |
|
219 | 10 | return $this->parseDataTypeString($matches[1]); |
|
220 | } else { |
||
221 | 2 | return ['raw', null]; |
|
222 | } |
||
223 | } |
||
224 | |||
225 | /** |
||
226 | * Parse data type string |
||
227 | * |
||
228 | * @param string $type |
||
229 | * @return array |
||
230 | */ |
||
231 | 12 | private function parseDataTypeString($type) |
|
232 | { |
||
233 | 12 | $params = null; |
|
234 | |||
235 | 12 | if ($this->isScalarDataType($type)) { |
|
236 | 11 | $dataType = 'scalar'; |
|
237 | 11 | $params = $type; |
|
238 | 9 | } elseif (preg_match('~^DateTime(<(.*?)>)?$~', $type, $matches)) { |
|
239 | 2 | $dataType = 'datetime'; |
|
240 | 2 | if (3 === count($matches)) { |
|
241 | 2 | $params = $matches[2]; |
|
242 | } |
||
243 | } elseif ( |
||
244 | 9 | (preg_match('~Array(<(.*?)>)?$~si', $type, $matches)) || |
|
245 | 9 | (preg_match('~^(.*?)\[\]$~si', $type, $matches)) |
|
246 | ) { |
||
247 | 5 | $dataType = 'array'; |
|
248 | 5 | if (3 === count($matches)) { |
|
249 | 5 | $params = $this->parseDataTypeString($matches[2]); |
|
250 | 2 | } elseif (2 === count($matches)) { |
|
251 | 2 | $params = $this->parseDataTypeString($matches[1]); |
|
252 | } else { |
||
253 | 5 | $params = ['raw', null]; |
|
254 | } |
||
255 | } else { |
||
256 | 9 | $type = ltrim($type, '\\'); |
|
257 | |||
258 | 9 | if (!class_exists($type)) { |
|
259 | throw new \InvalidArgumentException(sprintf( |
||
260 | "Unknown object type '%s' specified", |
||
261 | $type |
||
262 | )); |
||
263 | } |
||
264 | |||
265 | 9 | $dataType = 'object'; |
|
266 | 9 | $params = $type; |
|
267 | } |
||
268 | |||
269 | 12 | return [$dataType, $params]; |
|
270 | } |
||
271 | |||
272 | /** |
||
273 | * Returns true if specified type scalar. False otherwise. |
||
274 | * |
||
275 | * @param string $type |
||
276 | * @return bool |
||
277 | */ |
||
278 | 12 | private function isScalarDataType($type) |
|
282 | |||
283 | /** |
||
284 | * Load discriminator metadata |
||
285 | * |
||
286 | * @param ApiObject $object |
||
287 | * @param ClassMetadata $metadata |
||
288 | */ |
||
289 | 8 | private function loadDiscriminatorMetadata(ApiObject $object, ClassMetadata $metadata) |
|
318 | |||
319 | /** |
||
320 | * Returns data path |
||
321 | * |
||
322 | * @param Property $annotation |
||
323 | * @param \ReflectionProperty $property |
||
324 | * @return string |
||
325 | */ |
||
326 | 12 | private function getDataPath(Property $annotation, \ReflectionProperty $property) |
|
341 | } |
||
342 |
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.