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 | serialize( $item ), |
||
54 | 'no' |
||
55 | ) ); |
||
56 | $added = ( 0 !== $rows_added ); |
||
57 | } |
||
58 | } |
||
59 | |||
60 | // Attempts to insert all the items in a single SQL query. May be subject to query size limits! |
||
61 | function add_all( $items ) { |
||
62 | global $wpdb; |
||
63 | $base_option_name = $this->get_next_data_row_option_name(); |
||
64 | |||
65 | $query = "INSERT INTO $wpdb->options (option_name, option_value, autoload) VALUES "; |
||
66 | |||
67 | $rows = array(); |
||
68 | |||
69 | for ( $i = 0; $i < count( $items ); $i += 1 ) { |
||
70 | $option_name = esc_sql( $base_option_name . '-' . $i ); |
||
71 | $option_value = esc_sql( serialize( $items[ $i ] ) ); |
||
72 | $rows[] = "('$option_name', '$option_value', 'no')"; |
||
73 | } |
||
74 | |||
75 | $rows_added = $wpdb->query( $query . join( ',', $rows ) ); |
||
76 | |||
77 | if ( count( $items ) === $rows_added ) { |
||
78 | return new WP_Error( 'row_count_mismatch', "The number of rows inserted didn't match the size of the input array" ); |
||
79 | } |
||
80 | } |
||
81 | |||
82 | // Peek at the front-most item on the queue without checking it out |
||
83 | function peek( $count = 1 ) { |
||
84 | $items = $this->fetch_items( $count ); |
||
85 | if ( $items ) { |
||
86 | return Jetpack_Sync_Utils::get_item_values( $items ); |
||
87 | } |
||
88 | |||
89 | return array(); |
||
90 | } |
||
91 | |||
92 | // lag is the difference in time between the age of the oldest item |
||
93 | // (aka first or frontmost item) and the current time |
||
94 | function lag( $now = null) { |
||
95 | global $wpdb; |
||
96 | |||
97 | $first_item_name = $wpdb->get_var( $wpdb->prepare( |
||
98 | "SELECT option_name FROM $wpdb->options WHERE option_name LIKE %s ORDER BY option_name ASC LIMIT 1", |
||
99 | "jpsq_{$this->id}-%" |
||
100 | ) ); |
||
101 | |||
102 | if ( ! $first_item_name ) { |
||
103 | return 0; |
||
104 | } |
||
105 | |||
106 | if ( null === $now ) { |
||
107 | $now = microtime( true ); |
||
108 | } |
||
109 | |||
110 | // break apart the item name to get the timestamp |
||
111 | $matches = null; |
||
112 | if ( preg_match( '/^jpsq_' . $this->id . '-(\d+\.\d+)-/', $first_item_name, $matches ) ) { |
||
113 | return $now - floatval( $matches[1] ); |
||
114 | } else { |
||
115 | return 0; |
||
116 | } |
||
117 | } |
||
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 | /** |
||
328 | * This option is specifically chosen to, as much as possible, preserve time order |
||
329 | * and minimise the possibility of collisions between multiple processes working |
||
330 | * at the same time. |
||
331 | * |
||
332 | * @return string |
||
333 | */ |
||
334 | protected function generate_option_name_timestamp() { |
||
337 | |||
338 | private function get_checkout_id() { |
||
356 | |||
357 | private function set_checkout_id( $checkout_id ) { |
||
381 | |||
382 | private function delete_checkout_id() { |
||
394 | |||
395 | private function get_lock_option_name() { |
||
398 | |||
399 | private function get_next_data_row_option_name() { |
||
411 | |||
412 | private function fetch_items( $limit = null ) { |
||
428 | |||
429 | private function validate_checkout( $buffer ) { |
||
446 | } |
||
447 | |||
466 |
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.