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 Channel 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 Channel, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
20 | class Channel |
||
21 | { |
||
22 | use EventHandlers; |
||
23 | |||
24 | /** |
||
25 | * The QOS prefetch count value |
||
26 | */ |
||
27 | const QOS_PREFETCH_COUNT = 3; |
||
28 | |||
29 | /** |
||
30 | * The QOS prefetch size value |
||
31 | */ |
||
32 | const QOS_PREFECTH_SIZE = 0; |
||
33 | |||
34 | /** |
||
35 | * This event raised on channel open |
||
36 | */ |
||
37 | const EVENT_ON_CHANNEL_OPEN_CALLBACK = 'event.on.channel.open.callback'; |
||
38 | |||
39 | /** |
||
40 | * This event raised on channel close |
||
41 | */ |
||
42 | const EVENT_ON_CHANNEL_CLOSE_CALLBACK = 'event.on.channel.close.callback'; |
||
43 | |||
44 | /** |
||
45 | * This event raised on channel consume ok frame |
||
46 | */ |
||
47 | const EVENT_ON_CHANNEL_CONSUMEOK_CALLBACK = 'event.on.channel.consumeOk.callback'; |
||
48 | |||
49 | /** |
||
50 | * This event raised on queue declare confirmation |
||
51 | */ |
||
52 | const EVENT_ON_CHANNEL_DECLARE_QUEUE_CALLBACK = 'event.channel.dispatch.declareQueue.callback'; |
||
53 | |||
54 | /** |
||
55 | * This event raised on queue delete confirmation |
||
56 | */ |
||
57 | const EVENT_ON_CHANNEL_DELETE_QUEUE_CALLBACK = 'event.on.channel.deleteQueue.callback'; |
||
58 | |||
59 | /** |
||
60 | * This event raised on queue purge confirmation |
||
61 | */ |
||
62 | const EVENT_ON_CHANNEL_PURGE_QUEUE_CALLBACK = 'event.on.channel.purgeQueue.callback'; |
||
63 | |||
64 | /** |
||
65 | * This event raised on queue bind confirmation |
||
66 | */ |
||
67 | const EVENT_ON_CHANNEL_BIND_QUEUE_CALLBACK = 'event.on.channel.bindQueue.callback'; |
||
68 | |||
69 | /** |
||
70 | * This event raised on queue unbind confirmation |
||
71 | */ |
||
72 | const EVENT_ON_CHANNEL_UNBIND_QUEUE_CALLBACK = 'event.on.channel.unbindQueue.callback'; |
||
73 | |||
74 | /** |
||
75 | * This event raised on BasicGet message income |
||
76 | */ |
||
77 | const EVENT_DISPATCH_MESSAGE = 'event.channel.dispatch.message'; |
||
78 | |||
79 | /** |
||
80 | * This event raised on BasicConsume message income |
||
81 | */ |
||
82 | const EVENT_DISPATCH_CONSUMER_MESSAGE = 'event.channel.dispatch.consumer.message'; |
||
83 | |||
84 | /** |
||
85 | * This event raised on exchange declare confirmation |
||
86 | */ |
||
87 | const EVENT_ON_CHANNEL_DECLARE_EXCHANGE_CALLBACK = 'event.channel.dispatch.declareExchange.callback'; |
||
88 | |||
89 | /** |
||
90 | * This event raised on exchange delete confirmation |
||
91 | */ |
||
92 | const EVENT_ON_CHANNEL_DELETE_EXCHANGE_CALLBACK = 'event.channel.dispatch.deleteExchange.callback'; |
||
93 | |||
94 | /** |
||
95 | * This event raised on exchange bind confirmation |
||
96 | */ |
||
97 | const EVENT_ON_CHANNEL_BIND_EXCHANGE_CALLBACK = 'event.channel.dispatch.bindExchange.callback'; |
||
98 | |||
99 | /** |
||
100 | * This event raised on exchange unbind confirmation |
||
101 | */ |
||
102 | const EVENT_ON_CHANNEL_UNBIND_EXCHANGE_CALLBACK = 'event.channel.dispatch.unbindExchange.callback'; |
||
103 | |||
104 | /** |
||
105 | * @var Connection |
||
106 | */ |
||
107 | protected $connection; |
||
108 | |||
109 | /** |
||
110 | * @var int |
||
111 | */ |
||
112 | protected $id; |
||
113 | |||
114 | /** |
||
115 | * @var array |
||
116 | */ |
||
117 | protected $consumers = []; |
||
118 | |||
119 | /** |
||
120 | * @var |
||
121 | */ |
||
122 | private $stack; |
||
123 | |||
124 | /** |
||
125 | * @var bool |
||
126 | */ |
||
127 | private $isConnected; |
||
128 | |||
129 | /** |
||
130 | * AMQPChannel constructor. |
||
131 | * @param Connection $connection |
||
132 | * @param callable|null $callback |
||
133 | * @throws \InvalidArgumentException |
||
134 | * @throws \PHPDaemon\Clients\AMQP\Driver\Protocol\Exception\AMQPProtocolException |
||
135 | * @throws \PHPDaemon\Clients\AMQP\Driver\Exception\AMQPConnectionException |
||
136 | */ |
||
137 | public function __construct(Connection $connection, callable $callback = null) |
||
189 | |||
190 | /** |
||
191 | * Dispatch incoming frame |
||
192 | * @param IncomingFrame $incomingFrame |
||
193 | * @throws \InvalidArgumentException |
||
194 | * @throws \PHPDaemon\Clients\AMQP\Driver\Protocol\Exception\AMQPProtocolException |
||
195 | */ |
||
196 | public function dispatch(IncomingFrame $incomingFrame) |
||
317 | |||
318 | /** |
||
319 | * Close the channel. |
||
320 | * |
||
321 | * @throws \InvalidArgumentException |
||
322 | * @throws \PHPDaemon\Clients\AMQP\Driver\Protocol\Exception\AMQPProtocolException |
||
323 | */ |
||
324 | public function close() |
||
333 | |||
334 | /** |
||
335 | * Check queue |
||
336 | * |
||
337 | * @param $name |
||
338 | * @param array $options |
||
339 | * @param callable|null $callback |
||
340 | */ |
||
341 | public function checkQueue($name, array $options = [], callable $callback = null) |
||
345 | |||
346 | /** |
||
347 | * DeclareQueue |
||
348 | * |
||
349 | * @param string $name a quque name |
||
350 | * @param array $options a queue options |
||
351 | * @param callable|null $callback |
||
352 | * @throws \InvalidArgumentException |
||
353 | * @throws \PHPDaemon\Clients\AMQP\Driver\Protocol\Exception\AMQPProtocolException |
||
354 | */ |
||
355 | public function declareQueue($name, array $options = [], callable $callback = null) |
||
380 | |||
381 | /** |
||
382 | * Delete Queue |
||
383 | * |
||
384 | * @param string $name a queue name |
||
385 | * @param array $options a options array |
||
386 | * @param callable|null $callback |
||
387 | * @throws \InvalidArgumentException |
||
388 | * @throws \PHPDaemon\Clients\AMQP\Driver\Protocol\Exception\AMQPProtocolException |
||
389 | */ |
||
390 | public function deleteQueue($name, array $options = [], callable $callback = null) |
||
404 | |||
405 | /** |
||
406 | * Purge queue messages |
||
407 | * |
||
408 | * @param string $name a queue name |
||
409 | * @param array $options a options array |
||
410 | * @param callable|null $callback |
||
411 | * @throws \InvalidArgumentException |
||
412 | * @throws \PHPDaemon\Clients\AMQP\Driver\Protocol\Exception\AMQPProtocolException |
||
413 | */ |
||
414 | View Code Duplication | public function purgeQueue($name, array $options = [], callable $callback = null) |
|
426 | |||
427 | /** |
||
428 | * Bind queue to exchange |
||
429 | * |
||
430 | * @param string $name a queue name |
||
431 | * @param string $exchangeName a exchange name |
||
432 | * @param string $routingKey a routing key |
||
433 | * @param array $options additional options |
||
434 | * @param callable|null $callback |
||
435 | * @throws \InvalidArgumentException |
||
436 | * @throws \PHPDaemon\Clients\AMQP\Driver\Protocol\Exception\AMQPProtocolException |
||
437 | */ |
||
438 | View Code Duplication | public function bindQueue($name, $exchangeName, $routingKey, array $options = [], callable $callback = null) |
|
457 | |||
458 | /** |
||
459 | * Unbind queue from exchange |
||
460 | * |
||
461 | * @param string $name a queue name |
||
462 | * @param string $exchangeName a exchange name |
||
463 | * @param string $routingKey a routing key |
||
464 | * @param callable|null $callback |
||
465 | * @throws \InvalidArgumentException |
||
466 | * @throws \PHPDaemon\Clients\AMQP\Driver\Protocol\Exception\AMQPProtocolException |
||
467 | */ |
||
468 | public function unbindQueue($name, $exchangeName, $routingKey, callable $callback = null) |
||
482 | |||
483 | /** |
||
484 | * Declare exchange |
||
485 | * |
||
486 | * @param string $name a name of exchange |
||
487 | * @param array $options exchange options |
||
488 | * @param callable|null $callback |
||
489 | * @throws \InvalidArgumentException |
||
490 | * @throws \PHPDaemon\Clients\AMQP\Driver\Protocol\Exception\AMQPProtocolException |
||
491 | */ |
||
492 | public function declareExchange($name, array $options = [], callable $callback = null) |
||
519 | |||
520 | /** |
||
521 | * Delete Exchange |
||
522 | * |
||
523 | * @param string $name a exchange name |
||
524 | * @param array $options a exchange options |
||
525 | * @param callable|null $callback |
||
526 | * @throws \InvalidArgumentException |
||
527 | * @throws \PHPDaemon\Clients\AMQP\Driver\Protocol\Exception\AMQPProtocolException |
||
528 | */ |
||
529 | View Code Duplication | public function deleteExchange($name, array $options = [], callable $callback = null) |
|
542 | |||
543 | /** |
||
544 | * Bind exchange |
||
545 | * |
||
546 | * @param string $name a source exchange name |
||
547 | * @param string $exchangeName a destination exchange name |
||
548 | * @param string $routingKey a routing key |
||
549 | * @param array $options |
||
550 | * @param callable|null $callback |
||
551 | * @throws \InvalidArgumentException |
||
552 | * @throws \PHPDaemon\Clients\AMQP\Driver\Protocol\Exception\AMQPProtocolException |
||
553 | * @throws \PHPDaemon\Clients\AMQP\Driver\Exception\AMQPChannelException |
||
554 | */ |
||
555 | View Code Duplication | public function bindExchange($name, $exchangeName, $routingKey, array $options = [], callable $callback = null) |
|
581 | |||
582 | /** |
||
583 | * Unbind exchange |
||
584 | * |
||
585 | * @param string $name a source exchange name |
||
586 | * @param string $exchangeName a destination exchange name |
||
587 | * @param string $routingKey a routing key |
||
588 | * @param array $options |
||
589 | * @param callable|null $callback |
||
590 | * @throws \InvalidArgumentException |
||
591 | * @throws \PHPDaemon\Clients\AMQP\Driver\Exception\AMQPChannelException |
||
592 | * @throws \PHPDaemon\Clients\AMQP\Driver\Protocol\Exception\AMQPProtocolException |
||
593 | */ |
||
594 | View Code Duplication | public function unbindExchange($name, $exchangeName, $routingKey, array $options = [], callable $callback = null) |
|
620 | |||
621 | /** |
||
622 | * Publish message to exchange |
||
623 | * |
||
624 | * @param string $content The message content |
||
625 | * @param string $exchangeName exchange name |
||
626 | * @param string $routingKey routing key |
||
627 | * @param array $options |
||
628 | * @throws \InvalidArgumentException |
||
629 | * @throws \PHPDaemon\Clients\AMQP\Driver\Protocol\Exception\AMQPProtocolException |
||
630 | */ |
||
631 | public function publish($content, $exchangeName, $routingKey, array $options = []) |
||
696 | |||
697 | /** |
||
698 | * Send message directly to queue |
||
699 | * |
||
700 | * @param string $content a message content |
||
701 | * @param string $name a queue name |
||
702 | * @param array $options |
||
703 | * @throws \InvalidArgumentException |
||
704 | * @throws \PHPDaemon\Clients\AMQP\Driver\Protocol\Exception\AMQPProtocolException |
||
705 | */ |
||
706 | public function sendToQueue($content, $name, array $options = []) |
||
710 | |||
711 | /** |
||
712 | * Bind a consumer to consume on message receive |
||
713 | * |
||
714 | * @param string $queueName a queue name |
||
715 | * @param array $options |
||
716 | * @param callable $callback |
||
717 | * @throws \InvalidArgumentException |
||
718 | * @throws \PHPDaemon\Clients\AMQP\Driver\Protocol\Exception\AMQPProtocolException |
||
719 | */ |
||
720 | public function consume($queueName, array $options = [], callable $callback) |
||
747 | |||
748 | /** |
||
749 | * Unbind consumer |
||
750 | * |
||
751 | * @param string $consumerTag |
||
752 | * @param array $options |
||
753 | * @throws \InvalidArgumentException |
||
754 | * @throws \PHPDaemon\Clients\AMQP\Driver\Protocol\Exception\AMQPProtocolException |
||
755 | */ |
||
756 | View Code Duplication | public function cancel($consumerTag, array $options = []) |
|
764 | |||
765 | /** |
||
766 | * get message from queue |
||
767 | * |
||
768 | * @param string $queueName a queue name |
||
769 | * @param array $options |
||
770 | * @param callable|null $callback |
||
771 | * @throws \InvalidArgumentException |
||
772 | * @throws \PHPDaemon\Clients\AMQP\Driver\Protocol\Exception\AMQPProtocolException |
||
773 | */ |
||
774 | View Code Duplication | public function get($queueName, array $options = [], callable $callback = null) |
|
789 | |||
790 | /** |
||
791 | * Ack message by delivery tag |
||
792 | * |
||
793 | * @param int $deliveryTag |
||
794 | * @param array $options |
||
795 | * @throws \InvalidArgumentException |
||
796 | * @throws \PHPDaemon\Clients\AMQP\Driver\Protocol\Exception\AMQPProtocolException |
||
797 | */ |
||
798 | View Code Duplication | public function ack($deliveryTag, array $options = []) |
|
806 | |||
807 | /** |
||
808 | * Nack message |
||
809 | * |
||
810 | * @param $deliveryTag |
||
811 | * @param array $options |
||
812 | * @throws \InvalidArgumentException |
||
813 | * @throws \PHPDaemon\Clients\AMQP\Driver\Protocol\Exception\AMQPProtocolException |
||
814 | */ |
||
815 | public function nack($deliveryTag, array $options = []) |
||
824 | |||
825 | /** |
||
826 | * Reject a message |
||
827 | * |
||
828 | * @param $deliveryTag |
||
829 | * @param array $options |
||
830 | * @throws \InvalidArgumentException |
||
831 | * @throws \PHPDaemon\Clients\AMQP\Driver\Protocol\Exception\AMQPProtocolException |
||
832 | */ |
||
833 | View Code Duplication | public function reject($deliveryTag, array $options = []) |
|
841 | |||
842 | /** |
||
843 | * Redeliver unacknowledged messages. |
||
844 | * |
||
845 | * @param bool $requeue |
||
846 | * @throws \InvalidArgumentException |
||
847 | * @throws \PHPDaemon\Clients\AMQP\Driver\Protocol\Exception\AMQPProtocolException |
||
848 | */ |
||
849 | public function recover($requeue = true) |
||
855 | |||
856 | /** |
||
857 | * @return mixed |
||
858 | */ |
||
859 | public function getId() |
||
863 | |||
864 | /** |
||
865 | * @return Connection |
||
866 | */ |
||
867 | public function getConnection() |
||
871 | |||
872 | /** |
||
873 | * @return bool |
||
874 | */ |
||
875 | public function isConnected() |
||
879 | |||
880 | /** |
||
881 | * @return $this |
||
882 | */ |
||
883 | private function triggerOneAndUnbind() |
||
898 | } |
||
899 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.