Complex classes like Full_Sync 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 Full_Sync, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 27 | class Full_Sync extends Module { |
||
| 28 | /** |
||
| 29 | * Prefix of the full sync status option name. |
||
| 30 | * |
||
| 31 | * @var string |
||
| 32 | */ |
||
| 33 | const STATUS_OPTION_PREFIX = 'jetpack_sync_full_'; |
||
| 34 | |||
| 35 | |||
| 36 | /** |
||
| 37 | * Enqueue Lock name. |
||
| 38 | * |
||
| 39 | * @var string |
||
| 40 | */ |
||
| 41 | const ENQUEUE_LOCK_NAME = 'full_sync_enqueue'; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Sync module name. |
||
| 45 | * |
||
| 46 | * @access public |
||
| 47 | * |
||
| 48 | * @return string |
||
| 49 | */ |
||
| 50 | public function name() { |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Initialize action listeners for full sync. |
||
| 56 | * |
||
| 57 | * @access public |
||
| 58 | * |
||
| 59 | * @param callable $callable Action handler callable. |
||
| 60 | */ |
||
| 61 | public function init_full_sync_listeners( $callable ) { |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Initialize the module in the sender. |
||
| 70 | * |
||
| 71 | * @access public |
||
| 72 | */ |
||
| 73 | public function init_before_send() { |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Start a full sync. |
||
| 80 | * |
||
| 81 | * @access public |
||
| 82 | * |
||
| 83 | * @param array $module_configs Full sync configuration for all sync modules. |
||
|
|
|||
| 84 | * @return bool Always returns true at success. |
||
| 85 | */ |
||
| 86 | public function start( $module_configs = null ) { |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Enqueue the next items to sync. |
||
| 173 | * |
||
| 174 | * @access public |
||
| 175 | * |
||
| 176 | * @param array $configs Full sync configuration for all sync modules. |
||
| 177 | */ |
||
| 178 | public function continue_enqueuing( $configs = null ) { |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Get Modules that are configured to Full Sync and haven't finished enqueuing |
||
| 190 | * |
||
| 191 | * @param array $configs Full sync configuration for all sync modules. |
||
| 192 | * |
||
| 193 | * @return array |
||
| 194 | */ |
||
| 195 | public function get_remaining_modules_to_enqueue( $configs ) { |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Enqueue the next items to sync. |
||
| 226 | * |
||
| 227 | * @access public |
||
| 228 | * |
||
| 229 | * @param array $configs Full sync configuration for all sync modules. |
||
| 230 | */ |
||
| 231 | public function enqueue( $configs = null ) { |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Enqueue 'jetpack_full_sync_end' and update 'queue_finished' status. |
||
| 273 | * |
||
| 274 | * @access public |
||
| 275 | * |
||
| 276 | * @param array $configs Full sync configuration for all sync modules. |
||
| 277 | */ |
||
| 278 | public function queue_full_sync_end( $configs ) { |
||
| 296 | |||
| 297 | /** |
||
| 298 | * Get the range (min ID, max ID and total items) of items to sync. |
||
| 299 | * |
||
| 300 | * @access public |
||
| 301 | * |
||
| 302 | * @param string $type Type of sync item to get the range for. |
||
| 303 | * @return array Array of min ID, max ID and total items in the range. |
||
| 304 | */ |
||
| 305 | public function get_range( $type ) { |
||
| 334 | |||
| 335 | /** |
||
| 336 | * Get the range for content (posts and comments) to sync. |
||
| 337 | * |
||
| 338 | * @access private |
||
| 339 | * |
||
| 340 | * @param array $config Full sync configuration for this all sync modules. |
||
| 341 | * @return array Array of range (min ID, max ID, total items) for all content types. |
||
| 342 | */ |
||
| 343 | private function get_content_range( $config ) { |
||
| 355 | |||
| 356 | /** |
||
| 357 | * Update the progress after sync modules actions have been processed on the server. |
||
| 358 | * |
||
| 359 | * @access public |
||
| 360 | * |
||
| 361 | * @param array $actions Actions that have been processed on the server. |
||
| 362 | */ |
||
| 363 | public function update_sent_progress_action( $actions ) { |
||
| 408 | |||
| 409 | /** |
||
| 410 | * Get the name of the action for an item in the sync queue. |
||
| 411 | * |
||
| 412 | * @access public |
||
| 413 | * |
||
| 414 | * @param array $queue_item Item of the sync queue. |
||
| 415 | * @return string|boolean Name of the action, false if queue item is invalid. |
||
| 416 | */ |
||
| 417 | public function get_action_name( $queue_item ) { |
||
| 423 | |||
| 424 | /** |
||
| 425 | * Retrieve the total number of items we're syncing in a particular queue item (action). |
||
| 426 | * `$queue_item[1]` is expected to contain chunks of items, and `$queue_item[1][0]` |
||
| 427 | * represents the first (and only) chunk of items to sync in that action. |
||
| 428 | * |
||
| 429 | * @access public |
||
| 430 | * |
||
| 431 | * @param array $queue_item Item of the sync queue that corresponds to a particular action. |
||
| 432 | * @return int Total number of items in the action. |
||
| 433 | */ |
||
| 434 | public function get_action_totals( $queue_item ) { |
||
| 445 | |||
| 446 | /** |
||
| 447 | * Retrieve the total number of items for a set of actions, grouped by action name. |
||
| 448 | * |
||
| 449 | * @access public |
||
| 450 | * |
||
| 451 | * @param array $actions An array of actions. |
||
| 452 | * @return array An array, representing the total number of items, grouped per action. |
||
| 453 | */ |
||
| 454 | public function get_actions_totals( $actions ) { |
||
| 468 | |||
| 469 | /** |
||
| 470 | * Whether full sync has started. |
||
| 471 | * |
||
| 472 | * @access public |
||
| 473 | * |
||
| 474 | * @return boolean |
||
| 475 | */ |
||
| 476 | public function is_started() { |
||
| 479 | |||
| 480 | /** |
||
| 481 | * Whether full sync has finished. |
||
| 482 | * |
||
| 483 | * @access public |
||
| 484 | * |
||
| 485 | * @return boolean |
||
| 486 | */ |
||
| 487 | public function is_finished() { |
||
| 490 | |||
| 491 | /** |
||
| 492 | * Retrieve the status of the current full sync. |
||
| 493 | * |
||
| 494 | * @access public |
||
| 495 | * |
||
| 496 | * @return array Full sync status. |
||
| 497 | */ |
||
| 498 | public function get_status() { |
||
| 543 | |||
| 544 | /** |
||
| 545 | * Clear all the full sync status options. |
||
| 546 | * |
||
| 547 | * @access public |
||
| 548 | */ |
||
| 549 | public function clear_status() { |
||
| 564 | |||
| 565 | /** |
||
| 566 | * Clear all the full sync data. |
||
| 567 | * |
||
| 568 | * @access public |
||
| 569 | */ |
||
| 570 | public function reset_data() { |
||
| 578 | |||
| 579 | /** |
||
| 580 | * Get the value of a full sync status option. |
||
| 581 | * |
||
| 582 | * @access private |
||
| 583 | * |
||
| 584 | * @param string $name Name of the option. |
||
| 585 | * @param mixed $default Default value of the option. |
||
| 586 | * @return mixed Option value. |
||
| 587 | */ |
||
| 588 | private function get_status_option( $name, $default = null ) { |
||
| 593 | |||
| 594 | /** |
||
| 595 | * Update the value of a full sync status option. |
||
| 596 | * |
||
| 597 | * @access private |
||
| 598 | * |
||
| 599 | * @param string $name Name of the option. |
||
| 600 | * @param mixed $value Value of the option. |
||
| 601 | * @param boolean $autoload Whether the option should be autoloaded at the beginning of the request. |
||
| 602 | */ |
||
| 603 | private function update_status_option( $name, $value, $autoload = false ) { |
||
| 606 | |||
| 607 | /** |
||
| 608 | * Set the full sync enqueue status. |
||
| 609 | * |
||
| 610 | * @access private |
||
| 611 | * |
||
| 612 | * @param array $new_status The new full sync enqueue status. |
||
| 613 | */ |
||
| 614 | private function set_enqueue_status( $new_status ) { |
||
| 617 | |||
| 618 | /** |
||
| 619 | * Delete full sync enqueue status. |
||
| 620 | * |
||
| 621 | * @access private |
||
| 622 | * |
||
| 623 | * @return boolean Whether the status was deleted. |
||
| 624 | */ |
||
| 625 | private function delete_enqueue_status() { |
||
| 628 | |||
| 629 | /** |
||
| 630 | * Retrieve the current full sync enqueue status. |
||
| 631 | * |
||
| 632 | * @access private |
||
| 633 | * |
||
| 634 | * @return array Full sync enqueue status. |
||
| 635 | */ |
||
| 636 | public function get_enqueue_status() { |
||
| 639 | |||
| 640 | /** |
||
| 641 | * Set the full sync enqueue configuration. |
||
| 642 | * |
||
| 643 | * @access private |
||
| 644 | * |
||
| 645 | * @param array $config The new full sync enqueue configuration. |
||
| 646 | */ |
||
| 647 | private function set_config( $config ) { |
||
| 650 | |||
| 651 | /** |
||
| 652 | * Delete full sync configuration. |
||
| 653 | * |
||
| 654 | * @access private |
||
| 655 | * |
||
| 656 | * @return boolean Whether the configuration was deleted. |
||
| 657 | */ |
||
| 658 | private function delete_config() { |
||
| 661 | |||
| 662 | /** |
||
| 663 | * Retrieve the current full sync enqueue config. |
||
| 664 | * |
||
| 665 | * @access private |
||
| 666 | * |
||
| 667 | * @return array Full sync enqueue config. |
||
| 668 | */ |
||
| 669 | private function get_config() { |
||
| 672 | |||
| 673 | } |
||
| 674 |
This check looks for
@paramannotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.