CurlTrait   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 43
Duplicated Lines 62.79 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 0
dl 27
loc 43
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B curl_post() 27 28 4

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: Odeen
5
 * Date: 2016/5/9
6
 * Time: 17:34.
7
 */
8
9
namespace Caikeal\LaravelSms\lib;
10
11
/**
12
 * Class CurlTrait.
13
 *
14
 * @function curl_post($url, $data, $header, $post = 1)
15
 */
16
trait CurlTrait
17
{
18
    public $BodyType = 'json';
19
20
    /**
21
     * create url post.
22
     *
23
     * @param $url
24
     * @param $data
25
     * @param $header
26
     * @param int $post
27
     *
28
     * @return string
29
     */
30 View Code Duplication
    public function curl_post($url, $data, $header, $post = 1)
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...
31
    {
32
        //初始化curl
33
        $ch = curl_init();
34
        //参数设置
35
        $res = curl_setopt($ch, CURLOPT_URL, $url);
0 ignored issues
show
Unused Code introduced by
$res 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...
36
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
37
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
38
        curl_setopt($ch, CURLOPT_HEADER, 0);
39
        curl_setopt($ch, CURLOPT_POST, $post);
40
        if ($post) {
41
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
42
        }
43
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
44
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
45
        $result = curl_exec($ch);
46
        //连接失败
47
        if ($result === false) {
48
            if ($this->BodyType === 'json') {
49
                $result = '{"statusCode":"172001","statusMsg":"网络错误"}';
50
            } else {
51
                $result = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?><Response><statusCode>172001</statusCode><statusMsg>网络错误</statusMsg></Response>';
52
            }
53
        }
54
        curl_close($ch);
55
56
        return $result;
57
    }
58
}
59