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 |
||
7 | View Code Duplication | class QueueDeclarer |
|
1 ignored issue
–
show
|
|||
8 | { |
||
9 | /** |
||
10 | * @var array |
||
11 | */ |
||
12 | private $declaredQueues = []; |
||
13 | |||
14 | /** |
||
15 | * @var Connection |
||
16 | */ |
||
17 | private $connection; |
||
18 | |||
19 | /** |
||
20 | * @var ChannelInterface |
||
21 | */ |
||
22 | private $channel; |
||
23 | |||
24 | 1 | public function __construct(Connection $connection, string $channelId = '') |
|
29 | |||
30 | /* |
||
31 | * This method will be run each time we attempt to queue a message. |
||
32 | * We will cache locally which queues we have already declared. |
||
33 | * Declaring a queue on each iteration of a worker consuming from |
||
34 | * a queue is really slow. |
||
35 | */ |
||
36 | 1 | public function declareQueue(Queue $queue) |
|
43 | } |
||
44 |
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.