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 |
||
43 | class AnnotationLoader implements LoaderInterface |
||
44 | { |
||
45 | /** |
||
46 | * @var Reader |
||
47 | */ |
||
48 | protected $reader; |
||
49 | |||
50 | /** |
||
51 | * Constructor |
||
52 | * |
||
53 | * @param Reader $reader |
||
54 | */ |
||
55 | 28 | public function __construct(Reader $reader) |
|
59 | |||
60 | /** |
||
61 | * @inheritdoc |
||
62 | */ |
||
63 | 18 | public function loadClassMetadata(\ReflectionClass $class) |
|
81 | |||
82 | /** |
||
83 | * Parse JSON API resource metadata |
||
84 | * |
||
85 | * @param ApiResource $resource |
||
86 | * @param \ReflectionClass $class |
||
87 | * @return ResourceMetadata |
||
88 | */ |
||
89 | 10 | private function loadResourceMetadata(ApiResource $resource, \ReflectionClass $class) |
|
90 | { |
||
91 | 10 | $metadata = new ResourceMetadata($class->name); |
|
92 | 10 | $metadata->setName($resource->name); |
|
93 | 10 | $metadata->setLoader($resource->loader); |
|
94 | |||
95 | 10 | $properties = $class->getProperties(); |
|
96 | 10 | foreach ($properties as $property) { |
|
97 | 10 | if ($property->getDeclaringClass()->name !== $class->name) { |
|
98 | 5 | continue; |
|
99 | } |
||
100 | |||
101 | 10 | foreach ($this->reader->getPropertyAnnotations($property) as $annotation) { |
|
102 | 10 | if ($annotation instanceof Attribute) { |
|
103 | 10 | $metadata->addAttribute($this->loadPropertyMetadata($annotation, $property)); |
|
104 | 10 | } elseif ($annotation instanceof Relationship) { |
|
105 | 10 | $metadata->addRelationship($this->loadPropertyMetadata($annotation, $property)); |
|
106 | 10 | } elseif ($annotation instanceof Id) { |
|
107 | 10 | $metadata->setIdMetadata($this->loadPropertyMetadata($annotation, $property)); |
|
108 | 10 | } |
|
109 | 10 | } |
|
110 | 10 | } |
|
111 | |||
112 | 10 | $methods = $class->getMethods(\ReflectionMethod::IS_PUBLIC); |
|
113 | 10 | foreach ($methods as $method) { |
|
114 | 9 | if ($method->getDeclaringClass()->name !== $class->name) { |
|
115 | 5 | continue; |
|
116 | } |
||
117 | |||
118 | 9 | foreach ($this->reader->getMethodAnnotations($method) as $annotation) { |
|
119 | 9 | if ($annotation instanceof VirtualAttribute) { |
|
120 | 9 | $metadata->addAttribute($this->loadVirtualMetadata($annotation, $method)); |
|
121 | 9 | } else if ($annotation instanceof VirtualRelationship) { |
|
122 | 9 | $metadata->addRelationship($this->loadVirtualMetadata($annotation, $method)); |
|
123 | 9 | } |
|
124 | 9 | } |
|
125 | 10 | } |
|
126 | |||
127 | 10 | $this->loadDiscriminatorMetadata($resource, $metadata); |
|
128 | |||
129 | 10 | return $metadata; |
|
130 | } |
||
131 | |||
132 | /** |
||
133 | * @param \ReflectionClass $class |
||
134 | * @param ApiObject|null $object |
||
135 | * @return ObjectMetadata |
||
136 | */ |
||
137 | 9 | private function loadObjectMetadata(\ReflectionClass $class, ApiObject $object = null) |
|
138 | { |
||
139 | 9 | $metadata = new ObjectMetadata($class->name); |
|
140 | |||
141 | 9 | $properties = $class->getProperties(); |
|
142 | 9 | View Code Duplication | foreach ($properties as $property) { |
143 | 9 | if ($property->getDeclaringClass()->name !== $class->name) { |
|
144 | 5 | continue; |
|
145 | } |
||
146 | |||
147 | 9 | $annotation = $this->reader->getPropertyAnnotation($property, Property::class); |
|
148 | /* @var $annotation Property */ |
||
149 | 9 | if (null === $annotation) { |
|
150 | continue; |
||
151 | } |
||
152 | |||
153 | 9 | $metadata->addProperty($this->loadPropertyMetadata($annotation, $property)); |
|
154 | 7 | } |
|
155 | |||
156 | 7 | $methods = $class->getMethods(\ReflectionMethod::IS_PUBLIC); |
|
157 | 7 | View Code Duplication | foreach ($methods as $method) { |
|
|||
158 | 6 | if ($method->getDeclaringClass()->name !== $class->name) { |
|
159 | 5 | continue; |
|
160 | } |
||
161 | |||
162 | 6 | $annotation = $this->reader->getMethodAnnotation($method, VirtualProperty::class); |
|
163 | /* @var $annotation VirtualProperty */ |
||
164 | 6 | if ($annotation === null) { |
|
165 | 6 | continue; |
|
166 | } |
||
167 | |||
168 | 2 | $metadata->addProperty($this->loadVirtualMetadata($annotation, $method)); |
|
169 | 7 | } |
|
170 | |||
171 | 7 | if (null !== $object) { |
|
172 | 3 | $this->loadDiscriminatorMetadata($object, $metadata); |
|
173 | 3 | } |
|
174 | |||
175 | 7 | return $metadata; |
|
176 | } |
||
177 | |||
178 | /** |
||
179 | * Parse JSON API document metadata |
||
180 | * |
||
181 | * @param ApiDocument $document |
||
182 | * @param \ReflectionClass $class |
||
183 | * @return DocumentMetadata |
||
184 | */ |
||
185 | 4 | private function loadDocumentMetadata(ApiDocument $document, \ReflectionClass $class) |
|
186 | { |
||
187 | 4 | $metadata = new DocumentMetadata($class->name); |
|
188 | 4 | $metadata->setAllowEmpty($document->allowEmpty); |
|
189 | |||
190 | 4 | $properties = $class->getProperties(); |
|
191 | 4 | foreach ($properties as $property) { |
|
192 | 4 | if ($property->getDeclaringClass()->name !== $class->name) { |
|
193 | continue; |
||
194 | } |
||
195 | |||
196 | |||
197 | 4 | foreach ($this->reader->getPropertyAnnotations($property) as $annotation) { |
|
198 | 4 | if ($annotation instanceof ApiContent) { |
|
199 | 4 | $metadata->setContentMetadata($this->loadPropertyMetadata($annotation, $property)); |
|
200 | 4 | } elseif ($annotation instanceof Metadata) { |
|
201 | 3 | $metadata->setMetadata($this->loadPropertyMetadata($annotation, $property)); |
|
202 | 3 | } |
|
203 | 4 | } |
|
204 | 4 | } |
|
205 | |||
206 | 4 | return $metadata; |
|
207 | } |
||
208 | |||
209 | /** |
||
210 | * Parse property metadata |
||
211 | * |
||
212 | * @param Property $annotation |
||
213 | * @param \ReflectionProperty $property |
||
214 | * @return PropertyMetadata |
||
215 | */ |
||
216 | 18 | private function loadPropertyMetadata(Property $annotation, \ReflectionProperty $property) |
|
217 | { |
||
218 | 18 | $metadata = new PropertyMetadata($property->name, $property->class); |
|
219 | |||
220 | 18 | list($dataType, $dataTypeParams) = $this->parseDataType($annotation, $property); |
|
221 | |||
222 | $metadata |
||
223 | 17 | ->setDataType($dataType) |
|
224 | 17 | ->setDataTypeParams($dataTypeParams) |
|
225 | 17 | ->setDataPath($this->getDataPath($annotation, $property)) |
|
226 | 17 | ->setConverter($annotation->converter) |
|
227 | 17 | ->setGroups($annotation->groups) |
|
228 | 17 | ->setLoaders($this->parseLoaders($annotation->loaders)); |
|
229 | |||
230 | 17 | if ($annotation->setter) { |
|
231 | $metadata->setSetter($annotation->setter); |
||
232 | 17 | } elseif (false === $property->isPublic()) { |
|
233 | 9 | $setter = 'set' . ucfirst($property->name); |
|
234 | 9 | if (false === $property->getDeclaringClass()->hasMethod($setter)) { |
|
235 | 1 | throw new \RuntimeException(sprintf( |
|
236 | 1 | "Couldn't find setter for non public property: %s:%s", |
|
237 | 1 | $property->class, |
|
238 | 1 | $property->name |
|
239 | 1 | )); |
|
240 | } |
||
241 | |||
242 | 8 | $metadata->setSetter($setter); |
|
243 | 8 | } |
|
244 | |||
245 | 16 | return $metadata; |
|
246 | } |
||
247 | |||
248 | /** |
||
249 | * Parse virtual property metadata |
||
250 | * |
||
251 | * @param VirtualProperty $annotation |
||
252 | * @param \ReflectionMethod $method |
||
253 | * @return PropertyMetadata |
||
254 | */ |
||
255 | 11 | private function loadVirtualMetadata(VirtualProperty $annotation, \ReflectionMethod $method) |
|
256 | { |
||
257 | 11 | if (empty($annotation->name)) { |
|
258 | throw new \InvalidArgumentException(sprintf( |
||
259 | "Virtual property name not specified: %s:%s()", |
||
260 | $method->class, |
||
261 | $method->name |
||
262 | )); |
||
263 | } |
||
264 | |||
265 | 11 | list($dataType, $dataTypeParams) = $this->parseVirtualDataType($annotation, $method); |
|
266 | |||
267 | 11 | $metadata = new PropertyMetadata($annotation->name, $method->class); |
|
268 | $metadata |
||
269 | 11 | ->setDataType($dataType) |
|
270 | 11 | ->setDataTypeParams($dataTypeParams) |
|
271 | 11 | ->setDataPath($this->getVirtualDataPath($annotation, $method)) |
|
272 | 11 | ->setConverter($annotation->converter) |
|
273 | 11 | ->setGroups($annotation->groups) |
|
274 | 11 | ->setSetter($method->name); |
|
275 | |||
276 | 11 | return $metadata; |
|
277 | } |
||
278 | |||
279 | /** |
||
280 | * Parse property data type |
||
281 | * |
||
282 | * @param Property $annotation |
||
283 | * @param \ReflectionProperty $property |
||
284 | * @return array |
||
285 | */ |
||
286 | 18 | private function parseDataType(Property $annotation, \ReflectionProperty $property) |
|
287 | { |
||
288 | 18 | if (!empty($annotation->parser)) { |
|
289 | 6 | View Code Duplication | if (!$property->getDeclaringClass()->hasMethod($annotation->parser)) { |
290 | 1 | throw new \InvalidArgumentException(sprintf( |
|
291 | 1 | "Custom parser function %s:%s() for property '%s' does not exist", |
|
292 | 1 | $property->class, |
|
293 | 1 | $annotation->parser, |
|
294 | 1 | $property->name |
|
295 | 1 | )); |
|
296 | } |
||
297 | 5 | return ['custom', $annotation->parser]; |
|
298 | 17 | } elseif (!empty($annotation->type)) { |
|
299 | 15 | return $this->parseDataTypeString($annotation->type); |
|
300 | 15 | } elseif (preg_match('~@var\s(.*?)\s~si', $property->getDocComment(), $matches)) { |
|
301 | 15 | return $this->parseDataTypeString($matches[1]); |
|
302 | } else { |
||
303 | 2 | return ['raw', null]; |
|
304 | } |
||
305 | } |
||
306 | |||
307 | /** |
||
308 | * Parse virtual property data type |
||
309 | * |
||
310 | * @param VirtualProperty $annotation |
||
311 | * @param \ReflectionMethod $method |
||
312 | * @return array |
||
313 | */ |
||
314 | 11 | private function parseVirtualDataType(VirtualProperty $annotation, \ReflectionMethod $method) |
|
315 | { |
||
316 | 11 | if (!empty($annotation->parser)) { |
|
317 | View Code Duplication | if (!$method->getDeclaringClass()->hasMethod($annotation->parser)) { |
|
318 | throw new \InvalidArgumentException(sprintf( |
||
319 | "Custom parser function %s:%s() for virtual property '%s' does not exist", |
||
320 | $method->class, |
||
321 | $annotation->parser, |
||
322 | $annotation->name |
||
323 | )); |
||
324 | } |
||
325 | return ['custom', $annotation->parser]; |
||
326 | 11 | } elseif (!empty($annotation->type)) { |
|
327 | 11 | return $this->parseDataTypeString($annotation->type); |
|
328 | } else { |
||
329 | return ['raw', null]; |
||
330 | } |
||
331 | } |
||
332 | |||
333 | /** |
||
334 | * Parse data type string |
||
335 | * |
||
336 | * @param string $type |
||
337 | * @return array |
||
338 | */ |
||
339 | 17 | private function parseDataTypeString($type) |
|
340 | { |
||
341 | 17 | $params = null; |
|
342 | |||
343 | 17 | if ('raw' === $type) { |
|
344 | 2 | $dataType = 'raw'; |
|
345 | 2 | $params = null; |
|
346 | 17 | } elseif ($this->isScalarDataType($type)) { |
|
347 | 16 | $dataType = 'scalar'; |
|
348 | 16 | $params = $type; |
|
349 | 17 | } elseif (preg_match('~^DateTime(<(.*?)>)?$~', $type, $matches)) { |
|
350 | 2 | $dataType = 'datetime'; |
|
351 | 2 | if (3 === count($matches)) { |
|
352 | 2 | $params = $matches[2]; |
|
353 | 2 | } |
|
354 | 2 | } elseif ( |
|
355 | 13 | (preg_match('~Array(<(.*?)>)?$~si', $type, $matches)) || |
|
356 | 13 | (preg_match('~^(.*?)\[\]$~si', $type, $matches)) |
|
357 | 13 | ) { |
|
358 | 6 | $dataType = 'array'; |
|
359 | 6 | if (3 === count($matches)) { |
|
360 | 5 | $params = $this->parseDataTypeString($matches[2]); |
|
361 | 6 | } elseif (2 === count($matches)) { |
|
362 | 3 | $params = $this->parseDataTypeString($matches[1]); |
|
363 | 3 | } else { |
|
364 | 2 | $params = ['raw', null]; |
|
365 | } |
||
366 | 6 | } else { |
|
367 | 13 | $type = ltrim($type, '\\'); |
|
368 | |||
369 | 13 | if (!class_exists($type)) { |
|
370 | throw new \InvalidArgumentException(sprintf( |
||
371 | "Unknown object type '%s' specified", |
||
372 | $type |
||
373 | )); |
||
374 | } |
||
375 | |||
376 | 13 | $dataType = 'object'; |
|
377 | 13 | $params = $type; |
|
378 | } |
||
379 | |||
380 | 17 | return [$dataType, $params]; |
|
381 | } |
||
382 | |||
383 | /** |
||
384 | * Returns true if specified type scalar. False otherwise. |
||
385 | * |
||
386 | * @param string $type |
||
387 | * @return bool |
||
388 | */ |
||
389 | 17 | private function isScalarDataType($type) |
|
393 | |||
394 | /** |
||
395 | * Load discriminator metadata |
||
396 | * |
||
397 | * @param ApiObject $object |
||
398 | * @param ClassMetadata $metadata |
||
399 | */ |
||
400 | 12 | private function loadDiscriminatorMetadata(ApiObject $object, ClassMetadata $metadata) |
|
401 | { |
||
430 | |||
431 | /** |
||
432 | * Returns data path |
||
433 | * |
||
434 | * @param Property $annotation |
||
435 | * @param \ReflectionProperty $property |
||
436 | * @return string |
||
437 | */ |
||
438 | 17 | private function getDataPath(Property $annotation, \ReflectionProperty $property) |
|
459 | |||
460 | /** |
||
461 | * Returns data path for virtual property |
||
462 | * |
||
463 | * @param VirtualProperty $annotation |
||
464 | * @param \ReflectionMethod $method |
||
465 | * @return string |
||
466 | */ |
||
467 | 11 | private function getVirtualDataPath(VirtualProperty $annotation, \ReflectionMethod $method) |
|
488 | |||
489 | /** |
||
490 | * Parse property loaders |
||
491 | * |
||
492 | * @param array|Loader[] $loaders |
||
493 | * @return array |
||
494 | */ |
||
495 | 17 | private function parseLoaders(array $loaders) |
|
512 | } |
||
513 |
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.