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 |
||
18 | class TLSSig |
||
19 | { |
||
20 | private $private_key = false; |
||
21 | private $public_key = false; |
||
22 | private $appid; |
||
23 | |||
24 | /** |
||
25 | * 设置Appid |
||
26 | * @param type $appid |
||
27 | */ |
||
28 | public function setAppid($appid) |
||
32 | |||
33 | /** |
||
34 | * 设置私钥 如果要生成usersig则需要私钥 |
||
35 | * @param string $private_key 私钥文件内容 |
||
36 | * @return bool 是否成功 |
||
37 | */ |
||
38 | public function setPrivateKey($private_key) |
||
46 | |||
47 | /** |
||
48 | * 设置公钥 如果要验证usersig则需要公钥 |
||
49 | * @param string $public_key 公钥文件内容 |
||
50 | * @return bool 是否成功 |
||
51 | */ |
||
52 | public function setPublicKey($public_key) |
||
60 | |||
61 | /** |
||
62 | * 用于url的base64encode |
||
63 | * '+' => '*', '/' => '-', '=' => '_' |
||
64 | * @param string $string 需要编码的数据 |
||
65 | * @return string 编码后的base64串,失败返回false |
||
66 | */ |
||
67 | View Code Duplication | private function base64Encode($string) |
|
76 | |||
77 | /** |
||
78 | * 用于url的base64decode |
||
79 | * '+' => '*', '/' => '-', '=' => '_' |
||
80 | * @param string $base64 需要解码的base64串 |
||
81 | * @return string 解码后的数据,失败返回false |
||
82 | */ |
||
83 | View Code Duplication | private function base64Decode($base64) |
|
93 | |||
94 | /** |
||
95 | * 根据json内容生成需要签名的buf串 |
||
96 | * @param array $json 票据json对象 |
||
97 | * @return string 按标准格式生成的用于签名的字符串 |
||
98 | * 失败时返回false |
||
99 | */ |
||
100 | private function genSignContent(array $json) |
||
119 | |||
120 | /** |
||
121 | * ECDSA-SHA256签名 |
||
122 | * @param string $data 需要签名的数据 |
||
123 | * @return string 返回签名 失败时返回false |
||
124 | */ |
||
125 | private function sign($data) |
||
133 | |||
134 | /** |
||
135 | * 验证ECDSA-SHA256签名 |
||
136 | * @param string $data 需要验证的数据原文 |
||
137 | * @param string $sig 需要验证的签名 |
||
138 | * @return int 1验证成功 0验证失败 |
||
139 | */ |
||
140 | private function verify($data, $sig) |
||
148 | |||
149 | /** |
||
150 | * 生成usersig |
||
151 | * @param string $identifier 用户名 |
||
152 | * @param uint $expire usersig有效期 默认为180天, 180 * 24 * 3600, 单位秒 |
||
153 | * @return string 生成的UserSig 失败时为false |
||
154 | */ |
||
155 | public function genSig($identifier, $expire = 15552000) |
||
183 | |||
184 | /** |
||
185 | * 验证usersig |
||
186 | * @param type $sig usersig |
||
187 | * @param type $identifier 需要验证用户名 |
||
188 | * @param type $init_time usersig中的生成时间 |
||
189 | * @param type $expire_time usersig中的有效期 如:3600秒 |
||
190 | * @param type $error_msg 失败时的错误信息 |
||
191 | * @return boolean 验证是否成功 |
||
192 | */ |
||
193 | public function verifySig($sig, $identifier, &$init_time, &$expire_time, &$error_msg) |
||
230 | } |
||
231 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..