Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
14 | final class ServerRequest implements ServerRequestInterface |
||
15 | { |
||
16 | use MessageTrait; |
||
17 | use RequestTrait; |
||
18 | |||
19 | /** @var array */ |
||
20 | private $attributes = []; |
||
21 | |||
22 | /** @var array */ |
||
23 | private $cookieParams = []; |
||
24 | |||
25 | /** @var array|object|null */ |
||
26 | private $parsedBody; |
||
27 | |||
28 | /** @var array */ |
||
29 | private $queryParams = []; |
||
30 | |||
31 | /** @var array */ |
||
32 | private $serverParams; |
||
33 | |||
34 | /** @var UploadedFileInterface[] */ |
||
35 | private $uploadedFiles = []; |
||
36 | |||
37 | /** |
||
38 | * @param string $method HTTP method |
||
39 | * @param string|UriInterface $uri URI |
||
40 | * @param array $headers Request headers |
||
41 | * @param string|resource|StreamInterface|null $body Request body |
||
42 | * @param string $version Protocol version |
||
43 | * @param array $serverParams Typically the $_SERVER superglobal |
||
44 | */ |
||
45 | 45 | View Code Duplication | public function __construct(string $method, $uri, array $headers = [], $body = null, string $version = '1.1', array $serverParams = []) |
67 | |||
68 | 3 | public function getServerParams(): array |
|
72 | |||
73 | 3 | public function getUploadedFiles(): array |
|
77 | |||
78 | 2 | public function withUploadedFiles(array $uploadedFiles) |
|
85 | |||
86 | 4 | public function getCookieParams(): array |
|
90 | |||
91 | 2 | public function withCookieParams(array $cookies) |
|
98 | |||
99 | 3 | public function getQueryParams(): array |
|
103 | |||
104 | 2 | public function withQueryParams(array $query) |
|
111 | |||
112 | 5 | public function getParsedBody() |
|
116 | |||
117 | 8 | public function withParsedBody($data) |
|
128 | |||
129 | 3 | public function getAttributes(): array |
|
133 | |||
134 | 4 | View Code Duplication | public function getAttribute($attribute, $default = null) |
142 | |||
143 | 5 | public function withAttribute($attribute, $value): self |
|
150 | |||
151 | 3 | View Code Duplication | public function withoutAttribute($attribute): self |
162 | } |
||
163 |