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 |
||
19 | class RestorablePhpFileCache extends PhpFileCache |
||
20 | { |
||
21 | const TAG_RESTORABLE_CACHE = 'config_cache.restorable'; |
||
22 | |||
23 | protected $builder; |
||
24 | protected $restoringDirectory; |
||
25 | |||
26 | /** |
||
27 | * Saves the cache to the temporary directory. |
||
28 | * |
||
29 | * @param string $id |
||
30 | */ |
||
31 | 7 | View Code Duplication | public function saveToTemp($id) |
40 | |||
41 | /** |
||
42 | * Restores the cache to the cache directory. |
||
43 | * |
||
44 | * @param string $id |
||
45 | */ |
||
46 | 4 | View Code Duplication | public function restore($id) |
57 | |||
58 | /** |
||
59 | * Sets a SaveAreaBuilder. |
||
60 | * |
||
61 | * @param SaveAreaBuilder $builder |
||
62 | * |
||
63 | * @return RestorablePhpFileCache |
||
64 | */ |
||
65 | 13 | public function setBuilder(SaveAreaBuilder $builder) |
|
71 | |||
72 | /** |
||
73 | * Sets a directory. |
||
74 | * |
||
75 | * @param string $directory |
||
76 | * |
||
77 | * @return RestorablePhpFileCache |
||
78 | */ |
||
79 | 11 | protected function setDirectory($directory) |
|
85 | |||
86 | /** |
||
87 | * Sets an original restoring directory. |
||
88 | * |
||
89 | * @param string $directory |
||
90 | * |
||
91 | * @return RestorablePhpFileCache |
||
92 | */ |
||
93 | 10 | protected function setRestoringDirectory($directory) |
|
99 | |||
100 | /** |
||
101 | * Prepares temporary directory to re-create cache. |
||
102 | * |
||
103 | * @return RestorablePhpFileCache |
||
104 | */ |
||
105 | 8 | protected function prepareTemporaryDirectory() |
|
115 | |||
116 | /** |
||
117 | * Restores an original directory. |
||
118 | * |
||
119 | * @return RestorablePhpFileCache |
||
120 | */ |
||
121 | 8 | protected function restoreDirectory() |
|
125 | } |
||
126 |
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.