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 |
||
16 | class AbstractConnection extends AbstractChannel |
||
17 | { |
||
18 | /** @var array */ |
||
19 | public static $LIBRARY_PROPERTIES = array( |
||
20 | 'product' => array('S', 'AMQPLib'), |
||
21 | 'platform' => array('S', 'PHP'), |
||
22 | 'version' => array('S', '2.6'), |
||
23 | 'information' => array('S', ''), |
||
24 | 'copyright' => array('S', ''), |
||
25 | 'capabilities' => array( |
||
26 | 'F', |
||
27 | array( |
||
28 | 'authentication_failure_close' => array('t', true), |
||
29 | 'publisher_confirms' => array('t', true), |
||
30 | 'consumer_cancel_notify' => array('t', true), |
||
31 | 'exchange_exchange_bindings' => array('t', true), |
||
32 | 'basic.nack' => array('t', true), |
||
33 | 'connection.blocked' => array('t', true) |
||
34 | ) |
||
35 | ) |
||
36 | ); |
||
37 | |||
38 | /** @var AMQPChannel[] */ |
||
39 | public $channels = array(); |
||
40 | |||
41 | /** @var int */ |
||
42 | protected $version_major; |
||
43 | |||
44 | /** @var int */ |
||
45 | protected $version_minor; |
||
46 | |||
47 | /** @var array */ |
||
48 | protected $server_properties; |
||
49 | |||
50 | /** @var array */ |
||
51 | protected $mechanisms; |
||
52 | |||
53 | /** @var array */ |
||
54 | protected $locales; |
||
55 | |||
56 | /** @var bool */ |
||
57 | protected $wait_tune_ok; |
||
58 | |||
59 | /** @var string */ |
||
60 | protected $known_hosts; |
||
61 | |||
62 | /** @var AMQPReader */ |
||
63 | protected $input; |
||
64 | |||
65 | /** @var string */ |
||
66 | protected $vhost; |
||
67 | |||
68 | /** @var bool */ |
||
69 | protected $insist; |
||
70 | |||
71 | /** @var string */ |
||
72 | protected $login_method; |
||
73 | |||
74 | /** @var string */ |
||
75 | protected $login_response; |
||
76 | |||
77 | /** @var string */ |
||
78 | protected $locale; |
||
79 | |||
80 | /** @var int */ |
||
81 | protected $heartbeat; |
||
82 | |||
83 | /** @var float */ |
||
84 | protected $last_frame; |
||
85 | |||
86 | /** @var SocketIO */ |
||
87 | protected $sock; |
||
88 | |||
89 | /** @var int */ |
||
90 | protected $channel_max = 65535; |
||
91 | |||
92 | /** @var int */ |
||
93 | protected $frame_max = 131072; |
||
94 | |||
95 | /** @var array Constructor parameters for clone */ |
||
96 | protected $construct_params; |
||
97 | |||
98 | /** @var bool Close the connection in destructor */ |
||
99 | protected $close_on_destruct = true; |
||
100 | |||
101 | /** @var bool Maintain connection status */ |
||
102 | protected $is_connected = false; |
||
103 | |||
104 | /** @var \PhpAmqpLib\Wire\IO\AbstractIO */ |
||
105 | protected $io; |
||
106 | |||
107 | /** @var \PhpAmqpLib\Wire\AMQPReader */ |
||
108 | protected $wait_frame_reader; |
||
109 | |||
110 | /** @var callable Handles connection blocking from the server */ |
||
111 | private $connection_block_handler; |
||
112 | |||
113 | /** @var callable Handles connection unblocking from the server */ |
||
114 | private $connection_unblock_handler; |
||
115 | |||
116 | /** @var int Connection timeout value*/ |
||
117 | protected $connection_timeout ; |
||
118 | |||
119 | /** |
||
120 | * Circular buffer to speed up prepare_content(). |
||
121 | * Max size limited by $prepare_content_cache_max_size. |
||
122 | * |
||
123 | * @var array |
||
124 | * @see prepare_content() |
||
125 | */ |
||
126 | private $prepare_content_cache; |
||
127 | |||
128 | /** @var int Maximal size of $prepare_content_cache */ |
||
129 | private $prepare_content_cache_max_size; |
||
130 | |||
131 | /** |
||
132 | * @param string $user |
||
133 | * @param string $password |
||
134 | * @param string $vhost |
||
135 | * @param bool $insist |
||
136 | * @param string $login_method |
||
137 | * @param null $login_response |
||
138 | * @param string $locale |
||
139 | * @param AbstractIO $io |
||
140 | * @param int $heartbeat |
||
141 | 90 | * @param int $connection_timeout |
|
142 | * @throws \Exception |
||
143 | */ |
||
144 | public function __construct( |
||
192 | |||
193 | 90 | /** |
|
194 | * Connects to the AMQP server |
||
195 | */ |
||
196 | protected function connect() |
||
249 | |||
250 | /** |
||
251 | 36 | * Reconnects using the original connection settings. |
|
252 | * This will not recreate any channels that were established previously |
||
253 | */ |
||
254 | 36 | public function reconnect() |
|
263 | |||
264 | /** |
||
265 | * Cloning will use the old properties to make a new connection to the same server |
||
266 | */ |
||
267 | public function __clone() |
||
271 | |||
272 | public function __destruct() |
||
278 | |||
279 | 36 | /** |
|
280 | * Attempts to close the connection safely |
||
281 | */ |
||
282 | 36 | protected function safeClose() |
|
292 | |||
293 | /** |
||
294 | * @param int $sec |
||
295 | * @param int $usec |
||
296 | * @return mixed |
||
297 | */ |
||
298 | public function select($sec, $usec = 0) |
||
302 | |||
303 | /** |
||
304 | * Allows to not close the connection |
||
305 | * it's useful after the fork when you don't want to close parent process connection |
||
306 | * |
||
307 | * @param bool $close |
||
308 | */ |
||
309 | public function set_close_on_destruct($close = true) |
||
313 | 84 | ||
314 | protected function close_input() |
||
323 | 84 | ||
324 | protected function close_socket() |
||
332 | |||
333 | 84 | /** |
|
334 | * @param $data |
||
335 | 84 | */ |
|
336 | public function write($data) |
||
347 | 84 | ||
348 | 84 | protected function do_close() |
|
354 | |||
355 | /** |
||
356 | 84 | * @return int |
|
357 | * @throws \PhpAmqpLib\Exception\AMQPRuntimeException |
||
358 | 84 | */ |
|
359 | 84 | public function get_free_channel_id() |
|
369 | |||
370 | /** |
||
371 | * @param string $channel |
||
372 | * @param int $class_id |
||
373 | * @param int $weight |
||
374 | * @param int $body_size |
||
375 | * @param string $packed_properties |
||
376 | 72 | * @param string $body |
|
377 | * @param AMQPWriter $pkt |
||
378 | 72 | */ |
|
379 | 72 | public function send_content($channel, $class_id, $weight, $body_size, $packed_properties, $body, $pkt = null) |
|
384 | |||
385 | /** |
||
386 | * Returns a new AMQPWriter or mutates the provided $pkt |
||
387 | * |
||
388 | * @param string $channel |
||
389 | * @param int $class_id |
||
390 | * @param int $weight |
||
391 | * @param int $body_size |
||
392 | * @param string $packed_properties |
||
393 | * @param string $body |
||
394 | 72 | * @param AMQPWriter $pkt |
|
395 | * @return AMQPWriter |
||
396 | 72 | */ |
|
397 | public function prepare_content($channel, $class_id, $weight, $body_size, $packed_properties, $body, $pkt = null) |
||
452 | |||
453 | /** |
||
454 | * @param string $channel |
||
455 | * @param array $method_sig |
||
456 | 84 | * @param AMQPWriter|string $args |
|
457 | * @param null $pkt |
||
458 | 84 | */ |
|
459 | 84 | protected function send_channel_method_frame($channel, $method_sig, $args = '', $pkt = null) |
|
465 | |||
466 | /** |
||
467 | * Returns a new AMQPWriter or mutates the provided $pkt |
||
468 | * |
||
469 | * @param string $channel |
||
470 | * @param array $method_sig |
||
471 | * @param AMQPWriter|string $args |
||
472 | 84 | * @param AMQPWriter $pkt |
|
473 | * @return AMQPWriter |
||
474 | 84 | */ |
|
475 | 84 | protected function prepare_channel_method_frame($channel, $method_sig, $args = '', $pkt = null) |
|
498 | |||
499 | /** |
||
500 | * Waits for a frame from the server |
||
501 | * |
||
502 | * @param int $timeout |
||
503 | * @return array |
||
504 | * @throws \Exception |
||
505 | 84 | * @throws \PhpAmqpLib\Exception\AMQPTimeoutException |
|
506 | * @throws \PhpAmqpLib\Exception\AMQPRuntimeException |
||
507 | 84 | */ |
|
508 | 42 | protected function wait_frame($timeout = 0) |
|
555 | |||
556 | /** |
||
557 | * Waits for a frame from the server destined for a particular channel. |
||
558 | * |
||
559 | * @param string $channel_id |
||
560 | 84 | * @param int $timeout |
|
561 | * @return array |
||
562 | */ |
||
563 | 84 | protected function wait_channel($channel_id, $timeout = 0) |
|
619 | |||
620 | 84 | /** |
|
621 | * Fetches a channel object identified by the numeric channel_id, or |
||
622 | * create that object if it doesn't already exist. |
||
623 | * |
||
624 | * @param int $channel_id |
||
625 | * @return AMQPChannel |
||
626 | */ |
||
627 | public function channel($channel_id = null) |
||
639 | 6 | ||
640 | /** |
||
641 | * Requests a connection close |
||
642 | 84 | * |
|
643 | * @param int $reply_code |
||
644 | 84 | * @param string $reply_text |
|
645 | 84 | * @param array $method_sig |
|
646 | 84 | * @return mixed|null |
|
647 | 84 | */ |
|
648 | 84 | public function close($reply_code = 0, $reply_text = '', $method_sig = array(0, 0)) |
|
675 | |||
676 | /** |
||
677 | * @param AMQPReader $reader |
||
678 | * @throws \PhpAmqpLib\Exception\AMQPProtocolConnectionException |
||
679 | */ |
||
680 | protected function connection_close(AMQPReader $reader) |
||
691 | 84 | ||
692 | /** |
||
693 | 84 | * Confirms a connection close |
|
694 | 84 | */ |
|
695 | protected function x_close_ok() |
||
702 | 84 | ||
703 | /** |
||
704 | 84 | * Confirm a connection close |
|
705 | 84 | * |
|
706 | 84 | * @param AMQPReader $args |
|
707 | 84 | */ |
|
708 | 84 | protected function connection_close_ok($args) |
|
712 | 42 | ||
713 | /** |
||
714 | 84 | * @param string $virtual_host |
|
715 | * @param string $capabilities |
||
716 | * @param bool $insist |
||
717 | * @return mixed |
||
718 | 84 | */ |
|
719 | protected function x_open($virtual_host, $capabilities = '', $insist = false) |
||
737 | |||
738 | /** |
||
739 | * Signals that the connection is ready |
||
740 | * |
||
741 | * @param AMQPReader $args |
||
742 | */ |
||
743 | protected function connection_open_ok($args) |
||
748 | |||
749 | /** |
||
750 | * Asks the client to use a different server |
||
751 | * |
||
752 | * @param AMQPReader $args |
||
753 | * @return string |
||
754 | */ |
||
755 | protected function connection_redirect($args) |
||
767 | |||
768 | /** |
||
769 | * Security mechanism challenge |
||
770 | * |
||
771 | * @param AMQPReader $args |
||
772 | */ |
||
773 | protected function connection_secure($args) |
||
777 | |||
778 | 84 | /** |
|
779 | * Security mechanism response |
||
780 | 84 | * |
|
781 | 84 | * @param string $response |
|
782 | 84 | */ |
|
783 | 84 | protected function x_secure_ok($response) |
|
789 | 84 | ||
790 | 84 | /** |
|
791 | 84 | * Starts connection negotiation |
|
792 | 42 | * |
|
793 | 84 | * @param AMQPReader $args |
|
794 | */ |
||
795 | protected function connection_start($args) |
||
811 | |||
812 | /** |
||
813 | * @param AMQPTable|array $clientProperties |
||
814 | * @param string $mechanism |
||
815 | * @param string $response |
||
816 | 84 | * @param string $locale |
|
817 | */ |
||
818 | 84 | protected function x_start_ok($clientProperties, $mechanism, $response, $locale) |
|
827 | |||
828 | /** |
||
829 | 84 | * Proposes connection tuning parameters |
|
830 | * |
||
831 | * @param AMQPReader $args |
||
832 | */ |
||
833 | 84 | protected function connection_tune($args) |
|
852 | |||
853 | /** |
||
854 | * Negotiates connection tuning parameters |
||
855 | * |
||
856 | * @param int $channel_max |
||
857 | * @param int $frame_max |
||
858 | * @param int $heartbeat |
||
859 | */ |
||
860 | protected function x_tune_ok($channel_max, $frame_max, $heartbeat) |
||
869 | |||
870 | /** |
||
871 | * @return SocketIO |
||
872 | */ |
||
873 | public function getSocket() |
||
877 | |||
878 | /** |
||
879 | * @return \PhpAmqpLib\Wire\IO\AbstractIO |
||
880 | */ |
||
881 | protected function getIO() |
||
885 | |||
886 | /** |
||
887 | * Handles connection blocked notifications |
||
888 | * |
||
889 | * @param AMQPReader $args |
||
890 | */ |
||
891 | protected function connection_blocked(AMQPReader $args) |
||
896 | |||
897 | /** |
||
898 | * Handles connection unblocked notifications |
||
899 | * |
||
900 | * @param AMQPReader $args |
||
901 | */ |
||
902 | protected function connection_unblocked(AMQPReader $args) |
||
907 | |||
908 | /** |
||
909 | * Sets a handler which is called whenever a connection.block is sent from the server |
||
910 | * |
||
911 | * @param callable $callback |
||
912 | */ |
||
913 | public function set_connection_block_handler($callback) |
||
917 | |||
918 | 90 | /** |
|
919 | * Sets a handler which is called whenever a connection.block is sent from the server |
||
920 | * |
||
921 | * @param callable $callback |
||
922 | */ |
||
923 | public function set_connection_unblock_handler($callback) |
||
927 | |||
928 | 90 | /** |
|
929 | 90 | * Gets the connection status |
|
930 | * |
||
931 | * @return bool |
||
932 | */ |
||
933 | public function isConnected() |
||
937 | |||
938 | 84 | /** |
|
939 | 84 | * Set the connection status |
|
940 | * |
||
941 | * @param bool $is_connected |
||
942 | 36 | */ |
|
943 | 36 | protected function setIsConnected($is_connected) |
|
947 | 90 | ||
948 | /** |
||
949 | * Closes all available channels |
||
950 | */ |
||
951 | protected function closeChannels() |
||
965 | |||
966 | /** |
||
967 | * Should the connection be attempted during construction? |
||
968 | * |
||
969 | * @return bool |
||
970 | */ |
||
971 | public function connectOnConstruct() |
||
975 | |||
976 | /** |
||
977 | * @return array |
||
978 | */ |
||
979 | public function getServerProperties() |
||
983 | } |
||
984 |
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..