Decryption   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 46
Duplicated Lines 10.87 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 5
loc 46
rs 10
c 0
b 0
f 0
wmc 11
lcom 1
cbo 3

1 Method

Rating   Name   Duplication   Size   Complexity  
C decryptionNoticeXML() 5 43 11

How to fix   Duplicated Code   

Duplicated Code

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
2
namespace OpenOauth;
3
4
use OpenOauth\Core\Core;
5
use OpenOauth\Core\Tools;
6
use OpenOauth\Core\WechatCode\WXBizMsgCrypt;
7
8
class Decryption extends Core
9
{
10
    public function decryptionNoticeXML()
0 ignored issues
show
Coding Style introduced by
decryptionNoticeXML uses the super-global variable $_GET which is generally not recommended.

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:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
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 == '') {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
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
}