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 | |||
| 17 | private $dequeue_max_bytes; |
||
| 18 | private $upload_max_bytes; |
||
| 19 | private $upload_max_rows; |
||
| 20 | private $max_dequeue_time; |
||
| 21 | private $sync_wait_time; |
||
| 22 | private $sync_wait_threshold; |
||
| 23 | private $sync_queue; |
||
| 24 | private $full_sync_queue; |
||
| 25 | private $codec; |
||
| 26 | |||
| 27 | // singleton functions |
||
| 28 | private static $instance; |
||
| 29 | |||
| 30 | public static function get_instance() { |
||
| 37 | |||
| 38 | // this is necessary because you can't use "new" when you declare instance properties >:( |
||
| 39 | protected function __construct() { |
||
| 43 | |||
| 44 | private function init() { |
||
| 49 | |||
| 50 | public function get_next_sync_time( $queue_name ) { |
||
| 53 | |||
| 54 | public function set_next_sync_time( $time, $queue_name ) { |
||
| 57 | |||
| 58 | public function do_full_sync() { |
||
| 61 | |||
| 62 | public function do_sync() { |
||
| 65 | |||
| 66 | public function do_sync_and_set_delays( $queue ) { |
||
| 93 | |||
| 94 | public function do_sync_for_queue( $queue ) { |
||
| 95 | |||
| 96 | // track how long we've been processing so we can avoid request timeouts |
||
| 97 | $start_time = microtime( true ); |
||
| 98 | |||
| 99 | do_action( 'jetpack_sync_before_send_queue_' . $queue->id ); |
||
| 100 | |||
| 101 | if ( $queue->size() === 0 ) { |
||
| 102 | return false; |
||
| 103 | } |
||
| 104 | |||
| 105 | // now that we're sure we are about to sync, try to |
||
| 106 | // ignore user abort so we can avoid getting into a |
||
| 107 | // bad state |
||
| 108 | if ( function_exists( 'ignore_user_abort' ) ) { |
||
| 109 | ignore_user_abort( true ); |
||
| 110 | } |
||
| 111 | |||
| 112 | $buffer = $queue->checkout_with_memory_limit( $this->dequeue_max_bytes, $this->upload_max_rows ); |
||
| 113 | |||
| 114 | if ( ! $buffer ) { |
||
| 115 | // buffer has no items |
||
| 116 | return false; |
||
| 117 | } |
||
| 118 | |||
| 119 | if ( is_wp_error( $buffer ) ) { |
||
| 120 | // another buffer is currently sending |
||
| 121 | return false; |
||
| 122 | } |
||
| 123 | |||
| 124 | $upload_size = 0; |
||
| 125 | $items_to_send = array(); |
||
| 126 | $items = $buffer->get_items(); |
||
| 127 | |||
| 128 | // set up current screen to avoid errors rendering content |
||
| 129 | require_once( ABSPATH . 'wp-admin/includes/class-wp-screen.php' ); |
||
| 130 | require_once( ABSPATH . 'wp-admin/includes/screen.php' ); |
||
| 131 | set_current_screen( 'sync' ); |
||
| 132 | |||
| 133 | $skipped_items_ids = array(); |
||
| 134 | |||
| 135 | // we estimate the total encoded size as we go by encoding each item individually |
||
| 136 | // this is expensive, but the only way to really know :/ |
||
| 137 | foreach ( $items as $key => $item ) { |
||
| 138 | // Suspending cache addition help prevent overloading in memory cache of large sites. |
||
| 139 | wp_suspend_cache_addition( true ); |
||
| 140 | /** |
||
| 141 | * Modify the data within an action before it is serialized and sent to the server |
||
| 142 | * For example, during full sync this expands Post ID's into full Post objects, |
||
| 143 | * so that we don't have to serialize the whole object into the queue. |
||
| 144 | * |
||
| 145 | * @since 4.2.0 |
||
| 146 | * |
||
| 147 | * @param array The action parameters |
||
| 148 | * @param int The ID of the user who triggered the action |
||
| 149 | */ |
||
| 150 | $item[1] = apply_filters( 'jetpack_sync_before_send_' . $item[0], $item[1], $item[2] ); |
||
| 151 | wp_suspend_cache_addition( false ); |
||
| 152 | if ( $item[1] === false ) { |
||
| 153 | $skipped_items_ids[] = $key; |
||
| 154 | continue; |
||
| 155 | } |
||
| 156 | |||
| 157 | $encoded_item = $this->codec->encode( $item ); |
||
| 158 | |||
| 159 | $upload_size += strlen( $encoded_item ); |
||
| 160 | |||
| 161 | if ( $upload_size > $this->upload_max_bytes && count( $items_to_send ) > 0 ) { |
||
| 162 | break; |
||
| 163 | } |
||
| 164 | |||
| 165 | $items_to_send[ $key ] = $encoded_item; |
||
| 166 | |||
| 167 | if ( microtime(true) - $start_time > $this->max_dequeue_time ) { |
||
| 168 | break; |
||
| 169 | } |
||
| 170 | } |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Fires when data is ready to send to the server. |
||
| 174 | * Return false or WP_Error to abort the sync (e.g. if there's an error) |
||
| 175 | * The items will be automatically re-sent later |
||
| 176 | * |
||
| 177 | * @since 4.2.0 |
||
| 178 | * |
||
| 179 | * @param array $data The action buffer |
||
| 180 | * @param string $codec The codec name used to encode the data |
||
| 181 | * @param double $time The current time |
||
| 182 | * @param string $queue The queue used to send ('sync' or 'full_sync') |
||
| 183 | */ |
||
| 184 | $processed_item_ids = apply_filters( 'jetpack_sync_send_data', $items_to_send, $this->codec->name(), microtime( true ), $queue->id ); |
||
| 185 | |||
| 186 | if ( ! $processed_item_ids || is_wp_error( $processed_item_ids ) ) { |
||
| 187 | $checked_in_item_ids = $queue->checkin( $buffer ); |
||
| 188 | |||
| 189 | if ( is_wp_error( $checked_in_item_ids ) ) { |
||
| 190 | error_log( 'Error checking in buffer: ' . $checked_in_item_ids->get_error_message() ); |
||
| 191 | $queue->force_checkin(); |
||
| 192 | } |
||
| 193 | |||
| 194 | if ( is_wp_error( $processed_item_ids ) ) { |
||
| 195 | return $processed_item_ids; |
||
| 196 | } |
||
| 197 | |||
| 198 | // returning a WP_Error is a sign to the caller that we should wait a while |
||
| 199 | // before syncing again |
||
| 200 | return new WP_Error( 'server_error' ); |
||
| 201 | |||
| 202 | } else { |
||
| 203 | |||
| 204 | // detect if the last item ID was an error |
||
| 205 | $had_wp_error = is_wp_error( end( $processed_item_ids ) ); |
||
| 206 | |||
| 207 | if ( $had_wp_error ) { |
||
| 208 | $wp_error = array_pop( $processed_item_ids ); |
||
| 209 | } |
||
| 210 | |||
| 211 | // also checkin any items that were skipped |
||
| 212 | if ( count( $skipped_items_ids ) > 0 ) { |
||
| 213 | $processed_item_ids = array_merge( $processed_item_ids, $skipped_items_ids ); |
||
| 214 | } |
||
| 215 | |||
| 216 | $processed_items = array_intersect_key( $items, array_flip( $processed_item_ids ) ); |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Allows us to keep track of all the actions that have been sent. |
||
| 220 | * Allows us to calculate the progress of specific actions. |
||
| 221 | * |
||
| 222 | * @since 4.2.0 |
||
| 223 | * |
||
| 224 | * @param array $processed_actions The actions that we send successfully. |
||
| 225 | */ |
||
| 226 | do_action( 'jetpack_sync_processed_actions', $processed_items ); |
||
| 227 | |||
| 228 | $queue->close( $buffer, $processed_item_ids ); |
||
| 229 | |||
| 230 | // returning a WP_Error is a sign to the caller that we should wait a while |
||
| 231 | // before syncing again |
||
| 232 | if ( $had_wp_error ) { |
||
| 233 | return $wp_error; |
||
|
|
|||
| 234 | } |
||
| 235 | } |
||
| 236 | |||
| 237 | return true; |
||
| 238 | } |
||
| 239 | |||
| 240 | function get_sync_queue() { |
||
| 241 | return $this->sync_queue; |
||
| 242 | } |
||
| 243 | |||
| 244 | function get_full_sync_queue() { |
||
| 245 | return $this->full_sync_queue; |
||
| 246 | } |
||
| 247 | |||
| 248 | function get_codec() { |
||
| 249 | return $this->codec; |
||
| 250 | } |
||
| 251 | |||
| 252 | function send_checksum() { |
||
| 253 | require_once 'class.jetpack-sync-wp-replicastore.php'; |
||
| 254 | $store = new Jetpack_Sync_WP_Replicastore(); |
||
| 255 | do_action( 'jetpack_sync_checksum', $store->checksum_all() ); |
||
| 256 | } |
||
| 257 | |||
| 258 | function reset_sync_queue() { |
||
| 259 | $this->sync_queue->reset(); |
||
| 260 | } |
||
| 261 | |||
| 262 | function set_dequeue_max_bytes( $size ) { |
||
| 263 | $this->dequeue_max_bytes = $size; |
||
| 264 | } |
||
| 265 | |||
| 266 | // in bytes |
||
| 267 | function set_upload_max_bytes( $max_bytes ) { |
||
| 268 | $this->upload_max_bytes = $max_bytes; |
||
| 269 | } |
||
| 270 | |||
| 271 | // in rows |
||
| 272 | function set_upload_max_rows( $max_rows ) { |
||
| 273 | $this->upload_max_rows = $max_rows; |
||
| 274 | } |
||
| 275 | |||
| 276 | // in seconds |
||
| 277 | function set_sync_wait_time( $seconds ) { |
||
| 278 | $this->sync_wait_time = $seconds; |
||
| 279 | } |
||
| 280 | |||
| 281 | function get_sync_wait_time() { |
||
| 282 | return $this->sync_wait_time; |
||
| 283 | } |
||
| 284 | |||
| 285 | // in seconds |
||
| 286 | function set_sync_wait_threshold( $seconds ) { |
||
| 287 | $this->sync_wait_threshold = $seconds; |
||
| 288 | } |
||
| 289 | |||
| 290 | function get_sync_wait_threshold() { |
||
| 291 | return $this->sync_wait_threshold; |
||
| 292 | } |
||
| 293 | |||
| 294 | // in seconds |
||
| 295 | function set_max_dequeue_time( $seconds ) { |
||
| 296 | $this->max_dequeue_time = $seconds; |
||
| 297 | } |
||
| 298 | |||
| 299 | function set_defaults() { |
||
| 300 | $this->sync_queue = new Jetpack_Sync_Queue( 'sync' ); |
||
| 301 | $this->full_sync_queue = new Jetpack_Sync_Queue( 'full_sync' ); |
||
| 302 | $this->codec = new Jetpack_Sync_JSON_Deflate_Array_Codec(); |
||
| 303 | |||
| 304 | // saved settings |
||
| 305 | Jetpack_Sync_Settings::set_importing( null ); |
||
| 306 | $settings = Jetpack_Sync_Settings::get_settings(); |
||
| 307 | $this->set_dequeue_max_bytes( $settings['dequeue_max_bytes'] ); |
||
| 308 | $this->set_upload_max_bytes( $settings['upload_max_bytes'] ); |
||
| 309 | $this->set_upload_max_rows( $settings['upload_max_rows'] ); |
||
| 310 | $this->set_sync_wait_time( $settings['sync_wait_time'] ); |
||
| 311 | $this->set_sync_wait_threshold( $settings['sync_wait_threshold'] ); |
||
| 312 | $this->set_max_dequeue_time( Jetpack_Sync_Defaults::get_max_sync_execution_time() ); |
||
| 313 | } |
||
| 314 | |||
| 315 | function reset_data() { |
||
| 316 | $this->reset_sync_queue(); |
||
| 317 | |||
| 318 | foreach ( Jetpack_Sync_Modules::get_modules() as $module ) { |
||
| 319 | $module->reset_data(); |
||
| 320 | } |
||
| 321 | |||
| 322 | foreach ( array( 'sync', 'full_sync' ) as $queue_name ) { |
||
| 323 | delete_option( self::NEXT_SYNC_TIME_OPTION_NAME . '_' . $queue_name ); |
||
| 324 | } |
||
| 325 | |||
| 326 | Jetpack_Sync_Settings::reset_data(); |
||
| 327 | } |
||
| 328 | |||
| 329 | function uninstall() { |
||
| 339 | } |
||
| 340 |
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: