Complex classes like Jetpack_Sync_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 Jetpack_Sync_Sender, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 12 | class Jetpack_Sync_Sender { |
||
| 13 | |||
| 14 | const NEXT_SYNC_TIME_OPTION_NAME = 'jetpack_next_sync_time'; |
||
| 15 | const WPCOM_ERROR_SYNC_DELAY = 60; |
||
| 16 | const QUEUE_LOCKED_SYNC_DELAY = 10; |
||
| 17 | |||
| 18 | private $dequeue_max_bytes; |
||
| 19 | private $upload_max_bytes; |
||
| 20 | private $upload_max_rows; |
||
| 21 | private $max_dequeue_time; |
||
| 22 | private $sync_wait_time; |
||
| 23 | private $sync_wait_threshold; |
||
| 24 | private $sync_queue; |
||
| 25 | private $full_sync_queue; |
||
| 26 | private $codec; |
||
| 27 | |||
| 28 | // singleton functions |
||
| 29 | private static $instance; |
||
| 30 | |||
| 31 | public static function get_instance() { |
||
| 38 | |||
| 39 | // this is necessary because you can't use "new" when you declare instance properties >:( |
||
| 40 | protected function __construct() { |
||
| 44 | |||
| 45 | private function init() { |
||
| 50 | |||
| 51 | public function get_next_sync_time( $queue_name ) { |
||
| 52 | return (double) get_option( self::NEXT_SYNC_TIME_OPTION_NAME . '_' . $queue_name, 0 ); |
||
| 53 | } |
||
| 54 | |||
| 55 | public function set_next_sync_time( $time, $queue_name ) { |
||
| 56 | return update_option( self::NEXT_SYNC_TIME_OPTION_NAME . '_' . $queue_name, $time, true ); |
||
| 57 | } |
||
| 58 | |||
| 59 | public function do_full_sync() { |
||
| 60 | return $this->do_sync_and_set_delays( $this->full_sync_queue ); |
||
| 61 | } |
||
| 62 | |||
| 63 | public function do_sync() { |
||
| 64 | return $this->do_sync_and_set_delays( $this->sync_queue ); |
||
| 65 | } |
||
| 66 | |||
| 67 | public function do_sync_and_set_delays( $queue ) { |
||
| 68 | // don't sync if importing |
||
| 69 | if ( defined( 'WP_IMPORTING' ) && WP_IMPORTING ) { |
||
| 70 | return false; |
||
| 71 | } |
||
| 72 | |||
| 73 | // don't sync if we are throttled |
||
| 74 | if ( $this->get_next_sync_time( $queue->id ) > microtime( true ) ) { |
||
| 75 | return false; |
||
| 76 | } |
||
| 77 | |||
| 78 | $start_time = microtime( true ); |
||
| 79 | |||
| 80 | Jetpack_Sync_Settings::set_is_syncing( true ); |
||
| 81 | |||
| 82 | $sync_result = $this->do_sync_for_queue( $queue ); |
||
| 83 | |||
| 84 | Jetpack_Sync_Settings::set_is_syncing( false ); |
||
| 85 | |||
| 86 | $exceeded_sync_wait_threshold = ( microtime( true ) - $start_time ) > (double) $this->get_sync_wait_threshold(); |
||
| 87 | |||
| 88 | if ( is_wp_error( $sync_result ) ) { |
||
| 89 | if ( 'unclosed_buffer' === $sync_result->get_error_code() ) { |
||
| 90 | $this->set_next_sync_time( time() + self::QUEUE_LOCKED_SYNC_DELAY, $queue->id ); |
||
| 91 | } else { |
||
| 92 | $this->set_next_sync_time( time() + self::WPCOM_ERROR_SYNC_DELAY, $queue->id ); |
||
| 93 | } |
||
| 94 | $sync_result = false; |
||
| 95 | } elseif ( $exceeded_sync_wait_threshold ) { |
||
| 96 | // if we actually sent data and it took a while, wait before sending again |
||
| 97 | $this->set_next_sync_time( time() + $this->get_sync_wait_time(), $queue->id ); |
||
| 98 | } elseif ( ! ( $full_sync_result || $sync_result ) ) { |
||
|
|
|||
| 99 | // wait if both returned false |
||
| 100 | $this->set_next_sync_time( time() + self::WPCOM_REGULAR_SYNC_DELAY, $queue->id ); |
||
| 101 | } |
||
| 102 | |||
| 103 | return $sync_result; |
||
| 104 | } |
||
| 105 | |||
| 106 | public function get_items_to_send( $buffer, $encode = true ) { |
||
| 107 | // track how long we've been processing so we can avoid request timeouts |
||
| 108 | $start_time = microtime( true ); |
||
| 109 | $upload_size = 0; |
||
| 110 | $items_to_send = array(); |
||
| 111 | $items = $buffer->get_items(); |
||
| 112 | // set up current screen to avoid errors rendering content |
||
| 113 | require_once( ABSPATH . 'wp-admin/includes/class-wp-screen.php' ); |
||
| 114 | require_once( ABSPATH . 'wp-admin/includes/screen.php' ); |
||
| 115 | set_current_screen( 'sync' ); |
||
| 116 | $skipped_items_ids = array(); |
||
| 117 | // we estimate the total encoded size as we go by encoding each item individually |
||
| 118 | // this is expensive, but the only way to really know :/ |
||
| 119 | foreach ( $items as $key => $item ) { |
||
| 120 | // Suspending cache addition help prevent overloading in memory cache of large sites. |
||
| 121 | wp_suspend_cache_addition( true ); |
||
| 122 | /** |
||
| 123 | * Modify the data within an action before it is serialized and sent to the server |
||
| 124 | * For example, during full sync this expands Post ID's into full Post objects, |
||
| 125 | * so that we don't have to serialize the whole object into the queue. |
||
| 126 | * |
||
| 127 | * @since 4.2.0 |
||
| 128 | * |
||
| 129 | * @param array The action parameters |
||
| 130 | * @param int The ID of the user who triggered the action |
||
| 131 | */ |
||
| 132 | $item[1] = apply_filters( 'jetpack_sync_before_send_' . $item[0], $item[1], $item[2] ); |
||
| 133 | wp_suspend_cache_addition( false ); |
||
| 134 | if ( $item[1] === false ) { |
||
| 135 | $skipped_items_ids[] = $key; |
||
| 136 | continue; |
||
| 137 | } |
||
| 138 | $encoded_item = $encode ? $this->codec->encode( $item ) : $item; |
||
| 139 | $upload_size += strlen( $encoded_item ); |
||
| 140 | if ( $upload_size > $this->upload_max_bytes && count( $items_to_send ) > 0 ) { |
||
| 141 | break; |
||
| 142 | } |
||
| 143 | $items_to_send[ $key ] = $encoded_item; |
||
| 144 | if ( microtime(true) - $start_time > $this->max_dequeue_time ) { |
||
| 145 | break; |
||
| 146 | } |
||
| 147 | } |
||
| 148 | |||
| 149 | return array( $items_to_send, $skipped_items_ids, $items ); |
||
| 150 | } |
||
| 151 | |||
| 152 | public function do_sync_for_queue( $queue ) { |
||
| 153 | |||
| 154 | do_action( 'jetpack_sync_before_send_queue_' . $queue->id ); |
||
| 155 | if ( $queue->size() === 0 ) { |
||
| 156 | return false; |
||
| 157 | } |
||
| 158 | // now that we're sure we are about to sync, try to |
||
| 159 | // ignore user abort so we can avoid getting into a |
||
| 160 | // bad state |
||
| 161 | if ( function_exists( 'ignore_user_abort' ) ) { |
||
| 162 | ignore_user_abort( true ); |
||
| 163 | } |
||
| 164 | $buffer = $queue->checkout_with_memory_limit( $this->dequeue_max_bytes, $this->upload_max_rows ); |
||
| 165 | if ( ! $buffer ) { |
||
| 166 | // buffer has no items |
||
| 167 | return false; |
||
| 168 | } |
||
| 169 | if ( is_wp_error( $buffer ) ) { |
||
| 170 | return $buffer; |
||
| 171 | } |
||
| 172 | |||
| 173 | list( $items_to_send, $skipped_items_ids, $items ) = $this->get_items_to_send( $buffer, true ); |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Fires when data is ready to send to the server. |
||
| 177 | * Return false or WP_Error to abort the sync (e.g. if there's an error) |
||
| 178 | * The items will be automatically re-sent later |
||
| 179 | * |
||
| 180 | * @since 4.2.0 |
||
| 181 | * |
||
| 182 | * @param array $data The action buffer |
||
| 183 | * @param string $codec The codec name used to encode the data |
||
| 184 | * @param double $time The current time |
||
| 185 | * @param string $queue The queue used to send ('sync' or 'full_sync') |
||
| 186 | */ |
||
| 187 | Jetpack_Sync_Settings::set_is_sending( true ); |
||
| 188 | $processed_item_ids = apply_filters( 'jetpack_sync_send_data', $items_to_send, $this->codec->name(), microtime( true ), $queue->id ); |
||
| 189 | Jetpack_Sync_Settings::set_is_sending( false ); |
||
| 190 | |||
| 191 | if ( ! $processed_item_ids || is_wp_error( $processed_item_ids ) ) { |
||
| 192 | $checked_in_item_ids = $queue->checkin( $buffer ); |
||
| 193 | if ( is_wp_error( $checked_in_item_ids ) ) { |
||
| 194 | error_log( 'Error checking in buffer: ' . $checked_in_item_ids->get_error_message() ); |
||
| 195 | $queue->force_checkin(); |
||
| 196 | } |
||
| 197 | if ( is_wp_error( $processed_item_ids ) ) { |
||
| 198 | return $processed_item_ids; |
||
| 199 | } |
||
| 200 | // returning a WP_Error is a sign to the caller that we should wait a while |
||
| 201 | // before syncing again |
||
| 202 | return new WP_Error( 'server_error' ); |
||
| 203 | } else { |
||
| 204 | // detect if the last item ID was an error |
||
| 205 | $had_wp_error = is_wp_error( end( $processed_item_ids ) ); |
||
| 206 | if ( $had_wp_error ) { |
||
| 207 | $wp_error = array_pop( $processed_item_ids ); |
||
| 208 | } |
||
| 209 | // also checkin any items that were skipped |
||
| 210 | if ( count( $skipped_items_ids ) > 0 ) { |
||
| 211 | $processed_item_ids = array_merge( $processed_item_ids, $skipped_items_ids ); |
||
| 212 | } |
||
| 213 | $processed_items = array_intersect_key( $items, array_flip( $processed_item_ids ) ); |
||
| 214 | /** |
||
| 215 | * Allows us to keep track of all the actions that have been sent. |
||
| 216 | * Allows us to calculate the progress of specific actions. |
||
| 217 | * |
||
| 218 | * @since 4.2.0 |
||
| 219 | * |
||
| 220 | * @param array $processed_actions The actions that we send successfully. |
||
| 221 | */ |
||
| 222 | do_action( 'jetpack_sync_processed_actions', $processed_items ); |
||
| 223 | $queue->close( $buffer, $processed_item_ids ); |
||
| 224 | // returning a WP_Error is a sign to the caller that we should wait a while |
||
| 225 | // before syncing again |
||
| 226 | if ( $had_wp_error ) { |
||
| 227 | return $wp_error; |
||
| 228 | } |
||
| 229 | } |
||
| 230 | return true; |
||
| 231 | } |
||
| 232 | |||
| 233 | function get_sync_queue() { |
||
| 236 | |||
| 237 | function get_full_sync_queue() { |
||
| 240 | |||
| 241 | function get_codec() { |
||
| 244 | |||
| 245 | function send_checksum() { |
||
| 250 | |||
| 251 | function reset_sync_queue() { |
||
| 254 | |||
| 255 | function set_dequeue_max_bytes( $size ) { |
||
| 258 | |||
| 259 | // in bytes |
||
| 260 | function set_upload_max_bytes( $max_bytes ) { |
||
| 263 | |||
| 264 | // in rows |
||
| 265 | function set_upload_max_rows( $max_rows ) { |
||
| 268 | |||
| 269 | // in seconds |
||
| 270 | function set_sync_wait_time( $seconds ) { |
||
| 273 | |||
| 274 | function get_sync_wait_time() { |
||
| 277 | |||
| 278 | // in seconds |
||
| 279 | function set_sync_wait_threshold( $seconds ) { |
||
| 282 | |||
| 283 | function get_sync_wait_threshold() { |
||
| 286 | |||
| 287 | // in seconds |
||
| 288 | function set_max_dequeue_time( $seconds ) { |
||
| 289 | $this->max_dequeue_time = $seconds; |
||
| 290 | } |
||
| 291 | |||
| 292 | function set_defaults() { |
||
| 293 | $this->sync_queue = new Jetpack_Sync_Queue( 'sync' ); |
||
| 294 | $this->full_sync_queue = new Jetpack_Sync_Queue( 'full_sync' ); |
||
| 307 | |||
| 308 | function reset_data() { |
||
| 321 | |||
| 322 | function uninstall() { |
||
| 332 | } |
||
| 333 |
This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.
The variable may have been renamed without also renaming all references.