Complex classes like 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 Queue, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | class Queue { |
||
| 17 | /** |
||
| 18 | * The queue id. |
||
| 19 | * |
||
| 20 | * @var string |
||
| 21 | */ |
||
| 22 | public $id; |
||
| 23 | /** |
||
| 24 | * Keeps track of the rows. |
||
| 25 | * |
||
| 26 | * @var int |
||
| 27 | */ |
||
| 28 | private $row_iterator; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Queue constructor. |
||
| 32 | * |
||
| 33 | * @param string $id Name of the queue. |
||
| 34 | */ |
||
| 35 | public function __construct( $id ) { |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Add a single item to the queue. |
||
| 43 | * |
||
| 44 | * @param object $item Event object to add to queue. |
||
| 45 | */ |
||
| 46 | public function add( $item ) { |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Insert all the items in a single SQL query. May be subject to query size limits! |
||
| 66 | * |
||
| 67 | * @param array $items Array of events to add to the queue. |
||
| 68 | * |
||
| 69 | * @return bool|\WP_Error |
||
| 70 | */ |
||
| 71 | public function add_all( $items ) { |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Get the front-most item on the queue without checking it out. |
||
| 95 | * |
||
| 96 | * @param int $count Number of items to return when looking at the items. |
||
| 97 | * |
||
| 98 | * @return array |
||
| 99 | */ |
||
| 100 | public function peek( $count = 1 ) { |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Gets items with particular IDs. |
||
| 111 | * |
||
| 112 | * @param array $item_ids Array of item IDs to retrieve. |
||
| 113 | * |
||
| 114 | * @return array |
||
| 115 | */ |
||
| 116 | public function peek_by_id( $item_ids ) { |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Gets the queue lag. |
||
| 127 | * Lag is the difference in time between the age of the oldest item |
||
| 128 | * (aka first or frontmost item) and the current time. |
||
| 129 | * |
||
| 130 | * @param microtime $now The current time in microtime. |
||
| 131 | * |
||
| 132 | * @return float|int|mixed|null |
||
| 133 | */ |
||
| 134 | public function lag( $now = null ) { |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Resets the queue. |
||
| 163 | */ |
||
| 164 | public function reset() { |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Return the size of the queue. |
||
| 177 | * |
||
| 178 | * @return int |
||
| 179 | */ |
||
| 180 | public function size() { |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Lets you know if there is any items in the queue. |
||
| 193 | * |
||
| 194 | * We use this peculiar implementation because it's much faster than count(*). |
||
| 195 | * |
||
| 196 | * @return bool |
||
| 197 | */ |
||
| 198 | public function has_any_items() { |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Used to checkout the queue. |
||
| 212 | * |
||
| 213 | * @param int $buffer_size Size of the buffer to checkout. |
||
| 214 | * |
||
| 215 | * @return Automattic\Jetpack\Sync\Queue_Buffer|bool|int|\WP_Error |
||
| 216 | */ |
||
| 217 | public function checkout( $buffer_size ) { |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Given a list of items return the items ids. |
||
| 243 | * |
||
| 244 | * @param array $items List of item objects. |
||
| 245 | * |
||
| 246 | * @return array Ids of the items. |
||
| 247 | */ |
||
| 248 | public function get_ids( $items ) { |
||
| 256 | |||
| 257 | /** |
||
| 258 | * Pop elements from the queue. |
||
| 259 | * |
||
| 260 | * @param int $limit Number of items to pop from the queue. |
||
| 261 | * |
||
| 262 | * @return array|object|null |
||
| 263 | */ |
||
| 264 | public function pop( $limit ) { |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Get the items from the queue with a memory limit. |
||
| 276 | * |
||
| 277 | * This checks out rows until it either empties the queue or hits a certain memory limit |
||
| 278 | * it loads the sizes from the DB first so that it doesn't accidentally |
||
| 279 | * load more data into memory than it needs to. |
||
| 280 | * The only way it will load more items than $max_size is if a single queue item |
||
| 281 | * exceeds the memory limit, but in that case it will send that item by itself. |
||
| 282 | * |
||
| 283 | * @param int $max_memory (bytes) Maximum memory threshold. |
||
| 284 | * @param int $max_buffer_size Maximum buffer size (number of items). |
||
| 285 | * |
||
| 286 | * @return Automattic\Jetpack\Sync\Queue_Buffer|bool|int|\WP_Error |
||
| 287 | */ |
||
| 288 | public function checkout_with_memory_limit( $max_memory, $max_buffer_size = 500 ) { |
||
| 354 | |||
| 355 | /** |
||
| 356 | * Check in the queue. |
||
| 357 | * |
||
| 358 | * @param Automattic\Jetpack\Sync\Queue_Buffer $buffer Queue_Buffer object. |
||
| 359 | * |
||
| 360 | * @return bool|\WP_Error |
||
| 361 | */ |
||
| 362 | public function checkin( $buffer ) { |
||
| 373 | |||
| 374 | /** |
||
| 375 | * Close the buffer. |
||
| 376 | * |
||
| 377 | * @param Automattic\Jetpack\Sync\Queue_Buffer $buffer Queue_Buffer object. |
||
| 378 | * @param null|array $ids_to_remove Ids to remove from the queue. |
||
| 379 | * |
||
| 380 | * @return bool|\WP_Error |
||
| 381 | */ |
||
| 382 | public function close( $buffer, $ids_to_remove = null ) { |
||
| 400 | |||
| 401 | /** |
||
| 402 | * Delete elements from the queue. |
||
| 403 | * |
||
| 404 | * @param array $ids Ids to delete. |
||
| 405 | * |
||
| 406 | * @return bool|int |
||
| 407 | */ |
||
| 408 | private function delete( $ids ) { |
||
| 418 | |||
| 419 | /** |
||
| 420 | * Flushes all items from the queue. |
||
| 421 | * |
||
| 422 | * @return array |
||
| 423 | */ |
||
| 424 | public function flush_all() { |
||
| 430 | |||
| 431 | /** |
||
| 432 | * Get all the items from the queue. |
||
| 433 | * |
||
| 434 | * @return array|object|null |
||
| 435 | */ |
||
| 436 | public function get_all() { |
||
| 439 | |||
| 440 | /** |
||
| 441 | * Forces Checkin of the queue. |
||
| 442 | * Use with caution, this could allow multiple processes to delete |
||
| 443 | * and send from the queue at the same time |
||
| 444 | */ |
||
| 445 | public function force_checkin() { |
||
| 448 | |||
| 449 | /** |
||
| 450 | * Locks checkouts from the queue |
||
| 451 | * tries to wait up to $timeout seconds for the queue to be empty. |
||
| 452 | * |
||
| 453 | * @param int $timeout The wait time in seconds for the queue to be empty. |
||
| 454 | * |
||
| 455 | * @return bool|int|\WP_Error |
||
| 456 | */ |
||
| 457 | public function lock( $timeout = 30 ) { |
||
| 482 | |||
| 483 | /** |
||
| 484 | * Unlocks the queue. |
||
| 485 | * |
||
| 486 | * @return bool|int |
||
| 487 | */ |
||
| 488 | public function unlock() { |
||
| 491 | |||
| 492 | /** |
||
| 493 | * This option is specifically chosen to, as much as possible, preserve time order |
||
| 494 | * and minimise the possibility of collisions between multiple processes working |
||
| 495 | * at the same time. |
||
| 496 | * |
||
| 497 | * @return string |
||
| 498 | */ |
||
| 499 | protected function generate_option_name_timestamp() { |
||
| 502 | |||
| 503 | /** |
||
| 504 | * Gets the checkout ID. |
||
| 505 | * |
||
| 506 | * @return bool|string |
||
| 507 | */ |
||
| 508 | private function get_checkout_id() { |
||
| 526 | |||
| 527 | /** |
||
| 528 | * Sets the checkout id. |
||
| 529 | * |
||
| 530 | * @param string $checkout_id The ID of the checkout. |
||
| 531 | * |
||
| 532 | * @return bool|int |
||
| 533 | */ |
||
| 534 | private function set_checkout_id( $checkout_id ) { |
||
| 558 | |||
| 559 | /** |
||
| 560 | * Deletes the checkout ID. |
||
| 561 | * |
||
| 562 | * @return bool|int |
||
| 563 | */ |
||
| 564 | private function delete_checkout_id() { |
||
| 576 | |||
| 577 | /** |
||
| 578 | * Return the lock option name. |
||
| 579 | * |
||
| 580 | * @return string |
||
| 581 | */ |
||
| 582 | private function get_lock_option_name() { |
||
| 585 | |||
| 586 | /** |
||
| 587 | * Return the next data row option name. |
||
| 588 | * |
||
| 589 | * @return string |
||
| 590 | */ |
||
| 591 | private function get_next_data_row_option_name() { |
||
| 603 | |||
| 604 | /** |
||
| 605 | * Return the items in the queue. |
||
| 606 | * |
||
| 607 | * @param null|int $limit Limit to the number of items we fetch at once. |
||
| 608 | * |
||
| 609 | * @return array|object|null |
||
| 610 | */ |
||
| 611 | private function fetch_items( $limit = null ) { |
||
| 636 | |||
| 637 | /** |
||
| 638 | * Return items with specific ids. |
||
| 639 | * |
||
| 640 | * @param array $items_ids Array of event ids. |
||
| 641 | * |
||
| 642 | * @return array|object|null |
||
| 643 | */ |
||
| 644 | private function fetch_items_by_id( $items_ids ) { |
||
| 661 | |||
| 662 | /** |
||
| 663 | * Unserialize item values. |
||
| 664 | * |
||
| 665 | * @param array $items Events from the Queue to be serialized. |
||
| 666 | * |
||
| 667 | * @return mixed |
||
| 668 | */ |
||
| 669 | private function unserialize_values( $items ) { |
||
| 670 | array_walk( |
||
| 671 | $items, |
||
| 672 | function( $item ) { |
||
| 673 | $item->value = maybe_unserialize( $item->value ); |
||
| 674 | } |
||
| 675 | ); |
||
| 676 | |||
| 677 | return $items; |
||
| 678 | |||
| 679 | } |
||
| 680 | |||
| 681 | /** |
||
| 682 | * Return true if the buffer is still valid or an Error other wise. |
||
| 683 | * |
||
| 684 | * @param Automattic\Jetpack\Sync\Queue_Buffer $buffer The Queue_Buffer. |
||
| 685 | * |
||
| 686 | * @return bool|\WP_Error |
||
| 687 | */ |
||
| 688 | private function validate_checkout( $buffer ) { |
||
| 706 | } |
||
| 707 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: