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 |
||
| 26 | class Full_Sync extends Module { |
||
| 27 | /** |
||
| 28 | * Prefix of the full sync status option name. |
||
| 29 | * |
||
| 30 | * @var string |
||
| 31 | */ |
||
| 32 | const STATUS_OPTION_PREFIX = 'jetpack_sync_full_'; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Timeout between the previous and the next allowed full sync. |
||
| 36 | * |
||
| 37 | * @todo Remove this as it's no longer used since https://github.com/Automattic/jetpack/pull/4561 |
||
| 38 | * |
||
| 39 | * @var int |
||
| 40 | */ |
||
| 41 | const FULL_SYNC_TIMEOUT = 3600; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * |
||
| 45 | * Remaining Items to enqueue. |
||
| 46 | * |
||
| 47 | * @var int |
||
| 48 | */ |
||
| 49 | private $remaining_items_to_enqueue = 0; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * |
||
| 53 | * Per each module: total items to send, how many have been enqueued, the last object_id enqueued |
||
| 54 | * |
||
| 55 | * @var array |
||
| 56 | */ |
||
| 57 | private $enqueue_status; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Sync module name. |
||
| 61 | * |
||
| 62 | * @access public |
||
| 63 | * |
||
| 64 | * @return string |
||
| 65 | */ |
||
| 66 | public function name() { |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Initialize action listeners for full sync. |
||
| 72 | * |
||
| 73 | * @access public |
||
| 74 | * |
||
| 75 | * @param callable $callable Action handler callable. |
||
| 76 | */ |
||
| 77 | public function init_full_sync_listeners( $callable ) { |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Initialize the module in the sender. |
||
| 86 | * |
||
| 87 | * @access public |
||
| 88 | */ |
||
| 89 | public function init_before_send() { |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Start a full sync. |
||
| 96 | * |
||
| 97 | * @access public |
||
| 98 | * |
||
| 99 | * @param array $module_configs Full sync configuration for all sync modules. |
||
|
|
|||
| 100 | * @return bool Always returns true at success. |
||
| 101 | */ |
||
| 102 | public function start( $module_configs = null ) { |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Enqueue the next items to sync. |
||
| 189 | * |
||
| 190 | * @access public |
||
| 191 | * |
||
| 192 | * @param array $configs Full sync configuration for all sync modules. |
||
| 193 | * @param array $enqueue_status Current status of the queue, indexed by sync modules. |
||
| 194 | */ |
||
| 195 | public function continue_enqueuing( $configs = null, $enqueue_status = null ) { |
||
| 206 | |||
| 207 | /** |
||
| 208 | * |
||
| 209 | * Get Available Full Sync queue slots. |
||
| 210 | * |
||
| 211 | * @return int |
||
| 212 | */ |
||
| 213 | public function get_available_queue_slots() { |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Enqueue the next items to sync. |
||
| 223 | * |
||
| 224 | * @access public |
||
| 225 | * |
||
| 226 | * @param array $configs Full sync configuration for all sync modules. |
||
| 227 | */ |
||
| 228 | public function continue_enqueuing_with_lock( $configs = null ) { |
||
| 275 | |||
| 276 | /** |
||
| 277 | * Enqueue Full Sync Actions for the given module. |
||
| 278 | * |
||
| 279 | * @param Object $module The module to Enqueue. |
||
| 280 | * @param array $config The Full sync configuration for the module. |
||
| 281 | * @param array $status The Full sync enqueue status for the module. |
||
| 282 | * |
||
| 283 | * @return int |
||
| 284 | */ |
||
| 285 | public function enqueue_module( $module, $config, $status ) { |
||
| 311 | |||
| 312 | /** |
||
| 313 | * Get the range (min ID, max ID and total items) of items to sync. |
||
| 314 | * |
||
| 315 | * @access public |
||
| 316 | * |
||
| 317 | * @param string $type Type of sync item to get the range for. |
||
| 318 | * @return array Array of min ID, max ID and total items in the range. |
||
| 319 | */ |
||
| 320 | public function get_range( $type ) { |
||
| 349 | |||
| 350 | /** |
||
| 351 | * Get the range for content (posts and comments) to sync. |
||
| 352 | * |
||
| 353 | * @access private |
||
| 354 | * |
||
| 355 | * @param array $config Full sync configuration for this all sync modules. |
||
| 356 | * @return array Array of range (min ID, max ID, total items) for all content types. |
||
| 357 | */ |
||
| 358 | private function get_content_range( $config ) { |
||
| 370 | |||
| 371 | /** |
||
| 372 | * Update the progress after sync modules actions have been processed on the server. |
||
| 373 | * |
||
| 374 | * @access public |
||
| 375 | * |
||
| 376 | * @param array $actions Actions that have been processed on the server. |
||
| 377 | */ |
||
| 378 | public function update_sent_progress_action( $actions ) { |
||
| 423 | |||
| 424 | /** |
||
| 425 | * Get the name of the action for an item in the sync queue. |
||
| 426 | * |
||
| 427 | * @access public |
||
| 428 | * |
||
| 429 | * @param array $queue_item Item of the sync queue. |
||
| 430 | * @return string|boolean Name of the action, false if queue item is invalid. |
||
| 431 | */ |
||
| 432 | public function get_action_name( $queue_item ) { |
||
| 438 | |||
| 439 | /** |
||
| 440 | * Retrieve the total number of items we're syncing in a particular queue item (action). |
||
| 441 | * `$queue_item[1]` is expected to contain chunks of items, and `$queue_item[1][0]` |
||
| 442 | * represents the first (and only) chunk of items to sync in that action. |
||
| 443 | * |
||
| 444 | * @access public |
||
| 445 | * |
||
| 446 | * @param array $queue_item Item of the sync queue that corresponds to a particular action. |
||
| 447 | * @return int Total number of items in the action. |
||
| 448 | */ |
||
| 449 | public function get_action_totals( $queue_item ) { |
||
| 460 | |||
| 461 | /** |
||
| 462 | * Retrieve the total number of items for a set of actions, grouped by action name. |
||
| 463 | * |
||
| 464 | * @access public |
||
| 465 | * |
||
| 466 | * @param array $actions An array of actions. |
||
| 467 | * @return array An array, representing the total number of items, grouped per action. |
||
| 468 | */ |
||
| 469 | public function get_actions_totals( $actions ) { |
||
| 483 | |||
| 484 | /** |
||
| 485 | * Whether full sync has started. |
||
| 486 | * |
||
| 487 | * @access public |
||
| 488 | * |
||
| 489 | * @return boolean |
||
| 490 | */ |
||
| 491 | public function is_started() { |
||
| 494 | |||
| 495 | /** |
||
| 496 | * Whether full sync has finished. |
||
| 497 | * |
||
| 498 | * @access public |
||
| 499 | * |
||
| 500 | * @return boolean |
||
| 501 | */ |
||
| 502 | public function is_finished() { |
||
| 505 | |||
| 506 | /** |
||
| 507 | * Retrieve the status of the current full sync. |
||
| 508 | * |
||
| 509 | * @access public |
||
| 510 | * |
||
| 511 | * @return array Full sync status. |
||
| 512 | */ |
||
| 513 | public function get_status() { |
||
| 558 | |||
| 559 | /** |
||
| 560 | * Clear all the full sync status options. |
||
| 561 | * |
||
| 562 | * @access public |
||
| 563 | */ |
||
| 564 | public function clear_status() { |
||
| 579 | |||
| 580 | /** |
||
| 581 | * Clear all the full sync data. |
||
| 582 | * |
||
| 583 | * @access public |
||
| 584 | */ |
||
| 585 | public function reset_data() { |
||
| 592 | |||
| 593 | /** |
||
| 594 | * Get the value of a full sync status option. |
||
| 595 | * |
||
| 596 | * @access private |
||
| 597 | * |
||
| 598 | * @param string $name Name of the option. |
||
| 599 | * @param mixed $default Default value of the option. |
||
| 600 | * @return mixed Option value. |
||
| 601 | */ |
||
| 602 | private function get_status_option( $name, $default = null ) { |
||
| 607 | |||
| 608 | /** |
||
| 609 | * Update the value of a full sync status option. |
||
| 610 | * |
||
| 611 | * @access private |
||
| 612 | * |
||
| 613 | * @param string $name Name of the option. |
||
| 614 | * @param mixed $value Value of the option. |
||
| 615 | * @param boolean $autoload Whether the option should be autoloaded at the beginning of the request. |
||
| 616 | */ |
||
| 617 | private function update_status_option( $name, $value, $autoload = false ) { |
||
| 620 | |||
| 621 | /** |
||
| 622 | * Set the full sync enqueue status. |
||
| 623 | * |
||
| 624 | * @access private |
||
| 625 | * |
||
| 626 | * @param array $new_status The new full sync enqueue status. |
||
| 627 | */ |
||
| 628 | private function set_enqueue_status( $new_status ) { |
||
| 631 | |||
| 632 | /** |
||
| 633 | * Delete full sync enqueue status. |
||
| 634 | * |
||
| 635 | * @access private |
||
| 636 | * |
||
| 637 | * @return boolean Whether the status was deleted. |
||
| 638 | */ |
||
| 639 | private function delete_enqueue_status() { |
||
| 642 | |||
| 643 | /** |
||
| 644 | * Retrieve the current full sync enqueue status. |
||
| 645 | * |
||
| 646 | * @access private |
||
| 647 | * |
||
| 648 | * @return array Full sync enqueue status. |
||
| 649 | */ |
||
| 650 | public function get_enqueue_status() { |
||
| 653 | |||
| 654 | /** |
||
| 655 | * Set the full sync enqueue configuration. |
||
| 656 | * |
||
| 657 | * @access private |
||
| 658 | * |
||
| 659 | * @param array $config The new full sync enqueue configuration. |
||
| 660 | */ |
||
| 661 | private function set_config( $config ) { |
||
| 664 | |||
| 665 | /** |
||
| 666 | * Delete full sync configuration. |
||
| 667 | * |
||
| 668 | * @access private |
||
| 669 | * |
||
| 670 | * @return boolean Whether the configuration was deleted. |
||
| 671 | */ |
||
| 672 | private function delete_config() { |
||
| 675 | |||
| 676 | /** |
||
| 677 | * Retrieve the current full sync enqueue config. |
||
| 678 | * |
||
| 679 | * @access private |
||
| 680 | * |
||
| 681 | * @return array Full sync enqueue config. |
||
| 682 | */ |
||
| 683 | private function get_config() { |
||
| 686 | |||
| 687 | /** |
||
| 688 | * Update an option manually to bypass filters and caching. |
||
| 689 | * |
||
| 690 | * @access private |
||
| 691 | * |
||
| 692 | * @param string $name Option name. |
||
| 693 | * @param mixed $value Option value. |
||
| 694 | * @return int The number of updated rows in the database. |
||
| 695 | */ |
||
| 696 | private function write_option( $name, $value ) { |
||
| 725 | |||
| 726 | /** |
||
| 727 | * Update an option manually to bypass filters and caching. |
||
| 728 | * |
||
| 729 | * @access private |
||
| 730 | * |
||
| 731 | * @param string $name Option name. |
||
| 732 | * @param mixed $default Default option value. |
||
| 733 | * @return mixed Option value. |
||
| 734 | */ |
||
| 735 | private function read_option( $name, $default = null ) { |
||
| 751 | |||
| 752 | /** |
||
| 753 | * Prefix of the blog lock transient. |
||
| 754 | * |
||
| 755 | * @access public |
||
| 756 | * |
||
| 757 | * @var string |
||
| 758 | */ |
||
| 759 | const ENQUEUE_LOCK_TRANSIENT_PREFIX = 'jp_sync_enqueue_lock_'; |
||
| 760 | /** |
||
| 761 | * Lifetime of the blog lock transient. |
||
| 762 | * |
||
| 763 | * @access public |
||
| 764 | * |
||
| 765 | * @var int |
||
| 766 | */ |
||
| 767 | const ENQUEUE_LOCK_TRANSIENT_EXPIRY = 15; // Seconds. |
||
| 768 | |||
| 769 | /** |
||
| 770 | * Attempt to lock enqueueing when the server receives concurrent requests from the same blog. |
||
| 771 | * |
||
| 772 | * @access public |
||
| 773 | * |
||
| 774 | * @param int $expiry enqueue transient lifetime. |
||
| 775 | * @return boolean True if succeeded, false otherwise. |
||
| 776 | */ |
||
| 777 | View Code Duplication | public function attempt_enqueue_lock( $expiry = self::ENQUEUE_LOCK_TRANSIENT_EXPIRY ) { |
|
| 786 | /** |
||
| 787 | * Retrieve the enqueue lock transient name for the current blog. |
||
| 788 | * |
||
| 789 | * @access public |
||
| 790 | * |
||
| 791 | * @return string Name of the blog lock transient. |
||
| 792 | */ |
||
| 793 | private function get_concurrent_enqueue_transient_name() { |
||
| 796 | /** |
||
| 797 | * Remove the enqueue lock. |
||
| 798 | * |
||
| 799 | * @access public |
||
| 800 | */ |
||
| 801 | public function remove_enqueue_lock() { |
||
| 804 | } |
||
| 805 |
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.