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 ) { |
||
| 364 | |||
| 365 | /** |
||
| 366 | * Restart db upgrade |
||
| 367 | * |
||
| 368 | * @since 2.0.1 |
||
| 369 | * @access public |
||
| 370 | * |
||
| 371 | * @return bool |
||
| 372 | */ |
||
| 373 | public function __restart_db_update() { |
||
| 408 | |||
| 409 | /** |
||
| 410 | * Health check for updates. |
||
| 411 | * |
||
| 412 | * @since 2.0 |
||
| 413 | * @access public |
||
| 414 | * |
||
| 415 | * @param Give_Updates $give_updates |
||
| 416 | */ |
||
| 417 | public function __health_background_update( $give_updates ) { |
||
| 513 | |||
| 514 | |||
| 515 | /** |
||
| 516 | * Show update related notices |
||
| 517 | * |
||
| 518 | * @since 2.0 |
||
| 519 | * @access public |
||
| 520 | */ |
||
| 521 | public function __show_notice() { |
||
| 638 | |||
| 639 | /** |
||
| 640 | * Render Give Updates Completed page |
||
| 641 | * |
||
| 642 | * @since 1.8.12 |
||
| 643 | * @access public |
||
| 644 | */ |
||
| 645 | public function render_complete_page() { |
||
| 648 | |||
| 649 | /** |
||
| 650 | * Render Give Updates page |
||
| 651 | * |
||
| 652 | * @since 1.8.12 |
||
| 653 | * @access public |
||
| 654 | */ |
||
| 655 | public function render_page() { |
||
| 658 | |||
| 659 | /** |
||
| 660 | * Run database upgrades |
||
| 661 | * |
||
| 662 | * @since 2.0 |
||
| 663 | * @access private |
||
| 664 | */ |
||
| 665 | private function run_db_update() { |
||
| 690 | |||
| 691 | |||
| 692 | /** |
||
| 693 | * Delete resume updates |
||
| 694 | * |
||
| 695 | * @since 1.8.12 |
||
| 696 | * @access public |
||
| 697 | */ |
||
| 698 | public function __flush_resume_updates() { |
||
| 709 | |||
| 710 | |||
| 711 | /** |
||
| 712 | * Initialize updates |
||
| 713 | * |
||
| 714 | * @since 2.0 |
||
| 715 | * @access public |
||
| 716 | * |
||
| 717 | * @return void |
||
| 718 | */ |
||
| 719 | public function __give_start_updating() { |
||
| 738 | |||
| 739 | |||
| 740 | /** |
||
| 741 | * This function handle ajax query for dn update status. |
||
| 742 | * |
||
| 743 | * @since 2.0 |
||
| 744 | * @access public |
||
| 745 | * |
||
| 746 | * @return string |
||
| 747 | */ |
||
| 748 | public function __give_db_updates_info() { |
||
| 765 | |||
| 766 | /** |
||
| 767 | * Send ajax response |
||
| 768 | * |
||
| 769 | * @since 1.8.12 |
||
| 770 | * @access public |
||
| 771 | * |
||
| 772 | * @param $data |
||
| 773 | * @param string $type |
||
| 774 | */ |
||
| 775 | public function send_ajax_response( $data, $type = '' ) { |
||
| 806 | |||
| 807 | /** |
||
| 808 | * Set current update percentage. |
||
| 809 | * |
||
| 810 | * @since 1.8.12 |
||
| 811 | * @access public |
||
| 812 | * |
||
| 813 | * @param $total |
||
| 814 | * @param $current_total |
||
| 815 | */ |
||
| 816 | public function set_percentage( $total, $current_total ) { |
||
| 823 | |||
| 824 | /** |
||
| 825 | * Check if parent update completed or not. |
||
| 826 | * |
||
| 827 | * @since 2.0 |
||
| 828 | * @access private |
||
| 829 | * |
||
| 830 | * @param array $update |
||
| 831 | * |
||
| 832 | * @return bool|null |
||
| 833 | */ |
||
| 834 | public function is_parent_updates_completed( $update ) { |
||
| 857 | |||
| 858 | /** |
||
| 859 | * Flag to check if DB updates running or not. |
||
| 860 | * |
||
| 861 | * @since 2.0 |
||
| 862 | * @access public |
||
| 863 | * @return bool |
||
| 864 | */ |
||
| 865 | public function is_doing_updates() { |
||
| 868 | |||
| 869 | |||
| 870 | /** |
||
| 871 | * Check if update has valid dependency or not. |
||
| 872 | * |
||
| 873 | * @since 2.0 |
||
| 874 | * @access public |
||
| 875 | * |
||
| 876 | * @param $update |
||
| 877 | * |
||
| 878 | * @return bool |
||
| 879 | */ |
||
| 880 | public function has_valid_dependency( $update ) { |
||
| 894 | |||
| 895 | /** |
||
| 896 | * Get updates. |
||
| 897 | * |
||
| 898 | * @since 1.8.12 |
||
| 899 | * @access public |
||
| 900 | * |
||
| 901 | * @param string $update_type Tye of update. |
||
| 902 | * @param string $status Tye of update. |
||
| 903 | * |
||
| 904 | * @return array |
||
| 905 | */ |
||
| 906 | public function get_updates( $update_type = '', $status = 'all' ) { |
||
| 939 | |||
| 940 | /** |
||
| 941 | * Get addon update count. |
||
| 942 | * |
||
| 943 | * @since 1.8.12 |
||
| 944 | * @access public |
||
| 945 | * @return int |
||
| 946 | */ |
||
| 947 | public function get_total_plugin_update_count() { |
||
| 950 | |||
| 951 | /** |
||
| 952 | * Get total update count |
||
| 953 | * |
||
| 954 | * @since 1.8.12 |
||
| 955 | * @access public |
||
| 956 | * |
||
| 957 | * @return int |
||
| 958 | */ |
||
| 959 | public function get_total_update_count() { |
||
| 965 | |||
| 966 | /** |
||
| 967 | * Get total pending updates count |
||
| 968 | * |
||
| 969 | * @since 1.8.12 |
||
| 970 | * @access public |
||
| 971 | * |
||
| 972 | * @return int |
||
| 973 | */ |
||
| 974 | public function get_pending_db_update_count() { |
||
| 977 | |||
| 978 | /** |
||
| 979 | * Get total updates count |
||
| 980 | * |
||
| 981 | * @since 1.8.18 |
||
| 982 | * @access public |
||
| 983 | * |
||
| 984 | * @return int |
||
| 985 | */ |
||
| 986 | public function get_total_db_update_count() { |
||
| 989 | |||
| 990 | /** |
||
| 991 | * Get total new updates count |
||
| 992 | * |
||
| 993 | * @since 2.0 |
||
| 994 | * @access public |
||
| 995 | * |
||
| 996 | * @return int |
||
| 997 | */ |
||
| 998 | public function get_total_new_db_update_count() { |
||
| 1003 | |||
| 1004 | /** |
||
| 1005 | * Get total new updates count |
||
| 1006 | * |
||
| 1007 | * @since 2.0 |
||
| 1008 | * @access public |
||
| 1009 | * |
||
| 1010 | * @return int |
||
| 1011 | */ |
||
| 1012 | public function get_running_db_update() { |
||
| 1019 | |||
| 1020 | /** |
||
| 1021 | * Get database update processing percentage. |
||
| 1022 | * |
||
| 1023 | * @since 2.0 |
||
| 1024 | * @access public |
||
| 1025 | * @return float|int |
||
| 1026 | */ |
||
| 1027 | public function get_db_update_processing_percentage() { |
||
| 1047 | |||
| 1048 | |||
| 1049 | /** |
||
| 1050 | * Get all update ids. |
||
| 1051 | * |
||
| 1052 | * @since 2.0.3 |
||
| 1053 | * |
||
| 1054 | * @return array |
||
| 1055 | */ |
||
| 1056 | public function get_update_ids() { |
||
| 1062 | } |
||
| 1063 | |||
| 1065 |