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 |
||
20 | class QueueEntity implements PublisherInterface, ConsumerInterface, LoggerAwareInterface |
||
21 | { |
||
22 | use LoggerAwareTrait; |
||
23 | |||
24 | /** |
||
25 | * @const array Default connections parameters |
||
26 | */ |
||
27 | const DEFAULTS = [ |
||
28 | 'passive' => false, |
||
29 | 'durable' => false, |
||
30 | 'exclusive' => false, |
||
31 | 'auto_delete' => false, |
||
32 | 'internal' => false, |
||
33 | 'nowait' => false, |
||
34 | ]; |
||
35 | |||
36 | /** |
||
37 | * @var AMQPConnection |
||
38 | */ |
||
39 | protected $connection; |
||
40 | |||
41 | /** |
||
42 | * @var string |
||
43 | */ |
||
44 | protected $aliasName; |
||
45 | |||
46 | /** |
||
47 | * @var array |
||
48 | */ |
||
49 | protected $attributes; |
||
50 | |||
51 | /** |
||
52 | * @var int |
||
53 | */ |
||
54 | protected $prefetchCount = 1; |
||
55 | |||
56 | /** |
||
57 | * @var null|string|MessageProcessorInterface |
||
58 | */ |
||
59 | protected $messageProcessor = null; |
||
60 | |||
61 | /** |
||
62 | * @var int |
||
63 | */ |
||
64 | protected $limitMessageCount; |
||
65 | |||
66 | /** |
||
67 | * @var int |
||
68 | */ |
||
69 | protected $limitSecondsUptime; |
||
70 | |||
71 | /** |
||
72 | * @var int |
||
73 | */ |
||
74 | protected $limitMemoryConsumption; |
||
75 | |||
76 | /** |
||
77 | * @var double |
||
78 | */ |
||
79 | protected $startTime = 0; |
||
80 | |||
81 | /** |
||
82 | * @param AMQPConnection $connection |
||
83 | * @param string $aliasName |
||
84 | * @param array $exchangeDetails |
||
85 | * @return QueueEntity |
||
86 | */ |
||
87 | 11 | public static function createQueue(AMQPConnection $connection, string $aliasName, array $exchangeDetails) |
|
95 | |||
96 | /** |
||
97 | * @return string |
||
98 | */ |
||
99 | 3 | public function getAliasName(): string |
|
103 | |||
104 | /** |
||
105 | * ExchangeEntity constructor. |
||
106 | * |
||
107 | * @param AMQPConnection $connection |
||
108 | * @param string $aliasName |
||
109 | * @param array $attributes |
||
110 | */ |
||
111 | 11 | public function __construct(AMQPConnection $connection, string $aliasName, array $attributes = []) |
|
117 | |||
118 | /** |
||
119 | * @param int $prefetchCount |
||
120 | * @return ConsumerInterface |
||
121 | */ |
||
122 | 2 | public function setPrefetchCount(int $prefetchCount): ConsumerInterface |
|
127 | |||
128 | /** |
||
129 | * @param string $messageProcessor |
||
130 | * @return ConsumerInterface |
||
131 | */ |
||
132 | 2 | public function setMessageProcessor(string $messageProcessor): ConsumerInterface |
|
137 | |||
138 | /** |
||
139 | * @return AMQPConnection |
||
140 | */ |
||
141 | 4 | protected function getConnection(): AMQPConnection |
|
145 | |||
146 | /** |
||
147 | * @return AMQPChannel |
||
148 | */ |
||
149 | 4 | protected function getChannel(): AMQPChannel |
|
153 | |||
154 | /** |
||
155 | * Create the Queue |
||
156 | */ |
||
157 | 1 | public function create() |
|
158 | { |
||
159 | 1 | $this->getChannel() |
|
160 | 1 | ->queue_declare( |
|
161 | 1 | $this->attributes['name'], |
|
162 | 1 | $this->attributes['passive'], |
|
163 | 1 | $this->attributes['durable'], |
|
164 | 1 | $this->attributes['exclusive'], |
|
165 | 1 | $this->attributes['auto_delete'], |
|
166 | 1 | $this->attributes['internal'], |
|
167 | 1 | $this->attributes['nowait'] |
|
168 | ); |
||
169 | 1 | } |
|
170 | |||
171 | 1 | View Code Duplication | public function bind() |
184 | |||
185 | /** |
||
186 | * Delete the queue |
||
187 | */ |
||
188 | 1 | public function delete() |
|
192 | |||
193 | |||
194 | /** |
||
195 | * Publish a message |
||
196 | * |
||
197 | * @param string $message |
||
198 | * @param string $routingKey |
||
199 | * @return void |
||
200 | */ |
||
201 | 1 | public function publish(string $message, string $routingKey = '') |
|
211 | |||
212 | /** |
||
213 | * {@inheritdoc} |
||
214 | * |
||
215 | * @param int $messages |
||
216 | * @param int $seconds |
||
217 | * @param int $maxMemory |
||
218 | * @return int |
||
219 | */ |
||
220 | public function startConsuming(int $messages, int $seconds, int $maxMemory) |
||
245 | |||
246 | /** |
||
247 | * @return bool |
||
248 | */ |
||
249 | protected function shouldStopConsuming(): bool |
||
281 | |||
282 | /** |
||
283 | * Stop the consumer |
||
284 | */ |
||
285 | public function stopConsuming() |
||
290 | |||
291 | /** |
||
292 | * Setup the consumer |
||
293 | * |
||
294 | * @param int $messages |
||
295 | * @param int $seconds |
||
296 | * @param int $maxMemory |
||
297 | */ |
||
298 | protected function setupConsumer(int $messages, int $seconds, int $maxMemory) |
||
311 | |||
312 | private function setupChannelConsumer() |
||
331 | |||
332 | /** |
||
333 | * Handle shutdown - Usually in case "Allowed memory size of x bytes exhausted" |
||
334 | */ |
||
335 | private function registerShutdownHandler() |
||
342 | |||
343 | /** |
||
344 | * Register signals |
||
345 | */ |
||
346 | private function handleKillSignals() |
||
359 | |||
360 | /** |
||
361 | * Handle Kill Signals |
||
362 | * @param int $signalNumber |
||
363 | */ |
||
364 | private function catchKillSignal(int $signalNumber) |
||
369 | |||
370 | /** |
||
371 | * It is the tag that is listed in RabbitMQ UI as the consumer "name" |
||
372 | * |
||
373 | * @return string |
||
374 | */ |
||
375 | private function getConsumerTag(): string |
||
379 | |||
380 | /** |
||
381 | * @return MessageProcessorInterface |
||
382 | */ |
||
383 | private function getMessageProcessor(): MessageProcessorInterface |
||
390 | |||
391 | /** |
||
392 | * @param AMQPMessage $message |
||
393 | * @throws \Throwable |
||
394 | */ |
||
395 | public function consume(AMQPMessage $message) |
||
415 | } |
||
416 |
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.