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 bundle cache warmer definition. |
||
19 | * |
||
20 | * @param string $warmerName |
||
21 | * @return Definition |
||
22 | */ |
||
23 | View Code Duplication | private function createBundleCacheWarmer($warmerName) |
|
33 | |||
34 | /** |
||
35 | * Creates the cache clear command definition. |
||
36 | * |
||
37 | * @param string $warmerName |
||
38 | * @return Definition |
||
39 | */ |
||
40 | View Code Duplication | private function createCacheClearCommand($warmerName) |
|
49 | |||
50 | /** |
||
51 | * Creates the cache warmer service definition. |
||
52 | * |
||
53 | * @return Definition |
||
54 | */ |
||
55 | private function createCacheWarmer() |
||
64 | |||
65 | /** |
||
66 | * Creates a file cache service definition. |
||
67 | * |
||
68 | * @param string $subClassName |
||
69 | * @param array $cacheConfig |
||
70 | * @param ContainerBuilder $container |
||
71 | * @return Definition |
||
72 | */ |
||
73 | private function createFileCache($subClassName, array $cacheConfig, ContainerBuilder $container) |
||
83 | |||
84 | /** |
||
85 | * Creates the redis cache service definition. |
||
86 | * |
||
87 | * @param array $cacheConfig |
||
88 | * @return Definition |
||
89 | */ |
||
90 | private function createRedisCache(array $cacheConfig) |
||
97 | |||
98 | /** |
||
99 | * Gets the file cache directory. |
||
100 | * |
||
101 | * @param array $cacheConfig |
||
102 | * @param ContainerBuilder $container |
||
103 | * @return string |
||
104 | */ |
||
105 | private function getFileCacheDir(array $cacheConfig, ContainerBuilder $container) |
||
113 | |||
114 | /** |
||
115 | * {@inheritdoc} |
||
116 | */ |
||
117 | public function load(array $config, ContainerBuilder $container) |
||
153 | |||
154 | /** |
||
155 | * Loads cache warming services. |
||
156 | * |
||
157 | * @param ContainerBuilder $container |
||
158 | * @return self |
||
159 | */ |
||
160 | private function loadCacheWarming(ContainerBuilder $container) |
||
177 | } |
||
178 |
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.