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 ) { |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Enqueue the next items to sync. Once we have a lock. |
||
| 204 | * |
||
| 205 | * @param array $configs Full sync configuration for all sync modules. |
||
| 206 | * @param array $enqueue_status Current status of the queue, indexed by sync modules. |
||
| 207 | * @param Object $full_sync_queue The full sync queue. |
||
| 208 | */ |
||
| 209 | private function continue_enqueuing_with_lock( $configs, $enqueue_status, $full_sync_queue ) { |
||
| 282 | |||
| 283 | /** |
||
| 284 | * Get the range (min ID, max ID and total items) of items to sync. |
||
| 285 | * |
||
| 286 | * @access public |
||
| 287 | * |
||
| 288 | * @param string $type Type of sync item to get the range for. |
||
| 289 | * @return array Array of min ID, max ID and total items in the range. |
||
| 290 | */ |
||
| 291 | public function get_range( $type ) { |
||
| 320 | |||
| 321 | /** |
||
| 322 | * Get the range for content (posts and comments) to sync. |
||
| 323 | * |
||
| 324 | * @access private |
||
| 325 | * |
||
| 326 | * @param array $config Full sync configuration for this all sync modules. |
||
| 327 | * @return array Array of range (min ID, max ID, total items) for all content types. |
||
| 328 | */ |
||
| 329 | private function get_content_range( $config ) { |
||
| 341 | |||
| 342 | /** |
||
| 343 | * Update the progress after sync modules actions have been processed on the server. |
||
| 344 | * |
||
| 345 | * @access public |
||
| 346 | * |
||
| 347 | * @param array $actions Actions that have been processed on the server. |
||
| 348 | */ |
||
| 349 | public function update_sent_progress_action( $actions ) { |
||
| 394 | |||
| 395 | /** |
||
| 396 | * Get the name of the action for an item in the sync queue. |
||
| 397 | * |
||
| 398 | * @access public |
||
| 399 | * |
||
| 400 | * @param array $queue_item Item of the sync queue. |
||
| 401 | * @return string|boolean Name of the action, false if queue item is invalid. |
||
| 402 | */ |
||
| 403 | public function get_action_name( $queue_item ) { |
||
| 409 | |||
| 410 | /** |
||
| 411 | * Retrieve the total number of items we're syncing in a particular queue item (action). |
||
| 412 | * `$queue_item[1]` is expected to contain chunks of items, and `$queue_item[1][0]` |
||
| 413 | * represents the first (and only) chunk of items to sync in that action. |
||
| 414 | * |
||
| 415 | * @access public |
||
| 416 | * |
||
| 417 | * @param array $queue_item Item of the sync queue that corresponds to a particular action. |
||
| 418 | * @return int Total number of items in the action. |
||
| 419 | */ |
||
| 420 | public function get_action_totals( $queue_item ) { |
||
| 431 | |||
| 432 | /** |
||
| 433 | * Retrieve the total number of items for a set of actions, grouped by action name. |
||
| 434 | * |
||
| 435 | * @access public |
||
| 436 | * |
||
| 437 | * @param array $actions An array of actions. |
||
| 438 | * @return array An array, representing the total number of items, grouped per action. |
||
| 439 | */ |
||
| 440 | public function get_actions_totals( $actions ) { |
||
| 454 | |||
| 455 | /** |
||
| 456 | * Whether full sync has started. |
||
| 457 | * |
||
| 458 | * @access public |
||
| 459 | * |
||
| 460 | * @return boolean |
||
| 461 | */ |
||
| 462 | public function is_started() { |
||
| 465 | |||
| 466 | /** |
||
| 467 | * Whether full sync has finished. |
||
| 468 | * |
||
| 469 | * @access public |
||
| 470 | * |
||
| 471 | * @return boolean |
||
| 472 | */ |
||
| 473 | public function is_finished() { |
||
| 476 | |||
| 477 | /** |
||
| 478 | * Retrieve the status of the current full sync. |
||
| 479 | * |
||
| 480 | * @access public |
||
| 481 | * |
||
| 482 | * @return array Full sync status. |
||
| 483 | */ |
||
| 484 | public function get_status() { |
||
| 529 | |||
| 530 | /** |
||
| 531 | * Clear all the full sync status options. |
||
| 532 | * |
||
| 533 | * @access public |
||
| 534 | */ |
||
| 535 | public function clear_status() { |
||
| 550 | |||
| 551 | /** |
||
| 552 | * Clear all the full sync data. |
||
| 553 | * |
||
| 554 | * @access public |
||
| 555 | */ |
||
| 556 | public function reset_data() { |
||
| 563 | |||
| 564 | /** |
||
| 565 | * Get the value of a full sync status option. |
||
| 566 | * |
||
| 567 | * @access private |
||
| 568 | * |
||
| 569 | * @param string $name Name of the option. |
||
| 570 | * @param mixed $default Default value of the option. |
||
| 571 | * @return mixed Option value. |
||
| 572 | */ |
||
| 573 | private function get_status_option( $name, $default = null ) { |
||
| 578 | |||
| 579 | /** |
||
| 580 | * Update the value of a full sync status option. |
||
| 581 | * |
||
| 582 | * @access private |
||
| 583 | * |
||
| 584 | * @param string $name Name of the option. |
||
| 585 | * @param mixed $value Value of the option. |
||
| 586 | * @param boolean $autoload Whether the option should be autoloaded at the beginning of the request. |
||
| 587 | */ |
||
| 588 | private function update_status_option( $name, $value, $autoload = false ) { |
||
| 591 | |||
| 592 | /** |
||
| 593 | * Set the full sync enqueue status. |
||
| 594 | * |
||
| 595 | * @access private |
||
| 596 | * |
||
| 597 | * @param array $new_status The new full sync enqueue status. |
||
| 598 | */ |
||
| 599 | private function set_enqueue_status( $new_status ) { |
||
| 602 | |||
| 603 | /** |
||
| 604 | * Delete full sync enqueue status. |
||
| 605 | * |
||
| 606 | * @access private |
||
| 607 | * |
||
| 608 | * @return boolean Whether the status was deleted. |
||
| 609 | */ |
||
| 610 | private function delete_enqueue_status() { |
||
| 613 | |||
| 614 | /** |
||
| 615 | * Retrieve the current full sync enqueue status. |
||
| 616 | * |
||
| 617 | * @access private |
||
| 618 | * |
||
| 619 | * @return array Full sync enqueue status. |
||
| 620 | */ |
||
| 621 | public function get_enqueue_status() { |
||
| 624 | |||
| 625 | /** |
||
| 626 | * Set the full sync enqueue configuration. |
||
| 627 | * |
||
| 628 | * @access private |
||
| 629 | * |
||
| 630 | * @param array $config The new full sync enqueue configuration. |
||
| 631 | */ |
||
| 632 | private function set_config( $config ) { |
||
| 635 | |||
| 636 | /** |
||
| 637 | * Delete full sync configuration. |
||
| 638 | * |
||
| 639 | * @access private |
||
| 640 | * |
||
| 641 | * @return boolean Whether the configuration was deleted. |
||
| 642 | */ |
||
| 643 | private function delete_config() { |
||
| 646 | |||
| 647 | /** |
||
| 648 | * Retrieve the current full sync enqueue config. |
||
| 649 | * |
||
| 650 | * @access private |
||
| 651 | * |
||
| 652 | * @return array Full sync enqueue config. |
||
| 653 | */ |
||
| 654 | private function get_config() { |
||
| 657 | |||
| 658 | |||
| 659 | /** |
||
| 660 | * Prefix of the blog lock transient. |
||
| 661 | * |
||
| 662 | * @access public |
||
| 663 | * |
||
| 664 | * @var string |
||
| 665 | */ |
||
| 666 | const ENQUEUE_LOCK_TRANSIENT_PREFIX = 'jp_sync_enqueue_lock_'; |
||
| 667 | |||
| 668 | /** |
||
| 669 | * Lifetime of the blog lock transient. |
||
| 670 | * |
||
| 671 | * @access public |
||
| 672 | * |
||
| 673 | * @var int |
||
| 674 | */ |
||
| 675 | const ENQUEUE_LOCK_TRANSIENT_EXPIRY = 15; // Seconds. |
||
| 676 | |||
| 677 | /** |
||
| 678 | * Attempt to lock enqueueing when the server receives concurrent requests from the same blog. |
||
| 679 | * |
||
| 680 | * @access public |
||
| 681 | * |
||
| 682 | * @param int $expiry enqueue transient lifetime. |
||
| 683 | * @return boolean True if succeeded, false otherwise. |
||
| 684 | */ |
||
| 685 | View Code Duplication | public function attempt_enqueue_lock( $expiry = self::ENQUEUE_LOCK_TRANSIENT_EXPIRY ) { |
|
| 695 | |||
| 696 | /** |
||
| 697 | * Retrieve the enqueue lock transient name for the current blog. |
||
| 698 | * |
||
| 699 | * @access public |
||
| 700 | * |
||
| 701 | * @return string Name of the blog lock transient. |
||
| 702 | */ |
||
| 703 | private function get_concurrent_enqueue_transient_name() { |
||
| 706 | |||
| 707 | /** |
||
| 708 | * Remove the enqueue lock. |
||
| 709 | * |
||
| 710 | * @access public |
||
| 711 | */ |
||
| 712 | public function remove_enqueue_lock() { |
||
| 715 | |||
| 716 | } |
||
| 717 |
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.