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 |
||
42 | class AnnotationLoader implements LoaderInterface |
||
43 | { |
||
44 | /** |
||
45 | * @var Reader |
||
46 | */ |
||
47 | protected $reader; |
||
48 | |||
49 | /** |
||
50 | * Constructor |
||
51 | * |
||
52 | * @param Reader $reader |
||
53 | */ |
||
54 | 30 | public function __construct(Reader $reader) |
|
58 | |||
59 | /** |
||
60 | * @inheritdoc |
||
61 | */ |
||
62 | 19 | public function loadClassMetadata(\ReflectionClass $class) |
|
63 | { |
||
64 | 19 | if (class_exists('Doctrine\ORM\Proxy\Proxy') && $class->implementsInterface('Doctrine\ORM\Proxy\Proxy')) { |
|
65 | return $this->loadClassMetadata($class->getParentClass()); |
||
66 | } |
||
67 | |||
68 | 19 | if (null !== ($resource = $this->reader->getClassAnnotation($class, ApiResource::class))) { |
|
69 | /* @var $resource ApiResource */ |
||
70 | 11 | return $this->loadResourceMetadata($resource, $class); |
|
71 | 11 | } elseif (null !== ($document = $this->reader->getClassAnnotation($class, ApiDocument::class))) { |
|
72 | /* @var $document ApiDocument */ |
||
73 | 4 | return $this->loadDocumentMetadata($document, $class); |
|
74 | } else { |
||
75 | 9 | $object = $this->reader->getClassAnnotation($class, ApiObject::class); |
|
76 | |||
77 | 9 | return $this->loadObjectMetadata($class, $object); |
|
78 | } |
||
79 | } |
||
80 | |||
81 | /** |
||
82 | * Parse JSON API resource metadata |
||
83 | * |
||
84 | * @param ApiResource $resource |
||
85 | * @param \ReflectionClass $class |
||
86 | * @return ResourceMetadata |
||
87 | */ |
||
88 | 11 | private function loadResourceMetadata(ApiResource $resource, \ReflectionClass $class) |
|
130 | |||
131 | /** |
||
132 | * @param \ReflectionClass $class |
||
133 | * @param ApiObject|null $object |
||
134 | * @return ObjectMetadata |
||
135 | */ |
||
136 | 9 | private function loadObjectMetadata(\ReflectionClass $class, ApiObject $object = null) |
|
176 | |||
177 | /** |
||
178 | * Parse JSON API document metadata |
||
179 | * |
||
180 | * @param ApiDocument $document |
||
181 | * @param \ReflectionClass $class |
||
182 | * @return DocumentMetadata |
||
183 | */ |
||
184 | 4 | private function loadDocumentMetadata(ApiDocument $document, \ReflectionClass $class) |
|
207 | |||
208 | /** |
||
209 | * Parse property metadata |
||
210 | * |
||
211 | * @param Property $annotation |
||
212 | * @param \ReflectionProperty $property |
||
213 | * @return PropertyMetadata |
||
214 | */ |
||
215 | 19 | private function loadPropertyMetadata(Property $annotation, \ReflectionProperty $property) |
|
246 | |||
247 | /** |
||
248 | * Parse virtual property metadata |
||
249 | * |
||
250 | * @param VirtualProperty $annotation |
||
251 | * @param \ReflectionMethod $method |
||
252 | * @return PropertyMetadata |
||
253 | */ |
||
254 | 11 | private function loadVirtualMetadata(VirtualProperty $annotation, \ReflectionMethod $method) |
|
277 | |||
278 | /** |
||
279 | * Parse property data type |
||
280 | * |
||
281 | * @param Property $annotation |
||
282 | * @param \ReflectionProperty $property |
||
283 | * @return array |
||
284 | */ |
||
285 | 19 | private function parseDataType(Property $annotation, \ReflectionProperty $property) |
|
305 | |||
306 | /** |
||
307 | * Parse virtual property data type |
||
308 | * |
||
309 | * @param VirtualProperty $annotation |
||
310 | * @param \ReflectionMethod $method |
||
311 | * @return array |
||
312 | */ |
||
313 | 11 | private function parseVirtualDataType(VirtualProperty $annotation, \ReflectionMethod $method) |
|
331 | |||
332 | /** |
||
333 | * Parse data type string |
||
334 | * |
||
335 | * @param string $type |
||
336 | * @return array |
||
337 | */ |
||
338 | 18 | private function parseDataTypeString($type) |
|
339 | { |
||
340 | 18 | $params = null; |
|
341 | |||
342 | 18 | if ('raw' === $type) { |
|
343 | 2 | $dataType = 'raw'; |
|
344 | 2 | $params = null; |
|
345 | 18 | } elseif ($this->isScalarDataType($type)) { |
|
346 | 17 | $dataType = 'scalar'; |
|
347 | 17 | $params = $type; |
|
348 | 14 | } elseif (preg_match('~^DateTime(<(.*?)>)?$~', $type, $matches)) { |
|
349 | 2 | $dataType = 'datetime'; |
|
350 | 2 | if (3 === count($matches)) { |
|
351 | 2 | $params = $matches[2]; |
|
352 | } |
||
353 | } elseif ( |
||
354 | 14 | (preg_match('~Array(<(.*?)>)?$~si', $type, $matches)) || |
|
355 | 14 | (preg_match('~^(.*?)\[\]$~si', $type, $matches)) |
|
356 | ) { |
||
357 | 13 | $dataType = 'array'; |
|
358 | 13 | if (3 === count($matches)) { |
|
359 | 5 | $params = $this->parseDataTypeString($matches[2]); |
|
360 | 12 | } elseif (2 === count($matches)) { |
|
361 | 12 | $params = $this->parseDataTypeString($matches[1]); |
|
362 | } else { |
||
363 | 13 | $params = ['raw', null]; |
|
364 | } |
||
365 | } else { |
||
366 | 14 | $type = ltrim($type, '\\'); |
|
367 | |||
368 | 14 | if (!class_exists($type)) { |
|
369 | throw new \InvalidArgumentException(sprintf( |
||
370 | "Unknown object type '%s' specified", |
||
371 | $type |
||
372 | )); |
||
373 | } |
||
374 | |||
375 | 14 | $dataType = 'object'; |
|
376 | 14 | $params = $type; |
|
377 | } |
||
378 | |||
379 | 18 | return [$dataType, $params]; |
|
380 | } |
||
381 | |||
382 | /** |
||
383 | * Returns true if specified type scalar. False otherwise. |
||
384 | * |
||
385 | * @param string $type |
||
386 | * @return bool |
||
387 | */ |
||
388 | 18 | private function isScalarDataType($type) |
|
392 | |||
393 | /** |
||
394 | * Load discriminator metadata |
||
395 | * |
||
396 | * @param ApiObject $object |
||
397 | * @param ClassMetadata $metadata |
||
398 | */ |
||
399 | 13 | private function loadDiscriminatorMetadata(ApiObject $object, ClassMetadata $metadata) |
|
429 | |||
430 | /** |
||
431 | * Returns data path |
||
432 | * |
||
433 | * @param Property $annotation |
||
434 | * @param \ReflectionProperty $property |
||
435 | * @return string |
||
436 | */ |
||
437 | 18 | private function getDataPath(Property $annotation, \ReflectionProperty $property) |
|
458 | |||
459 | /** |
||
460 | * Returns data path for virtual property |
||
461 | * |
||
462 | * @param VirtualProperty $annotation |
||
463 | * @param \ReflectionMethod $method |
||
464 | * @return string |
||
465 | */ |
||
466 | 11 | private function getVirtualDataPath(VirtualProperty $annotation, \ReflectionMethod $method) |
|
487 | |||
488 | /** |
||
489 | * Parse property loaders |
||
490 | * |
||
491 | * @param array|Loader[] $loaders |
||
492 | * @return array |
||
493 | */ |
||
494 | 18 | private function parseLoaders(array $loaders) |
|
511 | } |
||
512 |
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.