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.
Completed
Branch dev (718e6b)
by Jérémy
04:44 queued 02:58
created

Request::parseData()   C

Complexity

Conditions 8
Paths 2

Size

Total Lines 43
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 43
rs 5.3846
cc 8
eloc 26
nc 2
nop 0
1
<?php
2
3
namespace CapMousse\ReactRestify\Http;
4
5
use React\Http\Request as ReactHttpRequest;
6
use Evenement\EventEmitter;
7
8
class Request extends EventEmitter
9
{
10
    /** @var React\Http\Request */
11
    public $httpRequest;
12
13
    /** @var mixed */
14
    private $content;
15
16
    /** @var array */
17
    private $data = [];
18
19
    /**
20
     * @param ReactHttpRequest $httpRequest
21
     */
22
    public function __construct(ReactHttpRequest $httpRequest)
23
    {
24
        $this->httpRequest = $httpRequest;
0 ignored issues
show
Documentation Bug introduced by
It seems like $httpRequest of type object<React\Http\Request> is incompatible with the declared type object<CapMousse\ReactRe...ttp\React\Http\Request> of property $httpRequest.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
25
    }
26
27
    /**
28
     * Set the raw data of the request
29
     * @param mixed $content
30
     */
31
    public function setContent($content)
32
    {
33
        $this->content = $content;
34
    }
35
36
    /**
37
     * Set the raw data of the request
38
     * @param mixed $content
0 ignored issues
show
Bug introduced by
There is no parameter named $content. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
39
     */
40
    public function getContent()
41
    {
42
        return $this->content;
43
    }
44
45
    public function getHeaders()
46
    {
47
        $headers = array_change_key_case($this->httpRequest->getHeaders(), CASE_LOWER);
48
        $headers = array_map(function ($value) {
49
            return strtolower($value);
50
        }, $headers);
51
52
        return $headers;
53
    }
54
55
    public function parseData()
56
    {
57
        $headers = $this->getHeaders();
58
59
        if (!in_array($this->httpRequest->getMethod(), ['PUT', 'POST'])) {
60
            return $this->emit('end');
61
        }
62
63
        $this->httpRequest->on('data', function($data) use ($headers, &$dataResult) {
64
            $dataResult .= $data;
65
66
            if (isset($headers["Content-Length"])) {
67
                if (strlen($dataResult) == $headers["Content-Length"]) {
68
                    $this->httpRequest->close();
69
                }
70
            } else {
71
                $this->httpRequest->close();
72
            }
73
        });
74
75
        $this->httpRequest->on('end', function() use ($headers, &$dataResult) {
76
            $error = null;
0 ignored issues
show
Unused Code introduced by
$error 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...
77
            $data = [];
78
79
            if ($dataResult === null) return $this->emit('end');
80
81
            if (isset($headers['content-type']) && $headers['content-type'] == 'application/json') {
82
                $jsonData = json_decode($dataResult, true);
83
84
                if ($jsonData === null) {
85
                    $this->emit('error', [json_last_error_msg()]);
86
                } else {
87
                    $data = $jsonData;
88
                }
89
            } else {
90
                parse_str($dataResult, $data);
91
            }
92
93
            $this->setContent($dataResult);
94
            $this->setData($data);
95
            $this->emit('end');            
96
        });
97
    }
98
99
    /**
100
     * Set the data array
101
     * @param array $data array of data
102
     */
103
    public function setData($data)
104
    {
105
        $this->data = array_merge($data, $this->data);
106
    }
107
108
    /**
109
     * Get the data array
110
     * @return array
111
     */
112
    public function getData()
113
    {
114
        return $this->data;
115
    }
116
117
    public function __get($name)
118
    {
119
        return isset($this->data[$name]) ? $this->data[$name] : false;
120
    }
121
122
    public function __set($name, $value)
123
    {
124
        $this->data[$name] = $value;
125
    }
126
}
127