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 |
||
13 | class RedisDriver extends BaseDriver |
||
14 | { |
||
15 | private $redis; |
||
16 | private $pre; |
||
17 | |||
18 | View Code Duplication | public function __construct($configs = [], $dir = 'Cache:OpenOauth:') |
|
45 | |||
46 | /** |
||
47 | * 根据缓存名获取缓存内容. |
||
48 | * |
||
49 | * @param string $name |
||
50 | * |
||
51 | * @return bool|mixed|string |
||
52 | */ |
||
53 | View Code Duplication | public function _get($name) |
|
63 | |||
64 | /** |
||
65 | * 根据缓存名 设置缓存值和超时时间. |
||
66 | * |
||
67 | * @param string $name 缓存名 |
||
68 | * @param void $value 缓存值 |
||
69 | * @param int $expires 超时时间 |
||
70 | * |
||
71 | * @return boolean; |
||
72 | */ |
||
73 | public function _set($name, $value, $expires) |
||
84 | |||
85 | /** |
||
86 | * 数据打包. |
||
87 | * |
||
88 | * @param void $data 缓存值 |
||
89 | * @param int $expires 超时时间 |
||
90 | * |
||
91 | * @return string |
||
92 | */ |
||
93 | private function packData($data) |
||
97 | |||
98 | /** |
||
99 | * 数据解包. |
||
100 | * |
||
101 | * @param $data |
||
102 | * |
||
103 | * @return mixed |
||
104 | */ |
||
105 | private function unpackData($data) |
||
109 | |||
110 | /** |
||
111 | * 创建缓存文件名. |
||
112 | * |
||
113 | * @param string $name 缓存名 |
||
114 | * |
||
115 | * @return string |
||
116 | */ |
||
117 | private function createFileName($name) |
||
121 | } |
||
122 |
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.