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 SYNC_THROTTLE_OPTION_NAME = 'jetpack_sync_min_wait'; |
||
| 15 | const NEXT_SYNC_TIME_OPTION_NAME = 'jetpack_next_sync_time'; |
||
| 16 | const WPCOM_ERROR_SYNC_DELAY = 60; |
||
| 17 | |||
| 18 | private $dequeue_max_bytes; |
||
| 19 | private $upload_max_bytes; |
||
| 20 | private $upload_max_rows; |
||
| 21 | private $sync_wait_time; |
||
| 22 | private $sync_queue; |
||
| 23 | private $full_sync_queue; |
||
| 24 | private $codec; |
||
| 25 | |||
| 26 | // singleton functions |
||
| 27 | private static $instance; |
||
| 28 | |||
| 29 | public static function get_instance() { |
||
| 30 | if ( null === self::$instance ) { |
||
| 31 | self::$instance = new self(); |
||
| 32 | } |
||
| 33 | |||
| 34 | return self::$instance; |
||
| 35 | } |
||
| 36 | |||
| 37 | // this is necessary because you can't use "new" when you declare instance properties >:( |
||
| 38 | protected function __construct() { |
||
| 39 | $this->set_defaults(); |
||
| 40 | $this->init(); |
||
| 41 | } |
||
| 42 | |||
| 43 | private function init() { |
||
| 44 | foreach ( Jetpack_Sync_Modules::get_modules() as $module ) { |
||
| 45 | $module->init_before_send(); |
||
| 46 | } |
||
| 47 | } |
||
| 48 | |||
| 49 | public function get_next_sync_time() { |
||
| 50 | return (double) get_option( self::NEXT_SYNC_TIME_OPTION_NAME, 0 ); |
||
| 51 | } |
||
| 52 | |||
| 53 | public function set_next_sync_time( $time ) { |
||
| 54 | return update_option( self::NEXT_SYNC_TIME_OPTION_NAME, $time, true ); |
||
| 55 | } |
||
| 56 | |||
| 57 | public function do_sync() { |
||
| 58 | // don't sync if importing |
||
| 59 | if ( defined( 'WP_IMPORTING' ) && WP_IMPORTING ) { |
||
| 60 | return false; |
||
| 61 | } |
||
| 62 | |||
| 63 | // don't sync if we are throttled |
||
| 64 | if ( $this->get_next_sync_time() > microtime( true ) ) { |
||
| 65 | return false; |
||
| 66 | } |
||
| 67 | |||
| 68 | $full_sync_result = $this->do_sync_for_queue( $this->full_sync_queue ); |
||
| 69 | $sync_result = $this->do_sync_for_queue( $this->sync_queue ); |
||
| 70 | |||
| 71 | if ( is_wp_error( $full_sync_result ) || is_wp_error( $sync_result ) ) { |
||
| 72 | $this->set_next_sync_time( time() + self::WPCOM_ERROR_SYNC_DELAY ); |
||
| 73 | $full_sync_result = false; |
||
| 74 | $sync_result = false; |
||
| 75 | } else { |
||
| 76 | $this->set_next_sync_time( time() + $this->get_sync_wait_time() ); |
||
| 77 | } |
||
| 78 | |||
| 79 | // we use OR here because if either one returns true then the caller should |
||
| 80 | // be allowed to call do_sync again, as there may be more items |
||
| 81 | return $full_sync_result || $sync_result; |
||
| 82 | } |
||
| 83 | |||
| 84 | public function do_sync_for_queue( $queue ) { |
||
| 85 | |||
| 86 | do_action( 'jetpack_sync_before_send_queue_' . $queue->id ); |
||
| 87 | |||
| 88 | if ( $queue->size() === 0 ) { |
||
| 89 | return false; |
||
| 90 | } |
||
| 91 | |||
| 92 | // now that we're sure we are about to sync, try to |
||
| 93 | // ignore user abort so we can avoid getting into a |
||
| 94 | // bad state |
||
| 95 | if ( function_exists( 'ignore_user_abort' ) ) { |
||
| 96 | ignore_user_abort( true ); |
||
| 97 | } |
||
| 98 | |||
| 99 | $buffer = $queue->checkout_with_memory_limit( $this->dequeue_max_bytes, $this->upload_max_rows ); |
||
| 100 | |||
| 101 | if ( ! $buffer ) { |
||
| 102 | // buffer has no items |
||
| 103 | return false; |
||
| 104 | } |
||
| 105 | |||
| 106 | if ( is_wp_error( $buffer ) ) { |
||
| 107 | // another buffer is currently sending |
||
| 108 | return false; |
||
| 109 | } |
||
| 110 | |||
| 111 | $upload_size = 0; |
||
| 112 | $items_to_send = array(); |
||
| 113 | $items = $buffer->get_items(); |
||
| 114 | |||
| 115 | // set up current screen to avoid errors rendering content |
||
| 116 | require_once(ABSPATH . 'wp-admin/includes/class-wp-screen.php'); |
||
| 117 | require_once(ABSPATH . 'wp-admin/includes/screen.php'); |
||
| 118 | set_current_screen( 'sync' ); |
||
| 119 | |||
| 120 | // we estimate the total encoded size as we go by encoding each item individually |
||
| 121 | // this is expensive, but the only way to really know :/ |
||
| 122 | foreach ( $items as $key => $item ) { |
||
| 123 | /** |
||
| 124 | * Modify the data within an action before it is serialized and sent to the server |
||
| 125 | * For example, during full sync this expands Post ID's into full Post objects, |
||
| 126 | * so that we don't have to serialize the whole object into the queue. |
||
| 127 | * |
||
| 128 | * @since 4.2.0 |
||
| 129 | * |
||
| 130 | * @param array The action parameters |
||
| 131 | */ |
||
| 132 | $item[1] = apply_filters( 'jetpack_sync_before_send_' . $item[0], $item[1], $item[2] ); |
||
| 133 | |||
| 134 | $encoded_item = $this->codec->encode( $item ); |
||
| 135 | |||
| 136 | $upload_size += strlen( $encoded_item ); |
||
| 137 | |||
| 138 | if ( $upload_size > $this->upload_max_bytes && count( $items_to_send ) > 0 ) { |
||
| 139 | break; |
||
| 140 | } |
||
| 141 | |||
| 142 | $items_to_send[ $key ] = $encoded_item; |
||
| 143 | } |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Fires when data is ready to send to the server. |
||
| 147 | * Return false or WP_Error to abort the sync (e.g. if there's an error) |
||
| 148 | * The items will be automatically re-sent later |
||
| 149 | * |
||
| 150 | * @since 4.2.0 |
||
| 151 | * |
||
| 152 | * @param array $data The action buffer |
||
| 153 | */ |
||
| 154 | $processed_item_ids = apply_filters( 'jetpack_sync_send_data', $items_to_send, $this->codec->name(), microtime( true ) ); |
||
| 155 | |||
| 156 | if ( ! $processed_item_ids || is_wp_error( $processed_item_ids ) ) { |
||
| 157 | $checked_in_item_ids = $queue->checkin( $buffer ); |
||
| 158 | |||
| 159 | if ( is_wp_error( $checked_in_item_ids ) ) { |
||
| 160 | error_log( 'Error checking in buffer: ' . $checked_in_item_ids->get_error_message() ); |
||
| 161 | $queue->force_checkin(); |
||
| 162 | } |
||
| 163 | |||
| 164 | // returning a WP_Error is a sign to the caller that we should wait a while |
||
| 165 | // before syncing again |
||
| 166 | return new WP_Error( 'server_error' ); |
||
| 167 | |||
| 168 | } else { |
||
| 169 | |||
| 170 | // detect if the last item ID was an error |
||
| 171 | $had_wp_error = is_wp_error( end( $processed_item_ids ) ); |
||
| 172 | |||
| 173 | if ( $had_wp_error ) { |
||
| 174 | $wp_error = array_pop( $processed_item_ids ); |
||
| 175 | } |
||
| 176 | |||
| 177 | $processed_items = array_intersect_key( $items, array_flip( $processed_item_ids ) ); |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Allows us to keep track of all the actions that have been sent. |
||
| 181 | * Allows us to calculate the progress of specific actions. |
||
| 182 | * |
||
| 183 | * @since 4.2.0 |
||
| 184 | * |
||
| 185 | * @param array $processed_actions The actions that we send successfully. |
||
| 186 | */ |
||
| 187 | do_action( 'jetpack_sync_processed_actions', $processed_items ); |
||
| 188 | |||
| 189 | $queue->close( $buffer, $processed_item_ids ); |
||
| 190 | |||
| 191 | // returning a WP_Error is a sign to the caller that we should wait a while |
||
| 192 | // before syncing again |
||
| 193 | if ( $had_wp_error ) { |
||
| 194 | return $wp_error; |
||
|
|
|||
| 195 | } |
||
| 196 | } |
||
| 197 | |||
| 198 | return true; |
||
| 199 | } |
||
| 200 | |||
| 201 | function get_sync_queue() { |
||
| 202 | return $this->sync_queue; |
||
| 203 | } |
||
| 204 | |||
| 205 | function get_full_sync_queue() { |
||
| 206 | return $this->full_sync_queue; |
||
| 207 | } |
||
| 208 | |||
| 209 | function get_codec() { |
||
| 210 | return $this->codec; |
||
| 211 | } |
||
| 212 | |||
| 213 | function send_checksum() { |
||
| 214 | require_once 'class.jetpack-sync-wp-replicastore.php'; |
||
| 215 | $store = new Jetpack_Sync_WP_Replicastore(); |
||
| 216 | do_action( 'jetpack_sync_checksum', $store->checksum_all() ); |
||
| 217 | } |
||
| 218 | |||
| 219 | function reset_sync_queue() { |
||
| 220 | Jetpack_Sync_Modules::get_module( 'full-sync' )->clear_status(); |
||
| 221 | $this->sync_queue->reset(); |
||
| 222 | $this->full_sync_queue->reset(); |
||
| 223 | } |
||
| 224 | |||
| 225 | function set_dequeue_max_bytes( $size ) { |
||
| 226 | $this->dequeue_max_bytes = $size; |
||
| 227 | } |
||
| 228 | |||
| 229 | // in bytes |
||
| 230 | function set_upload_max_bytes( $max_bytes ) { |
||
| 231 | $this->upload_max_bytes = $max_bytes; |
||
| 232 | } |
||
| 233 | |||
| 234 | // in rows |
||
| 235 | function set_upload_max_rows( $max_rows ) { |
||
| 236 | $this->upload_max_rows = $max_rows; |
||
| 237 | } |
||
| 238 | |||
| 239 | // in seconds |
||
| 240 | function set_sync_wait_time( $seconds ) { |
||
| 241 | $this->sync_wait_time = $seconds; |
||
| 242 | } |
||
| 243 | |||
| 244 | function get_sync_wait_time() { |
||
| 245 | return $this->sync_wait_time; |
||
| 246 | } |
||
| 247 | |||
| 248 | function set_defaults() { |
||
| 249 | $this->sync_queue = new Jetpack_Sync_Queue( 'sync' ); |
||
| 250 | $this->full_sync_queue = new Jetpack_Sync_Queue( 'full_sync' ); |
||
| 251 | $this->codec = new Jetpack_Sync_JSON_Deflate_Codec(); |
||
| 252 | |||
| 253 | // saved settings |
||
| 254 | $settings = Jetpack_Sync_Settings::get_settings(); |
||
| 255 | $this->set_dequeue_max_bytes( $settings['dequeue_max_bytes'] ); |
||
| 256 | $this->set_upload_max_bytes( $settings['upload_max_bytes'] ); |
||
| 257 | $this->set_upload_max_rows( $settings['upload_max_rows'] ); |
||
| 258 | $this->set_sync_wait_time( $settings['sync_wait_time'] ); |
||
| 259 | } |
||
| 260 | |||
| 261 | function reset_data() { |
||
| 262 | $this->reset_sync_queue(); |
||
| 263 | |||
| 264 | foreach ( Jetpack_Sync_Modules::get_modules() as $module ) { |
||
| 265 | $module->reset_data(); |
||
| 266 | } |
||
| 267 | |||
| 268 | delete_option( self::SYNC_THROTTLE_OPTION_NAME ); |
||
| 269 | delete_option( self::NEXT_SYNC_TIME_OPTION_NAME ); |
||
| 270 | |||
| 271 | Jetpack_Sync_Settings::reset_data(); |
||
| 272 | } |
||
| 273 | |||
| 274 | function uninstall() { |
||
| 287 | } |
||
| 288 |
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: