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 | 27 | public function __construct(Reader $reader) |
|
53 | |||
54 | /** |
||
55 | * @inheritdoc |
||
56 | */ |
||
57 | 17 | 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 | 9 | private function loadResourceMetadata(ApiResource $resource, \ReflectionClass $class) |
|
78 | { |
||
79 | 9 | $metadata = new ResourceMetadata($class->name); |
|
80 | 9 | $metadata->setName($resource->name); |
|
81 | 9 | $metadata->setLoader($resource->loader); |
|
82 | |||
83 | 9 | $properties = $class->getProperties(); |
|
84 | 9 | foreach ($properties as $property) { |
|
85 | 9 | if ($property->getDeclaringClass()->name !== $class->name) { |
|
86 | 5 | continue; |
|
87 | } |
||
88 | |||
89 | 9 | foreach ($this->reader->getPropertyAnnotations($property) as $annotation) { |
|
90 | 9 | if ($annotation instanceof Attribute) { |
|
91 | 9 | $metadata->addAttribute($this->loadPropertyMetadata($annotation, $property)); |
|
92 | 9 | } elseif ($annotation instanceof Relationship) { |
|
93 | 9 | $metadata->addRelationship($this->loadPropertyMetadata($annotation, $property)); |
|
94 | 9 | } elseif ($annotation instanceof Id) { |
|
95 | 9 | $metadata->setIdMetadata($this->loadPropertyMetadata($annotation, $property)); |
|
96 | } |
||
97 | } |
||
98 | } |
||
99 | |||
100 | 9 | $this->loadDiscriminatorMetadata($resource, $metadata); |
|
101 | |||
102 | 9 | return $metadata; |
|
103 | } |
||
104 | |||
105 | /** |
||
106 | * @param \ReflectionClass $class |
||
107 | * @param ApiObject|null $object |
||
108 | * @return ObjectMetadata |
||
109 | */ |
||
110 | 8 | private function loadObjectMetadata(\ReflectionClass $class, ApiObject $object = null) |
|
111 | { |
||
112 | 8 | $metadata = new ObjectMetadata($class->name); |
|
113 | |||
114 | 8 | $properties = $class->getProperties(); |
|
115 | 8 | View Code Duplication | foreach ($properties as $property) { |
116 | 8 | if ($property->getDeclaringClass()->name !== $class->name) { |
|
117 | 5 | continue; |
|
118 | } |
||
119 | |||
120 | 8 | $annotation = $this->reader->getPropertyAnnotation($property, Property::class); |
|
121 | 8 | if (null !== $annotation) { |
|
122 | 8 | $metadata->addProperty($this->loadPropertyMetadata($annotation, $property)); |
|
123 | } |
||
124 | } |
||
125 | |||
126 | 6 | if (null !== $object) { |
|
127 | 2 | $this->loadDiscriminatorMetadata($object, $metadata); |
|
128 | } |
||
129 | |||
130 | 6 | return $metadata; |
|
131 | } |
||
132 | |||
133 | /** |
||
134 | * Parse JSON API document metadata |
||
135 | * |
||
136 | * @param ApiDocument $document |
||
137 | * @param \ReflectionClass $class |
||
138 | * @return DocumentMetadata |
||
139 | */ |
||
140 | 4 | private function loadDocumentMetadata(ApiDocument $document, \ReflectionClass $class) |
|
161 | |||
162 | /** |
||
163 | * Parse property metadata |
||
164 | * |
||
165 | * @param Property $annotation |
||
166 | * @param \ReflectionProperty $property |
||
167 | * @return PropertyMetadata |
||
168 | */ |
||
169 | 17 | private function loadPropertyMetadata(Property $annotation, \ReflectionProperty $property) |
|
199 | |||
200 | /** |
||
201 | * Parse property data type |
||
202 | * |
||
203 | * @param Property $annotation |
||
204 | * @param \ReflectionProperty $property |
||
205 | * @return array |
||
206 | */ |
||
207 | 17 | private function parseDataType(Property $annotation, \ReflectionProperty $property) |
|
227 | |||
228 | /** |
||
229 | * Parse data type string |
||
230 | * |
||
231 | * @param string $type |
||
232 | * @return array |
||
233 | */ |
||
234 | 16 | private function parseDataTypeString($type) |
|
277 | |||
278 | /** |
||
279 | * Returns true if specified type scalar. False otherwise. |
||
280 | * |
||
281 | * @param string $type |
||
282 | * @return bool |
||
283 | */ |
||
284 | 16 | private function isScalarDataType($type) |
|
288 | |||
289 | /** |
||
290 | * Load discriminator metadata |
||
291 | * |
||
292 | * @param ApiObject $object |
||
293 | * @param ClassMetadata $metadata |
||
294 | */ |
||
295 | 11 | private function loadDiscriminatorMetadata(ApiObject $object, ClassMetadata $metadata) |
|
325 | |||
326 | /** |
||
327 | * Returns data path |
||
328 | * |
||
329 | * @param Property $annotation |
||
330 | * @param \ReflectionProperty $property |
||
331 | * @return string |
||
332 | */ |
||
333 | 16 | private function getDataPath(Property $annotation, \ReflectionProperty $property) |
|
354 | } |
||
355 |