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 | |||
41 | private $fileLoader; |
||
42 | private $resourceNameCollectionFactory; |
||
43 | private $resourceMetadataFactory; |
||
44 | private $operationPathResolver; |
||
45 | private $container; |
||
46 | private $formats; |
||
47 | private $resourceClassDirectories; |
||
48 | |||
49 | public function __construct(KernelInterface $kernel, ResourceNameCollectionFactoryInterface $resourceNameCollectionFactory, ResourceMetadataFactoryInterface $resourceMetadataFactory, OperationPathResolverInterface $operationPathResolver, ContainerInterface $container, array $formats, array $resourceClassDirectories = []) |
||
59 | |||
60 | /** |
||
61 | * {@inheritdoc} |
||
62 | */ |
||
63 | public function load($data, $type = null): RouteCollection |
||
64 | { |
||
65 | $routeCollection = new RouteCollection(); |
||
66 | foreach ($this->resourceClassDirectories as $directory) { |
||
67 | $routeCollection->addResource(new DirectoryResource($directory, '/\.php$/')); |
||
68 | } |
||
69 | |||
70 | $this->loadExternalFiles($routeCollection); |
||
71 | |||
72 | foreach ($this->resourceNameCollectionFactory->create() as $resourceClass) { |
||
73 | $resourceMetadata = $this->resourceMetadataFactory->create($resourceClass); |
||
74 | $resourceShortName = $resourceMetadata->getShortName(); |
||
75 | |||
76 | if (null === $resourceShortName) { |
||
77 | throw new InvalidResourceException(sprintf('Resource %s has no short name defined.', $resourceClass)); |
||
78 | } |
||
79 | |||
80 | View Code Duplication | if (null !== $collectionOperations = $resourceMetadata->getCollectionOperations()) { |
|
|
|||
81 | foreach ($collectionOperations as $operationName => $operation) { |
||
82 | $this->addRoute($routeCollection, $resourceClass, $operationName, $operation, $resourceShortName, true); |
||
83 | } |
||
84 | } |
||
85 | |||
86 | if (null !== $itemOperations = $resourceMetadata->getItemOperations()) { |
||
87 | foreach ($itemOperations as $operationName => $operation) { |
||
88 | $this->addRoute($routeCollection, $resourceClass, $operationName, $operation, $resourceShortName, false); |
||
89 | } |
||
90 | } |
||
91 | } |
||
92 | |||
93 | return $routeCollection; |
||
94 | } |
||
95 | |||
96 | /** |
||
97 | * {@inheritdoc} |
||
98 | */ |
||
99 | public function supports($resource, $type = null) |
||
103 | |||
104 | /** |
||
105 | * Load external files. |
||
106 | * |
||
107 | * @param RouteCollection $routeCollection |
||
108 | */ |
||
109 | private function loadExternalFiles(RouteCollection $routeCollection) |
||
117 | |||
118 | /** |
||
119 | * Creates and adds a route for the given operation to the route collection. |
||
120 | * |
||
121 | * @param RouteCollection $routeCollection |
||
122 | * @param string $resourceClass |
||
123 | * @param string $operationName |
||
124 | * @param array $operation |
||
125 | * @param string $resourceShortName |
||
126 | * @param bool $collection |
||
127 | * |
||
128 | * @throws RuntimeException |
||
129 | */ |
||
130 | private function addRoute(RouteCollection $routeCollection, string $resourceClass, string $operationName, array $operation, string $resourceShortName, bool $collection) |
||
178 | } |
||
179 |
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.