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 |
||
16 | class ExchangeEntity implements PublisherInterface, AMQPEntityInterface |
||
17 | { |
||
18 | /** |
||
19 | * @const array Default connections parameters |
||
20 | */ |
||
21 | const DEFAULTS = [ |
||
22 | 'exchange_type' => 'topic', |
||
23 | 'passive' => false, |
||
24 | 'durable' => false, |
||
25 | 'auto_delete' => false, |
||
26 | 'internal' => false, |
||
27 | 'nowait' => false, |
||
28 | 'auto_create' => false, |
||
29 | 'throw_exception_on_redeclare' => true, |
||
30 | 'throw_exception_on_bind_fail' => true, |
||
31 | ]; |
||
32 | |||
33 | /** |
||
34 | * @var AMQPConnection |
||
35 | */ |
||
36 | protected $connection; |
||
37 | |||
38 | /** |
||
39 | * @var string |
||
40 | */ |
||
41 | protected $aliasName; |
||
42 | |||
43 | /** |
||
44 | * @var array |
||
45 | */ |
||
46 | protected $attributes; |
||
47 | |||
48 | /** |
||
49 | * ExchangeEntity constructor. |
||
50 | * |
||
51 | * @param AMQPConnection $connection |
||
52 | * @param string $aliasName |
||
53 | * @param array $attributes |
||
54 | */ |
||
55 | 13 | public function __construct(AMQPConnection $connection, string $aliasName, array $attributes = []) |
|
61 | |||
62 | /** |
||
63 | * @param AMQPConnection $connection |
||
64 | * @param string $aliasName |
||
65 | * @param array $exchangeDetails |
||
66 | * @return ExchangeEntity |
||
67 | */ |
||
68 | 13 | public static function createExchange(AMQPConnection $connection, string $aliasName, array $exchangeDetails) |
|
76 | |||
77 | /** |
||
78 | * @return string |
||
79 | */ |
||
80 | 2 | public function getAliasName(): string |
|
84 | |||
85 | /** |
||
86 | * @return AMQPConnection |
||
87 | */ |
||
88 | 8 | protected function getConnection(): AMQPConnection |
|
92 | |||
93 | /** |
||
94 | * @return AMQPChannel |
||
95 | */ |
||
96 | 8 | protected function getChannel(): AMQPChannel |
|
100 | |||
101 | /** |
||
102 | * Create the Queue |
||
103 | */ |
||
104 | 4 | View Code Duplication | public function create() |
126 | |||
127 | /** |
||
128 | * @throws AMQPProtocolChannelException |
||
129 | */ |
||
130 | 3 | View Code Duplication | public function bind() |
152 | |||
153 | /** |
||
154 | * Delete the queue |
||
155 | */ |
||
156 | 1 | public function delete() |
|
160 | |||
161 | /** |
||
162 | * Publish a message |
||
163 | * |
||
164 | * @param string $message |
||
165 | * @param string $routingKey |
||
166 | * @return mixed|void |
||
167 | * @throws AMQPProtocolChannelException |
||
168 | */ |
||
169 | 3 | View Code Duplication | public function publish(string $message, string $routingKey = '') |
182 | } |
||
183 |
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.