Conditions | 11 |
Paths | 5 |
Total Lines | 43 |
Code Lines | 23 |
Lines | 5 |
Ratio | 11.63 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
10 | public function decryptionNoticeXML() |
||
|
|||
11 | { |
||
12 | //接收微信的xml 消息 |
||
13 | $xml = file_get_contents("php://input"); |
||
14 | |||
15 | $msg_signature = $_GET['msg_signature']; |
||
16 | $timestamp = $_GET['timestamp']; |
||
17 | $nonce = $_GET['nonce']; |
||
18 | |||
19 | if (!is_string($xml) || $xml == '') { |
||
20 | $this->setError('xml参数错误'); |
||
21 | |||
22 | return false; |
||
23 | } |
||
24 | |||
25 | View Code Duplication | if (!is_string($msg_signature) || $xml == '' || !is_numeric($timestamp) || $timestamp <= 0 || !is_string($nonce) || $nonce == '') { |
|
26 | $this->setError('签名参数错误'); |
||
27 | |||
28 | return false; |
||
29 | } |
||
30 | |||
31 | $verify_signature = Tools::verifySignature($this->configs->component_app_token); |
||
32 | |||
33 | if (!$verify_signature) { |
||
34 | $this->setError('签名验证失败'); |
||
35 | |||
36 | return false; |
||
37 | } |
||
38 | |||
39 | //解密XML |
||
40 | $msgCrypt = new WXBizMsgCrypt($this->configs->component_app_token, $this->configs->component_app_key, $this->configs->component_app_id); |
||
41 | $xmlInfo = ''; |
||
42 | |||
43 | $errCode = $msgCrypt->decryptMsg($msg_signature, $timestamp, $nonce, $xml, $xmlInfo); |
||
44 | |||
45 | if ($errCode == 0) { |
||
46 | $xml_array = json_decode(json_encode(simplexml_load_string($xmlInfo, 'SimpleXMLElement', LIBXML_NOCDATA)), true); |
||
47 | |||
48 | return $xml_array; |
||
49 | } else { |
||
50 | return false; |
||
51 | } |
||
52 | } |
||
53 | } |
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: