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:
Complex classes like ServerApi often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ServerApi, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 12 | class ServerApi extends BaseApi |
||
| 13 | { |
||
| 14 | private static $XML_OBJECT; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * 监听 |
||
| 18 | * |
||
| 19 | * @param string $target |
||
| 20 | * @param string|callable $event |
||
| 21 | * @param callable $callback |
||
| 22 | * |
||
| 23 | * @return bool Server |
||
| 24 | */ |
||
| 25 | |||
| 26 | public function on($target, $event, $callback = null) |
||
| 78 | |||
| 79 | /** |
||
| 80 | * 验证签名 |
||
| 81 | * |
||
| 82 | * string $signature [签名] |
||
| 83 | * int $timestamp [时间戳] |
||
| 84 | * string $nonce [随机字符串] |
||
| 85 | * |
||
| 86 | * @param string $token [token] |
||
| 87 | * |
||
| 88 | * @return mixed|bool |
||
| 89 | */ |
||
| 90 | public function checkSignature($token = null) |
||
| 91 | { |
||
| 92 | $config_token = Api::getToken(); |
||
| 93 | $signature = isset($_GET['signature']) ? $_GET['signature'] : null; |
||
| 94 | $timestamp = isset($_GET['timestamp']) ? $_GET['timestamp'] : null; |
||
| 95 | $nonce = isset($_GET['nonce']) ? $_GET['nonce'] : null; |
||
| 96 | $token = empty($token) ? $config_token : $token; |
||
| 97 | $tmpArr = [$token, $timestamp, $nonce]; |
||
| 98 | sort($tmpArr, SORT_STRING); |
||
| 99 | $tmpStr = implode($tmpArr); |
||
| 100 | $tmpStr = sha1($tmpStr); |
||
| 101 | |||
| 102 | if ($tmpStr == $signature && $signature != null) { |
||
| 103 | return $this; |
||
| 104 | } |
||
| 105 | |||
| 106 | return false; |
||
| 107 | } |
||
| 108 | |||
| 109 | /** |
||
| 110 | * 生成签名 - 用作 转发到第三方 |
||
| 111 | * |
||
| 112 | * @param string $token |
||
| 113 | * @param string $time |
||
| 114 | * @param string $nonce |
||
| 115 | * |
||
| 116 | * @return bool|string |
||
| 117 | */ |
||
| 118 | public function makeSignature($token = null, $time = null, $nonce = null) |
||
| 133 | |||
| 134 | /** |
||
| 135 | * 推送到第三方 |
||
| 136 | * |
||
| 137 | * @param string $url [地址] |
||
| 138 | * @param string $token [token] |
||
| 139 | * @param string $xml [XML] |
||
| 140 | * @param bool $encipher [是否加密] |
||
| 141 | * |
||
| 142 | * @return array|bool XML |
||
| 143 | */ |
||
| 144 | public function receiveAgent($url = '', $token = '', $xml = '', $encipher = false) |
||
| 145 | { |
||
| 146 | $object = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA); |
||
| 147 | |||
| 148 | $object = json_decode(json_encode($object), true); |
||
| 149 | |||
| 150 | if (isset($object['Encrypt']) && !empty($object['Encrypt']) && $object['Encrypt'] != '') { |
||
| 151 | $xml = $this->decryptMsg($xml); |
||
| 152 | } |
||
| 153 | |||
| 154 | if ($encipher) { |
||
| 155 | $xml = $this->encryptMsg($xml); |
||
| 156 | |||
| 157 | $object_enc = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA); |
||
| 158 | $object_enc = json_decode(json_encode($object_enc), true); |
||
| 159 | $postQueryStr = []; |
||
| 160 | $postQueryStr['timestamp'] = $object_enc['TimeStamp']; |
||
| 161 | $postQueryStr['nonce'] = $object_enc['Nonce']; |
||
| 162 | $postQueryStr['signature'] = $this->makeSignature($token, $postQueryStr['timestamp'], $postQueryStr['nonce']); |
||
| 163 | $postQueryStr['msg_signature'] = $object_enc['MsgSignature']; |
||
| 164 | |||
| 165 | $array = []; |
||
| 166 | $array['ToUserName'] = $object['ToUserName']; |
||
| 167 | $array['Encrypt'] = $object_enc['Encrypt']; |
||
| 168 | |||
| 169 | $xml = $this->arrayToXml($array); |
||
| 170 | } else { |
||
| 171 | $postQueryStr['timestamp'] = time(); |
||
| 172 | $postQueryStr['nonce'] = $this->getRandomStr(); |
||
| 173 | $postQueryStr['signature'] = $this->makeSignature($token, $postQueryStr['timestamp'], $postQueryStr['nonce']); |
||
| 174 | } |
||
| 175 | |||
| 176 | asort($postQueryStr); |
||
| 177 | $postQueryStr = http_build_query($postQueryStr); |
||
| 178 | |||
| 179 | $rest = $this->https_xml($url . '?' . $postQueryStr, $xml); |
||
| 180 | |||
| 181 | if ($rest) { |
||
| 182 | $object_rest = simplexml_load_string($rest, 'SimpleXMLElement', LIBXML_NOCDATA); |
||
| 183 | $object_rest = json_decode(json_encode($object_rest), true); |
||
| 184 | |||
| 185 | if (!empty($object_rest['MsgSignature']) && !empty($object_rest['TimeStamp']) && !empty($object_rest['Nonce']) && !empty($object_rest['Encrypt'])) { |
||
| 186 | $rest = $this->decryptMsg($rest, $object_rest['MsgSignature'], $object_rest['TimeStamp'], $object_rest['Nonce']); |
||
| 187 | } |
||
| 188 | $object_rest = simplexml_load_string($rest, 'SimpleXMLElement', LIBXML_NOCDATA); |
||
| 189 | $rest_array = json_decode(json_encode($object_rest), true); |
||
| 190 | |||
| 191 | return $rest_array; |
||
| 192 | } |
||
| 193 | |||
| 194 | return false; |
||
| 195 | } |
||
| 196 | |||
| 197 | /** |
||
| 198 | * 加密XML |
||
| 199 | * |
||
| 200 | * @param $xml |
||
| 201 | * |
||
| 202 | * @return string |
||
| 203 | */ |
||
| 204 | public function encryptMsg($xml) |
||
| 226 | |||
| 227 | /** |
||
| 228 | * 解密XML |
||
| 229 | * |
||
| 230 | * @param $xml |
||
| 231 | * @param null $msg_signature |
||
| 232 | * @param null $timeStamp |
||
| 233 | * @param null $nonce |
||
| 234 | * |
||
| 235 | * @return string |
||
| 236 | */ |
||
| 237 | public function decryptMsg($xml, $msg_signature = null, $timeStamp = null, $nonce = null) |
||
| 258 | |||
| 259 | /** |
||
| 260 | * 魔术方法 处理 返回结果 |
||
| 261 | * |
||
| 262 | * @param string $target [事件] |
||
| 263 | * @param $event |
||
| 264 | * |
||
| 265 | * @return string $event [类型] |
||
| 266 | * @return array $xml [xml] |
||
| 267 | */ |
||
| 268 | public function __call($target, $event) |
||
| 305 | |||
| 306 | /** |
||
| 307 | * array转xml. |
||
| 308 | * |
||
| 309 | * @param array $arr |
||
| 310 | * @param bool $flag |
||
| 311 | * @param bool $especial |
||
| 312 | * |
||
| 313 | * @return string |
||
| 314 | */ |
||
| 315 | public function arrayToXml($arr = [], $flag = true, $especial = false) |
||
| 349 | |||
| 350 | public function getXmlObject() |
||
| 354 | |||
| 355 | /** |
||
| 356 | * 第三方 post 推送 |
||
| 357 | * |
||
| 358 | * @param $url |
||
| 359 | * @param null $xml |
||
| 360 | * |
||
| 361 | * @return mixed |
||
| 362 | */ |
||
| 363 | public function https_xml($url, $xml = null) |
||
| 379 | |||
| 380 | /** |
||
| 381 | * 随机生成16位字符串 |
||
| 382 | * |
||
| 383 | * @return string 生成的字符串 |
||
| 384 | */ |
||
| 385 | View Code Duplication | public function getRandomStr() |
|
| 396 | } |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: