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 Give_Updates 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 Give_Updates, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 8 | class Give_Updates { |
||
|
|
|||
| 9 | |||
| 10 | /** |
||
| 11 | * Instance. |
||
| 12 | * |
||
| 13 | * @since |
||
| 14 | * @access static |
||
| 15 | * @var |
||
| 16 | */ |
||
| 17 | static private $instance; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * Instance. |
||
| 21 | * |
||
| 22 | * @since |
||
| 23 | * @access public |
||
| 24 | * @var Give_Background_Updater |
||
| 25 | */ |
||
| 26 | static public $background_updater; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Updates |
||
| 30 | * |
||
| 31 | * @since 1.8.12 |
||
| 32 | * @access private |
||
| 33 | * @var array |
||
| 34 | */ |
||
| 35 | private $updates = array(); |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Current update percentage number |
||
| 39 | * |
||
| 40 | * @since 1.8.12 |
||
| 41 | * @access private |
||
| 42 | * @var array |
||
| 43 | */ |
||
| 44 | public $percentage = 0; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Current update step number |
||
| 48 | * |
||
| 49 | * @since 1.8.12 |
||
| 50 | * @access private |
||
| 51 | * @var array |
||
| 52 | */ |
||
| 53 | public $step = 1; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Current update number |
||
| 57 | * |
||
| 58 | * @since 1.8.12 |
||
| 59 | * @access private |
||
| 60 | * @var array |
||
| 61 | */ |
||
| 62 | public $update = 1; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Singleton pattern. |
||
| 66 | * |
||
| 67 | * @since 1.8.12 |
||
| 68 | * @access private |
||
| 69 | * |
||
| 70 | * @param Give_Updates . |
||
| 71 | */ |
||
| 72 | private function __construct() { |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Register updates |
||
| 77 | * |
||
| 78 | * @since 1.8.12 |
||
| 79 | * @access public |
||
| 80 | * |
||
| 81 | * @param array $args |
||
| 82 | */ |
||
| 83 | public function register( $args ) { |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Get instance. |
||
| 115 | * |
||
| 116 | * @since |
||
| 117 | * @access static |
||
| 118 | * @return static |
||
| 119 | */ |
||
| 120 | static function get_instance() { |
||
| 127 | |||
| 128 | /** |
||
| 129 | * |
||
| 130 | * Setup hook |
||
| 131 | * |
||
| 132 | * @since 1.8.12 |
||
| 133 | * @access public |
||
| 134 | */ |
||
| 135 | public function setup() { |
||
| 136 | /** |
||
| 137 | * Load file |
||
| 138 | */ |
||
| 139 | require_once GIVE_PLUGIN_DIR . 'includes/class-give-background-updater.php'; |
||
| 140 | require_once GIVE_PLUGIN_DIR . 'includes/admin/upgrades/upgrade-functions.php'; |
||
| 141 | |||
| 142 | self::$background_updater = new Give_Background_Updater(); |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Setup hooks. |
||
| 146 | */ |
||
| 147 | add_action( 'init', array( $this, '__register_upgrade' ), 9999 ); |
||
| 148 | add_action( 'give_set_upgrade_completed', array( $this, '__flush_resume_updates' ), 9999 ); |
||
| 149 | add_action( 'wp_ajax_give_db_updates_info', array( $this, '__give_db_updates_info' ) ); |
||
| 150 | add_action( 'wp_ajax_give_run_db_updates', array( $this, '__give_start_updating' ) ); |
||
| 151 | add_action( 'admin_init', array( $this, '__redirect_admin' ) ); |
||
| 152 | add_action( 'admin_init', array( $this, '__pause_db_update' ), -1 ); |
||
| 153 | add_action( 'admin_init', array( $this, '__restart_db_update' ), -1 ); |
||
| 154 | add_action( 'admin_notices', array( $this, '__show_notice' ) ); |
||
| 155 | add_action( 'give_restart_db_upgrade', array( $this, '__health_background_update' ) ); |
||
| 156 | |||
| 157 | if ( is_admin() ) { |
||
| 158 | add_action( 'admin_init', array( $this, '__change_donations_label' ), 9999 ); |
||
| 159 | add_action( 'admin_menu', array( $this, '__register_menu' ), 9999 ); |
||
| 160 | } |
||
| 161 | } |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Register plugin add-on updates. |
||
| 165 | * |
||
| 166 | * @since 1.8.12 |
||
| 167 | * @access public |
||
| 168 | */ |
||
| 169 | public function __register_plugin_addon_updates() { |
||
| 181 | |||
| 182 | |||
| 183 | /** |
||
| 184 | * Fire custom action hook to register updates |
||
| 185 | * |
||
| 186 | * @since 1.8.12 |
||
| 187 | * @access public |
||
| 188 | */ |
||
| 189 | public function __register_upgrade() { |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Rename `Donations` menu title if updates exists |
||
| 204 | * |
||
| 205 | * @since 1.8.12 |
||
| 206 | * @access public |
||
| 207 | */ |
||
| 208 | function __change_donations_label() { |
||
| 235 | |||
| 236 | /** |
||
| 237 | * Register updates menu |
||
| 238 | * |
||
| 239 | * @since 1.8.12 |
||
| 240 | * @access public |
||
| 241 | */ |
||
| 242 | public function __register_menu() { |
||
| 289 | |||
| 290 | |||
| 291 | /** |
||
| 292 | * Show update related notices |
||
| 293 | * |
||
| 294 | * @since 2.0 |
||
| 295 | * @access public |
||
| 296 | */ |
||
| 297 | public function __redirect_admin() { |
||
| 311 | |||
| 312 | |||
| 313 | /** |
||
| 314 | * Pause db upgrade |
||
| 315 | * |
||
| 316 | * @since 2.0.1 |
||
| 317 | * @access public |
||
| 318 | * |
||
| 319 | * @return bool |
||
| 320 | */ |
||
| 321 | View Code Duplication | public function __pause_db_update() { |
|
| 352 | |||
| 353 | /** |
||
| 354 | * Restart db upgrade |
||
| 355 | * |
||
| 356 | * @since 2.0.1 |
||
| 357 | * @access public |
||
| 358 | * |
||
| 359 | * @return bool |
||
| 360 | */ |
||
| 361 | View Code Duplication | public function __restart_db_update() { |
|
| 393 | |||
| 394 | /** |
||
| 395 | * Health check for updates. |
||
| 396 | * |
||
| 397 | * @since 2.0 |
||
| 398 | * @access public |
||
| 399 | * |
||
| 400 | * @param Give_Updates $give_updates |
||
| 401 | */ |
||
| 402 | public function __health_background_update( $give_updates ) { |
||
| 464 | |||
| 465 | |||
| 466 | /** |
||
| 467 | * Show update related notices |
||
| 468 | * |
||
| 469 | * @since 2.0 |
||
| 470 | * @access public |
||
| 471 | */ |
||
| 472 | public function __show_notice() { |
||
| 581 | |||
| 582 | /** |
||
| 583 | * Render Give Updates Completed page |
||
| 584 | * |
||
| 585 | * @since 1.8.12 |
||
| 586 | * @access public |
||
| 587 | */ |
||
| 588 | public function render_complete_page() { |
||
| 591 | |||
| 592 | /** |
||
| 593 | * Render Give Updates page |
||
| 594 | * |
||
| 595 | * @since 1.8.12 |
||
| 596 | * @access public |
||
| 597 | */ |
||
| 598 | public function render_page() { |
||
| 601 | |||
| 602 | /** |
||
| 603 | * Run database upgrades |
||
| 604 | * |
||
| 605 | * @since 2.0 |
||
| 606 | * @access private |
||
| 607 | */ |
||
| 608 | private function run_db_update() { |
||
| 633 | |||
| 634 | |||
| 635 | /** |
||
| 636 | * Delete resume updates |
||
| 637 | * |
||
| 638 | * @since 1.8.12 |
||
| 639 | * @access public |
||
| 640 | */ |
||
| 641 | public function __flush_resume_updates() { |
||
| 649 | |||
| 650 | |||
| 651 | /** |
||
| 652 | * Initialize updates |
||
| 653 | * |
||
| 654 | * @since 2.0 |
||
| 655 | * @access public |
||
| 656 | * |
||
| 657 | * @return void |
||
| 658 | */ |
||
| 659 | public function __give_start_updating() { |
||
| 678 | |||
| 679 | |||
| 680 | /** |
||
| 681 | * This function handle ajax query for dn update status. |
||
| 682 | * |
||
| 683 | * @since 2.0 |
||
| 684 | * @access public |
||
| 685 | * |
||
| 686 | * @return string |
||
| 687 | */ |
||
| 688 | public function __give_db_updates_info() { |
||
| 705 | |||
| 706 | /** |
||
| 707 | * Send ajax response |
||
| 708 | * |
||
| 709 | * @since 1.8.12 |
||
| 710 | * @access public |
||
| 711 | * |
||
| 712 | * @param $data |
||
| 713 | * @param string $type |
||
| 714 | */ |
||
| 715 | public function send_ajax_response( $data, $type = '' ) { |
||
| 746 | |||
| 747 | /** |
||
| 748 | * Set current update percentage. |
||
| 749 | * |
||
| 750 | * @since 1.8.12 |
||
| 751 | * @access public |
||
| 752 | * |
||
| 753 | * @param $total |
||
| 754 | * @param $current_total |
||
| 755 | */ |
||
| 756 | public function set_percentage( $total, $current_total ) { |
||
| 763 | |||
| 764 | /** |
||
| 765 | * Check if parent update completed or not. |
||
| 766 | * |
||
| 767 | * @since 2.0 |
||
| 768 | * @access private |
||
| 769 | * |
||
| 770 | * @param array $update |
||
| 771 | * |
||
| 772 | * @return bool|null |
||
| 773 | */ |
||
| 774 | public function is_parent_updates_completed( $update ) { |
||
| 798 | |||
| 799 | /** |
||
| 800 | * Flag to check if DB updates running or not. |
||
| 801 | * |
||
| 802 | * @since 2.0 |
||
| 803 | * @access public |
||
| 804 | * @return bool |
||
| 805 | */ |
||
| 806 | public function is_doing_updates() { |
||
| 809 | |||
| 810 | |||
| 811 | /** |
||
| 812 | * Check if update has valid dependency or not. |
||
| 813 | * |
||
| 814 | * @since 2.0 |
||
| 815 | * @access public |
||
| 816 | * |
||
| 817 | * @param $update |
||
| 818 | * |
||
| 819 | * @return bool |
||
| 820 | */ |
||
| 821 | public function has_valid_dependency( $update ) { |
||
| 835 | |||
| 836 | /** |
||
| 837 | * Get updates. |
||
| 838 | * |
||
| 839 | * @since 1.8.12 |
||
| 840 | * @access public |
||
| 841 | * |
||
| 842 | * @param string $update_type Tye of update. |
||
| 843 | * @param string $status Tye of update. |
||
| 844 | * |
||
| 845 | * @return array |
||
| 846 | */ |
||
| 847 | public function get_updates( $update_type = '', $status = 'all' ) { |
||
| 880 | |||
| 881 | /** |
||
| 882 | * Get addon update count. |
||
| 883 | * |
||
| 884 | * @since 1.8.12 |
||
| 885 | * @access public |
||
| 886 | * @return int |
||
| 887 | */ |
||
| 888 | public function get_total_plugin_update_count() { |
||
| 891 | |||
| 892 | /** |
||
| 893 | * Get total update count |
||
| 894 | * |
||
| 895 | * @since 1.8.12 |
||
| 896 | * @access public |
||
| 897 | * |
||
| 898 | * @return int |
||
| 899 | */ |
||
| 900 | public function get_total_update_count() { |
||
| 906 | |||
| 907 | /** |
||
| 908 | * Get total pending updates count |
||
| 909 | * |
||
| 910 | * @since 1.8.12 |
||
| 911 | * @access public |
||
| 912 | * |
||
| 913 | * @return int |
||
| 914 | */ |
||
| 915 | public function get_pending_db_update_count() { |
||
| 918 | |||
| 919 | /** |
||
| 920 | * Get total updates count |
||
| 921 | * |
||
| 922 | * @since 1.8.18 |
||
| 923 | * @access public |
||
| 924 | * |
||
| 925 | * @return int |
||
| 926 | */ |
||
| 927 | public function get_total_db_update_count() { |
||
| 930 | |||
| 931 | /** |
||
| 932 | * Get total new updates count |
||
| 933 | * |
||
| 934 | * @since 2.0 |
||
| 935 | * @access public |
||
| 936 | * |
||
| 937 | * @return int |
||
| 938 | */ |
||
| 939 | public function get_total_new_db_update_count() { |
||
| 944 | |||
| 945 | /** |
||
| 946 | * Get total new updates count |
||
| 947 | * |
||
| 948 | * @since 2.0 |
||
| 949 | * @access public |
||
| 950 | * |
||
| 951 | * @return int |
||
| 952 | */ |
||
| 953 | public function get_running_db_update() { |
||
| 960 | |||
| 961 | /** |
||
| 962 | * Get database update processing percentage. |
||
| 963 | * |
||
| 964 | * @since 2.0 |
||
| 965 | * @access public |
||
| 966 | * @return float|int |
||
| 967 | */ |
||
| 968 | public function get_db_update_processing_percentage() { |
||
| 988 | } |
||
| 989 | |||
| 991 |