QcloudAgent   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 115
Duplicated Lines 20.87 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 1
dl 24
loc 115
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A formatMobile() 0 13 2
A sendContentSms() 12 12 1
A sendTemplateSms() 12 12 1
A sendVoiceCode() 0 9 1
A sendContentVoice() 0 10 1
A request() 0 9 1
A genSign() 0 14 2
A setResult() 0 16 4
A getRandom() 0 4 1

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
3
namespace Toplan\PhpSms;
4
5
/**
6
 * Class SendCloudAgent
7
 *
8
 * @property string $appId
9
 * @property string $appKey
10
 */
11
class QcloudAgent extends Agent implements TemplateSms, ContentSms, VoiceCode, ContentVoice
12
{
13
    protected $sendSms = 'https://yun.tim.qq.com/v5/tlssmssvr/sendsms';
14
    protected $sendVoiceCode = 'https://yun.tim.qq.com/v5/tlsvoicesvr/sendvoice';
15
    protected $sendVoicePrompt = 'https://yun.tim.qq.com/v5/tlsvoicesvr/sendvoiceprompt';
16
    protected $random;
17
18
    public function formatMobile(array $list)
19
    {
20
        $list = array_map(function ($value) {
21
            return [
22
                'nationcode'    => $value['nation'],
23
                'mobile'        => $value['number'],
24
            ];
25
        }, array_filter($list, function ($value) {
26
            return is_array($value);
27
        }));
28
29
        return count($list) === 1 ? array_pop($list) : array_values($list);
30
    }
31
32 View Code Duplication
    public function sendContentSms($to, $content)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
33
    {
34
        $params = [
35
            'type'   => 0, // 0:普通短信 1:营销短信
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
36
            'msg'    => $content,
37
            'tel'    => $to,
38
            'time'   => time(),
39
        ];
40
        $this->random = $this->getRandom();
41
        $sendUrl = "{$this->sendSms}?sdkappid={$this->appId}&random={$this->random}";
42
        $this->request($sendUrl, $params);
43
    }
44
45 View Code Duplication
    public function sendTemplateSms($to, $tempId, array $data)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
46
    {
47
        $params = [
48
            'tel'    => $to,
49
            'tpl_id' => $tempId,
50
            'params' => array_values($data),
51
            'time'   => time(),
52
        ];
53
        $this->random = $this->getRandom();
54
        $sendUrl = "{$this->sendSms}?sdkappid={$this->appId}&random={$this->random}";
55
        $this->request($sendUrl, $params);
56
    }
57
58
    public function sendVoiceCode($to, $code)
59
    {
60
        $params = [
61
            'tel'    => $to,
62
            'msg'    => $code,
63
        ];
64
        $sendUrl = "{$this->sendVoiceCode}?sdkappid={$this->appId}&random={$this->random}";
65
        $this->request($sendUrl, $params);
66
    }
67
68
    public function sendContentVoice($to, $content)
69
    {
70
        $params = [
71
            'tel'        => $to,
72
            'prompttype' => 2,
73
            'promptfile' => $content,
74
        ];
75
        $sendUrl = "{$this->sendVoicePrompt}?sdkappid={$this->appId}&random={$this->random}";
76
        $this->request($sendUrl, $params);
77
    }
78
79
    protected function request($sendUrl, array $params)
80
    {
81
        $params['sig'] = $this->genSign($params);
82
        $params = $this->params($params);
83
        $result = $this->curlPost($sendUrl, [], [
84
            CURLOPT_POSTFIELDS => json_encode($params),
85
        ]);
86
        $this->setResult($result);
87
    }
88
89
    protected function genSign($params)
90
    {
91
        $mobileStr = null;
0 ignored issues
show
Unused Code introduced by
$mobileStr is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
92
        if (array_key_exists('mobile', $params['tel'])) {
93
            $mobileStr = $params['tel']['mobile'];
94
        } else {
95
            $mobileStr = implode(',', array_map(function ($value) {
96
                return $value['mobile'];
97
            }, $params['tel']));
98
        }
99
        $signature = "appkey={$this->appKey}&random={$this->random}&time={$params['time']}&mobile={$mobileStr}";
100
101
        return hash('sha256', $signature, false);
102
    }
103
104
    protected function setResult($result)
105
    {
106
        if ($result['request']) {
107
            $this->result(Agent::INFO, $result['response']);
108
            $result = json_decode($result['response'], true);
109
            if (isset($result['result'])) {
110
                $this->result(Agent::SUCCESS, $result['result'] === 0);
111
                $this->result(Agent::CODE, $result['result']);
112
            } elseif (isset($result['ErrorCode'])) {
113
                $this->result(Agent::CODE, $result['ErrorCode']);
114
                $this->result(Agent::INFO, $result['ErrorInfo']);
115
            }
116
        } else {
117
            $this->result(Agent::INFO, 'request failed');
118
        }
119
    }
120
121
    protected function getRandom()
122
    {
123
        return rand(100000, 999999);
124
    }
125
}
126