Completed
Pull Request — master (#267)
by
unknown
11:00
created

HttpResponse::get_headers_from_curl_response()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 3
nop 1
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
1
<?php
2
namespace QiniuRtc;
3
4
class HttpResponse
5
{
6
    private $code;
7
    private $raw_body;
8
    private $body;
9
    private $headers;
10
11
    /**
12
     * @param int $code response code of the cURL request
13
     * @param string $raw_body the raw body of the cURL response
14
     * @param string $headers raw header string from cURL response
15
     */
16
    public function __construct($code, $raw_body, $headers)
17
    {
18
        $this->code     = $code;
19
        $this->headers  = $this->get_headers_from_curl_response($headers);
20
        $this->raw_body = $raw_body;
21
        $this->body     = $raw_body;
22
        $json           = json_decode($raw_body, true);
23
        if (json_last_error() == JSON_ERROR_NONE) {
24
            $this->body = $json;
25
        }
26
    }
27
28
    /**
29
     * Return a property of the response if it exists.
30
     * Possibilities include: code, raw_body, headers, body (if the response is json-decodable)
31
     * @return mixed
32
     */
33
    public function __get($property)
34
    {
35
        if (property_exists($this, $property)) {
36
            return $this->$property;
37
        }
38
    }
39
40
    /**
41
     * Set the properties of this object
42
     * @param string $property the property name
43
     * @param mixed $value the property value
44
     */
45
    public function __set($property, $value)
46
    {
47
        if (property_exists($this, $property)) {
48
            $this->$property = $value;
49
        }
50
        return $this;
51
    }
52
53
    /**
54
     * Retrieve the cURL response headers from the
55
     * header string and convert it into an array
56
     * @param  string $headers header string from cURL response
57
     * @return array
58
     */
59
    private function get_headers_from_curl_response($headers)
60
    {
61
        $headers = explode("\r\n", $headers);
62
        array_shift($headers);
63
        foreach ($headers as $line) {
64
            if (strstr($line, ': ')) {
65
                list($key, $value) = explode(': ', $line);
66
                $result[$key] = $value;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$result was never initialized. Although not strictly required by PHP, it is generally a good practice to add $result = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
67
            }
68
        }
69
        return $result;
0 ignored issues
show
Bug introduced by
The variable $result does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
70
    }
71
}
72