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 | * 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 ) { |
||
| 193 | |||
| 194 | /** |
||
| 195 | * |
||
| 196 | * Get Available Full Sync queue slots. |
||
| 197 | * |
||
| 198 | * @return int |
||
| 199 | */ |
||
| 200 | public function get_available_queue_slots() { |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Get Modules that are configured to Full Sync and haven't finished enqueuing |
||
| 210 | * |
||
| 211 | * @param array $configs Full sync configuration for all sync modules. |
||
| 212 | * @param array $enqueue_status Current status of the queue, indexed by sync modules. |
||
| 213 | * |
||
| 214 | * @return array |
||
| 215 | */ |
||
| 216 | public function get_remaining_modules_to_enqueue( $configs, $enqueue_status ) { |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Enqueue the next items to sync. |
||
| 240 | * |
||
| 241 | * @access public |
||
| 242 | * |
||
| 243 | * @param array $configs Full sync configuration for all sync modules. |
||
| 244 | */ |
||
| 245 | private function _continue_enqueuing( $configs = null ) { |
||
| 272 | |||
| 273 | /** |
||
| 274 | * Enqueue 'jetpack_full_sync_end' and update 'queue_finished' status. |
||
| 275 | * |
||
| 276 | * @access public |
||
| 277 | * |
||
| 278 | * @param array $configs Full sync configuration for all sync modules. |
||
| 279 | */ |
||
| 280 | public function queue_full_sync_end( $configs ) { |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Get the range (min ID, max ID and total items) of items to sync. |
||
| 301 | * |
||
| 302 | * @access public |
||
| 303 | * |
||
| 304 | * @param string $type Type of sync item to get the range for. |
||
| 305 | * @return array Array of min ID, max ID and total items in the range. |
||
| 306 | */ |
||
| 307 | public function get_range( $type ) { |
||
| 336 | |||
| 337 | /** |
||
| 338 | * Get the range for content (posts and comments) to sync. |
||
| 339 | * |
||
| 340 | * @access private |
||
| 341 | * |
||
| 342 | * @param array $config Full sync configuration for this all sync modules. |
||
| 343 | * @return array Array of range (min ID, max ID, total items) for all content types. |
||
| 344 | */ |
||
| 345 | private function get_content_range( $config ) { |
||
| 357 | |||
| 358 | /** |
||
| 359 | * Update the progress after sync modules actions have been processed on the server. |
||
| 360 | * |
||
| 361 | * @access public |
||
| 362 | * |
||
| 363 | * @param array $actions Actions that have been processed on the server. |
||
| 364 | */ |
||
| 365 | public function update_sent_progress_action( $actions ) { |
||
| 410 | |||
| 411 | /** |
||
| 412 | * Get the name of the action for an item in the sync queue. |
||
| 413 | * |
||
| 414 | * @access public |
||
| 415 | * |
||
| 416 | * @param array $queue_item Item of the sync queue. |
||
| 417 | * @return string|boolean Name of the action, false if queue item is invalid. |
||
| 418 | */ |
||
| 419 | public function get_action_name( $queue_item ) { |
||
| 425 | |||
| 426 | /** |
||
| 427 | * Retrieve the total number of items we're syncing in a particular queue item (action). |
||
| 428 | * `$queue_item[1]` is expected to contain chunks of items, and `$queue_item[1][0]` |
||
| 429 | * represents the first (and only) chunk of items to sync in that action. |
||
| 430 | * |
||
| 431 | * @access public |
||
| 432 | * |
||
| 433 | * @param array $queue_item Item of the sync queue that corresponds to a particular action. |
||
| 434 | * @return int Total number of items in the action. |
||
| 435 | */ |
||
| 436 | public function get_action_totals( $queue_item ) { |
||
| 447 | |||
| 448 | /** |
||
| 449 | * Retrieve the total number of items for a set of actions, grouped by action name. |
||
| 450 | * |
||
| 451 | * @access public |
||
| 452 | * |
||
| 453 | * @param array $actions An array of actions. |
||
| 454 | * @return array An array, representing the total number of items, grouped per action. |
||
| 455 | */ |
||
| 456 | public function get_actions_totals( $actions ) { |
||
| 470 | |||
| 471 | /** |
||
| 472 | * Whether full sync has started. |
||
| 473 | * |
||
| 474 | * @access public |
||
| 475 | * |
||
| 476 | * @return boolean |
||
| 477 | */ |
||
| 478 | public function is_started() { |
||
| 481 | |||
| 482 | /** |
||
| 483 | * Whether full sync has finished. |
||
| 484 | * |
||
| 485 | * @access public |
||
| 486 | * |
||
| 487 | * @return boolean |
||
| 488 | */ |
||
| 489 | public function is_finished() { |
||
| 492 | |||
| 493 | /** |
||
| 494 | * Retrieve the status of the current full sync. |
||
| 495 | * |
||
| 496 | * @access public |
||
| 497 | * |
||
| 498 | * @return array Full sync status. |
||
| 499 | */ |
||
| 500 | public function get_status() { |
||
| 545 | |||
| 546 | /** |
||
| 547 | * Clear all the full sync status options. |
||
| 548 | * |
||
| 549 | * @access public |
||
| 550 | */ |
||
| 551 | public function clear_status() { |
||
| 566 | |||
| 567 | /** |
||
| 568 | * Clear all the full sync data. |
||
| 569 | * |
||
| 570 | * @access public |
||
| 571 | */ |
||
| 572 | public function reset_data() { |
||
| 579 | |||
| 580 | /** |
||
| 581 | * Get the value of a full sync status option. |
||
| 582 | * |
||
| 583 | * @access private |
||
| 584 | * |
||
| 585 | * @param string $name Name of the option. |
||
| 586 | * @param mixed $default Default value of the option. |
||
| 587 | * @return mixed Option value. |
||
| 588 | */ |
||
| 589 | private function get_status_option( $name, $default = null ) { |
||
| 594 | |||
| 595 | /** |
||
| 596 | * Update the value of a full sync status option. |
||
| 597 | * |
||
| 598 | * @access private |
||
| 599 | * |
||
| 600 | * @param string $name Name of the option. |
||
| 601 | * @param mixed $value Value of the option. |
||
| 602 | * @param boolean $autoload Whether the option should be autoloaded at the beginning of the request. |
||
| 603 | */ |
||
| 604 | private function update_status_option( $name, $value, $autoload = false ) { |
||
| 607 | |||
| 608 | /** |
||
| 609 | * Set the full sync enqueue status. |
||
| 610 | * |
||
| 611 | * @access private |
||
| 612 | * |
||
| 613 | * @param array $new_status The new full sync enqueue status. |
||
| 614 | */ |
||
| 615 | private function set_enqueue_status( $new_status ) { |
||
| 618 | |||
| 619 | /** |
||
| 620 | * Delete full sync enqueue status. |
||
| 621 | * |
||
| 622 | * @access private |
||
| 623 | * |
||
| 624 | * @return boolean Whether the status was deleted. |
||
| 625 | */ |
||
| 626 | private function delete_enqueue_status() { |
||
| 629 | |||
| 630 | /** |
||
| 631 | * Retrieve the current full sync enqueue status. |
||
| 632 | * |
||
| 633 | * @access private |
||
| 634 | * |
||
| 635 | * @return array Full sync enqueue status. |
||
| 636 | */ |
||
| 637 | public function get_enqueue_status() { |
||
| 640 | |||
| 641 | /** |
||
| 642 | * Set the full sync enqueue configuration. |
||
| 643 | * |
||
| 644 | * @access private |
||
| 645 | * |
||
| 646 | * @param array $config The new full sync enqueue configuration. |
||
| 647 | */ |
||
| 648 | private function set_config( $config ) { |
||
| 651 | |||
| 652 | /** |
||
| 653 | * Delete full sync configuration. |
||
| 654 | * |
||
| 655 | * @access private |
||
| 656 | * |
||
| 657 | * @return boolean Whether the configuration was deleted. |
||
| 658 | */ |
||
| 659 | private function delete_config() { |
||
| 662 | |||
| 663 | /** |
||
| 664 | * Retrieve the current full sync enqueue config. |
||
| 665 | * |
||
| 666 | * @access private |
||
| 667 | * |
||
| 668 | * @return array Full sync enqueue config. |
||
| 669 | */ |
||
| 670 | private function get_config() { |
||
| 673 | |||
| 674 | /** |
||
| 675 | * Update an option manually to bypass filters and caching. |
||
| 676 | * |
||
| 677 | * @access private |
||
| 678 | * |
||
| 679 | * @param string $name Option name. |
||
| 680 | * @param mixed $value Option value. |
||
| 681 | * @return int The number of updated rows in the database. |
||
| 682 | */ |
||
| 683 | private function write_option( $name, $value ) { |
||
| 712 | |||
| 713 | /** |
||
| 714 | * Update an option manually to bypass filters and caching. |
||
| 715 | * |
||
| 716 | * @access private |
||
| 717 | * |
||
| 718 | * @param string $name Option name. |
||
| 719 | * @param mixed $default Default option value. |
||
| 720 | * @return mixed Option value. |
||
| 721 | */ |
||
| 722 | private function read_option( $name, $default = null ) { |
||
| 738 | |||
| 739 | /** |
||
| 740 | * Prefix of the blog lock transient. |
||
| 741 | * |
||
| 742 | * @access public |
||
| 743 | * |
||
| 744 | * @var string |
||
| 745 | */ |
||
| 746 | const ENQUEUE_LOCK_TRANSIENT_PREFIX = 'jp_sync_enqueue_lock_'; |
||
| 747 | /** |
||
| 748 | * Lifetime of the blog lock transient. |
||
| 749 | * |
||
| 750 | * @access public |
||
| 751 | * |
||
| 752 | * @var int |
||
| 753 | */ |
||
| 754 | const ENQUEUE_LOCK_TRANSIENT_EXPIRY = 15; // Seconds. |
||
| 755 | |||
| 756 | /** |
||
| 757 | * Attempt to lock enqueueing when the server receives concurrent requests from the same blog. |
||
| 758 | * |
||
| 759 | * @access public |
||
| 760 | * |
||
| 761 | * @param int $expiry enqueue transient lifetime. |
||
| 762 | * @return boolean True if succeeded, false otherwise. |
||
| 763 | */ |
||
| 764 | View Code Duplication | public function attempt_enqueue_lock( $expiry = self::ENQUEUE_LOCK_TRANSIENT_EXPIRY ) { |
|
| 773 | /** |
||
| 774 | * Retrieve the enqueue lock transient name for the current blog. |
||
| 775 | * |
||
| 776 | * @access public |
||
| 777 | * |
||
| 778 | * @return string Name of the blog lock transient. |
||
| 779 | */ |
||
| 780 | private function get_concurrent_enqueue_transient_name() { |
||
| 783 | /** |
||
| 784 | * Remove the enqueue lock. |
||
| 785 | * |
||
| 786 | * @access public |
||
| 787 | */ |
||
| 788 | public function remove_enqueue_lock() { |
||
| 791 | } |
||
| 792 |
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.