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 | * @param array $enqueue_status Current status of the queue, indexed by sync modules. |
||
| 178 | */ |
||
| 179 | public function continue_enqueuing( $configs = null, $enqueue_status = 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_with_lock( $configs = null, $enqueue_status = null ) { |
||
| 279 | |||
| 280 | /** |
||
| 281 | * Get the range (min ID, max ID and total items) of items to sync. |
||
| 282 | * |
||
| 283 | * @access public |
||
| 284 | * |
||
| 285 | * @param string $type Type of sync item to get the range for. |
||
| 286 | * @return array Array of min ID, max ID and total items in the range. |
||
| 287 | */ |
||
| 288 | public function get_range( $type ) { |
||
| 317 | |||
| 318 | /** |
||
| 319 | * Get the range for content (posts and comments) to sync. |
||
| 320 | * |
||
| 321 | * @access private |
||
| 322 | * |
||
| 323 | * @param array $config Full sync configuration for this all sync modules. |
||
| 324 | * @return array Array of range (min ID, max ID, total items) for all content types. |
||
| 325 | */ |
||
| 326 | private function get_content_range( $config ) { |
||
| 338 | |||
| 339 | /** |
||
| 340 | * Update the progress after sync modules actions have been processed on the server. |
||
| 341 | * |
||
| 342 | * @access public |
||
| 343 | * |
||
| 344 | * @param array $actions Actions that have been processed on the server. |
||
| 345 | */ |
||
| 346 | public function update_sent_progress_action( $actions ) { |
||
| 391 | |||
| 392 | /** |
||
| 393 | * Get the name of the action for an item in the sync queue. |
||
| 394 | * |
||
| 395 | * @access public |
||
| 396 | * |
||
| 397 | * @param array $queue_item Item of the sync queue. |
||
| 398 | * @return string|boolean Name of the action, false if queue item is invalid. |
||
| 399 | */ |
||
| 400 | public function get_action_name( $queue_item ) { |
||
| 406 | |||
| 407 | /** |
||
| 408 | * Retrieve the total number of items we're syncing in a particular queue item (action). |
||
| 409 | * `$queue_item[1]` is expected to contain chunks of items, and `$queue_item[1][0]` |
||
| 410 | * represents the first (and only) chunk of items to sync in that action. |
||
| 411 | * |
||
| 412 | * @access public |
||
| 413 | * |
||
| 414 | * @param array $queue_item Item of the sync queue that corresponds to a particular action. |
||
| 415 | * @return int Total number of items in the action. |
||
| 416 | */ |
||
| 417 | public function get_action_totals( $queue_item ) { |
||
| 428 | |||
| 429 | /** |
||
| 430 | * Retrieve the total number of items for a set of actions, grouped by action name. |
||
| 431 | * |
||
| 432 | * @access public |
||
| 433 | * |
||
| 434 | * @param array $actions An array of actions. |
||
| 435 | * @return array An array, representing the total number of items, grouped per action. |
||
| 436 | */ |
||
| 437 | public function get_actions_totals( $actions ) { |
||
| 451 | |||
| 452 | /** |
||
| 453 | * Whether full sync has started. |
||
| 454 | * |
||
| 455 | * @access public |
||
| 456 | * |
||
| 457 | * @return boolean |
||
| 458 | */ |
||
| 459 | public function is_started() { |
||
| 462 | |||
| 463 | /** |
||
| 464 | * Whether full sync has finished. |
||
| 465 | * |
||
| 466 | * @access public |
||
| 467 | * |
||
| 468 | * @return boolean |
||
| 469 | */ |
||
| 470 | public function is_finished() { |
||
| 473 | |||
| 474 | /** |
||
| 475 | * Retrieve the status of the current full sync. |
||
| 476 | * |
||
| 477 | * @access public |
||
| 478 | * |
||
| 479 | * @return array Full sync status. |
||
| 480 | */ |
||
| 481 | public function get_status() { |
||
| 526 | |||
| 527 | /** |
||
| 528 | * Clear all the full sync status options. |
||
| 529 | * |
||
| 530 | * @access public |
||
| 531 | */ |
||
| 532 | public function clear_status() { |
||
| 547 | |||
| 548 | /** |
||
| 549 | * Clear all the full sync data. |
||
| 550 | * |
||
| 551 | * @access public |
||
| 552 | */ |
||
| 553 | public function reset_data() { |
||
| 560 | |||
| 561 | /** |
||
| 562 | * Get the value of a full sync status option. |
||
| 563 | * |
||
| 564 | * @access private |
||
| 565 | * |
||
| 566 | * @param string $name Name of the option. |
||
| 567 | * @param mixed $default Default value of the option. |
||
| 568 | * @return mixed Option value. |
||
| 569 | */ |
||
| 570 | private function get_status_option( $name, $default = null ) { |
||
| 575 | |||
| 576 | /** |
||
| 577 | * Update the value of a full sync status option. |
||
| 578 | * |
||
| 579 | * @access private |
||
| 580 | * |
||
| 581 | * @param string $name Name of the option. |
||
| 582 | * @param mixed $value Value of the option. |
||
| 583 | * @param boolean $autoload Whether the option should be autoloaded at the beginning of the request. |
||
| 584 | */ |
||
| 585 | private function update_status_option( $name, $value, $autoload = false ) { |
||
| 588 | |||
| 589 | /** |
||
| 590 | * Set the full sync enqueue status. |
||
| 591 | * |
||
| 592 | * @access private |
||
| 593 | * |
||
| 594 | * @param array $new_status The new full sync enqueue status. |
||
| 595 | */ |
||
| 596 | private function set_enqueue_status( $new_status ) { |
||
| 599 | |||
| 600 | /** |
||
| 601 | * Delete full sync enqueue status. |
||
| 602 | * |
||
| 603 | * @access private |
||
| 604 | * |
||
| 605 | * @return boolean Whether the status was deleted. |
||
| 606 | */ |
||
| 607 | private function delete_enqueue_status() { |
||
| 610 | |||
| 611 | /** |
||
| 612 | * Retrieve the current full sync enqueue status. |
||
| 613 | * |
||
| 614 | * @access private |
||
| 615 | * |
||
| 616 | * @return array Full sync enqueue status. |
||
| 617 | */ |
||
| 618 | public function get_enqueue_status() { |
||
| 621 | |||
| 622 | /** |
||
| 623 | * Set the full sync enqueue configuration. |
||
| 624 | * |
||
| 625 | * @access private |
||
| 626 | * |
||
| 627 | * @param array $config The new full sync enqueue configuration. |
||
| 628 | */ |
||
| 629 | private function set_config( $config ) { |
||
| 632 | |||
| 633 | /** |
||
| 634 | * Delete full sync configuration. |
||
| 635 | * |
||
| 636 | * @access private |
||
| 637 | * |
||
| 638 | * @return boolean Whether the configuration was deleted. |
||
| 639 | */ |
||
| 640 | private function delete_config() { |
||
| 643 | |||
| 644 | /** |
||
| 645 | * Retrieve the current full sync enqueue config. |
||
| 646 | * |
||
| 647 | * @access private |
||
| 648 | * |
||
| 649 | * @return array Full sync enqueue config. |
||
| 650 | */ |
||
| 651 | private function get_config() { |
||
| 654 | |||
| 655 | /** |
||
| 656 | * Update an option manually to bypass filters and caching. |
||
| 657 | * |
||
| 658 | * @access private |
||
| 659 | * |
||
| 660 | * @param string $name Option name. |
||
| 661 | * @param mixed $value Option value. |
||
| 662 | * @return int The number of updated rows in the database. |
||
| 663 | */ |
||
| 664 | private function write_option( $name, $value ) { |
||
| 693 | |||
| 694 | /** |
||
| 695 | * Update an option manually to bypass filters and caching. |
||
| 696 | * |
||
| 697 | * @access private |
||
| 698 | * |
||
| 699 | * @param string $name Option name. |
||
| 700 | * @param mixed $default Default option value. |
||
| 701 | * @return mixed Option value. |
||
| 702 | */ |
||
| 703 | private function read_option( $name, $default = null ) { |
||
| 719 | |||
| 720 | /** |
||
| 721 | * Prefix of the blog lock transient. |
||
| 722 | * |
||
| 723 | * @access public |
||
| 724 | * |
||
| 725 | * @var string |
||
| 726 | */ |
||
| 727 | const ENQUEUE_LOCK_TRANSIENT_PREFIX = 'jp_sync_enqueue_lock_'; |
||
| 728 | /** |
||
| 729 | * Lifetime of the blog lock transient. |
||
| 730 | * |
||
| 731 | * @access public |
||
| 732 | * |
||
| 733 | * @var int |
||
| 734 | */ |
||
| 735 | const ENQUEUE_LOCK_TRANSIENT_EXPIRY = 15; // Seconds. |
||
| 736 | |||
| 737 | /** |
||
| 738 | * Attempt to lock enqueueing when the server receives concurrent requests from the same blog. |
||
| 739 | * |
||
| 740 | * @access public |
||
| 741 | * |
||
| 742 | * @param int $expiry enqueue transient lifetime. |
||
| 743 | * @return boolean True if succeeded, false otherwise. |
||
| 744 | */ |
||
| 745 | View Code Duplication | public function attempt_enqueue_lock( $expiry = self::ENQUEUE_LOCK_TRANSIENT_EXPIRY ) { |
|
| 754 | /** |
||
| 755 | * Retrieve the enqueue lock transient name for the current blog. |
||
| 756 | * |
||
| 757 | * @access public |
||
| 758 | * |
||
| 759 | * @return string Name of the blog lock transient. |
||
| 760 | */ |
||
| 761 | private function get_concurrent_enqueue_transient_name() { |
||
| 764 | /** |
||
| 765 | * Remove the enqueue lock. |
||
| 766 | * |
||
| 767 | * @access public |
||
| 768 | */ |
||
| 769 | public function remove_enqueue_lock() { |
||
| 772 | } |
||
| 773 |
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.