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) |
|
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) |
|
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) |
|
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 | 1 | )); |
|
190 | } |
||
191 | |||
192 | 8 | $metadata->setSetter($setter); |
|
193 | 8 | } |
|
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) |
|
225 | |||
226 | /** |
||
227 | * Parse data type string |
||
228 | * |
||
229 | * @param string $type |
||
230 | * @return array |
||
231 | */ |
||
232 | 16 | private function parseDataTypeString($type) |
|
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 |