Complex classes like AbstractConnection 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 AbstractConnection, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 14 | class AbstractConnection extends AbstractChannel |
||
| 15 | { |
||
| 16 | /** @var array */ |
||
| 17 | public static $LIBRARY_PROPERTIES = array( |
||
| 18 | 'product' => array('S', 'AMQPLib'), |
||
| 19 | 'platform' => array('S', 'PHP'), |
||
| 20 | 'version' => array('S', '2.4'), |
||
| 21 | 'information' => array('S', ''), |
||
| 22 | 'copyright' => array('S', ''), |
||
| 23 | 'capabilities' => array( |
||
| 24 | 'F', |
||
| 25 | array( |
||
| 26 | 'authentication_failure_close' => array('t', true), |
||
| 27 | 'publisher_confirms' => array('t', true), |
||
| 28 | 'consumer_cancel_notify' => array('t', true), |
||
| 29 | 'exchange_exchange_bindings' => array('t', true), |
||
| 30 | 'basic.nack' => array('t', true), |
||
| 31 | 'connection.blocked' => array('t', true) |
||
| 32 | ) |
||
| 33 | ) |
||
| 34 | ); |
||
| 35 | |||
| 36 | /** @var AMQPChannel[] */ |
||
| 37 | public $channels = array(); |
||
| 38 | |||
| 39 | /** @var int */ |
||
| 40 | protected $version_major; |
||
| 41 | |||
| 42 | /** @var int */ |
||
| 43 | protected $version_minor; |
||
| 44 | |||
| 45 | /** @var array */ |
||
| 46 | protected $server_properties; |
||
| 47 | |||
| 48 | /** @var array */ |
||
| 49 | protected $mechanisms; |
||
| 50 | |||
| 51 | /** @var array */ |
||
| 52 | protected $locales; |
||
| 53 | |||
| 54 | /** @var bool */ |
||
| 55 | protected $wait_tune_ok; |
||
| 56 | |||
| 57 | /** @var string */ |
||
| 58 | protected $known_hosts; |
||
| 59 | |||
| 60 | /** @var AMQPReader */ |
||
| 61 | protected $input; |
||
| 62 | |||
| 63 | /** @var string */ |
||
| 64 | protected $vhost; |
||
| 65 | |||
| 66 | /** @var bool */ |
||
| 67 | protected $insist; |
||
| 68 | |||
| 69 | /** @var string */ |
||
| 70 | protected $login_method; |
||
| 71 | |||
| 72 | /** @var AMQPWriter */ |
||
| 73 | protected $login_response; |
||
| 74 | |||
| 75 | /** @var string */ |
||
| 76 | protected $locale; |
||
| 77 | |||
| 78 | /** @var int */ |
||
| 79 | protected $heartbeat; |
||
| 80 | |||
| 81 | /** @var float */ |
||
| 82 | protected $last_frame; |
||
| 83 | |||
| 84 | /** @var SocketIO */ |
||
| 85 | protected $sock; |
||
| 86 | |||
| 87 | /** @var int */ |
||
| 88 | protected $channel_max = 65535; |
||
| 89 | |||
| 90 | /** @var int */ |
||
| 91 | protected $frame_max = 131072; |
||
| 92 | |||
| 93 | /** @var array Constructor parameters for clone */ |
||
| 94 | protected $construct_params; |
||
| 95 | |||
| 96 | /** @var bool Close the connection in destructor */ |
||
| 97 | protected $close_on_destruct = true; |
||
| 98 | |||
| 99 | /** @var bool Maintain connection status */ |
||
| 100 | protected $is_connected = false; |
||
| 101 | |||
| 102 | /** @var \PhpAmqpLib\Wire\IO\AbstractIO */ |
||
| 103 | protected $io; |
||
| 104 | |||
| 105 | /** @var \PhpAmqpLib\Wire\AMQPReader */ |
||
| 106 | protected $wait_frame_reader; |
||
| 107 | |||
| 108 | /** @var callable Handles connection blocking from the server */ |
||
| 109 | private $connection_block_handler; |
||
| 110 | |||
| 111 | /** @var callable Handles connection unblocking from the server */ |
||
| 112 | private $connection_unblock_handler; |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Circular buffer to speed up prepare_content(). |
||
| 116 | * Max size limited by $prepare_content_cache_max_size. |
||
| 117 | * |
||
| 118 | * @var array |
||
| 119 | * @see prepare_content() |
||
| 120 | */ |
||
| 121 | private $prepare_content_cache; |
||
| 122 | |||
| 123 | /** @var int Maximal size of $prepare_content_cache */ |
||
| 124 | private $prepare_content_cache_max_size; |
||
| 125 | |||
| 126 | /** |
||
| 127 | * @param string $user |
||
| 128 | * @param string $password |
||
| 129 | * @param string $vhost |
||
| 130 | * @param bool $insist |
||
| 131 | * @param string $login_method |
||
| 132 | * @param null $login_response |
||
| 133 | * @param string $locale |
||
| 134 | * @param AbstractIO $io |
||
| 135 | 60 | * @param int $heartbeat |
|
| 136 | * @throws \Exception |
||
| 137 | */ |
||
| 138 | public function __construct( |
||
| 184 | |||
| 185 | 60 | /** |
|
| 186 | * Connects to the AMQP server |
||
| 187 | */ |
||
| 188 | protected function connect() |
||
| 236 | |||
| 237 | /** |
||
| 238 | 20 | * Reconnects using the original connection settings. |
|
| 239 | * This will not recreate any channels that were established previously |
||
| 240 | */ |
||
| 241 | 20 | public function reconnect() |
|
| 250 | |||
| 251 | /** |
||
| 252 | * Cloning will use the old properties to make a new connection to the same server |
||
| 253 | */ |
||
| 254 | public function __clone() |
||
| 258 | |||
| 259 | public function __destruct() |
||
| 265 | |||
| 266 | 20 | /** |
|
| 267 | * Attempts to close the connection safely |
||
| 268 | */ |
||
| 269 | 20 | protected function safeClose() |
|
| 279 | |||
| 280 | /** |
||
| 281 | * @param int $sec |
||
| 282 | * @param int $usec |
||
| 283 | * @return mixed |
||
| 284 | */ |
||
| 285 | public function select($sec, $usec = 0) |
||
| 289 | |||
| 290 | /** |
||
| 291 | * Allows to not close the connection |
||
| 292 | * it's useful after the fork when you don't want to close parent process connection |
||
| 293 | * |
||
| 294 | * @param bool $close |
||
| 295 | */ |
||
| 296 | public function set_close_on_destruct($close = true) |
||
| 300 | 60 | ||
| 301 | protected function close_input() |
||
| 310 | 60 | ||
| 311 | protected function close_socket() |
||
| 319 | |||
| 320 | 60 | /** |
|
| 321 | * @param $data |
||
| 322 | 60 | */ |
|
| 323 | public function write($data) |
||
| 334 | 60 | ||
| 335 | 60 | protected function do_close() |
|
| 341 | |||
| 342 | /** |
||
| 343 | 60 | * @return int |
|
| 344 | * @throws \PhpAmqpLib\Exception\AMQPRuntimeException |
||
| 345 | 60 | */ |
|
| 346 | 60 | public function get_free_channel_id() |
|
| 356 | |||
| 357 | /** |
||
| 358 | * @param string $channel |
||
| 359 | * @param int $class_id |
||
| 360 | * @param int $weight |
||
| 361 | * @param int $body_size |
||
| 362 | * @param string $packed_properties |
||
| 363 | 55 | * @param string $body |
|
| 364 | * @param AMQPWriter $pkt |
||
| 365 | 55 | */ |
|
| 366 | 55 | public function send_content($channel, $class_id, $weight, $body_size, $packed_properties, $body, $pkt = null) |
|
| 371 | |||
| 372 | /** |
||
| 373 | * Returns a new AMQPWriter or mutates the provided $pkt |
||
| 374 | * |
||
| 375 | * @param string $channel |
||
| 376 | * @param int $class_id |
||
| 377 | * @param int $weight |
||
| 378 | * @param int $body_size |
||
| 379 | * @param string $packed_properties |
||
| 380 | * @param string $body |
||
| 381 | 55 | * @param AMQPWriter $pkt |
|
| 382 | * @return AMQPWriter |
||
| 383 | 55 | */ |
|
| 384 | public function prepare_content($channel, $class_id, $weight, $body_size, $packed_properties, $body, $pkt = null) |
||
| 439 | |||
| 440 | /** |
||
| 441 | * @param $channel |
||
| 442 | * @param $method_sig |
||
| 443 | 60 | * @param string $args |
|
| 444 | * @param null $pkt |
||
| 445 | 60 | */ |
|
| 446 | 60 | protected function send_channel_method_frame($channel, $method_sig, $args = '', $pkt = null) |
|
| 452 | |||
| 453 | /** |
||
| 454 | * Returns a new AMQPWriter or mutates the provided $pkt |
||
| 455 | * |
||
| 456 | * @param $channel |
||
| 457 | * @param $method_sig |
||
| 458 | * @param string $args |
||
| 459 | 60 | * @param AMQPWriter $pkt |
|
| 460 | * @return null|AMQPWriter |
||
| 461 | 60 | */ |
|
| 462 | 60 | protected function prepare_channel_method_frame($channel, $method_sig, $args = '', $pkt = null) |
|
| 485 | |||
| 486 | /** |
||
| 487 | * Waits for a frame from the server |
||
| 488 | * |
||
| 489 | * @param int $timeout |
||
| 490 | * @return array |
||
| 491 | * @throws \Exception |
||
| 492 | 60 | * @throws \PhpAmqpLib\Exception\AMQPTimeoutException |
|
| 493 | * @throws \PhpAmqpLib\Exception\AMQPRuntimeException |
||
| 494 | 60 | */ |
|
| 495 | 48 | protected function wait_frame($timeout = 0) |
|
| 538 | |||
| 539 | /** |
||
| 540 | * Waits for a frame from the server destined for a particular channel. |
||
| 541 | * |
||
| 542 | * @param string $channel_id |
||
| 543 | 60 | * @param int $timeout |
|
| 544 | * @return array |
||
| 545 | */ |
||
| 546 | 60 | protected function wait_channel($channel_id, $timeout = 0) |
|
| 602 | 60 | ||
| 603 | /** |
||
| 604 | 60 | * Fetches a channel object identified by the numeric channel_id, or |
|
| 605 | * create that object if it doesn't already exist. |
||
| 606 | * |
||
| 607 | * @param string $channel_id |
||
| 608 | * @return AMQPChannel |
||
| 609 | */ |
||
| 610 | public function channel($channel_id = null) |
||
| 623 | 60 | ||
| 624 | 48 | /** |
|
| 625 | 48 | * Requests a connection close |
|
| 626 | 60 | * |
|
| 627 | 60 | * @param int $reply_code |
|
| 628 | 48 | * @param string $reply_text |
|
| 629 | 60 | * @param array $method_sig |
|
| 630 | * @return mixed|null |
||
| 631 | 60 | */ |
|
| 632 | public function close($reply_code = 0, $reply_text = '', $method_sig = array(0, 0)) |
||
| 654 | |||
| 655 | /** |
||
| 656 | * @param AMQPReader $args |
||
| 657 | * @throws \PhpAmqpLib\Exception\AMQPProtocolConnectionException |
||
| 658 | */ |
||
| 659 | protected function connection_close($args) |
||
| 670 | 60 | ||
| 671 | 60 | /** |
|
| 672 | * Confirms a connection close |
||
| 673 | */ |
||
| 674 | protected function x_close_ok() |
||
| 681 | 60 | ||
| 682 | 60 | /** |
|
| 683 | 60 | * Confirm a connection close |
|
| 684 | 60 | */ |
|
| 685 | 60 | protected function connection_close_ok($args) |
|
| 689 | 48 | ||
| 690 | /** |
||
| 691 | 60 | * @param string $virtual_host |
|
| 692 | * @param string $capabilities |
||
| 693 | * @param bool $insist |
||
| 694 | * @return mixed |
||
| 695 | 60 | */ |
|
| 696 | protected function x_open($virtual_host, $capabilities = '', $insist = false) |
||
| 714 | |||
| 715 | /** |
||
| 716 | * Signals that the connection is ready |
||
| 717 | * |
||
| 718 | * @param AMQPReader $args |
||
| 719 | */ |
||
| 720 | protected function connection_open_ok($args) |
||
| 725 | |||
| 726 | /** |
||
| 727 | * Asks the client to use a different server |
||
| 728 | * |
||
| 729 | * @param AMQPReader $args |
||
| 730 | * @return string |
||
| 731 | */ |
||
| 732 | protected function connection_redirect($args) |
||
| 744 | |||
| 745 | /** |
||
| 746 | * Security mechanism challenge |
||
| 747 | * |
||
| 748 | * @param AMQPReader $args |
||
| 749 | */ |
||
| 750 | protected function connection_secure($args) |
||
| 754 | |||
| 755 | 60 | /** |
|
| 756 | 60 | * Security mechanism response |
|
| 757 | 60 | */ |
|
| 758 | 60 | protected function x_secure_ok($response) |
|
| 764 | 60 | ||
| 765 | 60 | /** |
|
| 766 | 60 | * Starts connection negotiation |
|
| 767 | 48 | * |
|
| 768 | 60 | * @param AMQPReader $args |
|
| 769 | */ |
||
| 770 | protected function connection_start($args) |
||
| 786 | |||
| 787 | /** |
||
| 788 | * @param $client_properties |
||
| 789 | * @param $mechanism |
||
| 790 | * @param $response |
||
| 791 | 60 | * @param $locale |
|
| 792 | */ |
||
| 793 | 60 | protected function x_start_ok($client_properties, $mechanism, $response, $locale) |
|
| 802 | |||
| 803 | /** |
||
| 804 | 60 | * Proposes connection tuning parameters |
|
| 805 | * |
||
| 806 | * @param AMQPReader $args |
||
| 807 | */ |
||
| 808 | 60 | protected function connection_tune($args) |
|
| 827 | |||
| 828 | /** |
||
| 829 | * Negotiates connection tuning parameters |
||
| 830 | * |
||
| 831 | * @param $channel_max |
||
| 832 | * @param $frame_max |
||
| 833 | * @param $heartbeat |
||
| 834 | */ |
||
| 835 | protected function x_tune_ok($channel_max, $frame_max, $heartbeat) |
||
| 844 | |||
| 845 | /** |
||
| 846 | * @return SocketIO |
||
| 847 | */ |
||
| 848 | public function getSocket() |
||
| 852 | |||
| 853 | /** |
||
| 854 | * @return \PhpAmqpLib\Wire\IO\AbstractIO |
||
| 855 | */ |
||
| 856 | protected function getIO() |
||
| 860 | |||
| 861 | /** |
||
| 862 | * Handles connection blocked notifications |
||
| 863 | * |
||
| 864 | * @param AMQPReader $args |
||
| 865 | */ |
||
| 866 | protected function connection_blocked(AMQPReader $args) |
||
| 871 | |||
| 872 | /** |
||
| 873 | * Handles connection unblocked notifications |
||
| 874 | */ |
||
| 875 | protected function connection_unblocked(AMQPReader $args) |
||
| 880 | |||
| 881 | /** |
||
| 882 | * Sets a handler which is called whenever a connection.block is sent from the server |
||
| 883 | * |
||
| 884 | * @param callable $callback |
||
| 885 | */ |
||
| 886 | public function set_connection_block_handler($callback) |
||
| 890 | |||
| 891 | 60 | /** |
|
| 892 | * Sets a handler which is called whenever a connection.block is sent from the server |
||
| 893 | * |
||
| 894 | * @param callable $callback |
||
| 895 | */ |
||
| 896 | public function set_connection_unblock_handler($callback) |
||
| 900 | |||
| 901 | 60 | /** |
|
| 902 | 60 | * Gets the connection status |
|
| 903 | * |
||
| 904 | * @return bool |
||
| 905 | */ |
||
| 906 | public function isConnected() |
||
| 910 | |||
| 911 | 60 | /** |
|
| 912 | 60 | * Set the connection status |
|
| 913 | * |
||
| 914 | * @param bool $is_connected |
||
| 915 | 25 | */ |
|
| 916 | 25 | protected function setIsConnected($is_connected) |
|
| 920 | 60 | ||
| 921 | /** |
||
| 922 | * Closes all available channels |
||
| 923 | */ |
||
| 924 | protected function closeChannels() |
||
| 938 | |||
| 939 | /** |
||
| 940 | * Should the connection be attempted during construction? |
||
| 941 | * |
||
| 942 | * @return bool |
||
| 943 | */ |
||
| 944 | public function connectOnConstruct() |
||
| 948 | } |
||
| 949 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..