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 |
||
| 16 | class Sender { |
||
| 17 | /** |
||
| 18 | * Name of the option that stores the time of the next sync. |
||
| 19 | * |
||
| 20 | * @access public |
||
| 21 | * |
||
| 22 | * @var string |
||
| 23 | */ |
||
| 24 | const NEXT_SYNC_TIME_OPTION_NAME = 'jetpack_next_sync_time'; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Sync timeout after a WPCOM error. |
||
| 28 | * |
||
| 29 | * @access public |
||
| 30 | * |
||
| 31 | * @var int |
||
| 32 | */ |
||
| 33 | const WPCOM_ERROR_SYNC_DELAY = 60; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Sync timeout after a queue has been locked. |
||
| 37 | * |
||
| 38 | * @access public |
||
| 39 | * |
||
| 40 | * @var int |
||
| 41 | */ |
||
| 42 | const QUEUE_LOCKED_SYNC_DELAY = 10; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Maximum bytes to checkout without exceeding the memory limit. |
||
| 46 | * |
||
| 47 | * @access private |
||
| 48 | * |
||
| 49 | * @var int |
||
| 50 | */ |
||
| 51 | private $dequeue_max_bytes; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Maximum bytes in a single encoded item. |
||
| 55 | * |
||
| 56 | * @access private |
||
| 57 | * |
||
| 58 | * @var int |
||
| 59 | */ |
||
| 60 | private $upload_max_bytes; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Maximum number of sync items in a single action. |
||
| 64 | * |
||
| 65 | * @access private |
||
| 66 | * |
||
| 67 | * @var int |
||
| 68 | */ |
||
| 69 | private $upload_max_rows; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Maximum time for perfirming a checkout of items from the queue (in seconds). |
||
| 73 | * |
||
| 74 | * @access private |
||
| 75 | * |
||
| 76 | * @var int |
||
| 77 | */ |
||
| 78 | private $max_dequeue_time; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * How many seconds to wait after sending sync items after exceeding the sync wait threshold (in seconds). |
||
| 82 | * |
||
| 83 | * @access private |
||
| 84 | * |
||
| 85 | * @var int |
||
| 86 | */ |
||
| 87 | private $sync_wait_time; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * How much maximum time to wait for the checkout to finish (in seconds). |
||
| 91 | * |
||
| 92 | * @access private |
||
| 93 | * |
||
| 94 | * @var int |
||
| 95 | */ |
||
| 96 | private $sync_wait_threshold; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * How much maximum time to wait for the sync items to be queued for sending (in seconds). |
||
| 100 | * |
||
| 101 | * @access private |
||
| 102 | * |
||
| 103 | * @var int |
||
| 104 | */ |
||
| 105 | private $enqueue_wait_time; |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Incremental sync queue object. |
||
| 109 | * |
||
| 110 | * @access private |
||
| 111 | * |
||
| 112 | * @var Automattic\Jetpack\Sync\Queue |
||
| 113 | */ |
||
| 114 | private $sync_queue; |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Full sync queue object. |
||
| 118 | * |
||
| 119 | * @access private |
||
| 120 | * |
||
| 121 | * @var Automattic\Jetpack\Sync\Queue |
||
| 122 | */ |
||
| 123 | private $full_sync_queue; |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Codec object for encoding and decoding sync items. |
||
| 127 | * |
||
| 128 | * @access private |
||
| 129 | * |
||
| 130 | * @var Automattic\Jetpack\Sync\Codec_Interface |
||
| 131 | */ |
||
| 132 | private $codec; |
||
| 133 | |||
| 134 | /** |
||
| 135 | * The current user before we change or clear it. |
||
| 136 | * |
||
| 137 | * @access private |
||
| 138 | * |
||
| 139 | * @var \WP_User |
||
| 140 | */ |
||
| 141 | private $old_user; |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Container for the singleton instance of this class. |
||
| 145 | * |
||
| 146 | * @access private |
||
| 147 | * @static |
||
| 148 | * |
||
| 149 | * @var Automattic\Jetpack\Sync\Sender |
||
| 150 | */ |
||
| 151 | private static $instance; |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Retrieve the singleton instance of this class. |
||
| 155 | * |
||
| 156 | * @access public |
||
| 157 | * @static |
||
| 158 | * |
||
| 159 | * @return Sender |
||
| 160 | */ |
||
| 161 | public static function get_instance() { |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Constructor. |
||
| 171 | * This is necessary because you can't use "new" when you declare instance properties >:( |
||
| 172 | * |
||
| 173 | * @access protected |
||
| 174 | * @static |
||
| 175 | */ |
||
| 176 | protected function __construct() { |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Initialize the sender. |
||
| 183 | * Prepares the current user and initializes all sync modules. |
||
| 184 | * |
||
| 185 | * @access private |
||
| 186 | */ |
||
| 187 | private function init() { |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Detect if this is a XMLRPC request with a valid signature. |
||
| 198 | * If so, changes the user to the new one. |
||
| 199 | * |
||
| 200 | * @access public |
||
| 201 | */ |
||
| 202 | public function maybe_set_user_from_token() { |
||
| 214 | |||
| 215 | /** |
||
| 216 | * If we used to have a previous current user, revert back to it. |
||
| 217 | * |
||
| 218 | * @access public |
||
| 219 | */ |
||
| 220 | public function maybe_clear_user_from_token() { |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Retrieve the next sync time. |
||
| 228 | * |
||
| 229 | * @access public |
||
| 230 | * |
||
| 231 | * @param string $queue_name Name of the queue. |
||
| 232 | * @return float Timestamp of the next sync. |
||
| 233 | */ |
||
| 234 | public function get_next_sync_time( $queue_name ) { |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Set the next sync time. |
||
| 240 | * |
||
| 241 | * @access public |
||
| 242 | * |
||
| 243 | * @param int $time Timestamp of the next sync. |
||
| 244 | * @param string $queue_name Name of the queue. |
||
| 245 | * @return boolean True if update was successful, false otherwise. |
||
| 246 | */ |
||
| 247 | public function set_next_sync_time( $time, $queue_name ) { |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Trigger a full sync. |
||
| 253 | * |
||
| 254 | * @access public |
||
| 255 | * |
||
| 256 | * @return boolean|\WP_Error True if this sync sending was successful, error object otherwise. |
||
| 257 | */ |
||
| 258 | public function do_full_sync() { |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Enqueue the next sync items for sending. |
||
| 289 | * Will not be done if the current request is a WP import one. |
||
| 290 | * Will be delayed until the next sync time comes. |
||
| 291 | * |
||
| 292 | * @access private |
||
| 293 | */ |
||
| 294 | private function continue_full_sync_enqueue() { |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Trigger incremental sync. |
||
| 310 | * |
||
| 311 | * @access public |
||
| 312 | * |
||
| 313 | * @return boolean|\WP_Error True if this sync sending was successful, error object otherwise. |
||
| 314 | */ |
||
| 315 | public function do_sync() { |
||
| 318 | |||
| 319 | /** |
||
| 320 | * Trigger sync for a certain sync queue. |
||
| 321 | * Responsible for setting next sync time. |
||
| 322 | * Will not be delayed if the current request is a WP import one. |
||
| 323 | * Will be delayed until the next sync time comes. |
||
| 324 | * |
||
| 325 | * @access public |
||
| 326 | * |
||
| 327 | * @param Automattic\Jetpack\Sync\Queue $queue Queue object. |
||
| 328 | * @return boolean|\WP_Error True if this sync sending was successful, error object otherwise. |
||
| 329 | */ |
||
| 330 | public function do_sync_and_set_delays( $queue ) { |
||
| 384 | |||
| 385 | /** |
||
| 386 | * Retrieve the next sync items to send. |
||
| 387 | * |
||
| 388 | * @access public |
||
| 389 | * |
||
| 390 | * @param (array|Automattic\Jetpack\Sync\Queue_Buffer) $buffer_or_items Queue buffer or array of objects. |
||
| 391 | * @param boolean $encode Whether to encode the items. |
||
| 392 | * @return array Sync items to send. |
||
| 393 | */ |
||
| 394 | public function get_items_to_send( $buffer_or_items, $encode = true ) { |
||
| 441 | |||
| 442 | /** |
||
| 443 | * If supported, flush all response data to the client and finish the request. |
||
| 444 | * This allows for time consuming tasks to be performed without leaving the connection open. |
||
| 445 | * |
||
| 446 | * @access private |
||
| 447 | */ |
||
| 448 | private function fastcgi_finish_request() { |
||
| 453 | |||
| 454 | /** |
||
| 455 | * Perform sync for a certain sync queue. |
||
| 456 | * |
||
| 457 | * @access public |
||
| 458 | * |
||
| 459 | * @param Automattic\Jetpack\Sync\Queue $queue Queue object. |
||
| 460 | * @return boolean|\WP_Error True if this sync sending was successful, error object otherwise. |
||
| 461 | */ |
||
| 462 | public function do_sync_for_queue( $queue ) { |
||
| 565 | |||
| 566 | /** |
||
| 567 | * Immediately sends a single item without firing or enqueuing it |
||
| 568 | * |
||
| 569 | * @param string $action_name The action. |
||
| 570 | * @param array $data The data associated with the action. |
||
| 571 | * |
||
| 572 | * @return Items processed. TODO: this doesn't make much sense anymore, it should probably be just a bool. |
||
| 573 | */ |
||
| 574 | public function send_action( $action_name, $data = null ) { |
||
| 599 | |||
| 600 | /** |
||
| 601 | * Create an synthetic action for direct sending to WPCOM during full sync (for example) |
||
| 602 | * |
||
| 603 | * @access private |
||
| 604 | * |
||
| 605 | * @param string $action_name The action. |
||
| 606 | * @param array $data The data associated with the action. |
||
| 607 | * @return array An array of synthetic sync actions keyed by current microtime(true) |
||
| 608 | */ |
||
| 609 | private function create_action_to_send( $action_name, $data ) { |
||
| 620 | |||
| 621 | /** |
||
| 622 | * Returns any object that is able to be synced. |
||
| 623 | * |
||
| 624 | * @access public |
||
| 625 | * |
||
| 626 | * @param array $args the synchronized object parameters. |
||
| 627 | * @return string Encoded sync object. |
||
| 628 | */ |
||
| 629 | public function sync_object( $args ) { |
||
| 638 | |||
| 639 | /** |
||
| 640 | * Register additional sync XML-RPC methods available to Jetpack for authenticated users. |
||
| 641 | * |
||
| 642 | * @access public |
||
| 643 | * @since 7.8 |
||
| 644 | * |
||
| 645 | * @param array $jetpack_methods XML-RPC methods available to the Jetpack Server. |
||
| 646 | * @return array Filtered XML-RPC methods. |
||
| 647 | */ |
||
| 648 | public function register_jetpack_xmlrpc_methods( $jetpack_methods ) { |
||
| 652 | |||
| 653 | /** |
||
| 654 | * Get the incremental sync queue object. |
||
| 655 | * |
||
| 656 | * @access public |
||
| 657 | * |
||
| 658 | * @return Automattic\Jetpack\Sync\Queue Queue object. |
||
| 659 | */ |
||
| 660 | public function get_sync_queue() { |
||
| 663 | |||
| 664 | /** |
||
| 665 | * Get the full sync queue object. |
||
| 666 | * |
||
| 667 | * @access public |
||
| 668 | * |
||
| 669 | * @return Automattic\Jetpack\Sync\Queue Queue object. |
||
| 670 | */ |
||
| 671 | public function get_full_sync_queue() { |
||
| 674 | |||
| 675 | /** |
||
| 676 | * Get the codec object. |
||
| 677 | * |
||
| 678 | * @access public |
||
| 679 | * |
||
| 680 | * @return Automattic\Jetpack\Sync\Codec_Interface Codec object. |
||
| 681 | */ |
||
| 682 | public function get_codec() { |
||
| 685 | |||
| 686 | /** |
||
| 687 | * Determine the codec object. |
||
| 688 | * Use gzip deflate if supported. |
||
| 689 | * |
||
| 690 | * @access public |
||
| 691 | */ |
||
| 692 | public function set_codec() { |
||
| 699 | |||
| 700 | /** |
||
| 701 | * Compute and send all the checksums. |
||
| 702 | * |
||
| 703 | * @access public |
||
| 704 | */ |
||
| 705 | public function send_checksum() { |
||
| 709 | |||
| 710 | /** |
||
| 711 | * Reset the incremental sync queue. |
||
| 712 | * |
||
| 713 | * @access public |
||
| 714 | */ |
||
| 715 | public function reset_sync_queue() { |
||
| 718 | |||
| 719 | /** |
||
| 720 | * Reset the full sync queue. |
||
| 721 | * |
||
| 722 | * @access public |
||
| 723 | */ |
||
| 724 | public function reset_full_sync_queue() { |
||
| 727 | |||
| 728 | /** |
||
| 729 | * Set the maximum bytes to checkout without exceeding the memory limit. |
||
| 730 | * |
||
| 731 | * @access public |
||
| 732 | * |
||
| 733 | * @param int $size Maximum bytes to checkout. |
||
| 734 | */ |
||
| 735 | public function set_dequeue_max_bytes( $size ) { |
||
| 738 | |||
| 739 | /** |
||
| 740 | * Set the maximum bytes in a single encoded item. |
||
| 741 | * |
||
| 742 | * @access public |
||
| 743 | * |
||
| 744 | * @param int $max_bytes Maximum bytes in a single encoded item. |
||
| 745 | */ |
||
| 746 | public function set_upload_max_bytes( $max_bytes ) { |
||
| 749 | |||
| 750 | /** |
||
| 751 | * Set the maximum number of sync items in a single action. |
||
| 752 | * |
||
| 753 | * @access public |
||
| 754 | * |
||
| 755 | * @param int $max_rows Maximum number of sync items. |
||
| 756 | */ |
||
| 757 | public function set_upload_max_rows( $max_rows ) { |
||
| 760 | |||
| 761 | /** |
||
| 762 | * Set the sync wait time (in seconds). |
||
| 763 | * |
||
| 764 | * @access public |
||
| 765 | * |
||
| 766 | * @param int $seconds Sync wait time. |
||
| 767 | */ |
||
| 768 | public function set_sync_wait_time( $seconds ) { |
||
| 771 | |||
| 772 | /** |
||
| 773 | * Get current sync wait time (in seconds). |
||
| 774 | * |
||
| 775 | * @access public |
||
| 776 | * |
||
| 777 | * @return int Sync wait time. |
||
| 778 | */ |
||
| 779 | public function get_sync_wait_time() { |
||
| 782 | |||
| 783 | /** |
||
| 784 | * Set the enqueue wait time (in seconds). |
||
| 785 | * |
||
| 786 | * @access public |
||
| 787 | * |
||
| 788 | * @param int $seconds Enqueue wait time. |
||
| 789 | */ |
||
| 790 | public function set_enqueue_wait_time( $seconds ) { |
||
| 793 | |||
| 794 | /** |
||
| 795 | * Get current enqueue wait time (in seconds). |
||
| 796 | * |
||
| 797 | * @access public |
||
| 798 | * |
||
| 799 | * @return int Enqueue wait time. |
||
| 800 | */ |
||
| 801 | public function get_enqueue_wait_time() { |
||
| 804 | |||
| 805 | /** |
||
| 806 | * Set the sync wait threshold (in seconds). |
||
| 807 | * |
||
| 808 | * @access public |
||
| 809 | * |
||
| 810 | * @param int $seconds Sync wait threshold. |
||
| 811 | */ |
||
| 812 | public function set_sync_wait_threshold( $seconds ) { |
||
| 815 | |||
| 816 | /** |
||
| 817 | * Get current sync wait threshold (in seconds). |
||
| 818 | * |
||
| 819 | * @access public |
||
| 820 | * |
||
| 821 | * @return int Sync wait threshold. |
||
| 822 | */ |
||
| 823 | public function get_sync_wait_threshold() { |
||
| 826 | |||
| 827 | /** |
||
| 828 | * Set the maximum time for perfirming a checkout of items from the queue (in seconds). |
||
| 829 | * |
||
| 830 | * @access public |
||
| 831 | * |
||
| 832 | * @param int $seconds Maximum dequeue time. |
||
| 833 | */ |
||
| 834 | public function set_max_dequeue_time( $seconds ) { |
||
| 837 | |||
| 838 | /** |
||
| 839 | * Initialize the sync queues, codec and set the default settings. |
||
| 840 | * |
||
| 841 | * @access public |
||
| 842 | */ |
||
| 843 | public function set_defaults() { |
||
| 859 | |||
| 860 | /** |
||
| 861 | * Reset sync queues, modules and settings. |
||
| 862 | * |
||
| 863 | * @access public |
||
| 864 | */ |
||
| 865 | public function reset_data() { |
||
| 879 | |||
| 880 | /** |
||
| 881 | * Perform cleanup at the event of plugin uninstallation. |
||
| 882 | * |
||
| 883 | * @access public |
||
| 884 | */ |
||
| 885 | public function uninstall() { |
||
| 896 | } |
||
| 897 |
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..