Completed
Push — master ( e74872...1684aa )
by Maurício
01:57 queued 25s
created

ServerRequest   D

Complexity

Total Complexity 58

Size/Duplication

Total Lines 359
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 78
dl 0
loc 359
ccs 0
cts 96
cp 0
rs 4.5599
c 0
b 0
f 0
wmc 58

35 Methods

Rating   Name   Duplication   Size   Complexity  
A withCookieParams() 0 5 1
A withParsedBody() 0 5 1
A getHeader() 0 3 1
A getServerParams() 0 3 1
A withoutAttribute() 0 5 1
A withProtocolVersion() 0 5 1
A withUri() 0 5 1
A withoutHeader() 0 5 1
A withQueryParams() 0 5 1
A withMethod() 0 5 1
A getHeaders() 0 3 1
A getHeaderLine() 0 3 1
A getParsedBodyParam() 0 13 5
A withHeader() 0 5 1
A withBody() 0 5 1
A withAttribute() 0 5 1
A isPost() 0 3 1
A getBody() 0 3 1
A withRequestTarget() 0 5 1
A withUploadedFiles() 0 5 1
A withAddedHeader() 0 5 1
A getMethod() 0 3 1
A getCookieParams() 0 3 1
C getRoute() 0 28 15
A getQueryParams() 0 3 1
A hasHeader() 0 3 1
A getRequestTarget() 0 3 1
A getParam() 0 18 6
A getProtocolVersion() 0 3 1
A getAttribute() 0 3 1
A __construct() 0 3 1
A getAttributes() 0 3 1
A getUri() 0 3 1
A getParsedBody() 0 3 1
A getUploadedFiles() 0 3 1

How to fix   Complexity   

Complex Class

Complex classes like ServerRequest often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use ServerRequest, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpMyAdmin\Http;
6
7
use Psr\Http\Message\ServerRequestInterface;
8
use Psr\Http\Message\StreamInterface;
9
use Psr\Http\Message\UriInterface;
10
11
use function is_array;
12
use function is_object;
13
use function is_string;
14
use function property_exists;
15
16
class ServerRequest implements ServerRequestInterface
17
{
18
    /** @var ServerRequestInterface */
19
    private $serverRequest;
20
21
    final public function __construct(ServerRequestInterface $serverRequest)
22
    {
23
        $this->serverRequest = $serverRequest;
24
    }
25
26
    /**
27
     * @inheritDoc
28
     */
29
    public function getProtocolVersion()
30
    {
31
        return $this->serverRequest->getProtocolVersion();
32
    }
33
34
    /**
35
     * @inheritDoc
36
     */
37
    public function withProtocolVersion($version)
38
    {
39
        $serverRequest = $this->serverRequest->withProtocolVersion($version);
40
41
        return new static($serverRequest);
42
    }
43
44
    /**
45
     * @inheritDoc
46
     */
47
    public function getHeaders()
48
    {
49
        return $this->serverRequest->getHeaders();
50
    }
51
52
    /**
53
     * @inheritDoc
54
     */
55
    public function hasHeader($name)
56
    {
57
        return $this->serverRequest->hasHeader($name);
58
    }
59
60
    /**
61
     * @inheritDoc
62
     */
63
    public function getHeader($name)
64
    {
65
        return $this->serverRequest->getHeader($name);
66
    }
67
68
    /**
69
     * @inheritDoc
70
     */
71
    public function getHeaderLine($name)
72
    {
73
        return $this->serverRequest->getHeaderLine($name);
74
    }
75
76
    /**
77
     * @inheritDoc
78
     */
79
    public function withHeader($name, $value)
80
    {
81
        $serverRequest = $this->serverRequest->withHeader($name, $value);
82
83
        return new static($serverRequest);
84
    }
85
86
    /**
87
     * @inheritDoc
88
     */
89
    public function withAddedHeader($name, $value)
90
    {
91
        $serverRequest = $this->serverRequest->withAddedHeader($name, $value);
92
93
        return new static($serverRequest);
94
    }
95
96
    /**
97
     * @inheritDoc
98
     */
99
    public function withoutHeader($name)
100
    {
101
        $serverRequest = $this->serverRequest->withoutHeader($name);
102
103
        return new static($serverRequest);
104
    }
105
106
    /**
107
     * @inheritDoc
108
     */
109
    public function getBody()
110
    {
111
        return $this->serverRequest->getBody();
112
    }
113
114
    /**
115
     * @inheritDoc
116
     */
117
    public function withBody(StreamInterface $body)
118
    {
119
        $serverRequest = $this->serverRequest->withBody($body);
120
121
        return new static($serverRequest);
122
    }
123
124
    /**
125
     * @inheritDoc
126
     */
127
    public function getRequestTarget()
128
    {
129
        return $this->serverRequest->getRequestTarget();
130
    }
131
132
    /**
133
     * @inheritDoc
134
     */
135
    public function withRequestTarget($requestTarget)
136
    {
137
        $serverRequest = $this->serverRequest->withRequestTarget($requestTarget);
138
139
        return new static($serverRequest);
140
    }
141
142
    /**
143
     * @inheritDoc
144
     */
145
    public function getMethod()
146
    {
147
        return $this->serverRequest->getMethod();
148
    }
149
150
    /**
151
     * @inheritDoc
152
     */
153
    public function withMethod($method)
154
    {
155
        $serverRequest = $this->serverRequest->withMethod($method);
156
157
        return new static($serverRequest);
158
    }
159
160
    /**
161
     * @inheritDoc
162
     */
163
    public function getUri()
164
    {
165
        return $this->serverRequest->getUri();
166
    }
167
168
    /**
169
     * @inheritDoc
170
     */
171
    public function withUri(UriInterface $uri, $preserveHost = false)
172
    {
173
        $serverRequest = $this->serverRequest->withUri($uri, $preserveHost);
174
175
        return new static($serverRequest);
176
    }
177
178
    /**
179
     * @inheritDoc
180
     */
181
    public function getServerParams()
182
    {
183
        return $this->serverRequest->getServerParams();
184
    }
185
186
    /**
187
     * @inheritDoc
188
     */
189
    public function getCookieParams()
190
    {
191
        return $this->serverRequest->getCookieParams();
192
    }
193
194
    /**
195
     * @inheritDoc
196
     */
197
    public function withCookieParams(array $cookies)
198
    {
199
        $serverRequest = $this->serverRequest->withCookieParams($cookies);
200
201
        return new static($serverRequest);
202
    }
203
204
    /**
205
     * @inheritDoc
206
     */
207
    public function getQueryParams()
208
    {
209
        return $this->serverRequest->getQueryParams();
210
    }
211
212
    /**
213
     * @inheritDoc
214
     */
215
    public function withQueryParams(array $query)
216
    {
217
        $serverRequest = $this->serverRequest->withQueryParams($query);
218
219
        return new static($serverRequest);
220
    }
221
222
    /**
223
     * @inheritDoc
224
     */
225
    public function getUploadedFiles()
226
    {
227
        return $this->serverRequest->getUploadedFiles();
228
    }
229
230
    /**
231
     * @inheritDoc
232
     */
233
    public function withUploadedFiles(array $uploadedFiles)
234
    {
235
        $serverRequest = $this->serverRequest->withUploadedFiles($uploadedFiles);
236
237
        return new static($serverRequest);
238
    }
239
240
    /**
241
     * @inheritDoc
242
     */
243
    public function getParsedBody()
244
    {
245
        return $this->serverRequest->getParsedBody();
246
    }
247
248
    /**
249
     * @inheritDoc
250
     */
251
    public function withParsedBody($data)
252
    {
253
        $serverRequest = $this->serverRequest->withParsedBody($data);
254
255
        return new static($serverRequest);
256
    }
257
258
    /**
259
     * @inheritDoc
260
     */
261
    public function getAttributes()
262
    {
263
        return $this->serverRequest->getAttributes();
264
    }
265
266
    /**
267
     * @inheritDoc
268
     */
269
    public function getAttribute($name, $default = null)
270
    {
271
        return $this->serverRequest->getAttribute($name, $default);
272
    }
273
274
    /**
275
     * @inheritDoc
276
     */
277
    public function withAttribute($name, $value)
278
    {
279
        $serverRequest = $this->serverRequest->withAttribute($name, $value);
280
281
        return new static($serverRequest);
282
    }
283
284
    /**
285
     * @inheritDoc
286
     */
287
    public function withoutAttribute($name)
288
    {
289
        $serverRequest = $this->serverRequest->withoutAttribute($name);
290
291
        return new static($serverRequest);
292
    }
293
294
    /**
295
     * @param mixed $default
296
     *
297
     * @return mixed
298
     */
299
    public function getParam(string $param, $default = null)
300
    {
301
        $getParams = $this->getQueryParams();
302
        $postParams = $this->getParsedBody();
303
304
        if (is_array($postParams) && isset($postParams[$param])) {
305
            return $postParams[$param];
306
        }
307
308
        if (is_object($postParams) && property_exists($postParams, $param)) {
309
            return $postParams->$param;
310
        }
311
312
        if (isset($getParams[$param])) {
313
            return $getParams[$param];
314
        }
315
316
        return $default;
317
    }
318
319
    /**
320
     * @param mixed $default
321
     *
322
     * @return mixed
323
     */
324
    public function getParsedBodyParam(string $param, $default = null)
325
    {
326
        $postParams = $this->getParsedBody();
327
328
        if (is_array($postParams) && isset($postParams[$param])) {
329
            return $postParams[$param];
330
        }
331
332
        if (is_object($postParams) && property_exists($postParams, $param)) {
333
            return $postParams->$param;
334
        }
335
336
        return $default;
337
    }
338
339
    public function isPost(): bool
340
    {
341
        return $this->getMethod() === 'POST';
342
    }
343
344
    /**
345
     * @psalm-return non-empty-string
346
     */
347
    public function getRoute(): string
348
    {
349
        $getParams = $this->getQueryParams();
350
        $postParams = $this->getParsedBody();
351
        $route = '/';
352
        if (isset($getParams['route']) && is_string($getParams['route']) && $getParams['route'] !== '') {
353
            $route = $getParams['route'];
354
        } elseif (
355
            is_array($postParams)
356
            && isset($postParams['route'])
357
            && is_string($postParams['route'])
358
            && $postParams['route'] !== ''
359
        ) {
360
            $route = $postParams['route'];
361
        }
362
363
        /**
364
         * See FAQ 1.34.
365
         *
366
         * @see https://docs.phpmyadmin.net/en/latest/faq.html#faq1-34
367
         */
368
        $db = isset($getParams['db']) && is_string($getParams['db']) ? $getParams['db'] : '';
369
        if ($route === '/' && $db !== '') {
370
            $table = isset($getParams['table']) && is_string($getParams['table']) ? $getParams['table'] : '';
371
            $route = $table === '' ? '/database/structure' : '/sql';
372
        }
373
374
        return $route;
375
    }
376
}
377