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 | 9 | } |
|
97 | 9 | } |
|
98 | 9 | } |
|
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 | 6 | } |
|
124 | 6 | } |
|
125 | |||
126 | 6 | if (null !== $object) { |
|
127 | 2 | $this->loadDiscriminatorMetadata($object, $metadata); |
|
128 | 2 | } |
|
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) |
|
141 | { |
||
142 | 4 | $metadata = new DocumentMetadata($class->name); |
|
143 | 4 | $metadata->setAllowEmpty($document->allowEmpty); |
|
144 | |||
145 | 4 | $properties = $class->getProperties(); |
|
146 | 4 | View Code Duplication | foreach ($properties as $property) { |
147 | 4 | if ($property->getDeclaringClass()->name !== $class->name) { |
|
148 | continue; |
||
149 | } |
||
150 | |||
151 | 4 | $annotation = $this->reader->getPropertyAnnotation($property, ApiContent::class); |
|
152 | 4 | if (null !== $annotation) { |
|
153 | 4 | $metadata->setContentMetadata($this->loadPropertyMetadata($annotation, $property)); |
|
154 | |||
155 | 4 | break; |
|
156 | } |
||
157 | 4 | } |
|
158 | |||
159 | 4 | return $metadata; |
|
160 | } |
||
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) |
|
170 | { |
||
171 | 17 | $metadata = new PropertyMetadata($property->name, $property->class); |
|
172 | |||
173 | 17 | list($dataType, $dataTypeParams) = $this->parseDataType($annotation, $property); |
|
174 | |||
175 | $metadata |
||
176 | 16 | ->setDataType($dataType) |
|
177 | 16 | ->setDataTypeParams($dataTypeParams) |
|
178 | 16 | ->setDataPath($this->getDataPath($annotation, $property)) |
|
179 | 16 | ->setConverter($annotation->converter) |
|
180 | 16 | ->setGroups($annotation->groups); |
|
181 | |||
182 | 16 | if ($annotation->setter) { |
|
183 | $metadata->setSetter($annotation->setter); |
||
184 | 16 | } elseif (false === $property->isPublic()) { |
|
185 | 9 | $setter = 'set' . ucfirst($property->name); |
|
186 | 9 | if (false === $property->getDeclaringClass()->hasMethod($setter)) { |
|
187 | 1 | throw new \RuntimeException(sprintf( |
|
188 | 1 | "Couldn't find setter for non public property: %s:%s", |
|
189 | 1 | $property->class, |
|
190 | 1 | $property->name |
|
191 | 1 | )); |
|
192 | } |
||
193 | |||
194 | 8 | $metadata->setSetter($setter); |
|
195 | 8 | } |
|
196 | |||
197 | 15 | return $metadata; |
|
198 | } |
||
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) |
|
208 | { |
||
209 | 17 | if (!empty($annotation->parser)) { |
|
210 | 6 | if (!$property->getDeclaringClass()->hasMethod($annotation->parser)) { |
|
211 | 1 | throw new \InvalidArgumentException(sprintf( |
|
212 | 1 | "Custom parser function %s:%s() for property '%s' does not exist", |
|
213 | 1 | $property->class, |
|
214 | 1 | $annotation->parser, |
|
215 | 1 | $property->name |
|
216 | 1 | )); |
|
217 | } |
||
218 | 5 | return ['custom', $annotation->parser]; |
|
219 | 16 | } elseif (!empty($annotation->type)) { |
|
220 | 14 | return $this->parseDataTypeString($annotation->type); |
|
221 | 14 | } elseif (preg_match('~@var\s(.*?)\s~si', $property->getDocComment(), $matches)) { |
|
222 | 14 | return $this->parseDataTypeString($matches[1]); |
|
223 | } else { |
||
224 | 2 | return ['raw', null]; |
|
225 | } |
||
226 | } |
||
227 | |||
228 | /** |
||
229 | * Parse data type string |
||
230 | * |
||
231 | * @param string $type |
||
232 | * @return array |
||
233 | */ |
||
234 | 16 | private function parseDataTypeString($type) |
|
235 | { |
||
236 | 16 | $params = null; |
|
237 | |||
238 | 16 | if ('raw' === $type) { |
|
239 | 2 | $dataType = 'raw'; |
|
240 | 2 | $params = null; |
|
241 | 16 | } elseif ($this->isScalarDataType($type)) { |
|
242 | 15 | $dataType = 'scalar'; |
|
243 | 15 | $params = $type; |
|
244 | 16 | } elseif (preg_match('~^DateTime(<(.*?)>)?$~', $type, $matches)) { |
|
245 | 2 | $dataType = 'datetime'; |
|
246 | 2 | if (3 === count($matches)) { |
|
247 | 2 | $params = $matches[2]; |
|
248 | 2 | } |
|
249 | 2 | } elseif ( |
|
250 | 12 | (preg_match('~Array(<(.*?)>)?$~si', $type, $matches)) || |
|
251 | 12 | (preg_match('~^(.*?)\[\]$~si', $type, $matches)) |
|
252 | 12 | ) { |
|
253 | 5 | $dataType = 'array'; |
|
254 | 5 | if (3 === count($matches)) { |
|
255 | 5 | $params = $this->parseDataTypeString($matches[2]); |
|
256 | 5 | } elseif (2 === count($matches)) { |
|
257 | 2 | $params = $this->parseDataTypeString($matches[1]); |
|
258 | 2 | } else { |
|
259 | 2 | $params = ['raw', null]; |
|
260 | } |
||
261 | 5 | } else { |
|
262 | 12 | $type = ltrim($type, '\\'); |
|
263 | |||
264 | 12 | if (!class_exists($type)) { |
|
265 | throw new \InvalidArgumentException(sprintf( |
||
266 | "Unknown object type '%s' specified", |
||
267 | $type |
||
268 | )); |
||
269 | } |
||
270 | |||
271 | 12 | $dataType = 'object'; |
|
272 | 12 | $params = $type; |
|
273 | } |
||
274 | |||
275 | 16 | return [$dataType, $params]; |
|
276 | } |
||
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 |