GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Test Failed
Push — master ( 4481ca...5e00e9 )
by sunsky
10:52
created

JsonTrait   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 13
lcom 0
cbo 1
dl 0
loc 68
c 0
b 0
f 0
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A expectsJson() 0 4 1
A sendJson() 0 4 1
A json() 0 4 1
A unJson() 0 6 2
C jsonLastErrorMsg() 0 26 8
1
<?php
2
/**
3
 *
4
 * @author  [email protected] [email protected]
5
 * Date: 2017/6/16
6
 * Time: 10:02
7
 * @version $Id: $
8
 * @since 1.0
9
 * @copyright Sina Corp.
10
 */
11
12
namespace MultiHttp;
13
14
use MultiHttp\Exception\UnexpectedResponseException;
15
16
/**
17
 * Trait JsonTrait
18
 * @package MultiHttp
19
 */
20
trait JsonTrait
21
{
22
    /**
23
     * @return Request
24
     */
25
    public function expectsJson()
26
    {
27
        return $this->expectsMime('json');
0 ignored issues
show
Bug introduced by
It seems like expectsMime() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
28
    }
29
30
    /**
31
     * @return Request
32
     */
33
    public function sendJson()
34
    {
35
        return $this->sendMime('json');
0 ignored issues
show
Bug introduced by
It seems like sendMime() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
36
    }
37
38
    /**
39
     * @param $body
40
     * @return string
41
     */
42
    public function json($body)
43
    {
44
        return json_encode($body, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
45
    }
46
47
    /**
48
     * @param $body
49
     * @return mixed
50
     */
51
    public function unJson($body)
52
    {
53
        $parsed = json_decode($body, true);
54
        if(json_last_error() !== JSON_ERROR_NONE)throw new UnexpectedResponseException('parsing json occurs error: '.  self::jsonLastErrorMsg() . ', raw body: ' .$body  );
55
        return $parsed;
56
    }
57
58
    /**
59
     * @return string
60
     */
61
    private static function jsonLastErrorMsg(){
62
        if(function_exists('json_last_error_msg')) return json_last_error_msg();
63
        switch (json_last_error()) {
64
            case JSON_ERROR_NONE:
65
                return ' - No errors';
66
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
67
            case JSON_ERROR_DEPTH:
68
                return ' - Maximum stack depth exceeded';
69
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
70
            case JSON_ERROR_STATE_MISMATCH:
71
                return ' - Underflow or the modes mismatch';
72
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
73
            case JSON_ERROR_CTRL_CHAR:
74
                return ' - Unexpected control character found';
75
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
76
            case JSON_ERROR_SYNTAX:
77
                return ' - Syntax error, malformed JSON';
78
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
79
            case JSON_ERROR_UTF8:
80
                return ' - Malformed UTF-8 characters, possibly incorrectly encoded';
81
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
82
            default:
83
                return ' - Unknown error';
84
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
85
        }
86
    }
87
}
88
89