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 |
||
| 34 | class Jetpack_Sync_Queue { |
||
|
|
|||
| 35 | public $id; |
||
| 36 | private $row_iterator; |
||
| 37 | |||
| 38 | function __construct( $id ) { |
||
| 39 | $this->id = str_replace( '-', '_', $id ); // necessary to ensure we don't have ID collisions in the SQL |
||
| 40 | $this->row_iterator = 0; |
||
| 41 | $this->random_int = mt_rand( 1, 1000000 ); |
||
| 42 | } |
||
| 43 | |||
| 44 | function add( $item ) { |
||
| 45 | global $wpdb; |
||
| 46 | $added = false; |
||
| 47 | // this basically tries to add the option until enough time has elapsed that |
||
| 48 | // it has a unique (microtime-based) option key |
||
| 49 | while ( ! $added ) { |
||
| 50 | $rows_added = $wpdb->query( $wpdb->prepare( |
||
| 51 | "INSERT INTO $wpdb->options (option_name, option_value, autoload) VALUES (%s, %s,%s)", |
||
| 52 | $this->get_next_data_row_option_name(), |
||
| 53 | $this->serialize( $item ), |
||
| 54 | 'no' |
||
| 55 | ) ); |
||
| 56 | $added = ( 0 !== $rows_added ); |
||
| 57 | } |
||
| 58 | } |
||
| 59 | |||
| 60 | function serialize( $item ) { |
||
| 61 | if ( $item instanceof SimpleXMLElement ) { |
||
| 62 | $item = $item->asXML(); |
||
| 63 | } |
||
| 64 | return serialize( $item ); |
||
| 65 | } |
||
| 66 | |||
| 67 | // Attempts to insert all the items in a single SQL query. May be subject to query size limits! |
||
| 68 | function add_all( $items ) { |
||
| 69 | global $wpdb; |
||
| 70 | $base_option_name = $this->get_next_data_row_option_name(); |
||
| 71 | |||
| 72 | $query = "INSERT INTO $wpdb->options (option_name, option_value, autoload) VALUES "; |
||
| 73 | |||
| 74 | $rows = array(); |
||
| 75 | |||
| 76 | for ( $i = 0; $i < count( $items ); $i += 1 ) { |
||
| 77 | $option_name = esc_sql( $base_option_name . '-' . $i ); |
||
| 78 | $option_value = esc_sql( $this->serialize( $items[ $i ] ) ); |
||
| 79 | $rows[] = "('$option_name', '$option_value', 'no')"; |
||
| 80 | } |
||
| 81 | |||
| 82 | $rows_added = $wpdb->query( $query . join( ',', $rows ) ); |
||
| 83 | |||
| 84 | if ( count( $items ) === $rows_added ) { |
||
| 85 | return new WP_Error( 'row_count_mismatch', "The number of rows inserted didn't match the size of the input array" ); |
||
| 86 | } |
||
| 87 | } |
||
| 88 | |||
| 89 | // Peek at the front-most item on the queue without checking it out |
||
| 90 | function peek( $count = 1 ) { |
||
| 91 | $items = $this->fetch_items( $count ); |
||
| 92 | if ( $items ) { |
||
| 93 | return Jetpack_Sync_Utils::get_item_values( $items ); |
||
| 94 | } |
||
| 95 | |||
| 96 | return array(); |
||
| 97 | } |
||
| 98 | |||
| 99 | // lag is the difference in time between the age of the oldest item |
||
| 100 | // (aka first or frontmost item) and the current time |
||
| 101 | function lag() { |
||
| 102 | global $wpdb; |
||
| 103 | |||
| 104 | $first_item_name = $wpdb->get_var( $wpdb->prepare( |
||
| 105 | "SELECT option_name FROM $wpdb->options WHERE option_name LIKE %s ORDER BY option_name ASC LIMIT 1", |
||
| 106 | "jpsq_{$this->id}-%" |
||
| 107 | ) ); |
||
| 108 | |||
| 109 | if ( ! $first_item_name ) { |
||
| 110 | return 0; |
||
| 111 | } |
||
| 112 | |||
| 113 | // break apart the item name to get the timestamp |
||
| 114 | $matches = null; |
||
| 115 | if ( preg_match( '/^jpsq_' . $this->id . '-(\d+\.\d+)-/', $first_item_name, $matches ) ) { |
||
| 116 | return microtime( true ) - floatval( $matches[1] ); |
||
| 117 | } else { |
||
| 118 | return 0; |
||
| 119 | } |
||
| 120 | } |
||
| 121 | |||
| 122 | function reset() { |
||
| 129 | |||
| 130 | function size() { |
||
| 137 | |||
| 138 | // we use this peculiar implementation because it's much faster than count(*) |
||
| 139 | function has_any_items() { |
||
| 147 | |||
| 148 | function checkout( $buffer_size ) { |
||
| 171 | |||
| 172 | // this checks out rows until it either empties the queue or hits a certain memory limit |
||
| 173 | // it loads the sizes from the DB first so that it doesn't accidentally |
||
| 174 | // load more data into memory than it needs to. |
||
| 175 | // The only way it will load more items than $max_size is if a single queue item |
||
| 176 | // exceeds the memory limit, but in that case it will send that item by itself. |
||
| 177 | function checkout_with_memory_limit( $max_memory, $max_buffer_size = 500 ) { |
||
| 178 | if ( $this->get_checkout_id() ) { |
||
| 179 | return new WP_Error( 'unclosed_buffer', 'There is an unclosed buffer' ); |
||
| 180 | } |
||
| 181 | |||
| 182 | $buffer_id = uniqid(); |
||
| 183 | |||
| 184 | $result = $this->set_checkout_id( $buffer_id ); |
||
| 185 | |||
| 186 | if ( ! $result || is_wp_error( $result ) ) { |
||
| 187 | return $result; |
||
| 188 | } |
||
| 189 | |||
| 190 | // get the map of buffer_id -> memory_size |
||
| 191 | global $wpdb; |
||
| 192 | |||
| 193 | $items_with_size = $wpdb->get_results( |
||
| 194 | $wpdb->prepare( |
||
| 195 | "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", |
||
| 196 | "jpsq_{$this->id}-%", |
||
| 197 | $max_buffer_size |
||
| 198 | ), |
||
| 199 | OBJECT |
||
| 200 | ); |
||
| 201 | |||
| 202 | if ( count( $items_with_size ) === 0 ) { |
||
| 203 | return false; |
||
| 204 | } |
||
| 205 | |||
| 206 | $total_memory = 0; |
||
| 207 | |||
| 208 | $min_item_id = $max_item_id = $items_with_size[0]->id; |
||
| 209 | |||
| 210 | foreach ( $items_with_size as $id => $item_with_size ) { |
||
| 211 | $total_memory += $item_with_size->value_size; |
||
| 212 | |||
| 213 | // if this is the first item and it exceeds memory, allow loop to continue |
||
| 214 | // we will exit on the next iteration instead |
||
| 215 | if ( $total_memory > $max_memory && $id > 0 ) { |
||
| 216 | break; |
||
| 217 | } |
||
| 218 | |||
| 219 | $max_item_id = $item_with_size->id; |
||
| 220 | } |
||
| 221 | |||
| 222 | $query = $wpdb->prepare( |
||
| 223 | "SELECT option_name AS id, option_value AS value FROM $wpdb->options WHERE option_name >= %s and option_name <= %s ORDER BY option_name ASC", |
||
| 224 | $min_item_id, |
||
| 225 | $max_item_id |
||
| 226 | ); |
||
| 227 | |||
| 228 | $items = $wpdb->get_results( $query, OBJECT ); |
||
| 229 | foreach ( $items as $item ) { |
||
| 230 | $item->value = maybe_unserialize( $item->value ); |
||
| 231 | } |
||
| 232 | |||
| 233 | if ( count( $items ) === 0 ) { |
||
| 234 | $this->delete_checkout_id(); |
||
| 235 | |||
| 236 | return false; |
||
| 237 | } |
||
| 238 | |||
| 239 | $buffer = new Jetpack_Sync_Queue_Buffer( $buffer_id, $items ); |
||
| 240 | |||
| 241 | return $buffer; |
||
| 242 | } |
||
| 243 | |||
| 244 | function checkin( $buffer ) { |
||
| 255 | |||
| 256 | function close( $buffer, $ids_to_remove = null ) { |
||
| 280 | |||
| 281 | function flush_all() { |
||
| 287 | |||
| 288 | function get_all() { |
||
| 291 | |||
| 292 | // use with caution, this could allow multiple processes to delete |
||
| 293 | // and send from the queue at the same time |
||
| 294 | function force_checkin() { |
||
| 297 | |||
| 298 | // used to lock checkouts from the queue. |
||
| 299 | // tries to wait up to $timeout seconds for the queue to be empty |
||
| 300 | function lock( $timeout = 30 ) { |
||
| 325 | |||
| 326 | function unlock() { |
||
| 329 | |||
| 330 | private function get_checkout_id() { |
||
| 331 | global $wpdb; |
||
| 332 | $checkout_value = $wpdb->get_var( |
||
| 333 | $wpdb->prepare( |
||
| 334 | "SELECT option_value FROM $wpdb->options WHERE option_name = %s", |
||
| 348 | |||
| 349 | private function set_checkout_id( $checkout_id ) { |
||
| 373 | |||
| 374 | private function delete_checkout_id() { |
||
| 386 | |||
| 387 | private function get_lock_option_name() { |
||
| 390 | |||
| 391 | private function get_next_data_row_option_name() { |
||
| 408 | |||
| 409 | private function fetch_items( $limit = null ) { |
||
| 425 | |||
| 426 | private function validate_checkout( $buffer ) { |
||
| 443 | } |
||
| 444 | |||
| 463 |
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.