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 |
||
15 | class MetadataCache implements ServiceLoaderInterface |
||
16 | { |
||
17 | /** |
||
18 | * Creates the binary file cache service definition. |
||
19 | * |
||
20 | * @param array $cacheConfig |
||
21 | * @param ContainerBuilder $container |
||
22 | * @return self |
||
23 | */ |
||
24 | View Code Duplication | private function createBinaryFileCache(array $cacheConfig, ContainerBuilder $container) |
|
34 | |||
35 | /** |
||
36 | * Creates the file cache service definition. |
||
37 | * |
||
38 | * @param array $cacheConfig |
||
39 | * @param ContainerBuilder $container |
||
40 | * @return self |
||
41 | */ |
||
42 | View Code Duplication | private function createFileCache(array $cacheConfig, ContainerBuilder $container) |
|
52 | |||
53 | /** |
||
54 | * Creates the redis cache service definition. |
||
55 | * |
||
56 | * @param array $cacheConfig |
||
57 | * @param ContainerBuilder $container |
||
58 | * @return self |
||
59 | */ |
||
60 | private function createRedisCache(array $cacheConfig, ContainerBuilder $container) |
||
67 | |||
68 | /** |
||
69 | * Gets the file cache directory. |
||
70 | * |
||
71 | * @param array $cacheConfig |
||
72 | * @param ContainerBuilder $container |
||
73 | * @return string |
||
74 | */ |
||
75 | private function getFileCacheDir(array $cacheConfig, ContainerBuilder $container) |
||
83 | |||
84 | /** |
||
85 | * {@inheritdoc} |
||
86 | */ |
||
87 | public function load(array $config, ContainerBuilder $container) |
||
121 | |||
122 | /** |
||
123 | * Loads cache warming services. |
||
124 | * |
||
125 | * @param ContainerBuilder $container |
||
126 | * @return self |
||
127 | */ |
||
128 | private function loadCacheWarming(ContainerBuilder $container) |
||
147 | } |
||
148 |
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.