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 |
||
| 49 | class Channel implements ChannelInterface, WireSubscriberInterface |
||
| 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 | 56 | public function __construct(WireInterface $wire, $id) |
|
| 103 | |||
| 104 | /** |
||
| 105 | * {@inheritdoc} |
||
| 106 | */ |
||
| 107 | 20 | public function open() |
|
| 108 | { |
||
| 109 | 19 | if ($this->status != self::STATUS_CLOSED) { |
|
| 110 | 1 | return $this; |
|
| 111 | } |
||
| 112 | |||
| 113 | 19 | $this->wire->subscribe($this->id, $this); |
|
| 114 | 20 | $this->wire->send(new ChannelOpen($this->id, '')); |
|
| 115 | 19 | $this->wire->wait($this->id, ChannelOpenOk::class); |
|
| 116 | |||
| 117 | 19 | $this->status = self::STATUS_READY; |
|
| 118 | 19 | $this->mode = self::MODE_NORMAL; |
|
| 119 | |||
| 120 | 19 | return $this; |
|
| 121 | } |
||
| 122 | |||
| 123 | /** |
||
| 124 | * {@inheritdoc} |
||
| 125 | */ |
||
| 126 | 1 | public function flow($active) |
|
| 127 | { |
||
| 128 | 1 | $this->wire->send(new ChannelFlow($this->id, $active)); |
|
| 129 | |||
| 130 | /** @var ChannelFlowOk $frame */ |
||
| 131 | 1 | $frame = $this->wire->wait($this->id, ChannelFlowOk::class); |
|
| 132 | |||
| 133 | 1 | $this->status = $frame->isActive() ? self::STATUS_READY : |
|
| 134 | 1 | self::STATUS_INACTIVE; |
|
| 135 | |||
| 136 | 1 | return $this; |
|
| 137 | } |
||
| 138 | |||
| 139 | /** |
||
| 140 | * {@inheritdoc} |
||
| 141 | */ |
||
| 142 | 1 | public function serve($blocking = true) |
|
| 148 | |||
| 149 | /** |
||
| 150 | * {@inheritdoc} |
||
| 151 | */ |
||
| 152 | 1 | public function close() |
|
| 161 | |||
| 162 | /** |
||
| 163 | * {@inheritdoc} |
||
| 164 | */ |
||
| 165 | 1 | public function qos($prefetchSize, $prefetchCount, $globally = false) |
|
| 172 | |||
| 173 | /** |
||
| 174 | * {@inheritdoc} |
||
| 175 | */ |
||
| 176 | 4 | public function exchange($name) |
|
| 180 | |||
| 181 | /** |
||
| 182 | * {@inheritdoc} |
||
| 183 | */ |
||
| 184 | 14 | public function queue($name = '') |
|
| 188 | |||
| 189 | /** |
||
| 190 | * {@inheritdoc} |
||
| 191 | */ |
||
| 192 | 11 | public function consume($queue, callable $callback, $flags = 0, $tag = '', array $arguments = []) |
|
| 219 | |||
| 220 | /** |
||
| 221 | * {@inheritdoc} |
||
| 222 | */ |
||
| 223 | 7 | public function get($queue, $withAck = true) |
|
| 248 | |||
| 249 | /** |
||
| 250 | * {@inheritdoc} |
||
| 251 | */ |
||
| 252 | 2 | public function recover($requeue = true) |
|
| 259 | |||
| 260 | /** |
||
| 261 | * {@inheritdoc} |
||
| 262 | */ |
||
| 263 | 6 | public function cancel($tag, $flags = 0) |
|
| 277 | |||
| 278 | /** |
||
| 279 | * {@inheritdoc} |
||
| 280 | */ |
||
| 281 | 15 | public function publish(Message $message, $exchange = '', $routingKey = '', $flags = 0) |
|
| 299 | |||
| 300 | /** |
||
| 301 | * {@inheritdoc} |
||
| 302 | */ |
||
| 303 | 6 | public function ack($deliveryTag, $multiple = false) |
|
| 309 | |||
| 310 | /** |
||
| 311 | * {@inheritdoc} |
||
| 312 | */ |
||
| 313 | 4 | public function reject($deliveryTag, $requeue = true, $multiple = false) |
|
| 320 | |||
| 321 | /** |
||
| 322 | * {@inheritdoc} |
||
| 323 | */ |
||
| 324 | 2 | public function onReturn(callable $callable) |
|
| 330 | |||
| 331 | /** |
||
| 332 | * {@inheritdoc} |
||
| 333 | */ |
||
| 334 | 5 | public function selectConfirm(callable $callable, $noWait = false) |
|
| 348 | |||
| 349 | /** |
||
| 350 | * {@inheritdoc} |
||
| 351 | */ |
||
| 352 | 7 | public function selectTx() |
|
| 361 | |||
| 362 | /** |
||
| 363 | * {@inheritdoc} |
||
| 364 | */ |
||
| 365 | 6 | public function txCommit() |
|
| 376 | |||
| 377 | /** |
||
| 378 | * {@inheritdoc} |
||
| 379 | */ |
||
| 380 | 4 | public function txRollback() |
|
| 391 | |||
| 392 | /** |
||
| 393 | * {@inheritdoc} |
||
| 394 | */ |
||
| 395 | 10 | public function hasConsumer($tag) |
|
| 399 | |||
| 400 | /** |
||
| 401 | * {@inheritdoc} |
||
| 402 | */ |
||
| 403 | 3 | public function getConsumerTags() |
|
| 407 | |||
| 408 | /** |
||
| 409 | * @return string |
||
| 410 | */ |
||
| 411 | 3 | public function getStatus() |
|
| 415 | |||
| 416 | /** |
||
| 417 | * Sends frame to the server. |
||
| 418 | * |
||
| 419 | * @param Frame $frame |
||
| 420 | * |
||
| 421 | * @return $this |
||
| 422 | */ |
||
| 423 | private function send(Frame $frame) |
||
| 429 | |||
| 430 | /** |
||
| 431 | * @param Frame $frame |
||
| 432 | */ |
||
| 433 | 29 | public function dispatch(Frame $frame) |
|
| 451 | |||
| 452 | /** |
||
| 453 | * @param BasicDeliver $frame |
||
| 454 | * |
||
| 455 | * @throws \Exception |
||
| 456 | */ |
||
| 457 | 6 | private function onBasicDeliver(BasicDeliver $frame) |
|
| 480 | |||
| 481 | /** |
||
| 482 | * @param BasicReturn $frame |
||
| 483 | * |
||
| 484 | * @throws NoReturnException |
||
| 485 | */ |
||
| 486 | 4 | private function onBasicReturn(BasicReturn $frame) |
|
| 508 | |||
| 509 | /** |
||
| 510 | * @param BasicAck $frame |
||
| 511 | */ |
||
| 512 | 3 | private function onBasicAck(BasicAck $frame) |
|
| 516 | |||
| 517 | /** |
||
| 518 | * @param BasicNack $frame |
||
| 519 | */ |
||
| 520 | 2 | private function onBasicNack(BasicNack $frame) |
|
| 524 | |||
| 525 | /** |
||
| 526 | * @param bool $ok |
||
| 527 | * @param string $tag |
||
| 528 | * @param bool $multiple |
||
| 529 | */ |
||
| 530 | 5 | private function confirmPublishing($ok, $tag, $multiple) |
|
| 540 | |||
| 541 | /** |
||
| 542 | * @param BasicCancel $frame |
||
| 543 | */ |
||
| 544 | 3 | private function onBasicCancel(BasicCancel $frame) |
|
| 552 | |||
| 553 | /** |
||
| 554 | * @param ChannelFlow $frame |
||
| 555 | */ |
||
| 556 | 1 | private function onChannelFlow(ChannelFlow $frame) |
|
| 562 | |||
| 563 | /** |
||
| 564 | * @param ChannelClose $frame |
||
| 565 | * |
||
| 566 | * @throws AMQPException |
||
| 567 | */ |
||
| 568 | 3 | private function onChannelClose(ChannelClose $frame) |
|
| 576 | |||
| 577 | /** |
||
| 578 | * @return Header |
||
| 579 | */ |
||
| 580 | 15 | private function readHeader() |
|
| 584 | |||
| 585 | /** |
||
| 586 | * @param int $size |
||
| 587 | * |
||
| 588 | * @return string |
||
| 589 | */ |
||
| 590 | 15 | private function readContent($size) |
|
| 601 | } |
||
| 602 |
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: