Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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 | * Enqueue Lock name. |
||
| 37 | * |
||
| 38 | * @var string |
||
| 39 | */ |
||
| 40 | const ENQUEUE_LOCK_NAME = 'full_sync_enqueue'; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Sync module name. |
||
| 44 | * |
||
| 45 | * @access public |
||
| 46 | * |
||
| 47 | * @return string |
||
| 48 | */ |
||
| 49 | public function name() { |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Initialize action listeners for full sync. |
||
| 55 | * |
||
| 56 | * @access public |
||
| 57 | * |
||
| 58 | * @param callable $callable Action handler callable. |
||
| 59 | */ |
||
| 60 | public function init_full_sync_listeners( $callable ) { |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Initialize the module in the sender. |
||
| 69 | * |
||
| 70 | * @access public |
||
| 71 | */ |
||
| 72 | public function init_before_send() { |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Start a full sync. |
||
| 79 | * |
||
| 80 | * @access public |
||
| 81 | * |
||
| 82 | * @param array $module_configs Full sync configuration for all sync modules. |
||
|
|
|||
| 83 | * @return bool Always returns true at success. |
||
| 84 | */ |
||
| 85 | public function start( $module_configs = null ) { |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Enqueue the next items to sync. |
||
| 172 | * |
||
| 173 | * @access public |
||
| 174 | * |
||
| 175 | * @param array $configs Full sync configuration for all sync modules. |
||
| 176 | */ |
||
| 177 | View Code Duplication | public function continue_enqueuing( $configs = null ) { |
|
| 186 | |||
| 187 | /** |
||
| 188 | * Get Modules that are configured to Full Sync and haven't finished enqueuing |
||
| 189 | * |
||
| 190 | * @param array $configs Full sync configuration for all sync modules. |
||
| 191 | * |
||
| 192 | * @return array |
||
| 193 | */ |
||
| 194 | public function get_remaining_modules_to_enqueue( $configs ) { |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Enqueue the next items to sync. |
||
| 225 | * |
||
| 226 | * @access public |
||
| 227 | * |
||
| 228 | * @param array $configs Full sync configuration for all sync modules. |
||
| 229 | */ |
||
| 230 | public function enqueue( $configs = null ) { |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Enqueue 'jetpack_full_sync_end' and update 'queue_finished' status. |
||
| 272 | * |
||
| 273 | * @access public |
||
| 274 | * |
||
| 275 | * @param array $configs Full sync configuration for all sync modules. |
||
| 276 | */ |
||
| 277 | public function queue_full_sync_end( $configs ) { |
||
| 295 | |||
| 296 | /** |
||
| 297 | * Get the range (min ID, max ID and total items) of items to sync. |
||
| 298 | * |
||
| 299 | * @access public |
||
| 300 | * |
||
| 301 | * @param string $type Type of sync item to get the range for. |
||
| 302 | * @return array Array of min ID, max ID and total items in the range. |
||
| 303 | */ |
||
| 304 | View Code Duplication | public function get_range( $type ) { |
|
| 333 | |||
| 334 | /** |
||
| 335 | * Get the range for content (posts and comments) to sync. |
||
| 336 | * |
||
| 337 | * @access private |
||
| 338 | * |
||
| 339 | * @param array $config Full sync configuration for this all sync modules. |
||
| 340 | * @return array Array of range (min ID, max ID, total items) for all content types. |
||
| 341 | */ |
||
| 342 | View Code Duplication | private function get_content_range( $config ) { |
|
| 354 | |||
| 355 | /** |
||
| 356 | * Update the progress after sync modules actions have been processed on the server. |
||
| 357 | * |
||
| 358 | * @access public |
||
| 359 | * |
||
| 360 | * @param array $actions Actions that have been processed on the server. |
||
| 361 | */ |
||
| 362 | public function update_sent_progress_action( $actions ) { |
||
| 407 | |||
| 408 | /** |
||
| 409 | * Returns the progress percentage of a full sync. |
||
| 410 | * |
||
| 411 | * @access public |
||
| 412 | * |
||
| 413 | * @return int|null |
||
| 414 | */ |
||
| 415 | public function get_sync_progress_percentage() { |
||
| 450 | |||
| 451 | /** |
||
| 452 | * Get the name of the action for an item in the sync queue. |
||
| 453 | * |
||
| 454 | * @access public |
||
| 455 | * |
||
| 456 | * @param array $queue_item Item of the sync queue. |
||
| 457 | * @return string|boolean Name of the action, false if queue item is invalid. |
||
| 458 | */ |
||
| 459 | public function get_action_name( $queue_item ) { |
||
| 465 | |||
| 466 | /** |
||
| 467 | * Retrieve the total number of items we're syncing in a particular queue item (action). |
||
| 468 | * `$queue_item[1]` is expected to contain chunks of items, and `$queue_item[1][0]` |
||
| 469 | * represents the first (and only) chunk of items to sync in that action. |
||
| 470 | * |
||
| 471 | * @access public |
||
| 472 | * |
||
| 473 | * @param array $queue_item Item of the sync queue that corresponds to a particular action. |
||
| 474 | * @return int Total number of items in the action. |
||
| 475 | */ |
||
| 476 | public function get_action_totals( $queue_item ) { |
||
| 487 | |||
| 488 | /** |
||
| 489 | * Retrieve the total number of items for a set of actions, grouped by action name. |
||
| 490 | * |
||
| 491 | * @access public |
||
| 492 | * |
||
| 493 | * @param array $actions An array of actions. |
||
| 494 | * @return array An array, representing the total number of items, grouped per action. |
||
| 495 | */ |
||
| 496 | public function get_actions_totals( $actions ) { |
||
| 510 | |||
| 511 | /** |
||
| 512 | * Whether full sync has started. |
||
| 513 | * |
||
| 514 | * @access public |
||
| 515 | * |
||
| 516 | * @return boolean |
||
| 517 | */ |
||
| 518 | public function is_started() { |
||
| 521 | |||
| 522 | /** |
||
| 523 | * Whether full sync has finished. |
||
| 524 | * |
||
| 525 | * @access public |
||
| 526 | * |
||
| 527 | * @return boolean |
||
| 528 | */ |
||
| 529 | public function is_finished() { |
||
| 532 | |||
| 533 | /** |
||
| 534 | * Retrieve the status of the current full sync. |
||
| 535 | * |
||
| 536 | * @access public |
||
| 537 | * |
||
| 538 | * @return array Full sync status. |
||
| 539 | */ |
||
| 540 | public function get_status() { |
||
| 585 | |||
| 586 | /** |
||
| 587 | * Clear all the full sync status options. |
||
| 588 | * |
||
| 589 | * @access public |
||
| 590 | */ |
||
| 591 | public function clear_status() { |
||
| 606 | |||
| 607 | /** |
||
| 608 | * Clear all the full sync data. |
||
| 609 | * |
||
| 610 | * @access public |
||
| 611 | */ |
||
| 612 | public function reset_data() { |
||
| 620 | |||
| 621 | /** |
||
| 622 | * Get the value of a full sync status option. |
||
| 623 | * |
||
| 624 | * @access private |
||
| 625 | * |
||
| 626 | * @param string $name Name of the option. |
||
| 627 | * @param mixed $default Default value of the option. |
||
| 628 | * @return mixed Option value. |
||
| 629 | */ |
||
| 630 | private function get_status_option( $name, $default = null ) { |
||
| 635 | |||
| 636 | /** |
||
| 637 | * Update the value of a full sync status option. |
||
| 638 | * |
||
| 639 | * @access private |
||
| 640 | * |
||
| 641 | * @param string $name Name of the option. |
||
| 642 | * @param mixed $value Value of the option. |
||
| 643 | * @param boolean $autoload Whether the option should be autoloaded at the beginning of the request. |
||
| 644 | */ |
||
| 645 | private function update_status_option( $name, $value, $autoload = false ) { |
||
| 648 | |||
| 649 | /** |
||
| 650 | * Set the full sync enqueue status. |
||
| 651 | * |
||
| 652 | * @access private |
||
| 653 | * |
||
| 654 | * @param array $new_status The new full sync enqueue status. |
||
| 655 | */ |
||
| 656 | private function set_enqueue_status( $new_status ) { |
||
| 659 | |||
| 660 | /** |
||
| 661 | * Delete full sync enqueue status. |
||
| 662 | * |
||
| 663 | * @access private |
||
| 664 | * |
||
| 665 | * @return boolean Whether the status was deleted. |
||
| 666 | */ |
||
| 667 | private function delete_enqueue_status() { |
||
| 670 | |||
| 671 | /** |
||
| 672 | * Retrieve the current full sync enqueue status. |
||
| 673 | * |
||
| 674 | * @access private |
||
| 675 | * |
||
| 676 | * @return array Full sync enqueue status. |
||
| 677 | */ |
||
| 678 | public function get_enqueue_status() { |
||
| 681 | |||
| 682 | /** |
||
| 683 | * Set the full sync enqueue configuration. |
||
| 684 | * |
||
| 685 | * @access private |
||
| 686 | * |
||
| 687 | * @param array $config The new full sync enqueue configuration. |
||
| 688 | */ |
||
| 689 | private function set_config( $config ) { |
||
| 692 | |||
| 693 | /** |
||
| 694 | * Delete full sync configuration. |
||
| 695 | * |
||
| 696 | * @access private |
||
| 697 | * |
||
| 698 | * @return boolean Whether the configuration was deleted. |
||
| 699 | */ |
||
| 700 | private function delete_config() { |
||
| 703 | |||
| 704 | /** |
||
| 705 | * Retrieve the current full sync enqueue config. |
||
| 706 | * |
||
| 707 | * @access private |
||
| 708 | * |
||
| 709 | * @return array Full sync enqueue config. |
||
| 710 | */ |
||
| 711 | private function get_config() { |
||
| 714 | |||
| 715 | } |
||
| 716 |
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.