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 Kernel extends BaseKernel |
||
17 | { |
||
18 | use MicroKernelTrait; |
||
19 | |||
20 | /** |
||
21 | * @const string |
||
22 | */ |
||
23 | const CONFIG_EXTS = '.{php,xml,yaml,yml}'; |
||
24 | |||
25 | /** |
||
26 | * {@inheritdoc} |
||
27 | */ |
||
28 | public function getCacheDir() |
||
32 | |||
33 | /** |
||
34 | * {@inheritdoc} |
||
35 | */ |
||
36 | public function getLogDir() |
||
40 | |||
41 | /** |
||
42 | * {@inheritdoc} |
||
43 | */ |
||
44 | public function registerBundles() |
||
54 | |||
55 | /** |
||
56 | * {@inheritdoc} |
||
57 | */ |
||
58 | protected function configureContainer(ContainerBuilder $container, LoaderInterface $loader) |
||
59 | { |
||
60 | $container->setParameter('container.autowiring.strict_mode', true); |
||
61 | $container->setParameter('container.dumper.inline_class_loader', true); |
||
62 | $confDir = $this->getProjectDir().'/config'; |
||
63 | $loader->load($confDir.'/packages/*'.self::CONFIG_EXTS, 'glob'); |
||
64 | |||
65 | View Code Duplication | if (is_dir($confDir.'/packages/'.$this->environment)) { |
|
|
|||
66 | $loader->load($confDir.'/packages/'.$this->environment.'/**/*'.self::CONFIG_EXTS, 'glob'); |
||
67 | } |
||
68 | |||
69 | $loader->load($confDir.'/services'.self::CONFIG_EXTS, 'glob'); |
||
70 | $loader->load($confDir.'/services_'.$this->environment.self::CONFIG_EXTS, 'glob'); |
||
71 | } |
||
72 | |||
73 | /** |
||
74 | * {@inheritdoc} |
||
75 | */ |
||
76 | protected function configureRoutes(RouteCollectionBuilder $routes) |
||
77 | { |
||
78 | $confDir = $this->getProjectDir().'/config'; |
||
79 | |||
80 | if (is_dir($confDir.'/routes/')) { |
||
81 | $routes->import($confDir.'/routes/*'.self::CONFIG_EXTS, '/', 'glob'); |
||
82 | } |
||
83 | |||
84 | View Code Duplication | if (is_dir($confDir.'/routes/'.$this->environment)) { |
|
85 | $routes->import($confDir.'/routes/'.$this->environment.'/**/*'.self::CONFIG_EXTS, '/', 'glob'); |
||
86 | } |
||
87 | |||
88 | $routes->import($confDir.'/routes'.self::CONFIG_EXTS, '/', 'glob'); |
||
89 | } |
||
90 | } |
||
91 |
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.