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 |
||
| 20 | class Memcache extends Driver |
||
| 21 | { |
||
| 22 | /** |
||
| 23 | * 配置参数 |
||
| 24 | * @var array |
||
| 25 | */ |
||
| 26 | protected $options = [ |
||
| 27 | 'host' => '127.0.0.1', |
||
| 28 | 'port' => 11211, |
||
| 29 | 'expire' => 0, |
||
| 30 | 'timeout' => 0, // 超时时间(单位:毫秒) |
||
| 31 | 'persistent' => true, |
||
| 32 | 'prefix' => '', |
||
| 33 | 'tag_prefix' => 'tag:', |
||
| 34 | 'serialize' => [], |
||
| 35 | ]; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * 架构函数 |
||
| 39 | * @access public |
||
| 40 | * @param array $options 缓存参数 |
||
| 41 | * @throws \BadFunctionCallException |
||
| 42 | */ |
||
| 43 | public function __construct(array $options = []) |
||
| 71 | |||
| 72 | /** |
||
| 73 | * 判断缓存 |
||
| 74 | * @access public |
||
| 75 | * @param string $name 缓存变量名 |
||
| 76 | * @return bool |
||
| 77 | */ |
||
| 78 | public function has($name): bool |
||
| 84 | |||
| 85 | /** |
||
| 86 | * 读取缓存 |
||
| 87 | * @access public |
||
| 88 | * @param string $name 缓存变量名 |
||
| 89 | * @param mixed $default 默认值 |
||
| 90 | * @return mixed |
||
| 91 | */ |
||
| 92 | View Code Duplication | public function get($name, $default = null) |
|
| 100 | |||
| 101 | /** |
||
| 102 | * 写入缓存 |
||
| 103 | * @access public |
||
| 104 | * @param string $name 缓存变量名 |
||
| 105 | * @param mixed $value 存储数据 |
||
| 106 | * @param int|\DateTime $expire 有效时间(秒) |
||
| 107 | * @return bool |
||
| 108 | */ |
||
| 109 | View Code Duplication | public function set($name, $value, $expire = null): bool |
|
| 127 | |||
| 128 | /** |
||
| 129 | * 自增缓存(针对数值缓存) |
||
| 130 | * @access public |
||
| 131 | * @param string $name 缓存变量名 |
||
| 132 | * @param int $step 步长 |
||
| 133 | * @return false|int |
||
| 134 | */ |
||
| 135 | View Code Duplication | public function inc(string $name, int $step = 1) |
|
| 147 | |||
| 148 | /** |
||
| 149 | * 自减缓存(针对数值缓存) |
||
| 150 | * @access public |
||
| 151 | * @param string $name 缓存变量名 |
||
| 152 | * @param int $step 步长 |
||
| 153 | * @return false|int |
||
| 154 | */ |
||
| 155 | View Code Duplication | public function dec(string $name, int $step = 1) |
|
| 165 | |||
| 166 | /** |
||
| 167 | * 删除缓存 |
||
| 168 | * @access public |
||
| 169 | * @param string $name 缓存变量名 |
||
| 170 | * @param bool|false $ttl |
||
| 171 | * @return bool |
||
| 172 | */ |
||
| 173 | View Code Duplication | public function delete($name, $ttl = false): bool |
|
| 183 | |||
| 184 | /** |
||
| 185 | * 清除缓存 |
||
| 186 | * @access public |
||
| 187 | * @return bool |
||
| 188 | */ |
||
| 189 | public function clear(): bool |
||
| 195 | |||
| 196 | /** |
||
| 197 | * 删除缓存标签 |
||
| 198 | * @access public |
||
| 199 | * @param array $keys 缓存标识列表 |
||
| 200 | * @return void |
||
| 201 | */ |
||
| 202 | public function clearTag(array $keys): void |
||
| 208 | |||
| 209 | } |
||
| 210 |
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.