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 |
||
8 | abstract class AbstractMessage implements MessageInterface |
||
9 | { |
||
10 | /** |
||
11 | * @var string|null |
||
12 | */ |
||
13 | protected $protocolVersion; |
||
14 | |||
15 | const PROTOCOL_VERSION_1_0 = '1.0'; |
||
16 | const PROTOCOL_VERSION_1_1 = '1.1'; |
||
17 | |||
18 | /** |
||
19 | * @var string[]|array |
||
20 | */ |
||
21 | protected $headers; |
||
22 | |||
23 | /** |
||
24 | * @var StreamInterface |
||
25 | */ |
||
26 | protected $body; |
||
27 | |||
28 | /** |
||
29 | * {@inheritdoc} |
||
30 | */ |
||
31 | public function getProtocolVersion(): string |
||
35 | |||
36 | /** |
||
37 | * {@inheritdoc} |
||
38 | */ |
||
39 | public function withProtocolVersion($version): self |
||
43 | |||
44 | /** |
||
45 | * {@inheritdoc} |
||
46 | */ |
||
47 | public function getHeaders(): array |
||
51 | |||
52 | /** |
||
53 | * {@inheritdoc} |
||
54 | */ |
||
55 | public function hasHeader($name): bool |
||
59 | |||
60 | /** |
||
61 | * {@inheritdoc} |
||
62 | */ |
||
63 | public function getHeader($name): array |
||
71 | |||
72 | /** |
||
73 | * {@inheritdoc} |
||
74 | */ |
||
75 | public function getHeaderLine($name): string |
||
79 | |||
80 | /** |
||
81 | * {@inheritdoc} |
||
82 | */ |
||
83 | View Code Duplication | public function withHeader($name, $value): self |
|
92 | |||
93 | /** |
||
94 | * {@inheritdoc} |
||
95 | */ |
||
96 | View Code Duplication | public function withAddedHeader($name, $value) |
|
105 | |||
106 | /** |
||
107 | * {@inheritdoc} |
||
108 | */ |
||
109 | public function withoutHeader($name) |
||
118 | |||
119 | /** |
||
120 | * @return StreamInterface |
||
121 | */ |
||
122 | public function getBody(): StreamInterface |
||
126 | |||
127 | /** |
||
128 | * {@inheritdoc} |
||
129 | */ |
||
130 | public function withBody(StreamInterface $body) |
||
134 | |||
135 | /** |
||
136 | * @param array $parameters |
||
137 | * |
||
138 | * @return AbstractMessage |
||
139 | */ |
||
140 | abstract protected function with(array $parameters); |
||
141 | |||
142 | /** |
||
143 | * @param string $name |
||
144 | * |
||
145 | * @return string|null |
||
146 | */ |
||
147 | private function getOriginalHeaderName(string $name) |
||
162 | |||
163 | /** |
||
164 | * @param array|string $value |
||
165 | * @param array $originalValue |
||
166 | * |
||
167 | * @return array |
||
168 | */ |
||
169 | private function prepareHeader($value, array $originalValue = []): array |
||
173 | } |
||
174 |
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.