Passed
Pull Request — master (#10)
by Evgeniy
04:09 queued 01:08
created

ServerRequest::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 7
c 1
b 0
f 0
nc 1
nop 10
dl 0
loc 19
ccs 8
cts 8
cp 1
crap 1
rs 10

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace HttpSoft\Message;
6
7
use InvalidArgumentException;
8
use Psr\Http\Message\ServerRequestInterface;
9
use Psr\Http\Message\StreamInterface;
10
use Psr\Http\Message\UploadedFileInterface;
11
use Psr\Http\Message\UriInterface;
12
13
use function array_key_exists;
14
use function gettype;
15
use function get_class;
16
use function is_array;
17
use function is_object;
18
use function sprintf;
19
20
final class ServerRequest implements ServerRequestInterface
21
{
22
    use RequestTrait;
23
24
    /**
25
     * @var array
26
     */
27
    private array $attributes = [];
28
29
    /**
30
     * @var array
31
     */
32
    private array $cookieParams;
33
34
    /**
35
     * @var array|object|null
36
     */
37
    private $parsedBody;
38
39
    /**
40
     * @var array
41
     */
42
    private array $queryParams;
43
44
    /**
45
     * @var array
46
     */
47
    private array $serverParams;
48
49
    /**
50
     * @var array
51
     */
52
    private array $uploadedFiles;
53
54
    /**
55
     * @param array $serverParams
56
     * @param array $uploadedFiles
57
     * @param array $cookieParams
58
     * @param array $queryParams
59
     * @param array|object|null $parsedBody
60
     * @param string $method
61
     * @param UriInterface|string $uri
62
     * @param array $headers
63
     * @param StreamInterface|string|resource|null $body
64
     * @param string $protocol
65
     */
66 58
    public function __construct(
67
        array $serverParams = [],
68
        array $uploadedFiles = [],
69
        array $cookieParams = [],
70
        array $queryParams = [],
71
        $parsedBody = null,
72
        string $method = 'GET',
73
        $uri = '',
74
        array $headers = [],
75
        $body = null,
76
        string $protocol = '1.1'
77
    ) {
78 58
        $this->validateUploadedFiles($uploadedFiles);
79 58
        $this->uploadedFiles = $uploadedFiles;
80 58
        $this->serverParams = $serverParams;
81 58
        $this->cookieParams = $cookieParams;
82 58
        $this->queryParams = $queryParams;
83 58
        $this->parsedBody = $parsedBody;
84 58
        $this->init($method, $uri, $headers, $body, $protocol);
85 58
    }
86
87
    /**
88
     * {@inheritDoc}
89
     */
90 3
    public function getServerParams(): array
91
    {
92 3
        return $this->serverParams;
93
    }
94
95
    /**
96
     * {@inheritDoc}
97
     */
98 5
    public function getCookieParams(): array
99
    {
100 5
        return $this->cookieParams;
101
    }
102
103
    /**
104
     * {@inheritDoc}
105
     */
106 2
    public function withCookieParams(array $cookies): ServerRequestInterface
107
    {
108 2
        $new = clone $this;
109 2
        $new->cookieParams = $cookies;
110 2
        return $new;
111
    }
112
113
    /**
114
     * {@inheritDoc}
115
     */
116 4
    public function getQueryParams(): array
117
    {
118 4
        return $this->queryParams;
119
    }
120
121
    /**
122
     * {@inheritDoc}
123
     */
124 2
    public function withQueryParams(array $query): ServerRequestInterface
125
    {
126 2
        $new = clone $this;
127 2
        $new->queryParams = $query;
128 2
        return $new;
129
    }
130
131
    /**
132
     * {@inheritDoc}
133
     */
134 4
    public function getUploadedFiles(): array
135
    {
136 4
        return $this->uploadedFiles;
137
    }
138
139
    /**
140
     * {@inheritDoc}
141
     */
142 10
    public function withUploadedFiles(array $uploadedFiles): ServerRequestInterface
143
    {
144 10
        $this->validateUploadedFiles($uploadedFiles);
145 2
        $new = clone $this;
146 2
        $new->uploadedFiles = $uploadedFiles;
147 2
        return $new;
148
    }
149
150
    /**
151
     * {@inheritDoc}
152
     */
153 12
    public function getParsedBody()
154
    {
155 12
        return $this->parsedBody;
156
    }
157
158
    /**
159
     * {@inheritDoc}
160
     *
161
     * @psalm-suppress DocblockTypeContradiction
162
     */
163 15
    public function withParsedBody($data): ServerRequestInterface
164
    {
165 15
        if (!is_array($data) && !is_object($data) && $data !== null) {
166 9
            throw new InvalidArgumentException(sprintf(
167 9
                '"%s" is not valid Parsed Body. It must be a null, an array, or an object.',
168 9
                gettype($data)
169
            ));
170
        }
171
172 6
        $new = clone $this;
173 6
        $new->parsedBody = $data;
174 6
        return $new;
175
    }
176
177
    /**
178
     * {@inheritDoc}
179
     */
180 5
    public function getAttributes(): array
181
    {
182 5
        return $this->attributes;
183
    }
184
185
    /**
186
     * {@inheritDoc}
187
     */
188 5
    public function getAttribute($name, $default = null)
189
    {
190 5
        if (array_key_exists($name, $this->attributes)) {
191 4
            return $this->attributes[$name];
192
        }
193
194 4
        return $default;
195
    }
196
197
    /**
198
     * {@inheritDoc}
199
     */
200 5
    public function withAttribute($name, $value): ServerRequestInterface
201
    {
202 5
        if (array_key_exists($name, $this->attributes) && $this->attributes[$name] === $value) {
203
            return $this;
204
        }
205
206 5
        $new = clone $this;
207 5
        $new->attributes[$name] = $value;
208 5
        return $new;
209
    }
210
211
    /**
212
     * {@inheritDoc}
213
     */
214 2
    public function withoutAttribute($name): ServerRequestInterface
215
    {
216 2
        if (!array_key_exists($name, $this->attributes)) {
217
            return $this;
218
        }
219
220 2
        $new = clone $this;
221 2
        unset($new->attributes[$name]);
222 2
        return $new;
223
    }
224
225
    /**
226
     * @param array $uploadedFiles
227
     * @throws InvalidArgumentException
228
     * @psalm-suppress MixedAssignment
229
     */
230 58
    private function validateUploadedFiles(array $uploadedFiles): void
231
    {
232 58
        foreach ($uploadedFiles as $file) {
233 10
            if (is_array($file)) {
234
                $this->validateUploadedFiles($file);
235
                continue;
236
            }
237
238 10
            if (!$file instanceof UploadedFileInterface) {
239 8
                throw new InvalidArgumentException(sprintf(
240
                    'Invalid item in uploaded files structure.'
241 8
                    . '"%s" is not an instance of "\Psr\Http\Message\UploadedFileInterface".',
242 8
                    (is_object($file) ? get_class($file) : gettype($file))
243
                ));
244
            }
245
        }
246 58
    }
247
}
248