Completed
Push — master ( f81184...3aae6e )
by Andrii
04:04
created

Response::parseHeaders()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 19
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 4.0047

Importance

Changes 0
Metric Value
dl 0
loc 19
ccs 14
cts 15
cp 0.9333
rs 9.2
c 0
b 0
f 0
cc 4
eloc 14
nc 4
nop 1
crap 4.0047
1
<?php
2
/**
3
 * Tools to use API as ActiveRecord for Yii2
4
 *
5
 * @link      https://github.com/hiqdev/yii2-hiart
6
 * @package   yii2-hiart
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2015-2017, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hiqdev\hiart\stream;
12
13
use hiqdev\hiart\AbstractResponse;
14
15
/**
16
 * PHP stream response implementation.
17
 *
18
 * @author Andrii Vasyliev <[email protected]>
19
 */
20
class Response extends AbstractResponse
21
{
22
    protected $rawData;
23
24
    protected $headers;
25
26
    protected $statusCode;
27
28
    protected $reasonPhrase;
29
30 2
    public function __construct(Request $request, $rawData, array $rawHeaders)
31
    {
32 2
        $this->request = $request;
33 2
        $this->rawData = $rawData;
34 2
        $this->headers = $this->parseHeaders($rawHeaders);
35 2
    }
36
37 2
    public function getRawData()
38
    {
39 2
        return $this->rawData;
40
    }
41
42 2
    public function getHeader($name)
43
    {
44 2
        $name = strtolower($name);
45
46 2
        return isset($this->headers[$name]) ? $this->headers[$name] : null;
47
    }
48
49 2
    public function parseHeaders($headers)
50
    {
51 2
        foreach ($headers as $header) {
52 2
            if (strncmp($header, 'HTTP/', 5) === 0) {
53 2
                $parts = explode(' ', $header, 3);
54 2
                $this->version = substr($parts[0], 5);
0 ignored issues
show
Bug introduced by
The property version does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
55 2
                $this->statusCode = $parts[1];
56 2
                $this->reasonPhrase = $parts[2];
57 2
            } elseif (($pos = strpos($header, ':')) !== false) {
58 2
                $name = strtolower(trim(substr($header, 0, $pos)));
59 2
                $value = trim(substr($header, $pos + 1));
60 2
                $result[$name][] = $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...
61 2
            } else {
62
                $result['raw'][] = $header;
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...
63
            }
64 2
        }
65
66 2
        return $result;
67
    }
68
69 2
    public function getStatusCode()
70
    {
71 2
        return $this->statusCode;
72
    }
73
74
    public function getReasonPhrase()
75
    {
76
        return $this->reasonPhrase;
77
    }
78
}
79