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 | * @param bool $force |
||
| 320 | * |
||
| 321 | * @return bool |
||
| 322 | */ |
||
| 323 | public function __pause_db_update( $force = false ) { |
||
| 360 | |||
| 361 | /** |
||
| 362 | * Restart db upgrade |
||
| 363 | * |
||
| 364 | * @since 2.0.1 |
||
| 365 | * @access public |
||
| 366 | * |
||
| 367 | * @return bool |
||
| 368 | */ |
||
| 369 | public function __restart_db_update() { |
||
| 401 | |||
| 402 | /** |
||
| 403 | * Health check for updates. |
||
| 404 | * |
||
| 405 | * @since 2.0 |
||
| 406 | * @access public |
||
| 407 | * |
||
| 408 | * @param Give_Updates $give_updates |
||
| 409 | */ |
||
| 410 | public function __health_background_update( $give_updates ) { |
||
| 503 | |||
| 504 | |||
| 505 | /** |
||
| 506 | * Show update related notices |
||
| 507 | * |
||
| 508 | * @since 2.0 |
||
| 509 | * @access public |
||
| 510 | */ |
||
| 511 | public function __show_notice() { |
||
| 628 | |||
| 629 | /** |
||
| 630 | * Render Give Updates Completed page |
||
| 631 | * |
||
| 632 | * @since 1.8.12 |
||
| 633 | * @access public |
||
| 634 | */ |
||
| 635 | public function render_complete_page() { |
||
| 638 | |||
| 639 | /** |
||
| 640 | * Render Give Updates page |
||
| 641 | * |
||
| 642 | * @since 1.8.12 |
||
| 643 | * @access public |
||
| 644 | */ |
||
| 645 | public function render_page() { |
||
| 648 | |||
| 649 | /** |
||
| 650 | * Run database upgrades |
||
| 651 | * |
||
| 652 | * @since 2.0 |
||
| 653 | * @access private |
||
| 654 | */ |
||
| 655 | private function run_db_update() { |
||
| 680 | |||
| 681 | |||
| 682 | /** |
||
| 683 | * Delete resume updates |
||
| 684 | * |
||
| 685 | * @since 1.8.12 |
||
| 686 | * @access public |
||
| 687 | */ |
||
| 688 | public function __flush_resume_updates() { |
||
| 699 | |||
| 700 | |||
| 701 | /** |
||
| 702 | * Initialize updates |
||
| 703 | * |
||
| 704 | * @since 2.0 |
||
| 705 | * @access public |
||
| 706 | * |
||
| 707 | * @return void |
||
| 708 | */ |
||
| 709 | public function __give_start_updating() { |
||
| 728 | |||
| 729 | |||
| 730 | /** |
||
| 731 | * This function handle ajax query for dn update status. |
||
| 732 | * |
||
| 733 | * @since 2.0 |
||
| 734 | * @access public |
||
| 735 | * |
||
| 736 | * @return string |
||
| 737 | */ |
||
| 738 | public function __give_db_updates_info() { |
||
| 755 | |||
| 756 | /** |
||
| 757 | * Send ajax response |
||
| 758 | * |
||
| 759 | * @since 1.8.12 |
||
| 760 | * @access public |
||
| 761 | * |
||
| 762 | * @param $data |
||
| 763 | * @param string $type |
||
| 764 | */ |
||
| 765 | public function send_ajax_response( $data, $type = '' ) { |
||
| 796 | |||
| 797 | /** |
||
| 798 | * Set current update percentage. |
||
| 799 | * |
||
| 800 | * @since 1.8.12 |
||
| 801 | * @access public |
||
| 802 | * |
||
| 803 | * @param $total |
||
| 804 | * @param $current_total |
||
| 805 | */ |
||
| 806 | public function set_percentage( $total, $current_total ) { |
||
| 813 | |||
| 814 | /** |
||
| 815 | * Check if parent update completed or not. |
||
| 816 | * |
||
| 817 | * @since 2.0 |
||
| 818 | * @access private |
||
| 819 | * |
||
| 820 | * @param array $update |
||
| 821 | * |
||
| 822 | * @return bool|null |
||
| 823 | */ |
||
| 824 | public function is_parent_updates_completed( $update ) { |
||
| 847 | |||
| 848 | /** |
||
| 849 | * Flag to check if DB updates running or not. |
||
| 850 | * |
||
| 851 | * @since 2.0 |
||
| 852 | * @access public |
||
| 853 | * @return bool |
||
| 854 | */ |
||
| 855 | public function is_doing_updates() { |
||
| 858 | |||
| 859 | |||
| 860 | /** |
||
| 861 | * Check if update has valid dependency or not. |
||
| 862 | * |
||
| 863 | * @since 2.0 |
||
| 864 | * @access public |
||
| 865 | * |
||
| 866 | * @param $update |
||
| 867 | * |
||
| 868 | * @return bool |
||
| 869 | */ |
||
| 870 | public function has_valid_dependency( $update ) { |
||
| 884 | |||
| 885 | /** |
||
| 886 | * Get updates. |
||
| 887 | * |
||
| 888 | * @since 1.8.12 |
||
| 889 | * @access public |
||
| 890 | * |
||
| 891 | * @param string $update_type Tye of update. |
||
| 892 | * @param string $status Tye of update. |
||
| 893 | * |
||
| 894 | * @return array |
||
| 895 | */ |
||
| 896 | public function get_updates( $update_type = '', $status = 'all' ) { |
||
| 929 | |||
| 930 | /** |
||
| 931 | * Get addon update count. |
||
| 932 | * |
||
| 933 | * @since 1.8.12 |
||
| 934 | * @access public |
||
| 935 | * @return int |
||
| 936 | */ |
||
| 937 | public function get_total_plugin_update_count() { |
||
| 940 | |||
| 941 | /** |
||
| 942 | * Get total update count |
||
| 943 | * |
||
| 944 | * @since 1.8.12 |
||
| 945 | * @access public |
||
| 946 | * |
||
| 947 | * @return int |
||
| 948 | */ |
||
| 949 | public function get_total_update_count() { |
||
| 955 | |||
| 956 | /** |
||
| 957 | * Get total pending updates count |
||
| 958 | * |
||
| 959 | * @since 1.8.12 |
||
| 960 | * @access public |
||
| 961 | * |
||
| 962 | * @return int |
||
| 963 | */ |
||
| 964 | public function get_pending_db_update_count() { |
||
| 967 | |||
| 968 | /** |
||
| 969 | * Get total updates count |
||
| 970 | * |
||
| 971 | * @since 1.8.18 |
||
| 972 | * @access public |
||
| 973 | * |
||
| 974 | * @return int |
||
| 975 | */ |
||
| 976 | public function get_total_db_update_count() { |
||
| 979 | |||
| 980 | /** |
||
| 981 | * Get total new updates count |
||
| 982 | * |
||
| 983 | * @since 2.0 |
||
| 984 | * @access public |
||
| 985 | * |
||
| 986 | * @return int |
||
| 987 | */ |
||
| 988 | public function get_total_new_db_update_count() { |
||
| 993 | |||
| 994 | /** |
||
| 995 | * Get total new updates count |
||
| 996 | * |
||
| 997 | * @since 2.0 |
||
| 998 | * @access public |
||
| 999 | * |
||
| 1000 | * @return int |
||
| 1001 | */ |
||
| 1002 | public function get_running_db_update() { |
||
| 1009 | |||
| 1010 | /** |
||
| 1011 | * Get database update processing percentage. |
||
| 1012 | * |
||
| 1013 | * @since 2.0 |
||
| 1014 | * @access public |
||
| 1015 | * @return float|int |
||
| 1016 | */ |
||
| 1017 | public function get_db_update_processing_percentage() { |
||
| 1037 | |||
| 1038 | |||
| 1039 | /** |
||
| 1040 | * Get all update ids. |
||
| 1041 | * |
||
| 1042 | * @since 2.0.3 |
||
| 1043 | * |
||
| 1044 | * @return array |
||
| 1045 | */ |
||
| 1046 | public function get_update_ids() { |
||
| 1052 | } |
||
| 1053 | |||
| 1055 |