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 ) { |
||
| 405 | |||
| 406 | /** |
||
| 407 | * Delete elements from the queue. |
||
| 408 | * |
||
| 409 | * @param array $ids Ids to delete. |
||
| 410 | * |
||
| 411 | * @return bool|int |
||
| 412 | */ |
||
| 413 | private function delete( $ids ) { |
||
| 423 | |||
| 424 | /** |
||
| 425 | * Flushes all items from the queue. |
||
| 426 | * |
||
| 427 | * @return array |
||
| 428 | */ |
||
| 429 | public function flush_all() { |
||
| 435 | |||
| 436 | /** |
||
| 437 | * Get all the items from the queue. |
||
| 438 | * |
||
| 439 | * @return array|object|null |
||
| 440 | */ |
||
| 441 | public function get_all() { |
||
| 444 | |||
| 445 | /** |
||
| 446 | * Forces Checkin of the queue. |
||
| 447 | * Use with caution, this could allow multiple processes to delete |
||
| 448 | * and send from the queue at the same time |
||
| 449 | */ |
||
| 450 | public function force_checkin() { |
||
| 453 | |||
| 454 | /** |
||
| 455 | * Locks checkouts from the queue |
||
| 456 | * tries to wait up to $timeout seconds for the queue to be empty. |
||
| 457 | * |
||
| 458 | * @param int $timeout The wait time in seconds for the queue to be empty. |
||
| 459 | * |
||
| 460 | * @return bool|int|\WP_Error |
||
| 461 | */ |
||
| 462 | public function lock( $timeout = 30 ) { |
||
| 487 | |||
| 488 | /** |
||
| 489 | * Unlocks the queue. |
||
| 490 | * |
||
| 491 | * @return bool|int |
||
| 492 | */ |
||
| 493 | public function unlock() { |
||
| 496 | |||
| 497 | /** |
||
| 498 | * This option is specifically chosen to, as much as possible, preserve time order |
||
| 499 | * and minimise the possibility of collisions between multiple processes working |
||
| 500 | * at the same time. |
||
| 501 | * |
||
| 502 | * @return string |
||
| 503 | */ |
||
| 504 | protected function generate_option_name_timestamp() { |
||
| 507 | |||
| 508 | /** |
||
| 509 | * Gets the checkout ID. |
||
| 510 | * |
||
| 511 | * @return bool|string |
||
| 512 | */ |
||
| 513 | private function get_checkout_id() { |
||
| 531 | |||
| 532 | /** |
||
| 533 | * Sets the checkout id. |
||
| 534 | * |
||
| 535 | * @param string $checkout_id The ID of the checkout. |
||
| 536 | * |
||
| 537 | * @return bool|int |
||
| 538 | */ |
||
| 539 | private function set_checkout_id( $checkout_id ) { |
||
| 563 | |||
| 564 | /** |
||
| 565 | * Deletes the checkout ID. |
||
| 566 | * |
||
| 567 | * @return bool|int |
||
| 568 | */ |
||
| 569 | private function delete_checkout_id() { |
||
| 581 | |||
| 582 | /** |
||
| 583 | * Return the lock option name. |
||
| 584 | * |
||
| 585 | * @return string |
||
| 586 | */ |
||
| 587 | private function get_lock_option_name() { |
||
| 590 | |||
| 591 | /** |
||
| 592 | * Return the next data row option name. |
||
| 593 | * |
||
| 594 | * @return string |
||
| 595 | */ |
||
| 596 | private function get_next_data_row_option_name() { |
||
| 608 | |||
| 609 | /** |
||
| 610 | * Return the items in the queue. |
||
| 611 | * |
||
| 612 | * @param null|int $limit Limit to the number of items we fetch at once. |
||
| 613 | * |
||
| 614 | * @return array|object|null |
||
| 615 | */ |
||
| 616 | private function fetch_items( $limit = null ) { |
||
| 641 | |||
| 642 | /** |
||
| 643 | * Return items with specific ids. |
||
| 644 | * |
||
| 645 | * @param array $items_ids Array of event ids. |
||
| 646 | * |
||
| 647 | * @return array|object|null |
||
| 648 | */ |
||
| 649 | private function fetch_items_by_id( $items_ids ) { |
||
| 666 | |||
| 667 | /** |
||
| 668 | * Unserialize item values. |
||
| 669 | * |
||
| 670 | * @param array $items Events from the Queue to be serialized. |
||
| 671 | * |
||
| 672 | * @return mixed |
||
| 673 | */ |
||
| 674 | private function unserialize_values( $items ) { |
||
| 685 | |||
| 686 | /** |
||
| 687 | * Return true if the buffer is still valid or an Error other wise. |
||
| 688 | * |
||
| 689 | * @param Automattic\Jetpack\Sync\Queue_Buffer $buffer The Queue_Buffer. |
||
| 690 | * |
||
| 691 | * @return bool|\WP_Error |
||
| 692 | */ |
||
| 693 | private function validate_checkout( $buffer ) { |
||
| 711 | } |
||
| 712 |
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: