DaikonRequest   A
last analyzed

Complexity

Total Complexity 41

Size/Duplication

Total Lines 245
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 47
dl 0
loc 245
ccs 0
cts 82
cp 0
rs 9.1199
c 1
b 0
f 0
wmc 41

41 Methods

Rating   Name   Duplication   Size   Complexity  
A getUri() 0 3 1
A unwrap() 0 3 1
A getStatusCode() 0 3 1
A getHeader() 0 3 1
A withCookieParams() 0 3 1
A withUri() 0 3 1
A withStatusCode() 0 3 1
A withAttribute() 0 3 1
A getAttribute() 0 3 1
A getResponder() 0 3 1
A withRequestTarget() 0 3 1
A getAttributes() 0 3 1
A getMethod() 0 3 1
A withHeader() 0 3 1
A getBody() 0 3 1
A getProtocolVersion() 0 3 1
A withMethod() 0 3 1
A withoutAttribute() 0 3 1
A withParsedBody() 0 3 1
A getErrors() 0 3 1
A getQueryParams() 0 3 1
A withPayload() 0 3 1
A withResponder() 0 3 1
A __construct() 0 3 1
A withUploadedFiles() 0 3 1
A getPayload() 0 3 1
A getHeaders() 0 3 1
A withProtocolVersion() 0 3 1
A getParsedBody() 0 3 1
A getUploadedFiles() 0 3 1
A withBody() 0 3 1
A hasHeader() 0 3 1
A getHeaderLine() 0 3 1
A wrap() 0 3 1
A withErrors() 0 3 1
A withoutHeader() 0 3 1
A withAddedHeader() 0 3 1
A getRequestTarget() 0 3 1
A getCookieParams() 0 3 1
A withQueryParams() 0 3 1
A getServerParams() 0 3 1

How to fix   Complexity   

Complex Class

Complex classes like DaikonRequest 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 DaikonRequest, and based on these observations, apply Extract Interface, too.

1
<?php declare(strict_types=1);
2
/**
3
 * This file is part of the daikon-cqrs/boot project.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
9
namespace Daikon\Boot\Middleware\Action;
10
11
use Psr\Http\Message\ServerRequestInterface;
12
use Psr\Http\Message\StreamInterface;
13
use Psr\Http\Message\UriInterface;
14
15
class DaikonRequest implements ServerRequestInterface
16
{
17
    public const ERRORS = '_errors';
18
    public const PAYLOAD = '_payload';
19
    public const STATUS_CODE = '_status_code';
20
    public const RESPONDER = '_responder';
21
22
    protected ServerRequestInterface $serverRequest;
23
24
    private function __construct(ServerRequestInterface $serverRequest)
25
    {
26
        $this->serverRequest = $serverRequest;
27
    }
28
29
    /** @return static */
30
    public static function wrap(ServerRequestInterface $serverRequest): self
31
    {
32
        return new self($serverRequest);
33
    }
34
35
    public function unwrap(): ServerRequestInterface
36
    {
37
        return $this->serverRequest;
38
    }
39
40
    /**
41
     * @param mixed $errors
42
     * @return static
43
     */
44
    public function withErrors($errors): self
45
    {
46
        return static::wrap($this->serverRequest->withAttribute(self::ERRORS, $errors));
47
    }
48
49
    /**
50
     * @param mixed $default
51
     * @return mixed
52
     */
53
    public function getErrors($default = null)
54
    {
55
        return $this->serverRequest->getAttribute(self::ERRORS, $default);
56
    }
57
58
    /**
59
     * @param mixed $payload
60
     * @return static
61
     */
62
    public function withPayload($payload): self
63
    {
64
        return static::wrap($this->serverRequest->withAttribute(self::PAYLOAD, $payload));
65
    }
66
67
    /**
68
     * @param mixed $default
69
     * @return mixed
70
     */
71
    public function getPayload($default = null)
72
    {
73
        return $this->serverRequest->getAttribute(self::PAYLOAD, $default);
74
    }
75
76
    /**
77
     * @param mixed $responder
78
     * @return static
79
     */
80
    public function withResponder($responder): self
81
    {
82
        return static::wrap($this->serverRequest->withAttribute(self::RESPONDER, $responder));
83
    }
84
85
    /**
86
     * @param mixed $default
87
     * @return mixed
88
     */
89
    public function getResponder($default = null)
90
    {
91
        return $this->serverRequest->getAttribute(self::RESPONDER, $default);
92
    }
93
94
    /**
95
     * @param mixed $statusCode
96
     * @return static
97
     */
98
    public function withStatusCode($statusCode): self
99
    {
100
        return static::wrap($this->serverRequest->withAttribute(self::STATUS_CODE, $statusCode));
101
    }
102
103
    /**
104
     * @param mixed $default
105
     * @return mixed
106
     */
107
    public function getStatusCode($default = null)
108
    {
109
        return $this->getAttribute(self::STATUS_CODE, $default);
110
    }
111
112
    public function getServerParams()
113
    {
114
        return $this->serverRequest->getServerParams();
115
    }
116
117
    public function getCookieParams()
118
    {
119
        return $this->serverRequest->getCookieParams();
120
    }
121
122
    public function withCookieParams(array $cookies)
123
    {
124
        return static::wrap($this->serverRequest->withCookieParams($cookies));
125
    }
126
127
    public function getQueryParams()
128
    {
129
        return $this->serverRequest->getQueryParams();
130
    }
131
132
    public function withQueryParams(array $query)
133
    {
134
        return static::wrap($this->serverRequest->withQueryParams($query));
135
    }
136
137
    public function getUploadedFiles()
138
    {
139
        return $this->serverRequest->getUploadedFiles();
140
    }
141
142
    public function withUploadedFiles(array $uploadedFiles)
143
    {
144
        return static::wrap($this->serverRequest->withUploadedFiles($uploadedFiles));
145
    }
146
147
    public function getParsedBody()
148
    {
149
        return $this->serverRequest->getParsedBody();
150
    }
151
152
    public function withParsedBody($data)
153
    {
154
        return static::wrap($this->serverRequest->withParsedBody($data));
155
    }
156
157
    public function getAttributes()
158
    {
159
        return $this->serverRequest->getAttributes();
160
    }
161
162
    public function getAttribute($name, $default = null)
163
    {
164
        return $this->serverRequest->getAttribute($name, $default);
165
    }
166
167
    public function withAttribute($name, $value)
168
    {
169
        return static::wrap($this->serverRequest->withAttribute($name, $value));
170
    }
171
172
    public function withoutAttribute($name)
173
    {
174
        return static::wrap($this->serverRequest->withoutAttribute($name));
175
    }
176
177
    public function getRequestTarget()
178
    {
179
        return $this->serverRequest->getRequestTarget();
180
    }
181
182
    public function withRequestTarget($requestTarget)
183
    {
184
        return static::wrap($this->serverRequest->withRequestTarget($requestTarget));
185
    }
186
187
    public function getMethod()
188
    {
189
        return $this->serverRequest->getMethod();
190
    }
191
192
    public function withMethod($method)
193
    {
194
        return static::wrap($this->serverRequest->withMethod($method));
195
    }
196
197
    public function getUri()
198
    {
199
        return $this->serverRequest->getUri();
200
    }
201
202
    public function withUri(UriInterface $uri, $preserveHost = false)
203
    {
204
        return static::wrap($this->serverRequest->withUri($uri, $preserveHost));
205
    }
206
207
    public function getProtocolVersion()
208
    {
209
        return $this->serverRequest->getProtocolVersion();
210
    }
211
212
    public function withProtocolVersion($version)
213
    {
214
        return static::wrap($this->serverRequest->withProtocolVersion($version));
215
    }
216
217
    public function getHeaders()
218
    {
219
        return $this->serverRequest->getHeaders();
220
    }
221
222
    public function hasHeader($name)
223
    {
224
        return $this->serverRequest->hasHeader($name);
225
    }
226
227
    public function getHeader($name)
228
    {
229
        return $this->serverRequest->getHeader($name);
230
    }
231
232
    public function getHeaderLine($name)
233
    {
234
        return $this->serverRequest->getHeaderLine($name);
235
    }
236
237
    public function withHeader($name, $value)
238
    {
239
        return static::wrap($this->serverRequest->withHeader($name, $value));
240
    }
241
242
    public function withAddedHeader($name, $value)
243
    {
244
        return static::wrap($this->serverRequest->withAddedHeader($name, $value));
245
    }
246
247
    public function withoutHeader($name)
248
    {
249
        return static::wrap($this->serverRequest->withoutHeader($name));
250
    }
251
252
    public function getBody()
253
    {
254
        return $this->serverRequest->getBody();
255
    }
256
257
    public function withBody(StreamInterface $body)
258
    {
259
        return static::wrap($this->serverRequest->withBody($body));
260
    }
261
}
262