Completed
Push — master ( 9fe7e8...7331b7 )
by Aurimas
12:47
created

ServerRequest::__construct()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 19

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 24
rs 8.9714
cc 1
eloc 19
nc 1
nop 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
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
    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
        $this->validateUploadedFiles($uploadedFiles);
76
77
        $this->attributes = [];
78
79
        $this->serverParams  = $serverParams;
80
        $this->uploadedFiles = $uploadedFiles;
81
        $this->cookieParams  = $cookies;
82
        $this->queryParams   = $queryParams;
83
        $this->parsedBody    = $parsedBody;
84
85
        parent::__construct($method, $uri, $headers, $body, $protocolVersion);
86
    }
87
88
    /**
89
     * @inheritDoc
90
     */
91
    public function getServerParams() : array
92
    {
93
        return $this->serverParams;
94
    }
95
96
    /**
97
     * @inheritDoc
98
     */
99
    public function getCookieParams() : array
100
    {
101
        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
    public function getQueryParams() : array
119
    {
120
        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
    public function getUploadedFiles() : array
138
    {
139
        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
    public function getAttributes() : array
178
    {
179
        return $this->attributes;
180
    }
181
182
    /**
183
     * @inheritDoc
184
     */
185
    public function getAttribute($name, $default = null)
186
    {
187
        return $this->attributes[$name] ?? $default;
188
    }
189
190
    /**
191
     * @inheritDoc
192
     */
193
    public function withAttribute($name, $value)
194
    {
195
        $new = clone $this;
196
        $new->attributes[$name] = $value;
197
198
        return $new;
199
    }
200
201
    /**
202
     * @inheritDoc
203
     */
204
    public function withoutAttribute($name)
205
    {
206
        $new = clone $this;
207
        unset($new->attributes[$name]);
208
209
        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
    protected function validateUploadedFiles(array $uploadedFiles)
220
    {
221
        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
    }
232
}
233