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 FileStorage implements StorageInterface |
||
17 | { |
||
18 | /** |
||
19 | * @var string |
||
20 | */ |
||
21 | protected $dir; |
||
22 | |||
23 | /** |
||
24 | * @var int |
||
25 | */ |
||
26 | protected $gcProbability; |
||
27 | |||
28 | /** |
||
29 | * @const string |
||
30 | */ |
||
31 | const FILE_SUFFIX = '.phpsst'; |
||
32 | |||
33 | /** |
||
34 | * FileStorage constructor. |
||
35 | * @param string $dir |
||
36 | * @param int $gcProbability |
||
37 | */ |
||
38 | 9 | public function __construct($dir, $gcProbability) |
|
52 | |||
53 | /** |
||
54 | * @param Password $password |
||
55 | */ |
||
56 | 8 | View Code Duplication | public function insert(Password $password) |
64 | |||
65 | /** |
||
66 | * @param Password $password |
||
67 | */ |
||
68 | 2 | View Code Duplication | public function update(Password $password) |
76 | |||
77 | /** |
||
78 | * @param $key |
||
79 | * @return Password|null |
||
80 | */ |
||
81 | 3 | public function get($key) |
|
104 | |||
105 | /** |
||
106 | * @param Password $password |
||
107 | */ |
||
108 | 3 | public function delete(Password $password) |
|
112 | |||
113 | /** |
||
114 | */ |
||
115 | 6 | protected function garbageCollection() |
|
130 | |||
131 | /** |
||
132 | * @param Password $password |
||
133 | */ |
||
134 | 7 | protected function writeFile(Password $password) |
|
151 | |||
152 | /** |
||
153 | * @param Password $password |
||
154 | * @return string |
||
155 | */ |
||
156 | 4 | protected function getFileName(Password $password) |
|
160 | |||
161 | /** |
||
162 | * @param string $key |
||
163 | * @return string |
||
164 | */ |
||
165 | 4 | protected function getFileNameFromKey($key) |
|
169 | } |
||
170 |
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.