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 |
||
| 16 | final class ServerRequest implements ServerRequestInterface |
||
| 17 | { |
||
| 18 | use MessageTrait; |
||
| 19 | use RequestTrait; |
||
| 20 | |||
| 21 | /** @var array */ |
||
| 22 | private $attributes = []; |
||
| 23 | |||
| 24 | /** @var array */ |
||
| 25 | private $cookieParams = []; |
||
| 26 | |||
| 27 | /** @var null|array|object */ |
||
| 28 | private $parsedBody; |
||
| 29 | |||
| 30 | /** @var array */ |
||
| 31 | private $queryParams = []; |
||
| 32 | |||
| 33 | /** @var array */ |
||
| 34 | private $serverParams; |
||
| 35 | |||
| 36 | /** @var UploadedFileInterface[] */ |
||
| 37 | private $uploadedFiles = []; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @param string $method HTTP method |
||
| 41 | * @param string|UriInterface $uri URI |
||
| 42 | * @param array $headers Request headers |
||
| 43 | * @param string|null|resource|StreamInterface $body Request body |
||
| 44 | * @param string $version Protocol version |
||
| 45 | * @param array $serverParams Typically the $_SERVER superglobal |
||
| 46 | */ |
||
| 47 | 53 | View Code Duplication | public function __construct( |
| 74 | |||
| 75 | 3 | public function getServerParams(): array |
|
| 79 | |||
| 80 | 10 | public function getUploadedFiles(): array |
|
| 84 | |||
| 85 | 9 | public function withUploadedFiles(array $uploadedFiles) |
|
| 92 | |||
| 93 | 5 | public function getCookieParams(): array |
|
| 97 | |||
| 98 | 10 | public function withCookieParams(array $cookies) |
|
| 105 | |||
| 106 | 4 | public function getQueryParams(): array |
|
| 110 | |||
| 111 | 10 | public function withQueryParams(array $query) |
|
| 118 | |||
| 119 | 6 | public function getParsedBody() |
|
| 123 | |||
| 124 | 16 | public function withParsedBody($data) |
|
| 135 | |||
| 136 | 3 | public function getAttributes(): array |
|
| 140 | |||
| 141 | 4 | View Code Duplication | public function getAttribute($attribute, $default = null) |
| 149 | |||
| 150 | 5 | public function withAttribute($attribute, $value): self |
|
| 157 | |||
| 158 | 3 | View Code Duplication | public function withoutAttribute($attribute): self |
| 169 | } |
||
| 170 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.