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:
| 1 | <?php |
||
| 36 | final class ApiLoader extends Loader |
||
| 37 | { |
||
| 38 | const ROUTE_NAME_PREFIX = 'api_'; |
||
| 39 | const DEFAULT_ACTION_PATTERN = 'api_platform.action.'; |
||
| 40 | const SUBCOLLECTION_SUFFIX = '_get_subcollection'; |
||
| 41 | |||
| 42 | private $fileLoader; |
||
| 43 | private $propertyNameCollectionFactory; |
||
| 44 | private $propertyMetadataFactory; |
||
| 45 | private $resourceNameCollectionFactory; |
||
| 46 | private $resourceMetadataFactory; |
||
| 47 | private $operationPathResolver; |
||
| 48 | private $container; |
||
| 49 | private $formats; |
||
| 50 | |||
| 51 | public function __construct(KernelInterface $kernel, ResourceNameCollectionFactoryInterface $resourceNameCollectionFactory, ResourceMetadataFactoryInterface $resourceMetadataFactory, PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory, PropertyMetadataFactoryInterface $propertyMetadataFactory, OperationPathResolverInterface $operationPathResolver, ContainerInterface $container, array $formats) |
||
| 62 | |||
| 63 | /** |
||
| 64 | * {@inheritdoc} |
||
| 65 | */ |
||
| 66 | public function load($data, $type = null) |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Handles subcollection operations recursively and declare their corresponding routes. |
||
| 100 | * |
||
| 101 | * @param RouteCollection $routeCollection |
||
| 102 | * @param string $resourceClass |
||
| 103 | * @param string $rootResourceClass null on the first iteration, it then keeps track of the origin resource class |
||
| 104 | * @param string $rootResourceClass null on the first iteration, it then keeps track of the parent operation |
||
| 105 | */ |
||
| 106 | private function computeSubcollectionOperations(RouteCollection $routeCollection, string $resourceClass, $rootResourceClass = null, $parentOperation = null) |
||
| 107 | { |
||
| 108 | if (null === $rootResourceClass) { |
||
| 109 | $rootResourceClass = $resourceClass; |
||
| 110 | } |
||
| 111 | |||
| 112 | foreach ($this->propertyNameCollectionFactory->create($resourceClass) as $property) { |
||
| 113 | $propertyMetadata = $this->propertyMetadataFactory->create($resourceClass, $property); |
||
| 114 | |||
| 115 | if (!$propertyMetadata->hasSubcollection()) { |
||
| 116 | continue; |
||
| 117 | } |
||
| 118 | |||
| 119 | $subcollection = $propertyMetadata->getType()->isCollection() ? $propertyMetadata->getType()->getCollectionValueType()->getClassName() : $propertyMetadata->getType()->getClassName(); |
||
| 120 | |||
| 121 | $propertyName = Inflector::pluralize(Inflector::tableize($property)); |
||
| 122 | |||
| 123 | $operation = [ |
||
| 124 | 'property' => $property, |
||
| 125 | ]; |
||
| 126 | |||
| 127 | if (null === $parentOperation) { |
||
| 128 | $rootResourceMetadata = $this->resourceMetadataFactory->create($rootResourceClass); |
||
| 129 | $rootShortname = $rootResourceMetadata->getShortName(); |
||
| 130 | $resourceRouteName = Inflector::pluralize(Inflector::tableize($rootShortname)); |
||
| 131 | |||
| 132 | $operation['identifiers'] = [['id', $rootResourceClass]]; |
||
| 133 | $operation['route_name'] = sprintf('%s%s_%s%s', self::ROUTE_NAME_PREFIX, $resourceRouteName, $propertyName, self::SUBCOLLECTION_SUFFIX); |
||
| 134 | |||
| 135 | $operation['path'] = $this->operationPathResolver->resolveOperationPath($rootShortname, $operation, OperationTypes::SUBCOLLECTION); |
||
| 136 | } else { |
||
| 137 | $identifier = $parentOperation['property']; |
||
| 138 | |||
| 139 | $operation['identifiers'] = $parentOperation['identifiers']; |
||
| 140 | $operation['identifiers'][] = [$identifier, $resourceClass]; |
||
| 141 | $operation['route_name'] = str_replace(self::SUBCOLLECTION_SUFFIX, "_$propertyName".self::SUBCOLLECTION_SUFFIX, $parentOperation['route_name']); |
||
| 142 | |||
| 143 | $operation['path'] = $this->operationPathResolver->resolveOperationPath($parentOperation['path'], $operation, OperationTypes::SUBCOLLECTION); |
||
| 144 | } |
||
| 145 | |||
| 146 | $route = new Route( |
||
| 147 | $operation['path'], |
||
| 148 | [ |
||
| 149 | '_controller' => self::DEFAULT_ACTION_PATTERN.'get_subcollection', |
||
| 150 | '_format' => null, |
||
| 151 | '_api_resource_class' => $subcollection, |
||
| 152 | '_api_subcollection_operation_name' => 'get', |
||
| 153 | '_api_subcollection_context' => [ |
||
| 154 | 'property' => $operation['property'], |
||
| 155 | 'identifiers' => $operation['identifiers'], |
||
| 156 | ], |
||
| 157 | ], |
||
| 158 | [], |
||
| 159 | [], |
||
| 160 | '', |
||
| 161 | [], |
||
| 162 | ['GET'] |
||
| 163 | ); |
||
| 164 | |||
| 165 | $routeCollection->add($operation['route_name'], $route); |
||
| 166 | |||
| 167 | $this->computeSubcollectionOperations($routeCollection, $subcollection, $rootResourceClass, $operation); |
||
| 168 | } |
||
| 169 | } |
||
| 170 | |||
| 171 | /** |
||
| 172 | * {@inheritdoc} |
||
| 173 | */ |
||
| 174 | public function supports($resource, $type = null) |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Load external files. |
||
| 181 | * |
||
| 182 | * @param RouteCollection $routeCollection |
||
| 183 | */ |
||
| 184 | private function loadExternalFiles(RouteCollection $routeCollection) |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Creates and adds a route for the given operation to the route collection. |
||
| 195 | * |
||
| 196 | * @param RouteCollection $routeCollection |
||
| 197 | * @param string $resourceClass |
||
| 198 | * @param string $operationName |
||
| 199 | * @param array $operation |
||
| 200 | * @param string $resourceShortName |
||
| 201 | * @param bool $collection |
||
| 202 | * |
||
| 203 | * @throws RuntimeException |
||
| 204 | */ |
||
| 205 | private function addRoute(RouteCollection $routeCollection, string $resourceClass, string $operationName, array $operation, string $resourceShortName, string $operationType) |
||
| 252 | } |
||
| 253 |
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.