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:
Complex classes like QueueEntity often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use QueueEntity, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
22 | class QueueEntity implements PublisherInterface, ConsumerInterface, AMQPEntityInterface, LoggerAwareInterface |
||
23 | { |
||
24 | use LoggerAwareTrait; |
||
25 | |||
26 | /** |
||
27 | * @const array Default connections parameters |
||
28 | */ |
||
29 | const DEFAULTS = [ |
||
30 | // Whether to check if it exists or to verify existance using argument types (Throws PRECONDITION_FAILED) |
||
31 | 'passive' => false, |
||
32 | // Entities with durable will be re-created uppon server restart |
||
33 | 'durable' => false, |
||
34 | // whether to use it by only one channel, then it gets deleted |
||
35 | 'exclusive' => false, |
||
36 | // Whether to delete it when the queue has no event on it |
||
37 | 'auto_delete' => false, |
||
38 | // Whether the exchange can be used by a publisher or block it (declared just for internal "wiring") |
||
39 | 'internal' => false, |
||
40 | // Whether to receive a Declare confirmation |
||
41 | 'nowait' => false, |
||
42 | // Whether to auto create the entity before publishing/consuming it |
||
43 | 'auto_create' => false, |
||
44 | // whether to "hide" the exception on re-declare. |
||
45 | // if the `passive` filter is set true, this is redundant |
||
46 | 'throw_exception_on_redeclare' => true, |
||
47 | // whether to throw on exception when trying to |
||
48 | // bind to an in-existent queue/exchange |
||
49 | 'throw_exception_on_bind_fail' => true, |
||
50 | ]; |
||
51 | |||
52 | /** |
||
53 | * @var AMQPConnection |
||
54 | */ |
||
55 | protected $connection; |
||
56 | |||
57 | /** |
||
58 | * @var string |
||
59 | */ |
||
60 | protected $aliasName; |
||
61 | |||
62 | /** |
||
63 | * @var array |
||
64 | */ |
||
65 | protected $attributes; |
||
66 | |||
67 | /** |
||
68 | * @var int |
||
69 | */ |
||
70 | protected $prefetchCount = 1; |
||
71 | |||
72 | /** |
||
73 | * @var null|string|MessageProcessorInterface |
||
74 | */ |
||
75 | protected $messageProcessor = null; |
||
76 | |||
77 | /** |
||
78 | * @var int |
||
79 | */ |
||
80 | protected $limitMessageCount; |
||
81 | |||
82 | /** |
||
83 | * @var int |
||
84 | */ |
||
85 | protected $limitSecondsUptime; |
||
86 | |||
87 | /** |
||
88 | * @var int |
||
89 | */ |
||
90 | protected $limitMemoryConsumption; |
||
91 | |||
92 | /** |
||
93 | * @var double |
||
94 | */ |
||
95 | protected $startTime = 0; |
||
96 | |||
97 | /** |
||
98 | * @param AMQPConnection $connection |
||
99 | * @param string $aliasName |
||
100 | * @param array $exchangeDetails |
||
101 | * @return QueueEntity |
||
102 | */ |
||
103 | 17 | public static function createQueue(AMQPConnection $connection, string $aliasName, array $exchangeDetails) |
|
111 | |||
112 | /** |
||
113 | * @return string |
||
114 | */ |
||
115 | 3 | public function getAliasName(): string |
|
119 | |||
120 | /** |
||
121 | * ExchangeEntity constructor. |
||
122 | * |
||
123 | * @param AMQPConnection $connection |
||
124 | * @param string $aliasName |
||
125 | * @param array $attributes |
||
126 | */ |
||
127 | 17 | public function __construct(AMQPConnection $connection, string $aliasName, array $attributes = []) |
|
133 | |||
134 | /** |
||
135 | * @param int $prefetchCount |
||
136 | * @return ConsumerInterface |
||
137 | */ |
||
138 | 2 | public function setPrefetchCount(int $prefetchCount): ConsumerInterface |
|
143 | |||
144 | /** |
||
145 | * @param string $messageProcessor |
||
146 | * @return ConsumerInterface |
||
147 | */ |
||
148 | 2 | public function setMessageProcessor(string $messageProcessor): ConsumerInterface |
|
153 | |||
154 | /** |
||
155 | * @return AMQPConnection |
||
156 | */ |
||
157 | 8 | protected function getConnection(): AMQPConnection |
|
161 | |||
162 | /** |
||
163 | * @return AMQPChannel |
||
164 | */ |
||
165 | 8 | protected function getChannel(): AMQPChannel |
|
169 | |||
170 | /** |
||
171 | * Create the Queue |
||
172 | */ |
||
173 | 4 | View Code Duplication | public function create() |
195 | |||
196 | 4 | View Code Duplication | public function bind() |
218 | |||
219 | /** |
||
220 | * Delete the queue |
||
221 | */ |
||
222 | 1 | public function delete() |
|
226 | |||
227 | /** |
||
228 | * {@inheritdoc} |
||
229 | */ |
||
230 | 1 | public function reconnect() |
|
234 | |||
235 | /** |
||
236 | * Publish a message |
||
237 | * |
||
238 | * @param string $message |
||
239 | * @param string $routingKey |
||
240 | * @return mixed|void |
||
241 | * @throws AMQPProtocolChannelException |
||
242 | */ |
||
243 | 2 | View Code Duplication | public function publish(string $message, string $routingKey = '') |
257 | |||
258 | /** |
||
259 | * {@inheritdoc} |
||
260 | * |
||
261 | * @param int $messages |
||
262 | * @param int $seconds |
||
263 | * @param int $maxMemory |
||
264 | * @return int |
||
265 | */ |
||
266 | public function startConsuming(int $messages, int $seconds, int $maxMemory) |
||
290 | |||
291 | /** |
||
292 | * @return bool |
||
293 | */ |
||
294 | protected function shouldStopConsuming(): bool |
||
326 | |||
327 | /** |
||
328 | * Stop the consumer |
||
329 | */ |
||
330 | public function stopConsuming() |
||
338 | |||
339 | /** |
||
340 | * Setup the consumer |
||
341 | * |
||
342 | * @param int $messages |
||
343 | * @param int $seconds |
||
344 | * @param int $maxMemory |
||
345 | */ |
||
346 | protected function setupConsumer(int $messages, int $seconds, int $maxMemory) |
||
359 | |||
360 | private function setupChannelConsumer() |
||
384 | |||
385 | /** |
||
386 | * Handle shutdown - Usually in case "Allowed memory size of x bytes exhausted" |
||
387 | */ |
||
388 | private function registerShutdownHandler() |
||
395 | |||
396 | /** |
||
397 | * Register signals |
||
398 | */ |
||
399 | protected function handleKillSignals() |
||
411 | |||
412 | /** |
||
413 | * Handle Kill Signals |
||
414 | * @param int $signalNumber |
||
415 | */ |
||
416 | public function catchKillSignal(int $signalNumber) |
||
421 | |||
422 | /** |
||
423 | * It is the tag that is listed in RabbitMQ UI as the consumer "name" |
||
424 | * |
||
425 | * @return string |
||
426 | */ |
||
427 | private function getConsumerTag(): string |
||
431 | |||
432 | /** |
||
433 | * @return MessageProcessorInterface |
||
434 | */ |
||
435 | 1 | private function getMessageProcessor(): MessageProcessorInterface |
|
445 | |||
446 | /** |
||
447 | * @param AMQPMessage $message |
||
448 | * @throws \Throwable |
||
449 | */ |
||
450 | 1 | public function consume(AMQPMessage $message) |
|
470 | } |
||
471 |
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.