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 |
||
17 | class ExchangeEntity implements PublisherInterface, AMQPEntityInterface |
||
18 | { |
||
19 | /** |
||
20 | * @const int Retry count when a Channel Closed exeption is thrown |
||
21 | */ |
||
22 | const MAX_RETRIES = 3; |
||
23 | |||
24 | /** |
||
25 | * @const array Default connections parameters |
||
26 | */ |
||
27 | const DEFAULTS = [ |
||
28 | 'exchange_type' => 'topic', |
||
29 | // Whether to check if it exists or to verify existance using argument types (Throws PRECONDITION_FAILED) |
||
30 | 'passive' => false, |
||
31 | // Entities with durable will be re-created uppon server restart |
||
32 | 'durable' => false, |
||
33 | // Whether to delete it when no queues ar bind to it |
||
34 | 'auto_delete' => false, |
||
35 | // Whether the exchange can be used by a publisher or block it (declared just for internal "wiring") |
||
36 | 'internal' => false, |
||
37 | // Whether to receive a Declare confirmation |
||
38 | 'nowait' => false, |
||
39 | // Whether to auto create the entity before publishing/consuming it |
||
40 | 'auto_create' => false, |
||
41 | // whether to "hide" the exception on re-declare. |
||
42 | // if the `passive` filter is set true, this is redundant |
||
43 | 'throw_exception_on_redeclare' => true, |
||
44 | // whether to throw on exception when trying to |
||
45 | // bind to an in-existent queue/exchange |
||
46 | 'throw_exception_on_bind_fail' => true, |
||
47 | ]; |
||
48 | |||
49 | /** |
||
50 | * @var AMQPConnection |
||
51 | */ |
||
52 | protected $connection; |
||
53 | |||
54 | /** |
||
55 | * @var string |
||
56 | */ |
||
57 | protected $aliasName; |
||
58 | |||
59 | /** |
||
60 | * @var array |
||
61 | */ |
||
62 | protected $attributes; |
||
63 | |||
64 | /** |
||
65 | * @var int |
||
66 | */ |
||
67 | protected $retryCount = 0; |
||
68 | |||
69 | /** |
||
70 | * ExchangeEntity constructor. |
||
71 | * |
||
72 | * @param AMQPConnection $connection |
||
73 | * @param string $aliasName |
||
74 | * @param array $attributes |
||
75 | */ |
||
76 | 13 | public function __construct(AMQPConnection $connection, string $aliasName, array $attributes = []) |
|
82 | |||
83 | /** |
||
84 | * @param AMQPConnection $connection |
||
85 | * @param string $aliasName |
||
86 | * @param array $exchangeDetails |
||
87 | * @return ExchangeEntity |
||
88 | */ |
||
89 | 13 | public static function createExchange(AMQPConnection $connection, string $aliasName, array $exchangeDetails) |
|
97 | |||
98 | /** |
||
99 | * @return string |
||
100 | */ |
||
101 | 2 | public function getAliasName(): string |
|
105 | |||
106 | /** |
||
107 | * @return AMQPConnection |
||
108 | */ |
||
109 | 8 | protected function getConnection(): AMQPConnection |
|
113 | |||
114 | /** |
||
115 | * @return AMQPChannel |
||
116 | */ |
||
117 | 8 | protected function getChannel(): AMQPChannel |
|
121 | |||
122 | /** |
||
123 | * Create the Queue |
||
124 | */ |
||
125 | 4 | View Code Duplication | public function create() |
|
|||
126 | { |
||
127 | try { |
||
128 | 4 | $this->getChannel() |
|
129 | 4 | ->exchange_declare( |
|
130 | 4 | $this->attributes['name'], |
|
131 | 4 | $this->attributes['exchange_type'], |
|
132 | 4 | $this->attributes['passive'], |
|
133 | 4 | $this->attributes['durable'], |
|
134 | 4 | $this->attributes['auto_delete'], |
|
135 | 4 | $this->attributes['internal'], |
|
136 | 4 | $this->attributes['nowait'] |
|
137 | ); |
||
138 | } catch (AMQPProtocolChannelException $e) { |
||
139 | // 406 is a soft error triggered for precondition failure (when redeclaring with different parameters) |
||
140 | if (true === $this->attributes['throw_exception_on_redeclare'] || $e->amqp_reply_code !== 406) { |
||
141 | throw $e; |
||
142 | } |
||
143 | // a failure trigger channels closing process |
||
144 | $this->getConnection()->reconnect(); |
||
145 | } |
||
146 | 4 | } |
|
147 | |||
148 | /** |
||
149 | * @throws AMQPProtocolChannelException |
||
150 | */ |
||
151 | 5 | View Code Duplication | public function bind() |
173 | |||
174 | /** |
||
175 | * Delete the queue |
||
176 | */ |
||
177 | 1 | public function delete() |
|
181 | |||
182 | /** |
||
183 | * {@inheritdoc} |
||
184 | */ |
||
185 | public function reconnect() |
||
189 | |||
190 | /** |
||
191 | * Publish a message |
||
192 | * |
||
193 | * @param string $message |
||
194 | * @param string $routingKey |
||
195 | * @return mixed|void |
||
196 | * @throws AMQPProtocolChannelException |
||
197 | */ |
||
198 | 5 | View Code Duplication | public function publish(string $message, string $routingKey = '') |
223 | } |
||
224 |
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.