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 ApiDocumentationBuilder 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 ApiDocumentationBuilder, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 35 | final class ApiDocumentationBuilder implements ApiDocumentationBuilderInterface |
||
| 36 | { |
||
| 37 | private $resourceNameCollectionFactory; |
||
| 38 | private $resourceMetadataFactory; |
||
| 39 | private $propertyNameCollectionFactory; |
||
| 40 | private $propertyMetadataFactory; |
||
| 41 | private $contextBuilder; |
||
| 42 | private $resourceClassResolver; |
||
| 43 | private $operationMethodResolver; |
||
| 44 | private $urlGenerator; |
||
| 45 | private $title; |
||
| 46 | private $description; |
||
| 47 | private $iriConverter; |
||
| 48 | private $version; |
||
| 49 | private $host; |
||
| 50 | private $schema; |
||
| 51 | const SWAGGER_VERSION = '2.0'; |
||
| 52 | public function __construct(ResourceNameCollectionFactoryInterface $resourceNameCollectionFactory, ResourceMetadataFactoryInterface $resourceMetadataFactory, PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory, PropertyMetadataFactoryInterface $propertyMetadataFactory, ContextBuilderInterface $contextBuilder, ResourceClassResolverInterface $resourceClassResolver, OperationMethodResolverInterface $operationMethodResolver, UrlGeneratorInterface $urlGenerator, IriConverterInterface $iriConverter, string $title, string $description, string $version = null, string $host, string $schema) |
||
| 53 | { |
||
| 54 | $this->resourceNameCollectionFactory = $resourceNameCollectionFactory; |
||
| 55 | $this->resourceMetadataFactory = $resourceMetadataFactory; |
||
| 56 | $this->propertyNameCollectionFactory = $propertyNameCollectionFactory; |
||
| 57 | $this->propertyMetadataFactory = $propertyMetadataFactory; |
||
| 58 | $this->contextBuilder = $contextBuilder; |
||
| 59 | $this->resourceClassResolver = $resourceClassResolver; |
||
| 60 | $this->operationMethodResolver = $operationMethodResolver; |
||
| 61 | $this->urlGenerator = $urlGenerator; |
||
| 62 | $this->title = $title; |
||
| 63 | $this->description = $description; |
||
| 64 | $this->iriConverter = $iriConverter; |
||
| 65 | $this->version = $version; |
||
| 66 | $this->host = $host; |
||
| 67 | $this->schema[] = $schema; |
||
| 68 | } |
||
| 69 | |||
| 70 | /** |
||
| 71 | * {@inheritdoc} |
||
| 72 | */ |
||
| 73 | public function getApiDocumentation() |
||
| 74 | { |
||
| 75 | $classes = []; |
||
| 76 | $itemOperations = []; |
||
| 77 | $itemOperations['operation'] = []; |
||
| 78 | |||
| 79 | $itemOperationsDocs = []; |
||
| 80 | $properties = []; |
||
| 81 | |||
| 82 | foreach ($this->resourceNameCollectionFactory->create() as $resourceClass) { |
||
| 83 | $resourceMetadata = $this->resourceMetadataFactory->create($resourceClass); |
||
| 84 | |||
| 85 | $shortName = $resourceMetadata->getShortName(); |
||
| 86 | $prefixedShortName = ($iri = $resourceMetadata->getIri()) ? $iri : '#'.$shortName; |
||
| 87 | |||
| 88 | $class = [ |
||
| 89 | 'name' => $shortName, |
||
| 90 | 'externalDocs' => [ |
||
| 91 | 'url' => $prefixedShortName, |
||
| 92 | ], |
||
| 93 | ]; |
||
| 94 | |||
| 95 | if ($description = $resourceMetadata->getDescription()) { |
||
| 96 | $class = [ |
||
| 97 | 'name' => $shortName, |
||
| 98 | 'description' => $description, |
||
| 99 | 'externalDocs' => [ |
||
| 100 | 'url' => $prefixedShortName, |
||
| 101 | ], |
||
| 102 | ]; |
||
| 103 | } |
||
| 104 | |||
| 105 | $attributes = $resourceMetadata->getAttributes(); |
||
| 106 | $context = []; |
||
| 107 | |||
| 108 | if (isset($attributes['normalization_context']['groups'])) { |
||
| 109 | $context['serializer_groups'] = $attributes['normalization_context']['groups']; |
||
| 110 | } |
||
| 111 | |||
| 112 | View Code Duplication | if (isset($attributes['denormalization_context']['groups'])) { |
|
|
|
|||
| 113 | $context['serializer_groups'] = isset($context['serializer_groups']) ? array_merge($context['serializer_groups'], $attributes['denormalization_context']['groups']) : $context['serializer_groups']; |
||
| 114 | } |
||
| 115 | |||
| 116 | foreach ($this->propertyNameCollectionFactory->create($resourceClass, $context) as $propertyName) { |
||
| 117 | $propertyMetadata = $this->propertyMetadataFactory->create($resourceClass, $propertyName); |
||
| 118 | |||
| 119 | if ($propertyMetadata->isIdentifier() && !$propertyMetadata->isWritable()) { |
||
| 120 | continue; |
||
| 121 | } |
||
| 122 | $range = $this->getRange($propertyMetadata); |
||
| 123 | |||
| 124 | $property[$propertyName] = [ |
||
| 125 | 'type' => $range, |
||
| 126 | ]; |
||
| 127 | |||
| 128 | if (is_array($range)) { |
||
| 129 | $property[$propertyName] = $range; |
||
| 130 | } |
||
| 131 | |||
| 132 | $required = []; |
||
| 133 | |||
| 134 | if ($propertyMetadata->isRequired()) { |
||
| 135 | $required = array_merge($required, [$propertyName]); |
||
| 136 | } |
||
| 137 | |||
| 138 | if (!empty($required)) { |
||
| 139 | $properties[$shortName]['required'] = $required; |
||
| 140 | } |
||
| 141 | |||
| 142 | $properties[$shortName]['type'] = 'object'; |
||
| 143 | $properties[$shortName]['properties'] = $property; |
||
| 144 | } |
||
| 145 | |||
| 146 | if ($operations = $resourceMetadata->getItemOperations()) { |
||
| 147 | foreach ($operations as $operationName => $itemOperation) { |
||
| 148 | $swaggerOperation = $this->getSwaggerOperation($resourceClass, $resourceMetadata, $operationName, $itemOperation, $prefixedShortName, false); |
||
| 149 | $itemOperations['operation'] = array_merge($itemOperations['operation'], $swaggerOperation); |
||
| 150 | } |
||
| 151 | } |
||
| 152 | |||
| 153 | try { |
||
| 154 | $resourceClassIri = $this->iriConverter->getIriFromResourceClass($resourceClass); |
||
| 155 | } catch (InvalidArgumentException $e) { |
||
| 156 | $resourceClassIri = '/nopaths'; |
||
| 157 | } |
||
| 158 | $resourceClassIri .= '/{id}'; |
||
| 159 | |||
| 160 | $itemOperationsDocs[$resourceClassIri] = $itemOperations['operation']; |
||
| 161 | $classes[] = $class; |
||
| 162 | } |
||
| 163 | |||
| 164 | $doc['swagger'] = self::SWAGGER_VERSION; |
||
| 165 | if ('' !== $this->title) { |
||
| 166 | $doc['info']['title'] = $this->title; |
||
| 167 | } |
||
| 168 | |||
| 169 | if ('' !== $this->description) { |
||
| 170 | $doc['info']['description'] = $this->description; |
||
| 171 | } |
||
| 172 | $doc['info']['version'] = $this->version ?? '0.0.0'; |
||
| 173 | $doc['host'] = $this->host; |
||
| 174 | $doc['basePath'] = $this->urlGenerator->generate('api_jsonld_entrypoint'); |
||
| 175 | $doc['definitions'] = $properties; |
||
| 176 | $doc['externalDocs'] = ['description' => 'Find more about API Platform', 'url' => 'https://api-platform.com']; $doc['tags'] = $classes; |
||
| 177 | $doc['schemes'] = $this->schema; // more schema ? |
||
| 178 | $doc['paths'] = $itemOperationsDocs; |
||
| 179 | |||
| 180 | return $doc; |
||
| 181 | } |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Gets and populates if applicable a Swagger operation. |
||
| 185 | */ |
||
| 186 | private function getSwaggerOperation(string $resourceClass, ResourceMetadata $resourceMetadata, string $operationName, array $operation, string $prefixedShortName, bool $collection) : array |
||
| 291 | |||
| 292 | /** |
||
| 293 | * Gets the range of the property. |
||
| 294 | * |
||
| 295 | * @param PropertyMetadata $propertyMetadata |
||
| 296 | * |
||
| 297 | * @return string|null |
||
| 298 | */ |
||
| 299 | View Code Duplication | private function getRange(PropertyMetadata $propertyMetadata) |
|
| 343 | } |
||
| 344 |
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.