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 DocumentationNormalizer 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 DocumentationNormalizer, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
32 | final class DocumentationNormalizer implements NormalizerInterface |
||
33 | { |
||
34 | const FORMAT = 'jsonld'; |
||
35 | |||
36 | private $resourceMetadataFactory; |
||
37 | private $propertyNameCollectionFactory; |
||
38 | private $propertyMetadataFactory; |
||
39 | private $resourceClassResolver; |
||
40 | private $operationMethodResolver; |
||
41 | private $urlGenerator; |
||
42 | |||
43 | public function __construct(ResourceMetadataFactoryInterface $resourceMetadataFactory, PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory, PropertyMetadataFactoryInterface $propertyMetadataFactory, ResourceClassResolverInterface $resourceClassResolver, OperationMethodResolverInterface $operationMethodResolver, UrlGeneratorInterface $urlGenerator) |
||
52 | |||
53 | /** |
||
54 | * {@inheritdoc} |
||
55 | */ |
||
56 | public function normalize($object, $format = null, array $context = []) |
||
57 | { |
||
58 | $classes = []; |
||
59 | $entrypointProperties = []; |
||
60 | |||
61 | foreach ($object->getResourceNameCollection() as $resourceClass) { |
||
62 | $resourceMetadata = $this->resourceMetadataFactory->create($resourceClass); |
||
63 | $shortName = $resourceMetadata->getShortName(); |
||
64 | $prefixedShortName = $resourceMetadata->getIri() ?? "#$shortName"; |
||
65 | |||
66 | $this->populateEntrypointProperties($resourceClass, $resourceMetadata, $shortName, $prefixedShortName, $entrypointProperties); |
||
67 | $classes[] = $this->getClass($resourceClass, $resourceMetadata, $shortName, $prefixedShortName); |
||
68 | } |
||
69 | |||
70 | return $this->computeDoc($object, $this->getClasses($entrypointProperties, $classes)); |
||
71 | } |
||
72 | |||
73 | /** |
||
74 | * Populates entrypoint properties. |
||
75 | * |
||
76 | * @param string $resourceClass |
||
77 | * @param ResourceMetadata $resourceMetadata |
||
78 | * @param string $shortName |
||
79 | * @param string $prefixedShortName |
||
80 | * @param array $entrypointProperties |
||
81 | */ |
||
82 | private function populateEntrypointProperties(string $resourceClass, ResourceMetadata $resourceMetadata, string $shortName, string $prefixedShortName, array &$entrypointProperties) |
||
104 | |||
105 | /** |
||
106 | * Gets a Hydra class. |
||
107 | * |
||
108 | * @param string $resourceClass |
||
109 | * @param ResourceMetadata $resourceMetadata |
||
110 | * @param string $shortName |
||
111 | * @param string $prefixedShortName |
||
112 | * |
||
113 | * @return array |
||
114 | */ |
||
115 | private function getClass(string $resourceClass, ResourceMetadata $resourceMetadata, string $shortName, string $prefixedShortName): array |
||
132 | |||
133 | /** |
||
134 | * Gets the context for the property name factory. |
||
135 | * |
||
136 | * @param ResourceMetadata $resourceMetadata |
||
137 | * |
||
138 | * @return array |
||
139 | */ |
||
140 | private function getPropertyNameCollectionFactoryContext(ResourceMetadata $resourceMetadata): array |
||
141 | { |
||
142 | $attributes = $resourceMetadata->getAttributes(); |
||
143 | $context = []; |
||
144 | |||
145 | View Code Duplication | if (isset($attributes['normalization_context']['groups'])) { |
|
|
|||
146 | $context['serializer_groups'] = $attributes['normalization_context']['groups']; |
||
147 | } |
||
148 | |||
149 | if (isset($attributes['denormalization_context']['groups'])) { |
||
150 | View Code Duplication | if (isset($context['serializer_groups'])) { |
|
151 | $context['serializer_groups'] += $attributes['denormalization_context']['groups']; |
||
152 | } else { |
||
153 | $context['serializer_groups'] = $attributes['denormalization_context']['groups']; |
||
154 | } |
||
155 | } |
||
156 | |||
157 | return $context; |
||
158 | } |
||
159 | |||
160 | /** |
||
161 | * Gets Hydra properties. |
||
162 | * |
||
163 | * @param string $resourceClass |
||
164 | * @param ResourceMetadata $resourceMetadata |
||
165 | * @param string $shortName |
||
166 | * @param string $prefixedShortName |
||
167 | * |
||
168 | * @return array |
||
169 | */ |
||
170 | private function getHydraProperties(string $resourceClass, ResourceMetadata $resourceMetadata, string $shortName, string $prefixedShortName): array |
||
184 | |||
185 | /** |
||
186 | * Gets Hydra operations. |
||
187 | * |
||
188 | * @param string $resourceClass |
||
189 | * @param ResourceMetadata $resourceMetadata |
||
190 | * @param string $prefixedShortName |
||
191 | * @param bool $collection |
||
192 | * |
||
193 | * @return array |
||
194 | */ |
||
195 | private function getHydraOperations(string $resourceClass, ResourceMetadata $resourceMetadata, string $prefixedShortName, bool $collection): array |
||
208 | |||
209 | /** |
||
210 | * Gets and populates if applicable a Hydra operation. |
||
211 | * |
||
212 | * @param string $resourceClass |
||
213 | * @param ResourceMetadata $resourceMetadata |
||
214 | * @param string $operationName |
||
215 | * @param array $operation |
||
216 | * @param string $prefixedShortName |
||
217 | * @param bool $collection |
||
218 | * |
||
219 | * @return array |
||
220 | */ |
||
221 | private function getHydraOperation(string $resourceClass, ResourceMetadata $resourceMetadata, string $operationName, array $operation, string $prefixedShortName, bool $collection): array |
||
274 | |||
275 | /** |
||
276 | * Gets the range of the property. |
||
277 | * |
||
278 | * @param PropertyMetadata $propertyMetadata |
||
279 | * |
||
280 | * @return string|null |
||
281 | */ |
||
282 | private function getRange(PropertyMetadata $propertyMetadata) |
||
328 | |||
329 | /** |
||
330 | * Builds the classes array. |
||
331 | * |
||
332 | * @param array $entrypointProperties |
||
333 | * @param array $classes |
||
334 | * |
||
335 | * @return array |
||
336 | */ |
||
337 | private function getClasses(array $entrypointProperties, array $classes): array |
||
415 | |||
416 | /** |
||
417 | * Gets a property definition. |
||
418 | * |
||
419 | * @param PropertyMetadata $propertyMetadata |
||
420 | * @param string $propertyName |
||
421 | * @param string $prefixedShortName |
||
422 | * @param string $shortName |
||
423 | * |
||
424 | * @return array |
||
425 | */ |
||
426 | private function getProperty(PropertyMetadata $propertyMetadata, string $propertyName, string $prefixedShortName, string $shortName): array |
||
452 | |||
453 | /** |
||
454 | * Computes the documentation. |
||
455 | * |
||
456 | * @param Documentation $object |
||
457 | * @param array $classes |
||
458 | * |
||
459 | * @return array |
||
460 | */ |
||
461 | private function computeDoc(Documentation $object, array $classes): array |
||
478 | |||
479 | /** |
||
480 | * Builds the JSON-LD context for the API documentation. |
||
481 | * |
||
482 | * @return array |
||
483 | */ |
||
484 | private function getContext(): array |
||
500 | |||
501 | /** |
||
502 | * {@inheritdoc} |
||
503 | */ |
||
504 | public function supportsNormalization($data, $format = null, array $context = []) |
||
508 | } |
||
509 |
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.