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 |
||
| 16 | class ServiceProviderCompilationPass implements CompilerPassInterface |
||
| 17 | { |
||
| 18 | private $registryId; |
||
| 19 | |||
| 20 | private $registryProvider; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @param int $registryId |
||
| 24 | * @param RegistryProviderInterface $registryProvider |
||
| 25 | */ |
||
| 26 | public function __construct($registryId, RegistryProviderInterface $registryProvider) |
||
| 27 | { |
||
| 28 | $this->registryId = $registryId; |
||
| 29 | $this->registryProvider = $registryProvider; |
||
| 30 | } |
||
| 31 | |||
| 32 | /** |
||
| 33 | * You can modify the container here before it is dumped to PHP code. |
||
| 34 | * |
||
| 35 | * @param ContainerBuilder $container |
||
| 36 | */ |
||
| 37 | public function process(ContainerBuilder $container) |
||
| 38 | { |
||
| 39 | // Now, let's store the registry in the container (an empty version of it... it will be dynamically added at runtime): |
||
| 40 | $this->registerRegistry($container); |
||
| 41 | |||
| 42 | $registry = $this->registryProvider->getRegistry($container); |
||
| 43 | |||
| 44 | // Note: in the 'boot' method of a bundle, the container is available. |
||
| 45 | // We use that to push the lazy array in the container. |
||
| 46 | // The lazy array can be used by the registry that is also part of the container. |
||
| 47 | // The registry can itself be used by a factory that creates services! |
||
| 48 | |||
| 49 | $this->registerAcclimatedContainer($container); |
||
| 50 | |||
| 51 | foreach ($registry as $serviceProviderKey => $serviceProvider) { |
||
| 52 | $this->registerFactories($serviceProviderKey, $serviceProvider, $container); |
||
| 53 | } |
||
| 54 | |||
| 55 | foreach ($registry as $serviceProviderKey => $serviceProvider) { |
||
| 56 | $this->registerExtensions($serviceProviderKey, $serviceProvider, $container); |
||
| 57 | } |
||
| 58 | } |
||
| 59 | |||
| 60 | |||
| 61 | private function registerRegistry(ContainerBuilder $container) |
||
| 68 | |||
| 69 | private function registerAcclimatedContainer(ContainerBuilder $container) { |
||
| 75 | |||
| 76 | View Code Duplication | private function registerFactories($serviceProviderKey, ServiceProviderInterface $serviceProvider, ContainerBuilder $container) { |
|
|
|
|||
| 77 | $serviceFactories = $serviceProvider->getFactories(); |
||
| 83 | |||
| 84 | View Code Duplication | private function registerExtensions($serviceProviderKey, ServiceProviderInterface $serviceProvider, ContainerBuilder $container) { |
|
| 91 | |||
| 92 | private function registerService($serviceName, $serviceProviderKey, $callable, ContainerBuilder $container) { |
||
| 97 | |||
| 98 | private function extendService($serviceName, $serviceProviderKey, $callable, ContainerBuilder $container) { |
||
| 124 | |||
| 125 | private function getDecoratedServiceName($serviceName, ContainerBuilder $container) { |
||
| 132 | |||
| 133 | private function getServiceDefinitionFromCallable($serviceName, $serviceProviderKey, callable $callable) |
||
| 153 | } |
||
| 154 |
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.