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 ) { | ||
| 253 | |||
| 254 | /** | ||
| 255 | * Enqueue 'jetpack_full_sync_end' and update 'queue_finished' status. | ||
| 256 | * | ||
| 257 | * @access public | ||
| 258 | * | ||
| 259 | * @param array $configs Full sync configuration for all sync modules. | ||
| 260 | */ | ||
| 261 | 	public function queue_full_sync_end( $configs ) { | ||
| 279 | /** | ||
| 280 | * Enqueue Full Sync Actions for the given module. | ||
| 281 | * | ||
| 282 | * @param Object $module The module to Enqueue. | ||
| 283 | * @param array $configs The Full sync configuration for all modules. | ||
| 284 | * @param array $status The Full sync enqueue status for the module. | ||
| 285 | * | ||
| 286 | * @return int | ||
| 287 | */ | ||
| 288 | 	public function enqueue_module( $module, $configs, $status ) { | ||
| 314 | |||
| 315 | /** | ||
| 316 | * Get the range (min ID, max ID and total items) of items to sync. | ||
| 317 | * | ||
| 318 | * @access public | ||
| 319 | * | ||
| 320 | * @param string $type Type of sync item to get the range for. | ||
| 321 | * @return array Array of min ID, max ID and total items in the range. | ||
| 322 | */ | ||
| 323 | 	public function get_range( $type ) { | ||
| 352 | |||
| 353 | /** | ||
| 354 | * Get the range for content (posts and comments) to sync. | ||
| 355 | * | ||
| 356 | * @access private | ||
| 357 | * | ||
| 358 | * @param array $config Full sync configuration for this all sync modules. | ||
| 359 | * @return array Array of range (min ID, max ID, total items) for all content types. | ||
| 360 | */ | ||
| 361 | 	private function get_content_range( $config ) { | ||
| 373 | |||
| 374 | /** | ||
| 375 | * Update the progress after sync modules actions have been processed on the server. | ||
| 376 | * | ||
| 377 | * @access public | ||
| 378 | * | ||
| 379 | * @param array $actions Actions that have been processed on the server. | ||
| 380 | */ | ||
| 381 | 	public function update_sent_progress_action( $actions ) { | ||
| 426 | |||
| 427 | /** | ||
| 428 | * Get the name of the action for an item in the sync queue. | ||
| 429 | * | ||
| 430 | * @access public | ||
| 431 | * | ||
| 432 | * @param array $queue_item Item of the sync queue. | ||
| 433 | * @return string|boolean Name of the action, false if queue item is invalid. | ||
| 434 | */ | ||
| 435 | 	public function get_action_name( $queue_item ) { | ||
| 441 | |||
| 442 | /** | ||
| 443 | * Retrieve the total number of items we're syncing in a particular queue item (action). | ||
| 444 | * `$queue_item[1]` is expected to contain chunks of items, and `$queue_item[1][0]` | ||
| 445 | * represents the first (and only) chunk of items to sync in that action. | ||
| 446 | * | ||
| 447 | * @access public | ||
| 448 | * | ||
| 449 | * @param array $queue_item Item of the sync queue that corresponds to a particular action. | ||
| 450 | * @return int Total number of items in the action. | ||
| 451 | */ | ||
| 452 | 	public function get_action_totals( $queue_item ) { | ||
| 463 | |||
| 464 | /** | ||
| 465 | * Retrieve the total number of items for a set of actions, grouped by action name. | ||
| 466 | * | ||
| 467 | * @access public | ||
| 468 | * | ||
| 469 | * @param array $actions An array of actions. | ||
| 470 | * @return array An array, representing the total number of items, grouped per action. | ||
| 471 | */ | ||
| 472 | 	public function get_actions_totals( $actions ) { | ||
| 486 | |||
| 487 | /** | ||
| 488 | * Whether full sync has started. | ||
| 489 | * | ||
| 490 | * @access public | ||
| 491 | * | ||
| 492 | * @return boolean | ||
| 493 | */ | ||
| 494 | 	public function is_started() { | ||
| 497 | |||
| 498 | /** | ||
| 499 | * Whether full sync has finished. | ||
| 500 | * | ||
| 501 | * @access public | ||
| 502 | * | ||
| 503 | * @return boolean | ||
| 504 | */ | ||
| 505 | 	public function is_finished() { | ||
| 508 | |||
| 509 | /** | ||
| 510 | * Retrieve the status of the current full sync. | ||
| 511 | * | ||
| 512 | * @access public | ||
| 513 | * | ||
| 514 | * @return array Full sync status. | ||
| 515 | */ | ||
| 516 | 	public function get_status() { | ||
| 561 | |||
| 562 | /** | ||
| 563 | * Clear all the full sync status options. | ||
| 564 | * | ||
| 565 | * @access public | ||
| 566 | */ | ||
| 567 | 	public function clear_status() { | ||
| 582 | |||
| 583 | /** | ||
| 584 | * Clear all the full sync data. | ||
| 585 | * | ||
| 586 | * @access public | ||
| 587 | */ | ||
| 588 | 	public function reset_data() { | ||
| 595 | |||
| 596 | /** | ||
| 597 | * Get the value of a full sync status option. | ||
| 598 | * | ||
| 599 | * @access private | ||
| 600 | * | ||
| 601 | * @param string $name Name of the option. | ||
| 602 | * @param mixed $default Default value of the option. | ||
| 603 | * @return mixed Option value. | ||
| 604 | */ | ||
| 605 | 	private function get_status_option( $name, $default = null ) { | ||
| 610 | |||
| 611 | /** | ||
| 612 | * Update the value of a full sync status option. | ||
| 613 | * | ||
| 614 | * @access private | ||
| 615 | * | ||
| 616 | * @param string $name Name of the option. | ||
| 617 | * @param mixed $value Value of the option. | ||
| 618 | * @param boolean $autoload Whether the option should be autoloaded at the beginning of the request. | ||
| 619 | */ | ||
| 620 | 	private function update_status_option( $name, $value, $autoload = false ) { | ||
| 623 | |||
| 624 | /** | ||
| 625 | * Set the full sync enqueue status. | ||
| 626 | * | ||
| 627 | * @access private | ||
| 628 | * | ||
| 629 | * @param array $new_status The new full sync enqueue status. | ||
| 630 | */ | ||
| 631 | 	private function set_enqueue_status( $new_status ) { | ||
| 634 | |||
| 635 | /** | ||
| 636 | * Delete full sync enqueue status. | ||
| 637 | * | ||
| 638 | * @access private | ||
| 639 | * | ||
| 640 | * @return boolean Whether the status was deleted. | ||
| 641 | */ | ||
| 642 | 	private function delete_enqueue_status() { | ||
| 645 | |||
| 646 | /** | ||
| 647 | * Retrieve the current full sync enqueue status. | ||
| 648 | * | ||
| 649 | * @access private | ||
| 650 | * | ||
| 651 | * @return array Full sync enqueue status. | ||
| 652 | */ | ||
| 653 | 	public function get_enqueue_status() { | ||
| 656 | |||
| 657 | /** | ||
| 658 | * Set the full sync enqueue configuration. | ||
| 659 | * | ||
| 660 | * @access private | ||
| 661 | * | ||
| 662 | * @param array $config The new full sync enqueue configuration. | ||
| 663 | */ | ||
| 664 | 	private function set_config( $config ) { | ||
| 667 | |||
| 668 | /** | ||
| 669 | * Delete full sync configuration. | ||
| 670 | * | ||
| 671 | * @access private | ||
| 672 | * | ||
| 673 | * @return boolean Whether the configuration was deleted. | ||
| 674 | */ | ||
| 675 | 	private function delete_config() { | ||
| 678 | |||
| 679 | /** | ||
| 680 | * Retrieve the current full sync enqueue config. | ||
| 681 | * | ||
| 682 | * @access private | ||
| 683 | * | ||
| 684 | * @return array Full sync enqueue config. | ||
| 685 | */ | ||
| 686 | 	private function get_config() { | ||
| 689 | |||
| 690 | /** | ||
| 691 | * Update an option manually to bypass filters and caching. | ||
| 692 | * | ||
| 693 | * @access private | ||
| 694 | * | ||
| 695 | * @param string $name Option name. | ||
| 696 | * @param mixed $value Option value. | ||
| 697 | * @return int The number of updated rows in the database. | ||
| 698 | */ | ||
| 699 | 	private function write_option( $name, $value ) { | ||
| 728 | |||
| 729 | /** | ||
| 730 | * Update an option manually to bypass filters and caching. | ||
| 731 | * | ||
| 732 | * @access private | ||
| 733 | * | ||
| 734 | * @param string $name Option name. | ||
| 735 | * @param mixed $default Default option value. | ||
| 736 | * @return mixed Option value. | ||
| 737 | */ | ||
| 738 | 	private function read_option( $name, $default = null ) { | ||
| 754 | |||
| 755 | /** | ||
| 756 | * Prefix of the blog lock transient. | ||
| 757 | * | ||
| 758 | * @access public | ||
| 759 | * | ||
| 760 | * @var string | ||
| 761 | */ | ||
| 762 | const ENQUEUE_LOCK_TRANSIENT_PREFIX = 'jp_sync_enqueue_lock_'; | ||
| 763 | /** | ||
| 764 | * Lifetime of the blog lock transient. | ||
| 765 | * | ||
| 766 | * @access public | ||
| 767 | * | ||
| 768 | * @var int | ||
| 769 | */ | ||
| 770 | const ENQUEUE_LOCK_TRANSIENT_EXPIRY = 15; // Seconds. | ||
| 771 | |||
| 772 | /** | ||
| 773 | * Attempt to lock enqueueing when the server receives concurrent requests from the same blog. | ||
| 774 | * | ||
| 775 | * @access public | ||
| 776 | * | ||
| 777 | * @param int $expiry enqueue transient lifetime. | ||
| 778 | * @return boolean True if succeeded, false otherwise. | ||
| 779 | */ | ||
| 780 | View Code Duplication | 	public function attempt_enqueue_lock( $expiry = self::ENQUEUE_LOCK_TRANSIENT_EXPIRY ) { | |
| 789 | /** | ||
| 790 | * Retrieve the enqueue lock transient name for the current blog. | ||
| 791 | * | ||
| 792 | * @access public | ||
| 793 | * | ||
| 794 | * @return string Name of the blog lock transient. | ||
| 795 | */ | ||
| 796 | 	private function get_concurrent_enqueue_transient_name() { | ||
| 799 | /** | ||
| 800 | * Remove the enqueue lock. | ||
| 801 | * | ||
| 802 | * @access public | ||
| 803 | */ | ||
| 804 | 	public function remove_enqueue_lock() { | ||
| 807 | } | ||
| 808 | 
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.