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
Push — dev ( 790a5f...fc84f2 )
by Jérémy
01:52
created

Request   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 161
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 21
c 2
b 0
f 0
lcom 1
cbo 2
dl 0
loc 161
rs 10

13 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A setContent() 0 4 1
A getContent() 0 4 1
A getHeaders() 0 9 1
B parseData() 0 24 4
A onEnd() 0 7 3
A parseStr() 0 9 1
A parseJson() 0 13 2
A isJson() 0 6 2
A setData() 0 4 1
A getData() 0 4 1
A __get() 0 4 2
A __set() 0 4 1
1
<?php
2
3
namespace CapMousse\ReactRestify\Http;
4
5
use React\Http\Request as ReactHttpRequest;
6
use CapMousse\ReactRestify\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;
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
     * @return mixed
39
     */
40
    public function getContent()
41
    {
42
        return $this->content;
43
    }
44
45
    /**
46
     * Get formated headers
47
     * @return array
48
     */
49
    public function getHeaders()
50
    {
51
        $headers = array_change_key_case($this->httpRequest->getHeaders(), CASE_LOWER);
52
        $headers = array_map(function ($value) {
53
            return strtolower($value);
54
        }, $headers);
55
56
        return $headers;
57
    }
58
59
    /**
60
     * Parse request data
61
     * @return void
62
     */
63
    public function parseData()
64
    {
65
        $headers = $this->getHeaders();
66
67
        if (!in_array($this->httpRequest->getMethod(), ['PUT', 'POST'])) {
68
            return $this->emit('end');
69
        }
70
71
        $this->httpRequest->on('data', function($data) use ($headers, &$dataResult) {
72
            $dataResult .= $data;
73
74
            if (isset($headers["Content-Length"])) {
75
                if (strlen($dataResult) == $headers["Content-Length"]) {
76
                    $this->httpRequest->close();
77
                }
78
            } else {
79
                $this->httpRequest->close();
80
            }
81
        });
82
83
        $this->httpRequest->on('end', function() use (&$dataResult) {
84
            $this->onEnd($dataResult);     
85
        });
86
    }
87
88
    private function onEnd($dataResult)
89
    {
90
        if ($dataResult === null) return $this->emit('end');
91
92
        if ($this->isJson()) $this->paseJson($dataResult);
0 ignored issues
show
Bug introduced by
The method paseJson() does not exist on CapMousse\ReactRestify\Http\Request. Did you maybe mean parseJson()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
93
        else $this->parseStr($dataResult);
94
    }
95
96
    /**
97
     * Parse querystring
98
     * @param  string $dataString
99
     * @return void
100
     */
101
    private function parseStr($dataString)
102
    {
103
        $data = [];
104
        parse_str($dataString, $data);
105
106
        $this->setContent($dataString);
107
        $this->setData($data);
0 ignored issues
show
Bug introduced by
It seems like $data can also be of type null; however, CapMousse\ReactRestify\Http\Request::setData() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
108
        $this->emit('end');
109
    }
110
111
    /**
112
     * Parse json string
113
     * @param  string $jsonString 
114
     * @return void
115
     */
116
    private function parseJson($jsonString)
117
    {
118
        $jsonData = json_decode($jsonString, true);
119
120
        if ($jsonData === null) {
121
            $this->emit('error', [json_last_error_msg()]);
122
            return;
123
        }
124
125
        $this->setContent($jsonString);
126
        $this->setData($jsonData);
127
        $this->emit('end');
128
    }
129
130
    /**
131
     * Check if current request is a json request
132
     * @return boolean
133
     */
134
    public function isJson()
135
    {
136
        $header = $this->getHeaders();
0 ignored issues
show
Unused Code introduced by
$header 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...
137
138
        return isset($headers['content-type']) && $headers['content-type'] == 'application/json';
0 ignored issues
show
Bug introduced by
The variable $headers does not exist. Did you mean $header?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
139
    }
140
141
    /**
142
     * Set the data array
143
     * @param array $data array of data
144
     */
145
    public function setData($data)
146
    {
147
        $this->data = array_merge($data, $this->data);
148
    }
149
150
    /**
151
     * Get the data array
152
     * @return array
153
     */
154
    public function getData()
155
    {
156
        return $this->data;
157
    }
158
159
    public function __get($name)
160
    {
161
        return isset($this->data[$name]) ? $this->data[$name] : false;
162
    }
163
164
    public function __set($name, $value)
165
    {
166
        $this->data[$name] = $value;
167
    }
168
}
169