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 |
||
47 | class Channel implements ChannelInterface, WireSubscriberInterface, LoggerAwareInterface |
||
48 | { |
||
49 | use LoggerAwareTrait; |
||
50 | |||
51 | const STATUS_CLOSED = 0; |
||
52 | const STATUS_READY = 1; |
||
53 | const STATUS_INACTIVE = 2; |
||
54 | |||
55 | const MODE_NORMAL = 0; |
||
56 | const MODE_CONFIRM = 1; |
||
57 | const MODE_TX = 2; |
||
58 | |||
59 | /** |
||
60 | * @var int |
||
61 | */ |
||
62 | private $id; |
||
63 | |||
64 | /** |
||
65 | * @var WireInterface |
||
66 | */ |
||
67 | private $wire; |
||
68 | |||
69 | /** |
||
70 | * @var int |
||
71 | */ |
||
72 | private $status = self::STATUS_CLOSED; |
||
73 | |||
74 | /** |
||
75 | * @var int |
||
76 | */ |
||
77 | private $mode = self::MODE_NORMAL; |
||
78 | |||
79 | /** |
||
80 | * @var callable[] |
||
81 | */ |
||
82 | private $consumers = []; |
||
83 | |||
84 | /** |
||
85 | * @var callable |
||
86 | */ |
||
87 | private $returnCallable; |
||
88 | |||
89 | /** |
||
90 | * @var callable |
||
91 | */ |
||
92 | private $confirmCallable; |
||
93 | |||
94 | /** |
||
95 | * @param WireInterface $wire |
||
96 | * @param int $id |
||
97 | */ |
||
98 | 53 | public function __construct(WireInterface $wire, $id) |
|
104 | |||
105 | /** |
||
106 | * {@inheritdoc} |
||
107 | */ |
||
108 | 19 | public function open() |
|
124 | |||
125 | /** |
||
126 | * {@inheritdoc} |
||
127 | */ |
||
128 | 1 | public function flow($active) |
|
139 | |||
140 | /** |
||
141 | * {@inheritdoc} |
||
142 | */ |
||
143 | 1 | public function close() |
|
152 | |||
153 | /** |
||
154 | * {@inheritdoc} |
||
155 | */ |
||
156 | 2 | public function qos($prefetchSize, $prefetchCount, $globally = false) |
|
163 | |||
164 | /** |
||
165 | * {@inheritdoc} |
||
166 | */ |
||
167 | 4 | public function exchange($name) |
|
171 | |||
172 | /** |
||
173 | * {@inheritdoc} |
||
174 | */ |
||
175 | 14 | public function queue($name = '') |
|
179 | |||
180 | /** |
||
181 | * {@inheritdoc} |
||
182 | */ |
||
183 | 11 | public function consume($queue, callable $callback, $flags = 0, $tag = '', array $arguments = []) |
|
210 | |||
211 | /** |
||
212 | * {@inheritdoc} |
||
213 | */ |
||
214 | 7 | public function get($queue, $withAck = true) |
|
243 | |||
244 | /** |
||
245 | * {@inheritdoc} |
||
246 | */ |
||
247 | 2 | public function recover($requeue = true) |
|
254 | |||
255 | /** |
||
256 | * {@inheritdoc} |
||
257 | */ |
||
258 | 6 | public function cancel($tag, $flags = 0) |
|
272 | |||
273 | /** |
||
274 | * {@inheritdoc} |
||
275 | */ |
||
276 | 15 | public function publish(Message $message, $exchange = '', $routingKey = '', $flags = 0) |
|
294 | |||
295 | /** |
||
296 | * {@inheritdoc} |
||
297 | */ |
||
298 | 6 | public function ack($deliveryTag, $multiple = false) |
|
304 | |||
305 | /** |
||
306 | * {@inheritdoc} |
||
307 | */ |
||
308 | 4 | public function reject($deliveryTag, $requeue = true, $multiple = false) |
|
315 | |||
316 | /** |
||
317 | * {@inheritdoc} |
||
318 | */ |
||
319 | 2 | public function onReturn(callable $callable) |
|
325 | |||
326 | /** |
||
327 | * {@inheritdoc} |
||
328 | */ |
||
329 | 5 | public function selectConfirm(callable $callable, $noWait = false) |
|
343 | |||
344 | /** |
||
345 | * {@inheritdoc} |
||
346 | */ |
||
347 | 7 | public function selectTx() |
|
356 | |||
357 | /** |
||
358 | * {@inheritdoc} |
||
359 | */ |
||
360 | 5 | public function txCommit() |
|
371 | |||
372 | /** |
||
373 | * {@inheritdoc} |
||
374 | */ |
||
375 | 3 | public function txRollback() |
|
386 | |||
387 | /** |
||
388 | * {@inheritdoc} |
||
389 | */ |
||
390 | 10 | public function hasConsumer($tag) |
|
394 | |||
395 | /** |
||
396 | * {@inheritdoc} |
||
397 | */ |
||
398 | 3 | public function getConsumerTags() |
|
402 | |||
403 | /** |
||
404 | * @return string |
||
405 | */ |
||
406 | 3 | public function getStatus() |
|
410 | |||
411 | /** |
||
412 | * @return int |
||
413 | */ |
||
414 | public function getMode() |
||
418 | |||
419 | /** |
||
420 | * Sends frame to the server. |
||
421 | * |
||
422 | * @param Frame $frame |
||
423 | * |
||
424 | * @return $this |
||
425 | */ |
||
426 | 46 | private function send(Frame $frame) |
|
432 | |||
433 | /** |
||
434 | * @param string|array $type |
||
435 | * |
||
436 | * @return Frame |
||
437 | */ |
||
438 | 37 | private function wait($type) |
|
442 | |||
443 | /** |
||
444 | * @param Frame $frame |
||
445 | */ |
||
446 | 29 | public function dispatch(Frame $frame) |
|
476 | |||
477 | /** |
||
478 | * @param BasicDeliver $frame |
||
479 | * |
||
480 | * @throws \Exception |
||
481 | */ |
||
482 | 6 | private function onBasicDeliver(BasicDeliver $frame) |
|
512 | |||
513 | /** |
||
514 | * @param BasicReturn $frame |
||
515 | * |
||
516 | * @throws \Exception |
||
517 | */ |
||
518 | 4 | private function onBasicReturn(BasicReturn $frame) |
|
547 | |||
548 | /** |
||
549 | * @param BasicAck $frame |
||
550 | */ |
||
551 | 3 | View Code Duplication | private function onBasicAck(BasicAck $frame) |
561 | |||
562 | /** |
||
563 | * @param BasicNack $frame |
||
564 | */ |
||
565 | 2 | View Code Duplication | private function onBasicNack(BasicNack $frame) |
575 | |||
576 | /** |
||
577 | * @param BasicCancel $frame |
||
578 | */ |
||
579 | 3 | private function onBasicCancel(BasicCancel $frame) |
|
587 | |||
588 | /** |
||
589 | * @param ChannelFlow $frame |
||
590 | */ |
||
591 | 1 | private function onChannelFlow(ChannelFlow $frame) |
|
597 | |||
598 | /** |
||
599 | * @param ChannelClose $frame |
||
600 | * |
||
601 | * @throws AMQPException |
||
602 | */ |
||
603 | 3 | private function onChannelClose(ChannelClose $frame) |
|
611 | } |
||
612 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: