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 | * Get Modules that are configured to Full Sync and haven't finished enqueuing | ||
| 223 | * | ||
| 224 | * @param array $configs Full sync configuration for all sync modules. | ||
| 225 | * | ||
| 226 | * @return array | ||
| 227 | */ | ||
| 228 | 	public function get_remaining_modules_to_enqueue( $configs ) { | ||
| 250 | |||
| 251 | /** | ||
| 252 | * Enqueue the next items to sync. | ||
| 253 | * | ||
| 254 | * @access public | ||
| 255 | * | ||
| 256 | * @param array $configs Full sync configuration for all sync modules. | ||
| 257 | */ | ||
| 258 | 	public function continue_enqueuing_with_lock( $configs = null ) { | ||
| 279 | |||
| 280 | /** | ||
| 281 | * Enqueue 'jetpack_full_sync_end' and update 'queue_finished' status. | ||
| 282 | * | ||
| 283 | * @access public | ||
| 284 | * | ||
| 285 | * @param array $configs Full sync configuration for all sync modules. | ||
| 286 | */ | ||
| 287 | 	public function queue_full_sync_end( $configs ) { | ||
| 305 | /** | ||
| 306 | * Enqueue Full Sync Actions for the given module. | ||
| 307 | * | ||
| 308 | * @param Object $module The module to Enqueue. | ||
| 309 | * @param array $config The Full sync configuration for the modules. | ||
| 310 | * | ||
| 311 | * @return int | ||
| 312 | */ | ||
| 313 | 	public function enqueue_module( $module, $config ) { | ||
| 326 | |||
| 327 | /** | ||
| 328 | * Get the range (min ID, max ID and total items) of items to sync. | ||
| 329 | * | ||
| 330 | * @access public | ||
| 331 | * | ||
| 332 | * @param string $type Type of sync item to get the range for. | ||
| 333 | * @return array Array of min ID, max ID and total items in the range. | ||
| 334 | */ | ||
| 335 | 	public function get_range( $type ) { | ||
| 364 | |||
| 365 | /** | ||
| 366 | * Get the range for content (posts and comments) to sync. | ||
| 367 | * | ||
| 368 | * @access private | ||
| 369 | * | ||
| 370 | * @param array $config Full sync configuration for this all sync modules. | ||
| 371 | * @return array Array of range (min ID, max ID, total items) for all content types. | ||
| 372 | */ | ||
| 373 | 	private function get_content_range( $config ) { | ||
| 385 | |||
| 386 | /** | ||
| 387 | * Update the progress after sync modules actions have been processed on the server. | ||
| 388 | * | ||
| 389 | * @access public | ||
| 390 | * | ||
| 391 | * @param array $actions Actions that have been processed on the server. | ||
| 392 | */ | ||
| 393 | 	public function update_sent_progress_action( $actions ) { | ||
| 438 | |||
| 439 | /** | ||
| 440 | * Get the name of the action for an item in the sync queue. | ||
| 441 | * | ||
| 442 | * @access public | ||
| 443 | * | ||
| 444 | * @param array $queue_item Item of the sync queue. | ||
| 445 | * @return string|boolean Name of the action, false if queue item is invalid. | ||
| 446 | */ | ||
| 447 | 	public function get_action_name( $queue_item ) { | ||
| 453 | |||
| 454 | /** | ||
| 455 | * Retrieve the total number of items we're syncing in a particular queue item (action). | ||
| 456 | * `$queue_item[1]` is expected to contain chunks of items, and `$queue_item[1][0]` | ||
| 457 | * represents the first (and only) chunk of items to sync in that action. | ||
| 458 | * | ||
| 459 | * @access public | ||
| 460 | * | ||
| 461 | * @param array $queue_item Item of the sync queue that corresponds to a particular action. | ||
| 462 | * @return int Total number of items in the action. | ||
| 463 | */ | ||
| 464 | 	public function get_action_totals( $queue_item ) { | ||
| 475 | |||
| 476 | /** | ||
| 477 | * Retrieve the total number of items for a set of actions, grouped by action name. | ||
| 478 | * | ||
| 479 | * @access public | ||
| 480 | * | ||
| 481 | * @param array $actions An array of actions. | ||
| 482 | * @return array An array, representing the total number of items, grouped per action. | ||
| 483 | */ | ||
| 484 | 	public function get_actions_totals( $actions ) { | ||
| 498 | |||
| 499 | /** | ||
| 500 | * Whether full sync has started. | ||
| 501 | * | ||
| 502 | * @access public | ||
| 503 | * | ||
| 504 | * @return boolean | ||
| 505 | */ | ||
| 506 | 	public function is_started() { | ||
| 509 | |||
| 510 | /** | ||
| 511 | * Whether full sync has finished. | ||
| 512 | * | ||
| 513 | * @access public | ||
| 514 | * | ||
| 515 | * @return boolean | ||
| 516 | */ | ||
| 517 | 	public function is_finished() { | ||
| 520 | |||
| 521 | /** | ||
| 522 | * Retrieve the status of the current full sync. | ||
| 523 | * | ||
| 524 | * @access public | ||
| 525 | * | ||
| 526 | * @return array Full sync status. | ||
| 527 | */ | ||
| 528 | 	public function get_status() { | ||
| 573 | |||
| 574 | /** | ||
| 575 | * Clear all the full sync status options. | ||
| 576 | * | ||
| 577 | * @access public | ||
| 578 | */ | ||
| 579 | 	public function clear_status() { | ||
| 594 | |||
| 595 | /** | ||
| 596 | * Clear all the full sync data. | ||
| 597 | * | ||
| 598 | * @access public | ||
| 599 | */ | ||
| 600 | 	public function reset_data() { | ||
| 607 | |||
| 608 | /** | ||
| 609 | * Get the value of a full sync status option. | ||
| 610 | * | ||
| 611 | * @access private | ||
| 612 | * | ||
| 613 | * @param string $name Name of the option. | ||
| 614 | * @param mixed $default Default value of the option. | ||
| 615 | * @return mixed Option value. | ||
| 616 | */ | ||
| 617 | 	private function get_status_option( $name, $default = null ) { | ||
| 622 | |||
| 623 | /** | ||
| 624 | * Update the value of a full sync status option. | ||
| 625 | * | ||
| 626 | * @access private | ||
| 627 | * | ||
| 628 | * @param string $name Name of the option. | ||
| 629 | * @param mixed $value Value of the option. | ||
| 630 | * @param boolean $autoload Whether the option should be autoloaded at the beginning of the request. | ||
| 631 | */ | ||
| 632 | 	private function update_status_option( $name, $value, $autoload = false ) { | ||
| 635 | |||
| 636 | /** | ||
| 637 | * Set the full sync enqueue status. | ||
| 638 | * | ||
| 639 | * @access private | ||
| 640 | * | ||
| 641 | * @param array $new_status The new full sync enqueue status. | ||
| 642 | */ | ||
| 643 | 	private function set_enqueue_status( $new_status ) { | ||
| 646 | |||
| 647 | /** | ||
| 648 | * Delete full sync enqueue status. | ||
| 649 | * | ||
| 650 | * @access private | ||
| 651 | * | ||
| 652 | * @return boolean Whether the status was deleted. | ||
| 653 | */ | ||
| 654 | 	private function delete_enqueue_status() { | ||
| 657 | |||
| 658 | /** | ||
| 659 | * Retrieve the current full sync enqueue status. | ||
| 660 | * | ||
| 661 | * @access private | ||
| 662 | * | ||
| 663 | * @return array Full sync enqueue status. | ||
| 664 | */ | ||
| 665 | 	public function get_enqueue_status() { | ||
| 668 | |||
| 669 | /** | ||
| 670 | * Set the full sync enqueue configuration. | ||
| 671 | * | ||
| 672 | * @access private | ||
| 673 | * | ||
| 674 | * @param array $config The new full sync enqueue configuration. | ||
| 675 | */ | ||
| 676 | 	private function set_config( $config ) { | ||
| 679 | |||
| 680 | /** | ||
| 681 | * Delete full sync configuration. | ||
| 682 | * | ||
| 683 | * @access private | ||
| 684 | * | ||
| 685 | * @return boolean Whether the configuration was deleted. | ||
| 686 | */ | ||
| 687 | 	private function delete_config() { | ||
| 690 | |||
| 691 | /** | ||
| 692 | * Retrieve the current full sync enqueue config. | ||
| 693 | * | ||
| 694 | * @access private | ||
| 695 | * | ||
| 696 | * @return array Full sync enqueue config. | ||
| 697 | */ | ||
| 698 | 	private function get_config() { | ||
| 701 | |||
| 702 | /** | ||
| 703 | * Update an option manually to bypass filters and caching. | ||
| 704 | * | ||
| 705 | * @access private | ||
| 706 | * | ||
| 707 | * @param string $name Option name. | ||
| 708 | * @param mixed $value Option value. | ||
| 709 | * @return int The number of updated rows in the database. | ||
| 710 | */ | ||
| 711 | 	private function write_option( $name, $value ) { | ||
| 740 | |||
| 741 | /** | ||
| 742 | * Update an option manually to bypass filters and caching. | ||
| 743 | * | ||
| 744 | * @access private | ||
| 745 | * | ||
| 746 | * @param string $name Option name. | ||
| 747 | * @param mixed $default Default option value. | ||
| 748 | * @return mixed Option value. | ||
| 749 | */ | ||
| 750 | 	private function read_option( $name, $default = null ) { | ||
| 766 | |||
| 767 | /** | ||
| 768 | * Prefix of the blog lock transient. | ||
| 769 | * | ||
| 770 | * @access public | ||
| 771 | * | ||
| 772 | * @var string | ||
| 773 | */ | ||
| 774 | const ENQUEUE_LOCK_TRANSIENT_PREFIX = 'jp_sync_enqueue_lock_'; | ||
| 775 | /** | ||
| 776 | * Lifetime of the blog lock transient. | ||
| 777 | * | ||
| 778 | * @access public | ||
| 779 | * | ||
| 780 | * @var int | ||
| 781 | */ | ||
| 782 | const ENQUEUE_LOCK_TRANSIENT_EXPIRY = 15; // Seconds. | ||
| 783 | |||
| 784 | /** | ||
| 785 | * Attempt to lock enqueueing when the server receives concurrent requests from the same blog. | ||
| 786 | * | ||
| 787 | * @access public | ||
| 788 | * | ||
| 789 | * @param int $expiry enqueue transient lifetime. | ||
| 790 | * @return boolean True if succeeded, false otherwise. | ||
| 791 | */ | ||
| 792 | View Code Duplication | 	public function attempt_enqueue_lock( $expiry = self::ENQUEUE_LOCK_TRANSIENT_EXPIRY ) { | |
| 801 | /** | ||
| 802 | * Retrieve the enqueue lock transient name for the current blog. | ||
| 803 | * | ||
| 804 | * @access public | ||
| 805 | * | ||
| 806 | * @return string Name of the blog lock transient. | ||
| 807 | */ | ||
| 808 | 	private function get_concurrent_enqueue_transient_name() { | ||
| 811 | /** | ||
| 812 | * Remove the enqueue lock. | ||
| 813 | * | ||
| 814 | * @access public | ||
| 815 | */ | ||
| 816 | 	public function remove_enqueue_lock() { | ||
| 819 | } | ||
| 820 | 
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.