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 |
||
| 27 | final class SymfonyExtension implements Extension |
||
| 28 | { |
||
| 29 | /** |
||
| 30 | * Kernel used inside Behat contexts or to create services injected to them. |
||
| 31 | * Container is built before every scenario. |
||
| 32 | */ |
||
| 33 | const KERNEL_ID = 'sylius_symfony_extension.kernel'; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * The current container used in scenario contexts. |
||
| 37 | * To be used as a factory for current injected application services. |
||
| 38 | */ |
||
| 39 | const KERNEL_CONTAINER_ID = 'sylius_symfony_extension.kernel.container'; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Kernel used by Symfony2 driver to isolate web container from contexts' container. |
||
| 43 | * Container is built before every request. |
||
| 44 | */ |
||
| 45 | const DRIVER_KERNEL_ID = 'sylius_symfony_extension.driver_kernel'; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Kernel that should be used by extensions only. |
||
| 49 | * Container is built only once at the first use. |
||
| 50 | */ |
||
| 51 | const SHARED_KERNEL_ID = 'sylius_symfony_extension.shared_kernel'; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * The only container built by shared kernel. |
||
| 55 | * To be used as a factory for shared injected application services. |
||
| 56 | */ |
||
| 57 | const SHARED_KERNEL_CONTAINER_ID = 'sylius_symfony_extension.shared_kernel.container'; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * {@inheritdoc} |
||
| 61 | */ |
||
| 62 | public function getConfigKey() |
||
| 66 | |||
| 67 | /** |
||
| 68 | * {@inheritdoc} |
||
| 69 | */ |
||
| 70 | public function initialize(ExtensionManager $extensionManager) |
||
| 74 | |||
| 75 | /** |
||
| 76 | * {@inheritdoc} |
||
| 77 | */ |
||
| 78 | public function configure(ArrayNodeDefinition $builder) |
||
| 93 | |||
| 94 | /** |
||
| 95 | * {@inheritdoc} |
||
| 96 | */ |
||
| 97 | public function load(ContainerBuilder $container, array $config) |
||
| 109 | |||
| 110 | /** |
||
| 111 | * {@inheritdoc} |
||
| 112 | */ |
||
| 113 | public function process(ContainerBuilder $container) |
||
| 116 | |||
| 117 | /** |
||
| 118 | * @param ContainerBuilder $container |
||
| 119 | */ |
||
| 120 | private function loadKernel(ContainerBuilder $container, array $config) |
||
| 133 | |||
| 134 | /** |
||
| 135 | * @param ContainerBuilder $container |
||
| 136 | */ |
||
| 137 | View Code Duplication | private function loadKernelContainer(ContainerBuilder $container) |
|
| 147 | |||
| 148 | /** |
||
| 149 | * @param ContainerBuilder $container |
||
| 150 | */ |
||
| 151 | private function loadDriverKernel(ContainerBuilder $container) |
||
| 155 | |||
| 156 | /** |
||
| 157 | * @param ContainerBuilder $container |
||
| 158 | */ |
||
| 159 | private function loadSharedKernel(ContainerBuilder $container) |
||
| 163 | |||
| 164 | /** |
||
| 165 | * @param ContainerBuilder $container |
||
| 166 | */ |
||
| 167 | View Code Duplication | private function loadSharedKernelContainer(ContainerBuilder $container) |
|
| 177 | |||
| 178 | /** |
||
| 179 | * @param ContainerBuilder $container |
||
| 180 | */ |
||
| 181 | View Code Duplication | private function loadKernelRebooter(ContainerBuilder $container) |
|
| 188 | |||
| 189 | /** |
||
| 190 | * @param ExtensionManager $extensionManager |
||
| 191 | */ |
||
| 192 | private function registerSymfonyDriverFactory(ExtensionManager $extensionManager) |
||
| 205 | |||
| 206 | /** |
||
| 207 | * @param string $basePath |
||
| 208 | * @param string $kernelPath |
||
| 209 | * |
||
| 210 | * @return string|null |
||
| 211 | */ |
||
| 212 | private function getKernelFile($basePath, $kernelPath) |
||
| 227 | |||
| 228 | /** |
||
| 229 | * @param string $basePath |
||
| 230 | * @param string|null $bootstrapPath |
||
| 231 | * |
||
| 232 | * @throws \DomainException |
||
| 233 | */ |
||
| 234 | private function requireKernelBootstrapFile($basePath, $bootstrapPath) |
||
| 255 | } |
||
| 256 |
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.