Completed
Push — master ( bc5bb1...0df69c )
by Aurimas
02:42
created

ServerRequest::getParsedBody()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
namespace Thruster\Component\Http;
4
5
use GuzzleHttp\Psr7\Request;
6
use InvalidArgumentException;
7
use Psr\Http\Message\ServerRequestInterface;
8
use Psr\Http\Message\StreamInterface;
9
use Psr\Http\Message\UploadedFileInterface;
10
11
/**
12
 * Class ServerRequest
13
 *
14
 * @package Thruster\Component\Http
15
 * @author  Aurimas Niekis <[email protected]>
16
 */
17
class ServerRequest extends Request implements ServerRequestInterface
18
{
19
    /**
20
     * @var array|object|string
21
     */
22
    protected $parsedBody;
23
24
    /**
25
     * @var array
26
     */
27
    protected $serverParams;
28
29
    /**
30
     * @var array
31
     */
32
    protected $attributes;
33
34
    /**
35
     * @var array
36
     */
37
    protected $cookieParams;
38
39
    /**
40
     * @var array
41
     */
42
    protected $queryParams;
43
44
    /**
45
     * @var UploadedFileInterface[]
46
     */
47
    protected $uploadedFiles;
48
49
    /**
50
     * @param array                           $serverParams  Server parameters, typically from $_SERVER
51
     * @param array                           $uploadedFiles Upload file information, a tree of UploadedFiles
52
     * @param string                          $uri           URI for the request, if any.
53
     * @param string                          $method        HTTP method for the request, if any.
54
     * @param string|resource|StreamInterface $body          Message body, if any.
55
     * @param array                           $headers       Headers for the message, if any.
56
     * @param array                           $cookies       Cookies for the message, if any.
57
     * @param array                           $queryParams   Query params for the message, if any.
58
     * @param array|object                    $parsedBody    The deserialized body parameters, if any.
59
     * @param string                          $protocol      HTTP protocol version.
0 ignored issues
show
Documentation introduced by
There is no parameter named $protocol. Did you maybe mean $protocolVersion?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

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

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

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

Loading history...
60
     *
61
     * @throws InvalidArgumentException for any invalid value.
62
     */
63 5
    public function __construct(
64
        array $serverParams = [],
65
        array $uploadedFiles = [],
66
        $uri = null,
67
        string $method = null,
68
        $body = null,
69
        array $headers = [],
70
        array $cookies = [],
71
        array $queryParams = [],
72
        $parsedBody = null,
73
        string $protocolVersion = '1.1'
74
    ) {
75 5
        $this->validateUploadedFiles($uploadedFiles);
76
77 5
        $this->attributes = [];
78
79 5
        $this->serverParams  = $serverParams;
80 5
        $this->uploadedFiles = $uploadedFiles;
81 5
        $this->cookieParams  = $cookies;
82 5
        $this->queryParams   = $queryParams;
83 5
        $this->parsedBody    = $parsedBody;
84
85 5
        parent::__construct($method, $uri, $headers, $body, $protocolVersion);
86 5
    }
87
88
    /**
89
     * @inheritDoc
90
     */
91 4
    public function getServerParams() : array
92
    {
93 4
        return $this->serverParams;
94
    }
95
96
    /**
97
     * @inheritDoc
98
     */
99 4
    public function getCookieParams() : array
100
    {
101 4
        return $this->cookieParams;
102
    }
103
104
    /**
105
     * @inheritDoc
106
     */
107
    public function withCookieParams(array $cookies)
108
    {
109
        $new = clone $this;
110
        $new->cookieParams = $cookies;
111
112
        return $new;
113
    }
114
115
    /**
116
     * @inheritDoc
117
     */
118 4
    public function getQueryParams() : array
119
    {
120 4
        return $this->queryParams;
121
    }
122
123
    /**
124
     * @inheritDoc
125
     */
126
    public function withQueryParams(array $query)
127
    {
128
        $new = clone $this;
129
        $new->queryParams = $query;
130
131
        return $new;
132
    }
133
134
    /**
135
     * @inheritDoc
136
     */
137 4
    public function getUploadedFiles() : array
138
    {
139 4
        return $this->uploadedFiles;
140
    }
141
142
    /**
143
     * @inheritDoc
144
     */
145
    public function withUploadedFiles(array $uploadedFiles)
146
    {
147
        $this->validateUploadedFiles($uploadedFiles);
148
149
        $new = clone $this;
150
        $new->uploadedFiles = $uploadedFiles;
151
152
        return $new;
153
    }
154
155
    /**
156
     * @inheritDoc
157
     */
158
    public function getParsedBody()
159
    {
160
        return $this->parsedBody;
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->parsedBody; of type array|object|string adds the type string to the return on line 160 which is incompatible with the return type declared by the interface Psr\Http\Message\ServerR...nterface::getParsedBody of type null|array|object.
Loading history...
161
    }
162
163
    /**
164
     * @inheritDoc
165
     */
166
    public function withParsedBody($data)
167
    {
168
        $new = clone $this;
169
        $new->parsedBody = $data;
170
171
        return $new;
172
    }
173
174
    /**
175
     * @inheritDoc
176
     */
177 1
    public function getAttributes() : array
178
    {
179 1
        return $this->attributes;
180
    }
181
182
    /**
183
     * @inheritDoc
184
     */
185 1
    public function getAttribute($name, $default = null)
186
    {
187 1
        return $this->attributes[$name] ?? $default;
188
    }
189
190
    /**
191
     * @inheritDoc
192
     */
193 1
    public function withAttribute($name, $value)
194
    {
195 1
        $new = clone $this;
196 1
        $new->attributes[$name] = $value;
197
198 1
        return $new;
199
    }
200
201
    /**
202
     * @inheritDoc
203
     */
204 1
    public function withoutAttribute($name)
205
    {
206 1
        $new = clone $this;
207 1
        unset($new->attributes[$name]);
208
209 1
        return $new;
210
    }
211
212
    /**
213
     * Recursively validate the structure in an uploaded files array.
214
     *
215
     * @param array $uploadedFiles
216
     *
217
     * @throws InvalidArgumentException if any leaf is not an UploadedFileInterface instance.
218
     */
219 5
    protected function validateUploadedFiles(array $uploadedFiles)
220
    {
221 5
        foreach ($uploadedFiles as $file) {
222
            if (is_array($file)) {
223
                $this->validateUploadedFiles($file);
224
                continue;
225
            }
226
227
            if (!$file instanceof UploadedFileInterface) {
228
                throw new InvalidArgumentException('Invalid leaf in uploaded files structure');
229
            }
230
        }
231 5
    }
232
}
233