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 |
||
12 | final class Driver implements \Bernard\Driver |
||
13 | { |
||
14 | private $messages; |
||
15 | private $queues; |
||
16 | |||
17 | /** |
||
18 | * @param MongoCollection $queues Collection where queues will be stored |
||
19 | * @param MongoCollection $messages Collection where messages will be stored |
||
20 | */ |
||
21 | public function __construct(Collection $queues, Collection $messages) |
||
26 | |||
27 | /** |
||
28 | * {@inheritdoc} |
||
29 | */ |
||
30 | public function listQueues() |
||
34 | |||
35 | /** |
||
36 | * {@inheritdoc} |
||
37 | */ |
||
38 | public function createQueue($queueName) |
||
45 | |||
46 | /** |
||
47 | * {@inheritdoc} |
||
48 | */ |
||
49 | public function countMessages($queueName) |
||
56 | |||
57 | /** |
||
58 | * {@inheritdoc} |
||
59 | */ |
||
60 | View Code Duplication | public function pushMessage($queueName, $message) |
|
71 | |||
72 | /** |
||
73 | * {@inheritdoc} |
||
74 | */ |
||
75 | View Code Duplication | public function popMessage($queueName, $duration = 5) |
|
95 | |||
96 | /** |
||
97 | * {@inheritdoc} |
||
98 | */ |
||
99 | public function acknowledgeMessage($queueName, $receipt) |
||
106 | |||
107 | /** |
||
108 | * {@inheritdoc} |
||
109 | */ |
||
110 | public function peekQueue($queueName, $index = 0, $limit = 20) |
||
129 | |||
130 | /** |
||
131 | * {@inheritdoc} |
||
132 | */ |
||
133 | public function removeQueue($queueName) |
||
138 | |||
139 | /** |
||
140 | * {@inheritdoc} |
||
141 | */ |
||
142 | public function info() |
||
149 | } |
||
150 |
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.