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 |
||
9 | final class Request extends AbstractMessage implements RequestInterface |
||
10 | { |
||
11 | /** |
||
12 | * @var string|null |
||
13 | */ |
||
14 | private $requestTarget; |
||
15 | |||
16 | /** |
||
17 | * @var string|null |
||
18 | */ |
||
19 | private $method; |
||
20 | |||
21 | const METHOD_OPTIONS = 'OPTIONS'; |
||
22 | const METHOD_GET = 'GET'; |
||
23 | const METHOD_HEAD = 'HEAD'; |
||
24 | const METHOD_POST = 'POST'; |
||
25 | const METHOD_PUT = 'PUT'; |
||
26 | const METHOD_PATCH = 'PATCH'; |
||
27 | const METHOD_DELETE = 'DELETE'; |
||
28 | |||
29 | /** |
||
30 | * @var UriInterface |
||
31 | */ |
||
32 | private $uri; |
||
33 | |||
34 | /** |
||
35 | * @var RequestInterface|null |
||
36 | */ |
||
37 | protected $__previous; |
||
38 | |||
39 | /** |
||
40 | * @param UriInterface $uri |
||
41 | * @param string $method |
||
42 | * @param array $headers |
||
43 | * @param StreamInterface|null $body |
||
44 | * @param string $protocolVersion |
||
45 | * @param string|null $requestTarget |
||
46 | * @param RequestInterface|null $__previous |
||
47 | */ |
||
48 | public function __construct( |
||
65 | |||
66 | /** |
||
67 | * {@inheritdoc} |
||
68 | */ |
||
69 | public function getRequestTarget() |
||
85 | |||
86 | /** |
||
87 | * {@inheritdoc} |
||
88 | */ |
||
89 | public function withRequestTarget($requestTarget) |
||
93 | |||
94 | /** |
||
95 | * {@inheritdoc} |
||
96 | */ |
||
97 | public function getMethod() |
||
101 | |||
102 | /** |
||
103 | * {@inheritdoc} |
||
104 | */ |
||
105 | public function withMethod($method) |
||
109 | |||
110 | /** |
||
111 | * {@inheritdoc} |
||
112 | */ |
||
113 | public function getUri(): UriInterface |
||
117 | |||
118 | public function withUri(UriInterface $uri, $preserveHost = false) |
||
122 | |||
123 | /** |
||
124 | * @param array $parameters |
||
125 | * |
||
126 | * @return Request |
||
127 | */ |
||
128 | View Code Duplication | protected function with(array $parameters): self |
|
143 | } |
||
144 |
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.