Complex classes like Jetpack_Sync_Queue 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_Queue, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 30 | class Jetpack_Sync_Queue { |
||
|
|
|||
| 31 | public $id; |
||
| 32 | private $row_iterator; |
||
| 33 | |||
| 34 | function __construct( $id ) { |
||
| 35 | $this->id = str_replace( '-', '_', $id ); // necessary to ensure we don't have ID collisions in the SQL |
||
| 36 | $this->row_iterator = 0; |
||
| 37 | } |
||
| 38 | |||
| 39 | function add( $item ) { |
||
| 54 | |||
| 55 | // Attempts to insert all the items in a single SQL query. May be subject to query size limits! |
||
| 56 | function add_all( $items ) { |
||
| 76 | |||
| 77 | // Peek at the front-most item on the queue without checking it out |
||
| 78 | function peek( $count = 1 ) { |
||
| 86 | |||
| 87 | // lag is the difference in time between the age of the oldest item and the current time |
||
| 88 | function lag() { |
||
| 89 | global $wpdb; |
||
| 90 | |||
| 91 | $last_item_name = $wpdb->get_var( $wpdb->prepare( |
||
| 92 | "SELECT option_name FROM $wpdb->options WHERE option_name LIKE %s ORDER BY option_name ASC LIMIT 1", |
||
| 93 | "jpsq_{$this->id}-%" |
||
| 94 | ) ); |
||
| 95 | |||
| 96 | if ( ! $last_item_name ) { |
||
| 97 | return null; |
||
| 98 | } |
||
| 99 | |||
| 100 | // break apart the item name to get the timestamp |
||
| 101 | $matches = null; |
||
| 102 | if ( preg_match( '/^jpsq_' . $this->id . '-(\d+\.\d+)-/', $last_item_name, $matches ) ) { |
||
| 103 | return microtime( true ) - floatval( $matches[1] ); |
||
| 104 | } else { |
||
| 105 | return null; |
||
| 106 | } |
||
| 107 | } |
||
| 108 | |||
| 109 | function reset() { |
||
| 116 | |||
| 117 | function size() { |
||
| 124 | |||
| 125 | // we use this peculiar implementation because it's much faster than count(*) |
||
| 126 | function has_any_items() { |
||
| 134 | |||
| 135 | function checkout( $buffer_size ) { |
||
| 136 | if ( $this->get_checkout_id() ) { |
||
| 137 | return new WP_Error( 'unclosed_buffer', 'There is an unclosed buffer' ); |
||
| 138 | } |
||
| 139 | |||
| 140 | $buffer_id = uniqid(); |
||
| 141 | |||
| 142 | $result = $this->set_checkout_id( $buffer_id ); |
||
| 143 | |||
| 144 | if ( ! $result || is_wp_error( $result ) ) { |
||
| 145 | error_log( "badness setting checkout ID (this should not happen)" ); |
||
| 146 | |||
| 147 | return $result; |
||
| 148 | } |
||
| 149 | |||
| 150 | $items = $this->fetch_items( $buffer_size ); |
||
| 151 | |||
| 152 | if ( count( $items ) === 0 ) { |
||
| 153 | return false; |
||
| 154 | } |
||
| 155 | |||
| 156 | $buffer = new Jetpack_Sync_Queue_Buffer( $buffer_id, array_slice( $items, 0, $buffer_size ) ); |
||
| 157 | |||
| 158 | return $buffer; |
||
| 159 | } |
||
| 160 | |||
| 161 | // this checks out rows until it either empties the queue or hits a certain memory limit |
||
| 162 | // it loads the sizes from the DB first so that it doesn't accidentally |
||
| 163 | // load more data into memory than it needs to. |
||
| 164 | // The only way it will load more items than $max_size is if a single queue item |
||
| 165 | // exceeds the memory limit, but in that case it will send that item by itself. |
||
| 166 | function checkout_with_memory_limit( $max_memory, $max_buffer_size = 500 ) { |
||
| 167 | if ( $this->get_checkout_id() ) { |
||
| 168 | return new WP_Error( 'unclosed_buffer', 'There is an unclosed buffer' ); |
||
| 169 | } |
||
| 170 | |||
| 171 | $buffer_id = uniqid(); |
||
| 172 | |||
| 173 | $result = $this->set_checkout_id( $buffer_id ); |
||
| 174 | |||
| 175 | if ( ! $result || is_wp_error( $result ) ) { |
||
| 176 | error_log( "badness setting checkout ID (this should not happen)" ); |
||
| 177 | |||
| 178 | return $result; |
||
| 179 | } |
||
| 180 | |||
| 181 | // get the map of buffer_id -> memory_size |
||
| 182 | global $wpdb; |
||
| 183 | |||
| 184 | $items_with_size = $wpdb->get_results( |
||
| 185 | $wpdb->prepare( |
||
| 186 | "SELECT option_name AS id, LENGTH(option_value) AS value_size FROM $wpdb->options WHERE option_name LIKE %s ORDER BY option_name ASC LIMIT %d", |
||
| 187 | "jpsq_{$this->id}-%", |
||
| 188 | $max_buffer_size |
||
| 189 | ), |
||
| 190 | OBJECT |
||
| 191 | ); |
||
| 192 | |||
| 193 | $total_memory = 0; |
||
| 194 | $item_ids = array(); |
||
| 195 | |||
| 196 | foreach ( $items_with_size as $item_with_size ) { |
||
| 197 | $total_memory += $item_with_size->value_size; |
||
| 198 | |||
| 199 | // if this is the first item and it exceeds memory, allow loop to continue |
||
| 200 | // we will exit on the next iteration instead |
||
| 201 | if ( $total_memory > $max_memory && count( $item_ids ) > 0 ) { |
||
| 202 | break; |
||
| 203 | } |
||
| 204 | $item_ids[] = $item_with_size->id; |
||
| 205 | } |
||
| 206 | |||
| 207 | $items = $this->fetch_items_by_id( $item_ids ); |
||
| 208 | |||
| 209 | if ( count( $items ) === 0 ) { |
||
| 210 | $this->delete_checkout_id(); |
||
| 211 | |||
| 212 | return false; |
||
| 213 | } |
||
| 214 | |||
| 215 | $buffer = new Jetpack_Sync_Queue_Buffer( $buffer_id, $items ); |
||
| 216 | |||
| 217 | return $buffer; |
||
| 218 | } |
||
| 219 | |||
| 220 | function checkin( $buffer ) { |
||
| 221 | $is_valid = $this->validate_checkout( $buffer ); |
||
| 222 | |||
| 223 | if ( is_wp_error( $is_valid ) ) { |
||
| 224 | error_log( "Invalid checkin: " . $is_valid->get_error_message() ); |
||
| 225 | |||
| 226 | return $is_valid; |
||
| 227 | } |
||
| 228 | |||
| 229 | $this->delete_checkout_id(); |
||
| 230 | |||
| 231 | return true; |
||
| 232 | } |
||
| 233 | |||
| 234 | function close( $buffer, $ids_to_remove ) { |
||
| 235 | $is_valid = $this->validate_checkout( $buffer ); |
||
| 236 | |||
| 237 | if ( is_wp_error( $is_valid ) ) { |
||
| 238 | error_log( "Invalid close: " . $is_valid->get_error_message() ); |
||
| 239 | |||
| 240 | return $is_valid; |
||
| 241 | } |
||
| 242 | |||
| 243 | $this->delete_checkout_id(); |
||
| 244 | |||
| 245 | global $wpdb; |
||
| 246 | |||
| 247 | if ( count( $ids_to_remove ) > 0 ) { |
||
| 248 | $sql = "DELETE FROM $wpdb->options WHERE option_name IN (" . implode( ', ', array_fill( 0, count( $ids_to_remove ), '%s' ) ) . ')'; |
||
| 249 | $query = call_user_func_array( array( $wpdb, 'prepare' ), array_merge( array( $sql ), $ids_to_remove ) ); |
||
| 250 | $wpdb->query( $query ); |
||
| 251 | } |
||
| 252 | |||
| 253 | return true; |
||
| 254 | } |
||
| 255 | |||
| 256 | function flush_all() { |
||
| 257 | $items = Jetpack_Sync_Utils::get_item_values( $this->fetch_items() ); |
||
| 258 | $this->reset(); |
||
| 259 | |||
| 260 | return $items; |
||
| 261 | } |
||
| 262 | |||
| 263 | function get_all() { |
||
| 264 | return $this->fetch_items(); |
||
| 265 | } |
||
| 266 | |||
| 267 | // use with caution, this could allow multiple processes to delete |
||
| 268 | // and send from the queue at the same time |
||
| 269 | function force_checkin() { |
||
| 270 | $this->delete_checkout_id(); |
||
| 271 | } |
||
| 272 | |||
| 273 | // used to lock checkouts from the queue. |
||
| 274 | // tries to wait up to $timeout seconds for the queue to be empty |
||
| 275 | function lock( $timeout = 30 ) { |
||
| 276 | $tries = 0; |
||
| 277 | |||
| 278 | while ( $this->has_any_items() && $tries < $timeout ) { |
||
| 279 | sleep( 1 ); |
||
| 280 | $tries += 1; |
||
| 281 | } |
||
| 282 | |||
| 283 | if ( $tries === 30 ) { |
||
| 284 | return new WP_Error( 'lock_timeout', 'Timeout waiting for sync queue to empty' ); |
||
| 285 | } |
||
| 286 | |||
| 287 | if ( $this->get_checkout_id() ) { |
||
| 288 | return new WP_Error( 'unclosed_buffer', 'There is an unclosed buffer' ); |
||
| 289 | } |
||
| 290 | |||
| 291 | // hopefully this means we can acquire a checkout? |
||
| 292 | $result = $this->set_checkout_id( 'lock' ); |
||
| 293 | |||
| 294 | if ( ! $result || is_wp_error( $result ) ) { |
||
| 295 | error_log( "badness setting checkout ID (this should not happen)" ); |
||
| 296 | |||
| 297 | return $result; |
||
| 298 | } |
||
| 299 | |||
| 300 | return true; |
||
| 301 | } |
||
| 302 | |||
| 303 | function unlock() { |
||
| 306 | |||
| 307 | private function get_checkout_id() { |
||
| 310 | |||
| 311 | private function set_checkout_id( $checkout_id ) { |
||
| 312 | return set_transient( $this->get_checkout_transient_name(), $checkout_id, 5 * 60 ); // 5 minute timeout |
||
| 313 | } |
||
| 314 | |||
| 315 | private function delete_checkout_id() { |
||
| 318 | |||
| 319 | private function get_checkout_transient_name() { |
||
| 322 | |||
| 323 | private function get_next_data_row_option_name() { |
||
| 340 | |||
| 341 | private function fetch_items( $limit = null ) { |
||
| 357 | |||
| 358 | private function fetch_items_by_id( $item_ids ) { |
||
| 359 | global $wpdb; |
||
| 360 | |||
| 361 | if ( count( $item_ids ) > 0 ) { |
||
| 362 | $sql = "SELECT option_name AS id, option_value AS value FROM $wpdb->options WHERE option_name IN (" . implode( ', ', array_fill( 0, count( $item_ids ), '%s' ) ) . ') ORDER BY option_name ASC'; |
||
| 363 | $query = call_user_func_array( array( $wpdb, 'prepare' ), array_merge( array( $sql ), $item_ids ) ); |
||
| 364 | $items = $wpdb->get_results( $query, OBJECT ); |
||
| 365 | foreach ( $items as $item ) { |
||
| 366 | $item->value = maybe_unserialize( $item->value ); |
||
| 367 | } |
||
| 368 | |||
| 374 | |||
| 375 | private function validate_checkout( $buffer ) { |
||
| 392 | } |
||
| 393 | |||
| 412 |
Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.