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 | var $is_full_sync_capable = false; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Prefix of the full sync status option name. |
||
| 32 | * |
||
| 33 | * @var string |
||
| 34 | */ |
||
| 35 | const STATUS_OPTION_PREFIX = 'jetpack_sync_full_'; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Sync module name. |
||
| 39 | * |
||
| 40 | * @access public |
||
| 41 | * |
||
| 42 | * @return string |
||
| 43 | */ |
||
| 44 | public function name() { |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Initialize action listeners for full sync. |
||
| 50 | * |
||
| 51 | * @access public |
||
| 52 | * |
||
| 53 | * @param callable $callable Action handler callable. |
||
| 54 | */ |
||
| 55 | public function init_full_sync_listeners( $callable ) { |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Initialize the module in the sender. |
||
| 64 | * |
||
| 65 | * @access public |
||
| 66 | */ |
||
| 67 | public function init_before_send() { |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Start a full sync. |
||
| 74 | * |
||
| 75 | * @access public |
||
| 76 | * |
||
| 77 | * @param array $module_configs Full sync configuration for all sync modules. |
||
|
|
|||
| 78 | * @return bool Always returns true at success. |
||
| 79 | */ |
||
| 80 | public function start( $module_configs = null ) { |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Enqueue the next items to sync. |
||
| 177 | * |
||
| 178 | * @access public |
||
| 179 | * |
||
| 180 | * @param array $configs Full sync configuration for all sync modules. |
||
| 181 | * @param array $enqueue_status Current status of the queue, indexed by sync modules. |
||
| 182 | */ |
||
| 183 | public function continue_enqueuing( $configs = null, $enqueue_status = null ) { |
||
| 268 | |||
| 269 | /** |
||
| 270 | * Get the range (min ID, max ID and total items) of items to sync. |
||
| 271 | * |
||
| 272 | * @access public |
||
| 273 | * |
||
| 274 | * @param string $type Type of sync item to get the range for. |
||
| 275 | * @return array Array of min ID, max ID and total items in the range. |
||
| 276 | */ |
||
| 277 | public function get_range( $type ) { |
||
| 306 | |||
| 307 | /** |
||
| 308 | * Get the range for content (posts and comments) to sync. |
||
| 309 | * |
||
| 310 | * @access private |
||
| 311 | * |
||
| 312 | * @param array $config Full sync configuration for this all sync modules. |
||
| 313 | * @return array Array of range (min ID, max ID, total items) for all content types. |
||
| 314 | */ |
||
| 315 | private function get_content_range( $config ) { |
||
| 327 | |||
| 328 | /** |
||
| 329 | * Update the progress after sync modules actions have been processed on the server. |
||
| 330 | * |
||
| 331 | * @access public |
||
| 332 | * |
||
| 333 | * @param array $actions Actions that have been processed on the server. |
||
| 334 | */ |
||
| 335 | public function update_sent_progress_action( $actions ) { |
||
| 380 | |||
| 381 | /** |
||
| 382 | * Get the name of the action for an item in the sync queue. |
||
| 383 | * |
||
| 384 | * @access public |
||
| 385 | * |
||
| 386 | * @param array $queue_item Item of the sync queue. |
||
| 387 | * @return string|boolean Name of the action, false if queue item is invalid. |
||
| 388 | */ |
||
| 389 | public function get_action_name( $queue_item ) { |
||
| 395 | |||
| 396 | /** |
||
| 397 | * Retrieve the total number of items we're syncing in a particular queue item (action). |
||
| 398 | * `$queue_item[1]` is expected to contain chunks of items, and `$queue_item[1][0]` |
||
| 399 | * represents the first (and only) chunk of items to sync in that action. |
||
| 400 | * |
||
| 401 | * @access public |
||
| 402 | * |
||
| 403 | * @param array $queue_item Item of the sync queue that corresponds to a particular action. |
||
| 404 | * @return int Total number of items in the action. |
||
| 405 | */ |
||
| 406 | public function get_action_totals( $queue_item ) { |
||
| 417 | |||
| 418 | /** |
||
| 419 | * Retrieve the total number of items for a set of actions, grouped by action name. |
||
| 420 | * |
||
| 421 | * @access public |
||
| 422 | * |
||
| 423 | * @param array $actions An array of actions. |
||
| 424 | * @return array An array, representing the total number of items, grouped per action. |
||
| 425 | */ |
||
| 426 | public function get_actions_totals( $actions ) { |
||
| 440 | |||
| 441 | /** |
||
| 442 | * Whether full sync has started. |
||
| 443 | * |
||
| 444 | * @access public |
||
| 445 | * |
||
| 446 | * @return boolean |
||
| 447 | */ |
||
| 448 | public function is_started() { |
||
| 451 | |||
| 452 | /** |
||
| 453 | * Whether full sync has finished. |
||
| 454 | * |
||
| 455 | * @access public |
||
| 456 | * |
||
| 457 | * @return boolean |
||
| 458 | */ |
||
| 459 | public function is_finished() { |
||
| 462 | |||
| 463 | /** |
||
| 464 | * Retrieve the status of the current full sync. |
||
| 465 | * |
||
| 466 | * @access public |
||
| 467 | * |
||
| 468 | * @return array Full sync status. |
||
| 469 | */ |
||
| 470 | public function get_status() { |
||
| 515 | |||
| 516 | /** |
||
| 517 | * Clear all the full sync status options. |
||
| 518 | * |
||
| 519 | * @access public |
||
| 520 | */ |
||
| 521 | public function clear_status() { |
||
| 537 | |||
| 538 | /** |
||
| 539 | * Clear all the full sync data. |
||
| 540 | * |
||
| 541 | * @access public |
||
| 542 | */ |
||
| 543 | public function reset_data() { |
||
| 550 | |||
| 551 | /** |
||
| 552 | * Get the value of a full sync status option. |
||
| 553 | * |
||
| 554 | * @access private |
||
| 555 | * |
||
| 556 | * @param string $name Name of the option. |
||
| 557 | * @param mixed $default Default value of the option. |
||
| 558 | * @return mixed Option value. |
||
| 559 | */ |
||
| 560 | private function get_status_option( $name, $default = null ) { |
||
| 565 | |||
| 566 | /** |
||
| 567 | * Update the value of a full sync status option. |
||
| 568 | * |
||
| 569 | * @access private |
||
| 570 | * |
||
| 571 | * @param string $name Name of the option. |
||
| 572 | * @param mixed $value Value of the option. |
||
| 573 | * @param boolean $autoload Whether the option should be autoloaded at the beginning of the request. |
||
| 574 | */ |
||
| 575 | private function update_status_option( $name, $value, $autoload = false ) { |
||
| 578 | |||
| 579 | /** |
||
| 580 | * Set the full sync enqueue status. |
||
| 581 | * |
||
| 582 | * @access private |
||
| 583 | * |
||
| 584 | * @param array $new_status The new full sync enqueue status. |
||
| 585 | */ |
||
| 586 | private function set_enqueue_status( $new_status ) { |
||
| 589 | |||
| 590 | /** |
||
| 591 | * Delete full sync enqueue status. |
||
| 592 | * |
||
| 593 | * @access private |
||
| 594 | * |
||
| 595 | * @return boolean Whether the status was deleted. |
||
| 596 | */ |
||
| 597 | private function delete_enqueue_status() { |
||
| 600 | |||
| 601 | /** |
||
| 602 | * Retrieve the current full sync enqueue status. |
||
| 603 | * |
||
| 604 | * @access private |
||
| 605 | * |
||
| 606 | * @return array Full sync enqueue status. |
||
| 607 | */ |
||
| 608 | private function get_enqueue_status() { |
||
| 611 | |||
| 612 | /** |
||
| 613 | * Set the full sync enqueue configuration. |
||
| 614 | * |
||
| 615 | * @access private |
||
| 616 | * |
||
| 617 | * @param array $config The new full sync enqueue configuration. |
||
| 618 | */ |
||
| 619 | private function set_config( $config ) { |
||
| 622 | |||
| 623 | /** |
||
| 624 | * Delete full sync configuration. |
||
| 625 | * |
||
| 626 | * @access private |
||
| 627 | * |
||
| 628 | * @return boolean Whether the configuration was deleted. |
||
| 629 | */ |
||
| 630 | private function delete_config() { |
||
| 633 | |||
| 634 | /** |
||
| 635 | * Retrieve the current full sync enqueue config. |
||
| 636 | * |
||
| 637 | * @access private |
||
| 638 | * |
||
| 639 | * @return array Full sync enqueue config. |
||
| 640 | */ |
||
| 641 | private function get_config() { |
||
| 644 | |||
| 645 | /** |
||
| 646 | * Update an option manually to bypass filters and caching. |
||
| 647 | * |
||
| 648 | * @access private |
||
| 649 | * |
||
| 650 | * @param string $name Option name. |
||
| 651 | * @param mixed $value Option value. |
||
| 652 | * @return int The number of updated rows in the database. |
||
| 653 | */ |
||
| 654 | private function write_option( $name, $value ) { |
||
| 683 | |||
| 684 | /** |
||
| 685 | * Update an option manually to bypass filters and caching. |
||
| 686 | * |
||
| 687 | * @access private |
||
| 688 | * |
||
| 689 | * @param string $name Option name. |
||
| 690 | * @param mixed $default Default option value. |
||
| 691 | * @return mixed Option value. |
||
| 692 | */ |
||
| 693 | private function read_option( $name, $default = null ) { |
||
| 709 | } |
||
| 710 |
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.