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 Captcha |
||
14 | { |
||
15 | const DEFAULT_NAME = 'default'; |
||
16 | |||
17 | protected $config = [ |
||
18 | /** |
||
19 | * 默认验证码长度 |
||
20 | * @var int |
||
21 | */ |
||
22 | 'length' => 4, |
||
23 | /** |
||
24 | * 验证码内容 |
||
25 | * @var string |
||
26 | */ |
||
27 | 'charset' => 'abcdefghijklmnpqrstuvwxyz123456789' |
||
28 | ]; |
||
29 | |||
30 | /** |
||
31 | * 存储驱动 |
||
32 | * @var Session |
||
33 | */ |
||
34 | protected $store; |
||
35 | |||
36 | |||
37 | public function __construct(Session $session, array $config = []) |
||
43 | |||
44 | /** |
||
45 | * 设置验证码配置 |
||
46 | * @param array $config |
||
47 | * @return $this |
||
48 | */ |
||
49 | public function setConfig(array $config) |
||
58 | |||
59 | /** |
||
60 | * 获取配置 |
||
61 | * @return array |
||
62 | */ |
||
63 | public function getConfig() |
||
67 | |||
68 | /** |
||
69 | * 生成验证码 |
||
70 | * @param string $name |
||
71 | * @param array $config |
||
72 | * @return Image |
||
73 | */ |
||
74 | public function make($name = null, array $config = []) |
||
85 | |||
86 | /** |
||
87 | * 仅测试正确性, 不删除验证码 |
||
88 | * @param $input |
||
89 | * @param string $name |
||
90 | * @return bool |
||
91 | */ |
||
92 | public function test($input, $name = null) |
||
102 | |||
103 | /** |
||
104 | * 检测正确性,并删除验证码 |
||
105 | * @param $input |
||
106 | * @param string $name |
||
107 | * @return bool |
||
108 | */ |
||
109 | public function check($input, $name = null) |
||
117 | |||
118 | /** |
||
119 | * 生成验证码 |
||
120 | * @param array|string $charset |
||
121 | * @param int $length |
||
122 | * @return string |
||
123 | */ |
||
124 | protected function generate($charset, $length = 4) |
||
135 | |||
136 | |||
137 | /** |
||
138 | * 加密字符串 |
||
139 | * @param $value |
||
140 | * @return bool|string |
||
141 | */ |
||
142 | protected function hash($value) |
||
152 | |||
153 | /** |
||
154 | * 返回存储到session中的键全名 |
||
155 | * @param string $name |
||
156 | * @return string |
||
157 | */ |
||
158 | protected function getFullName($name) |
||
162 | |||
163 | /** |
||
164 | * @param $name |
||
165 | * @return bool |
||
166 | */ |
||
167 | protected function has($name) |
||
171 | |||
172 | /** |
||
173 | * 存储验证码 |
||
174 | * @param $name |
||
175 | * @param $code |
||
176 | */ |
||
177 | protected function store($name, $code) |
||
181 | |||
182 | /** |
||
183 | * 从存储中获取验证码 |
||
184 | * @param $name |
||
185 | * @return mixed |
||
186 | */ |
||
187 | protected function get($name) |
||
191 | |||
192 | /** |
||
193 | * 从存储中删除验证码 |
||
194 | * @param $name |
||
195 | * @return mixed |
||
196 | */ |
||
197 | protected function remove($name) |
||
201 | } |
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.