Complex classes like Sender 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 Sender, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
15 | class Sender { |
||
16 | /** |
||
17 | * Name of the option that stores the time of the next sync. |
||
18 | * |
||
19 | * @access public |
||
20 | * |
||
21 | * @var string |
||
22 | */ |
||
23 | const NEXT_SYNC_TIME_OPTION_NAME = 'jetpack_next_sync_time'; |
||
24 | |||
25 | /** |
||
26 | * Sync timeout after a WPCOM error. |
||
27 | * |
||
28 | * @access public |
||
29 | * |
||
30 | * @var int |
||
31 | */ |
||
32 | const WPCOM_ERROR_SYNC_DELAY = 60; |
||
33 | |||
34 | /** |
||
35 | * Sync timeout after a queue has been locked. |
||
36 | * |
||
37 | * @access public |
||
38 | * |
||
39 | * @var int |
||
40 | */ |
||
41 | const QUEUE_LOCKED_SYNC_DELAY = 10; |
||
42 | |||
43 | /** |
||
44 | * Maximum bytes to checkout without exceeding the memory limit. |
||
45 | * |
||
46 | * @access private |
||
47 | * |
||
48 | * @var int |
||
49 | */ |
||
50 | private $dequeue_max_bytes; |
||
51 | |||
52 | /** |
||
53 | * Maximum bytes in a single encoded item. |
||
54 | * |
||
55 | * @access private |
||
56 | * |
||
57 | * @var int |
||
58 | */ |
||
59 | private $upload_max_bytes; |
||
60 | |||
61 | /** |
||
62 | * Maximum number of sync items in a single action. |
||
63 | * |
||
64 | * @access private |
||
65 | * |
||
66 | * @var int |
||
67 | */ |
||
68 | private $upload_max_rows; |
||
69 | |||
70 | /** |
||
71 | * Maximum time for perfirming a checkout of items from the queue (in seconds). |
||
72 | * |
||
73 | * @access private |
||
74 | * |
||
75 | * @var int |
||
76 | */ |
||
77 | private $max_dequeue_time; |
||
78 | |||
79 | /** |
||
80 | * How many seconds to wait after sending sync items after exceeding the sync wait threshold (in seconds). |
||
81 | * |
||
82 | * @access private |
||
83 | * |
||
84 | * @var int |
||
85 | */ |
||
86 | private $sync_wait_time; |
||
87 | |||
88 | /** |
||
89 | * How much maximum time to wait for the checkout to finish (in seconds). |
||
90 | * |
||
91 | * @access private |
||
92 | * |
||
93 | * @var int |
||
94 | */ |
||
95 | private $sync_wait_threshold; |
||
96 | |||
97 | /** |
||
98 | * How much maximum time to wait for the sync items to be queued for sending (in seconds). |
||
99 | * |
||
100 | * @access private |
||
101 | * |
||
102 | * @var int |
||
103 | */ |
||
104 | private $enqueue_wait_time; |
||
105 | |||
106 | /** |
||
107 | * Incremental sync queue object. |
||
108 | * |
||
109 | * @access private |
||
110 | * |
||
111 | * @var Automattic\Jetpack\Sync\Queue |
||
112 | */ |
||
113 | private $sync_queue; |
||
114 | |||
115 | /** |
||
116 | * Full sync queue object. |
||
117 | * |
||
118 | * @access private |
||
119 | * |
||
120 | * @var Automattic\Jetpack\Sync\Queue |
||
121 | */ |
||
122 | private $full_sync_queue; |
||
123 | |||
124 | /** |
||
125 | * Codec object for encoding and decoding sync items. |
||
126 | * |
||
127 | * @access private |
||
128 | * |
||
129 | * @var Automattic\Jetpack\Sync\Codec_Interface |
||
130 | */ |
||
131 | private $codec; |
||
132 | |||
133 | /** |
||
134 | * The current user before we change or clear it. |
||
135 | * |
||
136 | * @access private |
||
137 | * |
||
138 | * @var \WP_User |
||
139 | */ |
||
140 | private $old_user; |
||
141 | |||
142 | /** |
||
143 | * Container for the singleton instance of this class. |
||
144 | * |
||
145 | * @access private |
||
146 | * @static |
||
147 | * |
||
148 | * @var Automattic\Jetpack\Sync\Sender |
||
149 | */ |
||
150 | private static $instance; |
||
151 | |||
152 | /** |
||
153 | * Retrieve the singleton instance of this class. |
||
154 | * |
||
155 | * @access public |
||
156 | * @static |
||
157 | * |
||
158 | * @return Sender |
||
159 | */ |
||
160 | public static function get_instance() { |
||
167 | |||
168 | /** |
||
169 | * Constructor. |
||
170 | * This is necessary because you can't use "new" when you declare instance properties >:( |
||
171 | * |
||
172 | * @access protected |
||
173 | * @static |
||
174 | */ |
||
175 | protected function __construct() { |
||
179 | |||
180 | /** |
||
181 | * Initialize the sender. |
||
182 | * Prepares the current user and initializes all sync modules. |
||
183 | * |
||
184 | * @access private |
||
185 | */ |
||
186 | private function init() { |
||
194 | |||
195 | /** |
||
196 | * Detect if this is a XMLRPC request with a valid signature. |
||
197 | * If so, changes the user to the new one. |
||
198 | * |
||
199 | * @access public |
||
200 | */ |
||
201 | public function maybe_set_user_from_token() { |
||
212 | |||
213 | /** |
||
214 | * If we used to have a previous current user, revert back to it. |
||
215 | * |
||
216 | * @access public |
||
217 | */ |
||
218 | public function maybe_clear_user_from_token() { |
||
223 | |||
224 | /** |
||
225 | * Retrieve the next sync time. |
||
226 | * |
||
227 | * @access public |
||
228 | * |
||
229 | * @param string $queue_name Name of the queue. |
||
230 | * @return float Timestamp of the next sync. |
||
231 | */ |
||
232 | public function get_next_sync_time( $queue_name ) { |
||
235 | |||
236 | /** |
||
237 | * Set the next sync time. |
||
238 | * |
||
239 | * @access public |
||
240 | * |
||
241 | * @param int $time Timestamp of the next sync. |
||
242 | * @param string $queue_name Name of the queue. |
||
243 | * @return boolean True if update was successful, false otherwise. |
||
244 | */ |
||
245 | public function set_next_sync_time( $time, $queue_name ) { |
||
248 | |||
249 | /** |
||
250 | * Trigger a full sync. |
||
251 | * |
||
252 | * @access public |
||
253 | * |
||
254 | * @return boolean|\WP_Error True if this sync sending was successful, error object otherwise. |
||
255 | */ |
||
256 | public function do_full_sync() { |
||
266 | |||
267 | /** |
||
268 | * Enqueue the next sync items for sending. |
||
269 | * Will not be done if the current request is a WP import one. |
||
270 | * Will be delayed until the next sync time comes. |
||
271 | * |
||
272 | * @access private |
||
273 | */ |
||
274 | private function continue_full_sync_enqueue() { |
||
287 | |||
288 | /** |
||
289 | * Trigger incremental sync. |
||
290 | * |
||
291 | * @access public |
||
292 | * |
||
293 | * @return boolean|\WP_Error True if this sync sending was successful, error object otherwise. |
||
294 | */ |
||
295 | public function do_sync() { |
||
298 | |||
299 | /** |
||
300 | * Trigger sync for a certain sync queue. |
||
301 | * Responsible for setting next sync time. |
||
302 | * Will not be delayed if the current request is a WP import one. |
||
303 | * Will be delayed until the next sync time comes. |
||
304 | * |
||
305 | * @access public |
||
306 | * |
||
307 | * @param Automattic\Jetpack\Sync\Queue $queue Queue object. |
||
308 | * @return boolean|\WP_Error True if this sync sending was successful, error object otherwise. |
||
309 | */ |
||
310 | public function do_sync_and_set_delays( $queue ) { |
||
349 | |||
350 | /** |
||
351 | * Retrieve the next sync items to send. |
||
352 | * |
||
353 | * @access public |
||
354 | * |
||
355 | * @param (array|Automattic\Jetpack\Sync\Queue_Buffer) $buffer_or_items Queue buffer or array of objects. |
||
356 | * @param boolean $encode Whether to encode the items. |
||
357 | * @return array Sync items to send. |
||
358 | */ |
||
359 | public function get_items_to_send( $buffer_or_items, $encode = true ) { |
||
406 | |||
407 | /** |
||
408 | * If supported, flush all response data to the client and finish the request. |
||
409 | * This allows for time consuming tasks to be performed without leaving the connection open. |
||
410 | * |
||
411 | * @access private |
||
412 | */ |
||
413 | private function fastcgi_finish_request() { |
||
418 | |||
419 | /** |
||
420 | * Perform sync for a certain sync queue. |
||
421 | * |
||
422 | * @access public |
||
423 | * |
||
424 | * @param Automattic\Jetpack\Sync\Queue $queue Queue object. |
||
425 | * @return boolean|\WP_Error True if this sync sending was successful, error object otherwise. |
||
426 | */ |
||
427 | public function do_sync_for_queue( $queue ) { |
||
522 | |||
523 | /** |
||
524 | * Immediately sends a single item without firing or enqueuing it |
||
525 | * |
||
526 | * @param string $action_name The action. |
||
527 | * @param array $data The data associated with the action. |
||
528 | * |
||
529 | * @return Items processed. TODO: this doesn't make much sense anymore, it should probably be just a bool. |
||
530 | */ |
||
531 | public function send_action( $action_name, $data = null ) { |
||
556 | |||
557 | /** |
||
558 | * Create an synthetic action for direct sending to WPCOM during full sync (for example) |
||
559 | * |
||
560 | * @access private |
||
561 | * |
||
562 | * @param string $action_name The action. |
||
563 | * @param array $data The data associated with the action. |
||
564 | * @return array An array of synthetic sync actions keyed by current microtime(true) |
||
565 | */ |
||
566 | private function create_action_to_send( $action_name, $data ) { |
||
577 | |||
578 | /** |
||
579 | * Returns any object that is able to be synced. |
||
580 | * |
||
581 | * @access public |
||
582 | * |
||
583 | * @param array $args the synchronized object parameters. |
||
584 | * @return string Encoded sync object. |
||
585 | */ |
||
586 | public function sync_object( $args ) { |
||
595 | |||
596 | /** |
||
597 | * Register additional sync XML-RPC methods available to Jetpack for authenticated users. |
||
598 | * |
||
599 | * @access public |
||
600 | * @since 7.8 |
||
601 | * |
||
602 | * @param array $jetpack_methods XML-RPC methods available to the Jetpack Server. |
||
603 | * @return array Filtered XML-RPC methods. |
||
604 | */ |
||
605 | public function register_jetpack_xmlrpc_methods( $jetpack_methods ) { |
||
609 | |||
610 | /** |
||
611 | * Get the incremental sync queue object. |
||
612 | * |
||
613 | * @access public |
||
614 | * |
||
615 | * @return Automattic\Jetpack\Sync\Queue Queue object. |
||
616 | */ |
||
617 | public function get_sync_queue() { |
||
620 | |||
621 | /** |
||
622 | * Get the full sync queue object. |
||
623 | * |
||
624 | * @access public |
||
625 | * |
||
626 | * @return Automattic\Jetpack\Sync\Queue Queue object. |
||
627 | */ |
||
628 | public function get_full_sync_queue() { |
||
631 | |||
632 | /** |
||
633 | * Get the codec object. |
||
634 | * |
||
635 | * @access public |
||
636 | * |
||
637 | * @return Automattic\Jetpack\Sync\Codec_Interface Codec object. |
||
638 | */ |
||
639 | public function get_codec() { |
||
642 | |||
643 | /** |
||
644 | * Determine the codec object. |
||
645 | * Use gzip deflate if supported. |
||
646 | * |
||
647 | * @access public |
||
648 | */ |
||
649 | public function set_codec() { |
||
656 | |||
657 | /** |
||
658 | * Compute and send all the checksums. |
||
659 | * |
||
660 | * @access public |
||
661 | */ |
||
662 | public function send_checksum() { |
||
666 | |||
667 | /** |
||
668 | * Reset the incremental sync queue. |
||
669 | * |
||
670 | * @access public |
||
671 | */ |
||
672 | public function reset_sync_queue() { |
||
675 | |||
676 | /** |
||
677 | * Reset the full sync queue. |
||
678 | * |
||
679 | * @access public |
||
680 | */ |
||
681 | public function reset_full_sync_queue() { |
||
684 | |||
685 | /** |
||
686 | * Set the maximum bytes to checkout without exceeding the memory limit. |
||
687 | * |
||
688 | * @access public |
||
689 | * |
||
690 | * @param int $size Maximum bytes to checkout. |
||
691 | */ |
||
692 | public function set_dequeue_max_bytes( $size ) { |
||
695 | |||
696 | /** |
||
697 | * Set the maximum bytes in a single encoded item. |
||
698 | * |
||
699 | * @access public |
||
700 | * |
||
701 | * @param int $max_bytes Maximum bytes in a single encoded item. |
||
702 | */ |
||
703 | public function set_upload_max_bytes( $max_bytes ) { |
||
706 | |||
707 | /** |
||
708 | * Set the maximum number of sync items in a single action. |
||
709 | * |
||
710 | * @access public |
||
711 | * |
||
712 | * @param int $max_rows Maximum number of sync items. |
||
713 | */ |
||
714 | public function set_upload_max_rows( $max_rows ) { |
||
717 | |||
718 | /** |
||
719 | * Set the sync wait time (in seconds). |
||
720 | * |
||
721 | * @access public |
||
722 | * |
||
723 | * @param int $seconds Sync wait time. |
||
724 | */ |
||
725 | public function set_sync_wait_time( $seconds ) { |
||
728 | |||
729 | /** |
||
730 | * Get current sync wait time (in seconds). |
||
731 | * |
||
732 | * @access public |
||
733 | * |
||
734 | * @return int Sync wait time. |
||
735 | */ |
||
736 | public function get_sync_wait_time() { |
||
739 | |||
740 | /** |
||
741 | * Set the enqueue wait time (in seconds). |
||
742 | * |
||
743 | * @access public |
||
744 | * |
||
745 | * @param int $seconds Enqueue wait time. |
||
746 | */ |
||
747 | public function set_enqueue_wait_time( $seconds ) { |
||
750 | |||
751 | /** |
||
752 | * Get current enqueue wait time (in seconds). |
||
753 | * |
||
754 | * @access public |
||
755 | * |
||
756 | * @return int Enqueue wait time. |
||
757 | */ |
||
758 | public function get_enqueue_wait_time() { |
||
761 | |||
762 | /** |
||
763 | * Set the sync wait threshold (in seconds). |
||
764 | * |
||
765 | * @access public |
||
766 | * |
||
767 | * @param int $seconds Sync wait threshold. |
||
768 | */ |
||
769 | public function set_sync_wait_threshold( $seconds ) { |
||
772 | |||
773 | /** |
||
774 | * Get current sync wait threshold (in seconds). |
||
775 | * |
||
776 | * @access public |
||
777 | * |
||
778 | * @return int Sync wait threshold. |
||
779 | */ |
||
780 | public function get_sync_wait_threshold() { |
||
783 | |||
784 | /** |
||
785 | * Set the maximum time for perfirming a checkout of items from the queue (in seconds). |
||
786 | * |
||
787 | * @access public |
||
788 | * |
||
789 | * @param int $seconds Maximum dequeue time. |
||
790 | */ |
||
791 | public function set_max_dequeue_time( $seconds ) { |
||
794 | |||
795 | /** |
||
796 | * Initialize the sync queues, codec and set the default settings. |
||
797 | * |
||
798 | * @access public |
||
799 | */ |
||
800 | public function set_defaults() { |
||
816 | |||
817 | /** |
||
818 | * Reset sync queues, modules and settings. |
||
819 | * |
||
820 | * @access public |
||
821 | */ |
||
822 | public function reset_data() { |
||
836 | |||
837 | /** |
||
838 | * Perform cleanup at the event of plugin uninstallation. |
||
839 | * |
||
840 | * @access public |
||
841 | */ |
||
842 | public function uninstall() { |
||
853 | } |
||
854 |
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..