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