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