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() { |
||
| 268 | |||
| 269 | /** |
||
| 270 | * Enqueue the next sync items for sending. |
||
| 271 | * Will not be done if the current request is a WP import one. |
||
| 272 | * Will be delayed until the next sync time comes. |
||
| 273 | * |
||
| 274 | * @access private |
||
| 275 | */ |
||
| 276 | private function continue_full_sync_enqueue() { |
||
| 289 | |||
| 290 | /** |
||
| 291 | * Trigger incremental sync. |
||
| 292 | * |
||
| 293 | * @access public |
||
| 294 | * |
||
| 295 | * @return boolean|\WP_Error True if this sync sending was successful, error object otherwise. |
||
| 296 | */ |
||
| 297 | public function do_sync() { |
||
| 300 | |||
| 301 | /** |
||
| 302 | * Trigger sync for a certain sync queue. |
||
| 303 | * Responsible for setting next sync time. |
||
| 304 | * Will not be delayed if the current request is a WP import one. |
||
| 305 | * Will be delayed until the next sync time comes. |
||
| 306 | * |
||
| 307 | * @access public |
||
| 308 | * |
||
| 309 | * @param Automattic\Jetpack\Sync\Queue $queue Queue object. |
||
| 310 | * @return boolean|\WP_Error True if this sync sending was successful, error object otherwise. |
||
| 311 | */ |
||
| 312 | public function do_sync_and_set_delays( $queue ) { |
||
| 351 | |||
| 352 | /** |
||
| 353 | * Retrieve the next sync items to send. |
||
| 354 | * |
||
| 355 | * @access public |
||
| 356 | * |
||
| 357 | * @param (array|Automattic\Jetpack\Sync\Queue_Buffer) $buffer_or_items Queue buffer or array of objects. |
||
| 358 | * @param boolean $encode Whether to encode the items. |
||
| 359 | * @return array Sync items to send. |
||
| 360 | */ |
||
| 361 | public function get_items_to_send( $buffer_or_items, $encode = true ) { |
||
| 408 | |||
| 409 | /** |
||
| 410 | * If supported, flush all response data to the client and finish the request. |
||
| 411 | * This allows for time consuming tasks to be performed without leaving the connection open. |
||
| 412 | * |
||
| 413 | * @access private |
||
| 414 | */ |
||
| 415 | private function fastcgi_finish_request() { |
||
| 420 | |||
| 421 | /** |
||
| 422 | * Perform sync for a certain sync queue. |
||
| 423 | * |
||
| 424 | * @access public |
||
| 425 | * |
||
| 426 | * @param Automattic\Jetpack\Sync\Queue $queue Queue object. |
||
| 427 | * @return boolean|\WP_Error True if this sync sending was successful, error object otherwise. |
||
| 428 | */ |
||
| 429 | public function do_sync_for_queue( $queue ) { |
||
| 532 | |||
| 533 | /** |
||
| 534 | * Immediately sends a single item without firing or enqueuing it |
||
| 535 | * |
||
| 536 | * @param string $action_name The action. |
||
| 537 | * @param array $data The data associated with the action. |
||
| 538 | * |
||
| 539 | * @return Items processed. TODO: this doesn't make much sense anymore, it should probably be just a bool. |
||
| 540 | */ |
||
| 541 | public function send_action( $action_name, $data = null ) { |
||
| 566 | |||
| 567 | /** |
||
| 568 | * Create an synthetic action for direct sending to WPCOM during full sync (for example) |
||
| 569 | * |
||
| 570 | * @access private |
||
| 571 | * |
||
| 572 | * @param string $action_name The action. |
||
| 573 | * @param array $data The data associated with the action. |
||
| 574 | * @return array An array of synthetic sync actions keyed by current microtime(true) |
||
| 575 | */ |
||
| 576 | private function create_action_to_send( $action_name, $data ) { |
||
| 587 | |||
| 588 | /** |
||
| 589 | * Returns any object that is able to be synced. |
||
| 590 | * |
||
| 591 | * @access public |
||
| 592 | * |
||
| 593 | * @param array $args the synchronized object parameters. |
||
| 594 | * @return string Encoded sync object. |
||
| 595 | */ |
||
| 596 | public function sync_object( $args ) { |
||
| 605 | |||
| 606 | /** |
||
| 607 | * Register additional sync XML-RPC methods available to Jetpack for authenticated users. |
||
| 608 | * |
||
| 609 | * @access public |
||
| 610 | * @since 7.8 |
||
| 611 | * |
||
| 612 | * @param array $jetpack_methods XML-RPC methods available to the Jetpack Server. |
||
| 613 | * @return array Filtered XML-RPC methods. |
||
| 614 | */ |
||
| 615 | public function register_jetpack_xmlrpc_methods( $jetpack_methods ) { |
||
| 619 | |||
| 620 | /** |
||
| 621 | * Get the incremental sync queue object. |
||
| 622 | * |
||
| 623 | * @access public |
||
| 624 | * |
||
| 625 | * @return Automattic\Jetpack\Sync\Queue Queue object. |
||
| 626 | */ |
||
| 627 | public function get_sync_queue() { |
||
| 630 | |||
| 631 | /** |
||
| 632 | * Get the full sync queue object. |
||
| 633 | * |
||
| 634 | * @access public |
||
| 635 | * |
||
| 636 | * @return Automattic\Jetpack\Sync\Queue Queue object. |
||
| 637 | */ |
||
| 638 | public function get_full_sync_queue() { |
||
| 641 | |||
| 642 | /** |
||
| 643 | * Get the codec object. |
||
| 644 | * |
||
| 645 | * @access public |
||
| 646 | * |
||
| 647 | * @return Automattic\Jetpack\Sync\Codec_Interface Codec object. |
||
| 648 | */ |
||
| 649 | public function get_codec() { |
||
| 652 | |||
| 653 | /** |
||
| 654 | * Determine the codec object. |
||
| 655 | * Use gzip deflate if supported. |
||
| 656 | * |
||
| 657 | * @access public |
||
| 658 | */ |
||
| 659 | public function set_codec() { |
||
| 666 | |||
| 667 | /** |
||
| 668 | * Compute and send all the checksums. |
||
| 669 | * |
||
| 670 | * @access public |
||
| 671 | */ |
||
| 672 | public function send_checksum() { |
||
| 676 | |||
| 677 | /** |
||
| 678 | * Reset the incremental sync queue. |
||
| 679 | * |
||
| 680 | * @access public |
||
| 681 | */ |
||
| 682 | public function reset_sync_queue() { |
||
| 685 | |||
| 686 | /** |
||
| 687 | * Reset the full sync queue. |
||
| 688 | * |
||
| 689 | * @access public |
||
| 690 | */ |
||
| 691 | public function reset_full_sync_queue() { |
||
| 694 | |||
| 695 | /** |
||
| 696 | * Set the maximum bytes to checkout without exceeding the memory limit. |
||
| 697 | * |
||
| 698 | * @access public |
||
| 699 | * |
||
| 700 | * @param int $size Maximum bytes to checkout. |
||
| 701 | */ |
||
| 702 | public function set_dequeue_max_bytes( $size ) { |
||
| 705 | |||
| 706 | /** |
||
| 707 | * Set the maximum bytes in a single encoded item. |
||
| 708 | * |
||
| 709 | * @access public |
||
| 710 | * |
||
| 711 | * @param int $max_bytes Maximum bytes in a single encoded item. |
||
| 712 | */ |
||
| 713 | public function set_upload_max_bytes( $max_bytes ) { |
||
| 716 | |||
| 717 | /** |
||
| 718 | * Set the maximum number of sync items in a single action. |
||
| 719 | * |
||
| 720 | * @access public |
||
| 721 | * |
||
| 722 | * @param int $max_rows Maximum number of sync items. |
||
| 723 | */ |
||
| 724 | public function set_upload_max_rows( $max_rows ) { |
||
| 727 | |||
| 728 | /** |
||
| 729 | * Set the sync wait time (in seconds). |
||
| 730 | * |
||
| 731 | * @access public |
||
| 732 | * |
||
| 733 | * @param int $seconds Sync wait time. |
||
| 734 | */ |
||
| 735 | public function set_sync_wait_time( $seconds ) { |
||
| 738 | |||
| 739 | /** |
||
| 740 | * Get current sync wait time (in seconds). |
||
| 741 | * |
||
| 742 | * @access public |
||
| 743 | * |
||
| 744 | * @return int Sync wait time. |
||
| 745 | */ |
||
| 746 | public function get_sync_wait_time() { |
||
| 749 | |||
| 750 | /** |
||
| 751 | * Set the enqueue wait time (in seconds). |
||
| 752 | * |
||
| 753 | * @access public |
||
| 754 | * |
||
| 755 | * @param int $seconds Enqueue wait time. |
||
| 756 | */ |
||
| 757 | public function set_enqueue_wait_time( $seconds ) { |
||
| 760 | |||
| 761 | /** |
||
| 762 | * Get current enqueue wait time (in seconds). |
||
| 763 | * |
||
| 764 | * @access public |
||
| 765 | * |
||
| 766 | * @return int Enqueue wait time. |
||
| 767 | */ |
||
| 768 | public function get_enqueue_wait_time() { |
||
| 771 | |||
| 772 | /** |
||
| 773 | * Set the sync wait threshold (in seconds). |
||
| 774 | * |
||
| 775 | * @access public |
||
| 776 | * |
||
| 777 | * @param int $seconds Sync wait threshold. |
||
| 778 | */ |
||
| 779 | public function set_sync_wait_threshold( $seconds ) { |
||
| 782 | |||
| 783 | /** |
||
| 784 | * Get current sync wait threshold (in seconds). |
||
| 785 | * |
||
| 786 | * @access public |
||
| 787 | * |
||
| 788 | * @return int Sync wait threshold. |
||
| 789 | */ |
||
| 790 | public function get_sync_wait_threshold() { |
||
| 793 | |||
| 794 | /** |
||
| 795 | * Set the maximum time for perfirming a checkout of items from the queue (in seconds). |
||
| 796 | * |
||
| 797 | * @access public |
||
| 798 | * |
||
| 799 | * @param int $seconds Maximum dequeue time. |
||
| 800 | */ |
||
| 801 | public function set_max_dequeue_time( $seconds ) { |
||
| 804 | |||
| 805 | /** |
||
| 806 | * Initialize the sync queues, codec and set the default settings. |
||
| 807 | * |
||
| 808 | * @access public |
||
| 809 | */ |
||
| 810 | public function set_defaults() { |
||
| 826 | |||
| 827 | /** |
||
| 828 | * Reset sync queues, modules and settings. |
||
| 829 | * |
||
| 830 | * @access public |
||
| 831 | */ |
||
| 832 | public function reset_data() { |
||
| 846 | |||
| 847 | /** |
||
| 848 | * Perform cleanup at the event of plugin uninstallation. |
||
| 849 | * |
||
| 850 | * @access public |
||
| 851 | */ |
||
| 852 | public function uninstall() { |
||
| 863 | } |
||
| 864 |
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..