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 |
||
26 | class File implements SessionHandlerInterface |
||
27 | { |
||
28 | protected $config = [ |
||
29 | 'path' => '', |
||
30 | 'expire' => 1440, |
||
31 | 'prefix' => '', |
||
32 | 'data_compress' => false, |
||
33 | 'gc_probability' => 1, |
||
34 | 'gc_divisor' => 100, |
||
35 | ]; |
||
36 | |||
37 | 3 | public function __construct(App $app, array $config = []) |
|
49 | |||
50 | /** |
||
51 | * 打开Session |
||
52 | * @access protected |
||
53 | * @throws Exception |
||
54 | */ |
||
55 | 3 | protected function init(): void |
|
68 | |||
69 | /** |
||
70 | * Session 垃圾回收 |
||
71 | * @access public |
||
72 | * @return void |
||
73 | */ |
||
74 | 3 | public function gc(): void |
|
87 | |||
88 | /** |
||
89 | * 查找文件 |
||
90 | * @param string $root |
||
91 | * @param Closure $filter |
||
92 | * @return Generator |
||
93 | */ |
||
94 | 3 | protected function findFiles(string $root, Closure $filter) |
|
109 | |||
110 | /** |
||
111 | * 取得变量的存储文件名 |
||
112 | * @access protected |
||
113 | * @param string $name 缓存变量名 |
||
114 | * @param bool $auto 是否自动创建目录 |
||
115 | * @return string |
||
116 | */ |
||
117 | 3 | protected function getFileName(string $name, bool $auto = false): string |
|
139 | |||
140 | /** |
||
141 | * 读取Session |
||
142 | * @access public |
||
143 | * @param string $sessID |
||
144 | * @return string |
||
145 | */ |
||
146 | 3 | public function read(string $sessID): string |
|
163 | |||
164 | /** |
||
165 | * 写文件(加锁) |
||
166 | * @param $path |
||
167 | * @param $content |
||
168 | * @return bool |
||
169 | */ |
||
170 | protected function writeFile($path, $content): bool |
||
174 | |||
175 | /** |
||
176 | * 读取文件内容(加锁) |
||
177 | * @param $path |
||
178 | * @return string |
||
179 | */ |
||
180 | 3 | protected function readFile($path): string |
|
202 | |||
203 | /** |
||
204 | * 写入Session |
||
205 | * @access public |
||
206 | * @param string $sessID |
||
207 | * @param string $sessData |
||
208 | * @return bool |
||
209 | */ |
||
210 | 3 | public function write(string $sessID, string $sessData): bool |
|
222 | |||
223 | /** |
||
224 | * 删除Session |
||
225 | * @access public |
||
226 | * @param string $sessID |
||
227 | * @return bool |
||
228 | */ |
||
229 | 3 | public function delete(string $sessID): bool |
|
237 | |||
238 | /** |
||
239 | * 判断文件是否存在后,删除 |
||
240 | * @access private |
||
241 | * @param string $file |
||
242 | * @return bool |
||
243 | */ |
||
244 | 3 | private function unlink(string $file): bool |
|
248 | |||
249 | } |
||
250 |
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.