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 AbstractClassMetadataLoader 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 AbstractClassMetadataLoader, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
24 | abstract class AbstractClassMetadataLoader implements ClassMetadataLoaderInterface |
||
25 | { |
||
26 | /** |
||
27 | * @var TypeParserInterface |
||
28 | */ |
||
29 | private $typeParser; |
||
30 | |||
31 | /** |
||
32 | * @var mixed[][] |
||
33 | */ |
||
34 | private $data = []; |
||
35 | |||
36 | /** |
||
37 | * @param TypeParserInterface|null $typeParser |
||
38 | */ |
||
39 | public function __construct(TypeParserInterface $typeParser = null) |
||
43 | |||
44 | /** |
||
45 | * {@inheritdoc} |
||
46 | */ |
||
47 | public function loadClassMetadata(ClassMetadataInterface $classMetadata) |
||
63 | |||
64 | /** |
||
65 | * @param string $class |
||
66 | * |
||
67 | * @return mixed[]|null |
||
68 | */ |
||
69 | abstract protected function loadData($class); |
||
70 | |||
71 | /** |
||
72 | * @param ClassMetadataInterface $classMetadata |
||
73 | * @param mixed[] $data |
||
74 | * |
||
75 | * @return bool |
||
76 | */ |
||
77 | private function doLoadClassMetadata(ClassMetadataInterface $classMetadata, array $data) |
||
97 | |||
98 | /** |
||
99 | * @param PropertyMetadataInterface $propertyMetadata |
||
100 | * @param mixed $data |
||
101 | */ |
||
102 | private function loadPropertyMetadata(PropertyMetadataInterface $propertyMetadata, $data) |
||
140 | |||
141 | /** |
||
142 | * @param PropertyMetadataInterface $propertyMetadata |
||
143 | * @param string $alias |
||
144 | */ |
||
145 | View Code Duplication | private function loadPropertyMetadataAlias(PropertyMetadataInterface $propertyMetadata, $alias) |
|
162 | |||
163 | /** |
||
164 | * @param PropertyMetadataInterface $propertyMetadata |
||
165 | * @param string $type |
||
166 | */ |
||
167 | private function loadPropertyMetadataType(PropertyMetadataInterface $propertyMetadata, $type) |
||
178 | |||
179 | /** |
||
180 | * @param PropertyMetadataInterface $propertyMetadata |
||
181 | * @param string $version |
||
182 | */ |
||
183 | View Code Duplication | private function loadPropertyMetadataSinceVersion(PropertyMetadataInterface $propertyMetadata, $version) |
|
200 | |||
201 | /** |
||
202 | * @param PropertyMetadataInterface $propertyMetadata |
||
203 | * @param string $version |
||
204 | */ |
||
205 | View Code Duplication | private function loadPropertyMetadataUntilVersion(PropertyMetadataInterface $propertyMetadata, $version) |
|
222 | |||
223 | /** |
||
224 | * @param PropertyMetadataInterface $propertyMetadata |
||
225 | * @param string|int $maxDepth |
||
226 | */ |
||
227 | private function loadPropertyMetadataMaxDepth(PropertyMetadataInterface $propertyMetadata, $maxDepth) |
||
247 | |||
248 | /** |
||
249 | * @param PropertyMetadataInterface $propertyMetadata |
||
250 | * @param string[] $groups |
||
251 | */ |
||
252 | private function loadPropertyMetadataGroups(PropertyMetadataInterface $propertyMetadata, $groups) |
||
280 | |||
281 | /** |
||
282 | * @param mixed[] $data |
||
283 | * |
||
284 | * @return string|null |
||
285 | */ |
||
286 | private function getExclusionPolicy($data) |
||
316 | |||
317 | /** |
||
318 | * @param bool $exclude |
||
319 | */ |
||
320 | View Code Duplication | private function validatePropertyMetadataExclude($exclude) |
|
329 | |||
330 | /** |
||
331 | * @param bool $expose |
||
332 | */ |
||
333 | View Code Duplication | private function validatePropertyMetadataExpose($expose) |
|
342 | |||
343 | /** |
||
344 | * @param mixed[] $property |
||
345 | * @param string $policy |
||
346 | * |
||
347 | * @return bool |
||
348 | */ |
||
349 | private function isPropertyMetadataExposed($property, $policy) |
||
356 | } |
||
357 |
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.