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() { |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Enqueue the next sync items for sending. |
||
| 283 | * Will not be done if the current request is a WP import one. |
||
| 284 | * Will be delayed until the next sync time comes. |
||
| 285 | * |
||
| 286 | * @access private |
||
| 287 | */ |
||
| 288 | private function continue_full_sync_enqueue() { |
||
| 301 | |||
| 302 | /** |
||
| 303 | * Trigger incremental sync. |
||
| 304 | * |
||
| 305 | * @access public |
||
| 306 | * |
||
| 307 | * @return boolean|\WP_Error True if this sync sending was successful, error object otherwise. |
||
| 308 | */ |
||
| 309 | public function do_sync() { |
||
| 312 | |||
| 313 | /** |
||
| 314 | * Trigger sync for a certain sync queue. |
||
| 315 | * Responsible for setting next sync time. |
||
| 316 | * Will not be delayed if the current request is a WP import one. |
||
| 317 | * Will be delayed until the next sync time comes. |
||
| 318 | * |
||
| 319 | * @access public |
||
| 320 | * |
||
| 321 | * @param Automattic\Jetpack\Sync\Queue $queue Queue object. |
||
| 322 | * @return boolean|\WP_Error True if this sync sending was successful, error object otherwise. |
||
| 323 | */ |
||
| 324 | public function do_sync_and_set_delays( $queue ) { |
||
| 363 | |||
| 364 | /** |
||
| 365 | * Retrieve the next sync items to send. |
||
| 366 | * |
||
| 367 | * @access public |
||
| 368 | * |
||
| 369 | * @param (array|Automattic\Jetpack\Sync\Queue_Buffer) $buffer_or_items Queue buffer or array of objects. |
||
| 370 | * @param boolean $encode Whether to encode the items. |
||
| 371 | * @return array Sync items to send. |
||
| 372 | */ |
||
| 373 | public function get_items_to_send( $buffer_or_items, $encode = true ) { |
||
| 420 | |||
| 421 | /** |
||
| 422 | * If supported, flush all response data to the client and finish the request. |
||
| 423 | * This allows for time consuming tasks to be performed without leaving the connection open. |
||
| 424 | * |
||
| 425 | * @access private |
||
| 426 | */ |
||
| 427 | private function fastcgi_finish_request() { |
||
| 432 | |||
| 433 | /** |
||
| 434 | * Perform sync for a certain sync queue. |
||
| 435 | * |
||
| 436 | * @access public |
||
| 437 | * |
||
| 438 | * @param Automattic\Jetpack\Sync\Queue $queue Queue object. |
||
| 439 | * @return boolean|\WP_Error True if this sync sending was successful, error object otherwise. |
||
| 440 | */ |
||
| 441 | public function do_sync_for_queue( $queue ) { |
||
| 544 | |||
| 545 | /** |
||
| 546 | * Immediately sends a single item without firing or enqueuing it |
||
| 547 | * |
||
| 548 | * @param string $action_name The action. |
||
| 549 | * @param array $data The data associated with the action. |
||
| 550 | * |
||
| 551 | * @return Items processed. TODO: this doesn't make much sense anymore, it should probably be just a bool. |
||
| 552 | */ |
||
| 553 | public function send_action( $action_name, $data = null ) { |
||
| 578 | |||
| 579 | /** |
||
| 580 | * Create an synthetic action for direct sending to WPCOM during full sync (for example) |
||
| 581 | * |
||
| 582 | * @access private |
||
| 583 | * |
||
| 584 | * @param string $action_name The action. |
||
| 585 | * @param array $data The data associated with the action. |
||
| 586 | * @return array An array of synthetic sync actions keyed by current microtime(true) |
||
| 587 | */ |
||
| 588 | private function create_action_to_send( $action_name, $data ) { |
||
| 599 | |||
| 600 | /** |
||
| 601 | * Returns any object that is able to be synced. |
||
| 602 | * |
||
| 603 | * @access public |
||
| 604 | * |
||
| 605 | * @param array $args the synchronized object parameters. |
||
| 606 | * @return string Encoded sync object. |
||
| 607 | */ |
||
| 608 | public function sync_object( $args ) { |
||
| 617 | |||
| 618 | /** |
||
| 619 | * Register additional sync XML-RPC methods available to Jetpack for authenticated users. |
||
| 620 | * |
||
| 621 | * @access public |
||
| 622 | * @since 7.8 |
||
| 623 | * |
||
| 624 | * @param array $jetpack_methods XML-RPC methods available to the Jetpack Server. |
||
| 625 | * @return array Filtered XML-RPC methods. |
||
| 626 | */ |
||
| 627 | public function register_jetpack_xmlrpc_methods( $jetpack_methods ) { |
||
| 631 | |||
| 632 | /** |
||
| 633 | * Get the incremental sync queue object. |
||
| 634 | * |
||
| 635 | * @access public |
||
| 636 | * |
||
| 637 | * @return Automattic\Jetpack\Sync\Queue Queue object. |
||
| 638 | */ |
||
| 639 | public function get_sync_queue() { |
||
| 642 | |||
| 643 | /** |
||
| 644 | * Get the full sync queue object. |
||
| 645 | * |
||
| 646 | * @access public |
||
| 647 | * |
||
| 648 | * @return Automattic\Jetpack\Sync\Queue Queue object. |
||
| 649 | */ |
||
| 650 | public function get_full_sync_queue() { |
||
| 653 | |||
| 654 | /** |
||
| 655 | * Get the codec object. |
||
| 656 | * |
||
| 657 | * @access public |
||
| 658 | * |
||
| 659 | * @return Automattic\Jetpack\Sync\Codec_Interface Codec object. |
||
| 660 | */ |
||
| 661 | public function get_codec() { |
||
| 664 | |||
| 665 | /** |
||
| 666 | * Determine the codec object. |
||
| 667 | * Use gzip deflate if supported. |
||
| 668 | * |
||
| 669 | * @access public |
||
| 670 | */ |
||
| 671 | public function set_codec() { |
||
| 678 | |||
| 679 | /** |
||
| 680 | * Compute and send all the checksums. |
||
| 681 | * |
||
| 682 | * @access public |
||
| 683 | */ |
||
| 684 | public function send_checksum() { |
||
| 688 | |||
| 689 | /** |
||
| 690 | * Reset the incremental sync queue. |
||
| 691 | * |
||
| 692 | * @access public |
||
| 693 | */ |
||
| 694 | public function reset_sync_queue() { |
||
| 697 | |||
| 698 | /** |
||
| 699 | * Reset the full sync queue. |
||
| 700 | * |
||
| 701 | * @access public |
||
| 702 | */ |
||
| 703 | public function reset_full_sync_queue() { |
||
| 706 | |||
| 707 | /** |
||
| 708 | * Set the maximum bytes to checkout without exceeding the memory limit. |
||
| 709 | * |
||
| 710 | * @access public |
||
| 711 | * |
||
| 712 | * @param int $size Maximum bytes to checkout. |
||
| 713 | */ |
||
| 714 | public function set_dequeue_max_bytes( $size ) { |
||
| 717 | |||
| 718 | /** |
||
| 719 | * Set the maximum bytes in a single encoded item. |
||
| 720 | * |
||
| 721 | * @access public |
||
| 722 | * |
||
| 723 | * @param int $max_bytes Maximum bytes in a single encoded item. |
||
| 724 | */ |
||
| 725 | public function set_upload_max_bytes( $max_bytes ) { |
||
| 728 | |||
| 729 | /** |
||
| 730 | * Set the maximum number of sync items in a single action. |
||
| 731 | * |
||
| 732 | * @access public |
||
| 733 | * |
||
| 734 | * @param int $max_rows Maximum number of sync items. |
||
| 735 | */ |
||
| 736 | public function set_upload_max_rows( $max_rows ) { |
||
| 739 | |||
| 740 | /** |
||
| 741 | * Set the sync wait time (in seconds). |
||
| 742 | * |
||
| 743 | * @access public |
||
| 744 | * |
||
| 745 | * @param int $seconds Sync wait time. |
||
| 746 | */ |
||
| 747 | public function set_sync_wait_time( $seconds ) { |
||
| 750 | |||
| 751 | /** |
||
| 752 | * Get current sync wait time (in seconds). |
||
| 753 | * |
||
| 754 | * @access public |
||
| 755 | * |
||
| 756 | * @return int Sync wait time. |
||
| 757 | */ |
||
| 758 | public function get_sync_wait_time() { |
||
| 761 | |||
| 762 | /** |
||
| 763 | * Set the enqueue wait time (in seconds). |
||
| 764 | * |
||
| 765 | * @access public |
||
| 766 | * |
||
| 767 | * @param int $seconds Enqueue wait time. |
||
| 768 | */ |
||
| 769 | public function set_enqueue_wait_time( $seconds ) { |
||
| 772 | |||
| 773 | /** |
||
| 774 | * Get current enqueue wait time (in seconds). |
||
| 775 | * |
||
| 776 | * @access public |
||
| 777 | * |
||
| 778 | * @return int Enqueue wait time. |
||
| 779 | */ |
||
| 780 | public function get_enqueue_wait_time() { |
||
| 783 | |||
| 784 | /** |
||
| 785 | * Set the sync wait threshold (in seconds). |
||
| 786 | * |
||
| 787 | * @access public |
||
| 788 | * |
||
| 789 | * @param int $seconds Sync wait threshold. |
||
| 790 | */ |
||
| 791 | public function set_sync_wait_threshold( $seconds ) { |
||
| 794 | |||
| 795 | /** |
||
| 796 | * Get current sync wait threshold (in seconds). |
||
| 797 | * |
||
| 798 | * @access public |
||
| 799 | * |
||
| 800 | * @return int Sync wait threshold. |
||
| 801 | */ |
||
| 802 | public function get_sync_wait_threshold() { |
||
| 805 | |||
| 806 | /** |
||
| 807 | * Set the maximum time for perfirming a checkout of items from the queue (in seconds). |
||
| 808 | * |
||
| 809 | * @access public |
||
| 810 | * |
||
| 811 | * @param int $seconds Maximum dequeue time. |
||
| 812 | */ |
||
| 813 | public function set_max_dequeue_time( $seconds ) { |
||
| 816 | |||
| 817 | /** |
||
| 818 | * Initialize the sync queues, codec and set the default settings. |
||
| 819 | * |
||
| 820 | * @access public |
||
| 821 | */ |
||
| 822 | public function set_defaults() { |
||
| 838 | |||
| 839 | /** |
||
| 840 | * Reset sync queues, modules and settings. |
||
| 841 | * |
||
| 842 | * @access public |
||
| 843 | */ |
||
| 844 | public function reset_data() { |
||
| 858 | |||
| 859 | /** |
||
| 860 | * Perform cleanup at the event of plugin uninstallation. |
||
| 861 | * |
||
| 862 | * @access public |
||
| 863 | */ |
||
| 864 | public function uninstall() { |
||
| 875 | } |
||
| 876 |
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..