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 | 19 | public function open() |
|
| 123 | |||
| 124 | /** |
||
| 125 | * {@inheritdoc} |
||
| 126 | */ |
||
| 127 | 1 | public function flow($active) |
|
| 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) |
|
| 247 | |||
| 248 | /** |
||
| 249 | * {@inheritdoc} |
||
| 250 | */ |
||
| 251 | 2 | public function recover($requeue = true) |
|
| 258 | |||
| 259 | /** |
||
| 260 | * {@inheritdoc} |
||
| 261 | */ |
||
| 262 | 6 | public function cancel($tag, $flags = 0) |
|
| 276 | |||
| 277 | /** |
||
| 278 | * {@inheritdoc} |
||
| 279 | */ |
||
| 280 | 15 | public function publish(Message $message, $exchange = '', $routingKey = '', $flags = 0) |
|
| 298 | |||
| 299 | /** |
||
| 300 | * {@inheritdoc} |
||
| 301 | */ |
||
| 302 | 6 | public function ack($deliveryTag, $multiple = false) |
|
| 308 | |||
| 309 | /** |
||
| 310 | * {@inheritdoc} |
||
| 311 | */ |
||
| 312 | 4 | public function reject($deliveryTag, $requeue = true, $multiple = false) |
|
| 319 | |||
| 320 | /** |
||
| 321 | * {@inheritdoc} |
||
| 322 | */ |
||
| 323 | 2 | public function onReturn(callable $callable) |
|
| 329 | |||
| 330 | /** |
||
| 331 | * {@inheritdoc} |
||
| 332 | */ |
||
| 333 | 5 | public function selectConfirm(callable $callable, $noWait = false) |
|
| 347 | |||
| 348 | /** |
||
| 349 | * {@inheritdoc} |
||
| 350 | */ |
||
| 351 | 7 | public function selectTx() |
|
| 360 | |||
| 361 | /** |
||
| 362 | * {@inheritdoc} |
||
| 363 | */ |
||
| 364 | 6 | public function txCommit() |
|
| 375 | |||
| 376 | /** |
||
| 377 | * {@inheritdoc} |
||
| 378 | */ |
||
| 379 | 4 | public function txRollback() |
|
| 390 | |||
| 391 | /** |
||
| 392 | * {@inheritdoc} |
||
| 393 | */ |
||
| 394 | 10 | public function hasConsumer($tag) |
|
| 398 | |||
| 399 | /** |
||
| 400 | * {@inheritdoc} |
||
| 401 | */ |
||
| 402 | 3 | public function getConsumerTags() |
|
| 406 | |||
| 407 | /** |
||
| 408 | * @return string |
||
| 409 | */ |
||
| 410 | 3 | public function getStatus() |
|
| 414 | |||
| 415 | /** |
||
| 416 | * Sends frame to the server. |
||
| 417 | * |
||
| 418 | * @param Frame $frame |
||
| 419 | * |
||
| 420 | * @return $this |
||
| 421 | */ |
||
| 422 | 46 | private function send(Frame $frame) |
|
| 428 | |||
| 429 | /** |
||
| 430 | * @param string|array $type |
||
| 431 | * |
||
| 432 | * @return Frame |
||
| 433 | */ |
||
| 434 | 37 | private function wait($type) |
|
| 438 | |||
| 439 | /** |
||
| 440 | * @param Frame $frame |
||
| 441 | */ |
||
| 442 | 29 | public function dispatch(Frame $frame) |
|
| 460 | |||
| 461 | /** |
||
| 462 | * @param BasicDeliver $frame |
||
| 463 | * |
||
| 464 | * @throws \Exception |
||
| 465 | */ |
||
| 466 | 6 | private function onBasicDeliver(BasicDeliver $frame) |
|
| 491 | |||
| 492 | /** |
||
| 493 | * @param BasicReturn $frame |
||
| 494 | * |
||
| 495 | * @throws NoReturnException |
||
| 496 | */ |
||
| 497 | 4 | private function onBasicReturn(BasicReturn $frame) |
|
| 521 | |||
| 522 | /** |
||
| 523 | * @param BasicAck $frame |
||
| 524 | */ |
||
| 525 | 3 | private function onBasicAck(BasicAck $frame) |
|
| 529 | |||
| 530 | /** |
||
| 531 | * @param BasicNack $frame |
||
| 532 | */ |
||
| 533 | 2 | private function onBasicNack(BasicNack $frame) |
|
| 537 | |||
| 538 | /** |
||
| 539 | * @param bool $ok |
||
| 540 | * @param string $tag |
||
| 541 | * @param bool $multiple |
||
| 542 | */ |
||
| 543 | 5 | private function confirmPublishing($ok, $tag, $multiple) |
|
| 553 | |||
| 554 | /** |
||
| 555 | * @param BasicCancel $frame |
||
| 556 | */ |
||
| 557 | 3 | private function onBasicCancel(BasicCancel $frame) |
|
| 565 | |||
| 566 | /** |
||
| 567 | * @param ChannelFlow $frame |
||
| 568 | */ |
||
| 569 | 1 | private function onChannelFlow(ChannelFlow $frame) |
|
| 575 | |||
| 576 | /** |
||
| 577 | * @param ChannelClose $frame |
||
| 578 | * |
||
| 579 | * @throws AMQPException |
||
| 580 | */ |
||
| 581 | 3 | private function onChannelClose(ChannelClose $frame) |
|
| 589 | |||
| 590 | /** |
||
| 591 | * @return Header |
||
| 592 | */ |
||
| 593 | 15 | private function readHeader() |
|
| 597 | |||
| 598 | /** |
||
| 599 | * @param int $size |
||
| 600 | * |
||
| 601 | * @return string |
||
| 602 | */ |
||
| 603 | 15 | private function readContent($size) |
|
| 614 | } |
||
| 615 |