ServerRequest::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 9
nc 2
nop 10
dl 0
loc 19
ccs 10
cts 10
cp 1
crap 2
rs 9.9666
c 0
b 0
f 0

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 Zapheus\Bridge\Psr;
4
5
use Psr\Http\Message\ServerRequestInterface;
6
use Psr\Http\Message\StreamInterface;
7
use Psr\Http\Message\UploadedFileInterface;
8
use Psr\Http\Message\UriInterface;
9
10
/**
11
 * Server Request
12
 *
13
 * @package Zapheus
14
 * @author  Rougin Gutib <[email protected]>
15
 */
16
class ServerRequest extends Request implements ServerRequestInterface
17
{
18
    /**
19
     * @var array
20
     */
21
    protected $attributes = array();
22
23
    /**
24
     * @var array
25
     */
26
    protected $cookies = array();
27
28
    /**
29
     * @var array|null|object
30
     */
31
    protected $data;
32
33
    /**
34
     * @var array
35
     */
36
    protected $query = array();
37
38
    /**
39
     * @var array
40
     */
41
    protected $server = array();
42
43
    /**
44
     * @var array
45
     */
46
    protected $uploaded = array();
47
48
    /**
49
     * Initializes the server request instance.
50
     *
51
     * @param array                                  $server
52
     * @param array                                  $cookies
53
     * @param array                                  $query
54
     * @param array                                  $uploaded
55
     * @param array|null|object                      $data
56
     * @param array                                  $attributes
57
     * @param \Psr\Http\Message\UriInterface|null    $uri
58
     * @param \Psr\Http\Message\StreamInterface|null $body
59
     * @param array                                  $headers
60
     * @param string                                 $version
61
     */
62 81
    public function __construct(array $server, array $cookies = array(), array $query = array(), array $uploaded = array(), $data = null, array $attributes = array(), UriInterface $uri = null, StreamInterface $body = null, array $headers = array(), $version = '1.1')
63
    {
64 81
        $uri = $uri === null ? Uri::instance($server) : $uri;
65
66 81
        $target = (string) $server['REQUEST_URI'];
67
68 81
        parent::__construct($server['REQUEST_METHOD'], $target, $uri, $body, $headers, $version);
69
70 81
        $this->cookies = $cookies;
71
72 81
        $this->attributes = $attributes;
73
74 81
        $this->data = $data;
75
76 81
        $this->query = (array) $query;
77
78 81
        $this->server = $server;
79
80 81
        $this->uploaded = UploadedFile::normalize($uploaded);
81 81
    }
82
83
    /**
84
     * Retrieves a single derived request attribute.
85
     *
86
     * @param  string $name
87
     * @param  mixed  $default
88
     * @return mixed
89
     */
90 6
    public function getAttribute($name, $default = null)
91
    {
92 6
        return isset($this->attributes[$name]) ? $this->attributes[$name] : $default;
93
    }
94
95
    /**
96
     * Retrieve attributes derived from the request.
97
     *
98
     * @return array
99
     */
100 30
    public function getAttributes()
101
    {
102 30
        return $this->attributes;
103
    }
104
105
    /**
106
     * Retrieve cookies.
107
     *
108
     * @return array
109
     */
110 30
    public function getCookieParams()
111
    {
112 30
        return $this->cookies;
113
    }
114
115
    /**
116
     * Retrieve any parameters provided in the request body.
117
     *
118
     * @return array|null|object
119
     */
120 30
    public function getParsedBody()
121
    {
122 30
        return $this->data;
123
    }
124
125
    /**
126
     * Retrieve query string arguments.
127
     *
128
     * @return array
129
     */
130 30
    public function getQueryParams()
131
    {
132 30
        return $this->query;
133
    }
134
135
    /**
136
     * Retrieve server parameters.
137
     *
138
     * @return array
139
     */
140 30
    public function getServerParams()
141
    {
142 30
        return $this->server;
143
    }
144
145
    /**
146
     * Retrieve normalized file upload data.
147
     *
148
     * @return \Psr\Http\Message\UploadedFileInterface[]
149
     */
150 30
    public function getUploadedFiles()
151
    {
152 30
        return $this->uploaded;
153
    }
154
155
    /**
156
     * Returns an instance with the specified derived request attribute.
157
     *
158
     * @param  string $name
159
     * @param  mixed  $value
160
     * @return static
161
     */
162 6
    public function withAttribute($name, $value)
163
    {
164 6
        $static = clone $this;
165
166 6
        $static->attributes[$name] = $value;
167
168 6
        return $static;
169
    }
170
171
    /**
172
     * Returns an instance with the specified cookies.
173
     *
174
     * @param  array $cookies
175
     * @return static
176
     */
177 3
    public function withCookieParams(array $cookies)
178
    {
179 3
        $static = clone $this;
180
181 3
        $static->cookies = $cookies;
182
183 3
        return $static;
184
    }
185
186
    /**
187
     * Returns an instance with the specified body parameters.
188
     *
189
     * @param  null|array|object $data
190
     * @return static
191
     *
192
     * @throws \InvalidArgumentException
193
     */
194 3
    public function withParsedBody($data)
195
    {
196
        // TODO: Add \InvalidArgumentException
197
198 3
        $static = clone $this;
199
200 3
        $static->data = $data;
201
202 3
        return $static;
203
    }
204
205
    /**
206
     * Returns an instance with the specified query string arguments.
207
     *
208
     * @param  array $query
209
     * @return static
210
     */
211 3
    public function withQueryParams(array $query)
212
    {
213 3
        $static = clone $this;
214
215 3
        $static->query = $query;
216
217 3
        return $static;
218
    }
219
220
    /**
221
     * Create a new instance with the specified uploaded files.
222
     *
223
     * @param  \Psr\Http\Message\UploadedFileInterface[] $uploaded
224
     * @return static
225
     *
226
     * @throws \InvalidArgumentException
227
     */
228 3
    public function withUploadedFiles(array $uploaded)
229
    {
230
        // TODO: Add \InvalidArgumentException
231
232 3
        $static = clone $this;
233
234 3
        $static->uploaded = $uploaded;
235
236 3
        return $static;
237
    }
238
239
    /**
240
     * Returns an instance that removes the specified derived request attribute.
241
     *
242
     * @param  string $name
243
     * @return static
244
     */
245 3
    public function withoutAttribute($name)
246
    {
247 3
        $static = clone $this;
248
249 3
        unset($static->attributes[$name]);
250
251 3
        return $static;
252
    }
253
}
254