Juhe   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 56
Duplicated Lines 8.93 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 1
dl 5
loc 56
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 2
B sendTemplateSMS() 5 42 6

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
 * Created by PhpStorm.
4
 * User: Caikeal
5
 * Date: 2016/8/29
6
 * Time: 10:56.
7
 */
8
9
namespace Caikeal\LaravelSms\lib;
10
11
class Juhe
12
{
13
    use CurlTrait;
14
    protected $appId;
15
16
    public function __construct(array $config, $BodyType = 'json')
17
    {
18
        $this->appId = $config['appId'];
19
        if (in_array($BodyType, ['xml', 'json'])) {
20
            $this->BodyType = $BodyType;
21
        }
22
    }
23
24
    public function sendTemplateSMS($to, $datas, $tempId)
25
    {
26
        $data = '';
27
        foreach ($datas as $kk => $vv) {
28
            if (is_array($vv)) {
29
                throw new \InvalidArgumentException('$datas must be a one-dimensional array, given two-dimensional arrays');
30
            }
31
32
            //处理key
33
            if (strpos($kk, '#') === false) {
34
                $key = '#'.$kk.'#';
35
            } else {
36
                $key = $kk;
37
            }
38
            //处理值
39
            if (strpos($vv, '#') === false) {
40
                $value = $vv;
41
            } else {
42
                $value = urlencode($vv);
43
            }
44
45
            $data = $data.$key.'='.$value.'&';
46
        }
47
        $data = trim($data, '&');
48
        //生成query params
49
        $body = ['mobile' => $to, 'tpl_id' => $tempId, 'key' => $this->appId, 'tpl_value' => $data, 'dtype' => $this->BodyType];
50
        // 生成包头
51
        $header = ["Accept:application/$this->BodyType", "Content-Type:application/$this->BodyType;charset=utf-8"];
52
        // 生成请求URL
53
        $query = http_build_query($body);
54
        $url = 'http://v.juhe.cn/sms/send?'.$query;
55
        // 发送请求
56
        $result = $this->curl_post($url, $body, $header, 0);
57
58 View Code Duplication
        if ($this->BodyType === 'json') {//JSON格式
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...
59
            $datas = json_decode($result);
60
        } else { //xml格式
61
            $datas = simplexml_load_string(trim($result, " \t\n\r"));
62
        }
63
64
        return $datas;
65
    }
66
}
67