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 ) { |
||
| 61 | |||
| 62 | // Attempts to insert all the items in a single SQL query. May be subject to query size limits! |
||
| 63 | function add_all( $items ) { |
||
| 85 | |||
| 86 | // Peek at the front-most item on the queue without checking it out |
||
| 87 | function peek( $count = 1 ) { |
||
| 95 | |||
| 96 | // lag is the difference in time between the age of the oldest item |
||
| 97 | // (aka first or frontmost item) and the current time |
||
| 98 | function lag() { |
||
| 118 | |||
| 119 | function reset() { |
||
| 126 | |||
| 127 | function size() { |
||
| 134 | |||
| 135 | // we use this peculiar implementation because it's much faster than count(*) |
||
| 136 | function has_any_items() { |
||
| 144 | |||
| 145 | function checkout( $buffer_size ) { |
||
| 168 | |||
| 169 | // this checks out rows until it either empties the queue or hits a certain memory limit |
||
| 170 | // it loads the sizes from the DB first so that it doesn't accidentally |
||
| 171 | // load more data into memory than it needs to. |
||
| 172 | // The only way it will load more items than $max_size is if a single queue item |
||
| 173 | // exceeds the memory limit, but in that case it will send that item by itself. |
||
| 174 | function checkout_with_memory_limit( $max_memory, $max_buffer_size = 500 ) { |
||
| 175 | if ( $this->get_checkout_id() ) { |
||
| 176 | return new WP_Error( 'unclosed_buffer', 'There is an unclosed buffer' ); |
||
| 177 | } |
||
| 178 | |||
| 179 | $buffer_id = uniqid(); |
||
| 180 | |||
| 181 | $result = $this->set_checkout_id( $buffer_id ); |
||
| 182 | |||
| 183 | if ( ! $result || is_wp_error( $result ) ) { |
||
| 184 | return $result; |
||
| 185 | } |
||
| 186 | |||
| 187 | // get the map of buffer_id -> memory_size |
||
| 188 | global $wpdb; |
||
| 189 | |||
| 190 | $items_with_size = $wpdb->get_results( |
||
| 191 | $wpdb->prepare( |
||
| 192 | "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", |
||
| 193 | "jpsq_{$this->id}-%", |
||
| 194 | $max_buffer_size |
||
| 195 | ), |
||
| 196 | OBJECT |
||
| 197 | ); |
||
| 198 | |||
| 199 | if ( count( $items_with_size ) === 0 ) { |
||
| 200 | return false; |
||
| 201 | } |
||
| 202 | |||
| 203 | $total_memory = 0; |
||
| 204 | |||
| 205 | $min_item_id = $max_item_id = $items_with_size[0]->id; |
||
| 206 | |||
| 207 | foreach ( $items_with_size as $id => $item_with_size ) { |
||
| 208 | $total_memory += $item_with_size->value_size; |
||
| 209 | |||
| 210 | // if this is the first item and it exceeds memory, allow loop to continue |
||
| 211 | // we will exit on the next iteration instead |
||
| 212 | if ( $total_memory > $max_memory && $id > 0 ) { |
||
| 213 | break; |
||
| 214 | } |
||
| 215 | |||
| 216 | $max_item_id = $item_with_size->id; |
||
| 217 | } |
||
| 218 | |||
| 219 | $query = $wpdb->prepare( |
||
| 220 | "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", |
||
| 221 | $min_item_id, |
||
| 222 | $max_item_id |
||
| 223 | ); |
||
| 224 | |||
| 225 | $items = $wpdb->get_results( $query, OBJECT ); |
||
| 226 | foreach ( $items as $item ) { |
||
| 227 | $item->value = maybe_unserialize( $item->value ); |
||
| 228 | } |
||
| 229 | |||
| 230 | if ( count( $items ) === 0 ) { |
||
| 231 | $this->delete_checkout_id(); |
||
| 232 | |||
| 233 | return false; |
||
| 234 | } |
||
| 235 | |||
| 236 | $buffer = new Jetpack_Sync_Queue_Buffer( $buffer_id, $items ); |
||
| 237 | |||
| 238 | return $buffer; |
||
| 239 | } |
||
| 240 | |||
| 241 | function checkin( $buffer ) { |
||
| 252 | |||
| 253 | function close( $buffer, $ids_to_remove = null ) { |
||
| 277 | |||
| 278 | function flush_all() { |
||
| 284 | |||
| 285 | function get_all() { |
||
| 288 | |||
| 289 | // use with caution, this could allow multiple processes to delete |
||
| 290 | // and send from the queue at the same time |
||
| 291 | function force_checkin() { |
||
| 294 | |||
| 295 | // used to lock checkouts from the queue. |
||
| 296 | // tries to wait up to $timeout seconds for the queue to be empty |
||
| 297 | function lock( $timeout = 30 ) { |
||
| 322 | |||
| 323 | function unlock() { |
||
| 326 | |||
| 327 | private function get_checkout_id() { |
||
| 330 | |||
| 331 | private function set_checkout_id( $checkout_id ) { |
||
| 334 | |||
| 335 | private function delete_checkout_id() { |
||
| 338 | |||
| 339 | private function get_checkout_transient_name() { |
||
| 342 | |||
| 343 | private function get_next_data_row_option_name() { |
||
| 360 | |||
| 361 | private function fetch_items( $limit = null ) { |
||
| 377 | |||
| 378 | private function validate_checkout( $buffer ) { |
||
| 395 | } |
||
| 396 | |||
| 415 |
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.