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 |
||
19 | class Queue implements QueueInterface |
||
20 | { |
||
21 | const FLAG_NO_WAIT = 0b00000001; |
||
22 | const FLAG_DURABLE = 0b00000010; |
||
23 | const FLAG_PASSIVE = 0b00000100; |
||
24 | const FLAG_EXCLUSIVE = 0b00001000; |
||
25 | const FLAG_AUTO_DELETE = 0b00010000; |
||
26 | const FLAG_INTERNAL = 0b00100000; |
||
27 | const FLAG_IF_UNUSED = 0b01000000; |
||
28 | const FLAG_IF_EMPTY = 0b10000000; |
||
29 | |||
30 | /** |
||
31 | * @var WireInterface |
||
32 | */ |
||
33 | private $wire; |
||
34 | |||
35 | /** |
||
36 | * @var int |
||
37 | */ |
||
38 | private $channel; |
||
39 | |||
40 | /** |
||
41 | * @var string |
||
42 | */ |
||
43 | private $name; |
||
44 | |||
45 | /** |
||
46 | * @var int|null |
||
47 | */ |
||
48 | private $messageCount; |
||
49 | |||
50 | /** |
||
51 | * @var int|null |
||
52 | */ |
||
53 | private $consumerCount; |
||
54 | |||
55 | /** |
||
56 | * @param WireInterface $wire |
||
57 | * @param int $channel |
||
58 | * @param string $name |
||
59 | */ |
||
60 | 25 | public function __construct(WireInterface $wire, $channel, $name) |
|
66 | |||
67 | /** |
||
68 | * {@inheritdoc} |
||
69 | */ |
||
70 | 15 | public function define($flags = 0, array $arguments = []) |
|
97 | |||
98 | /** |
||
99 | * {@inheritdoc} |
||
100 | */ |
||
101 | 3 | public function delete($flags = 0) |
|
123 | |||
124 | /** |
||
125 | * {@inheritdoc} |
||
126 | */ |
||
127 | 4 | View Code Duplication | public function bind($exchange, $routingKey = '', array $arguments = [], $flags = 0) |
145 | |||
146 | /** |
||
147 | * {@inheritdoc} |
||
148 | */ |
||
149 | 1 | public function unbind($exchange, $routingKey = '', array $arguments = []) |
|
164 | |||
165 | /** |
||
166 | * {@inheritdoc} |
||
167 | */ |
||
168 | 2 | public function purge($flags = 0) |
|
188 | |||
189 | /** |
||
190 | * {@inheritdoc} |
||
191 | */ |
||
192 | 4 | public function name() |
|
196 | |||
197 | /** |
||
198 | * {@inheritdoc} |
||
199 | */ |
||
200 | 7 | public function messagesCount() |
|
204 | |||
205 | /** |
||
206 | * {@inheritdoc} |
||
207 | */ |
||
208 | 1 | public function consumerCount() |
|
212 | |||
213 | /** |
||
214 | * @return string |
||
215 | */ |
||
216 | 14 | public function __toString() |
|
220 | |||
221 | /** |
||
222 | * @param Frame $frame |
||
223 | * |
||
224 | * @return $this |
||
225 | */ |
||
226 | 22 | private function send(Frame $frame) |
|
232 | |||
233 | /** |
||
234 | * @param string|array $type |
||
235 | * |
||
236 | * @return Frame |
||
237 | */ |
||
238 | 18 | private function wait($type) |
|
242 | } |
||
243 |
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.