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 static |
||
| 24 | * @var Give_Background_Updater |
||
| 25 | */ |
||
| 26 | static private $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() { |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Register plugin add-on updates. |
||
| 162 | * |
||
| 163 | * @since 1.8.12 |
||
| 164 | * @access public |
||
| 165 | */ |
||
| 166 | public function __register_plugin_addon_updates() { |
||
| 178 | |||
| 179 | |||
| 180 | /** |
||
| 181 | * Fire custom action hook to register updates |
||
| 182 | * |
||
| 183 | * @since 1.8.12 |
||
| 184 | * @access public |
||
| 185 | */ |
||
| 186 | public function __register_upgrade() { |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Rename `Donations` menu title if updates exists |
||
| 201 | * |
||
| 202 | * @since 1.8.12 |
||
| 203 | * @access public |
||
| 204 | */ |
||
| 205 | function __change_donations_label() { |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Register updates menu |
||
| 233 | * |
||
| 234 | * @since 1.8.12 |
||
| 235 | * @access public |
||
| 236 | */ |
||
| 237 | public function __register_menu() { |
||
| 281 | |||
| 282 | |||
| 283 | /** |
||
| 284 | * Show update related notices |
||
| 285 | * |
||
| 286 | * @since 2.0 |
||
| 287 | * @access public |
||
| 288 | */ |
||
| 289 | public function __redirect_admin() { |
||
| 303 | |||
| 304 | |||
| 305 | /** |
||
| 306 | * Show update related notices |
||
| 307 | * |
||
| 308 | * @since 2.0 |
||
| 309 | * @access public |
||
| 310 | */ |
||
| 311 | public function __show_notice() { |
||
| 385 | |||
| 386 | /** |
||
| 387 | * Render Give Updates Completed page |
||
| 388 | * |
||
| 389 | * @since 1.8.12 |
||
| 390 | * @access public |
||
| 391 | */ |
||
| 392 | public function render_complete_page() { |
||
| 395 | |||
| 396 | /** |
||
| 397 | * Render Give Updates page |
||
| 398 | * |
||
| 399 | * @since 1.8.12 |
||
| 400 | * @access public |
||
| 401 | */ |
||
| 402 | public function render_page() { |
||
| 405 | |||
| 406 | /** |
||
| 407 | * Run database upgrades |
||
| 408 | * |
||
| 409 | * @since 2.0 |
||
| 410 | * @access private |
||
| 411 | */ |
||
| 412 | private function run_db_update() { |
||
| 437 | |||
| 438 | |||
| 439 | /** |
||
| 440 | * Delete resume updates |
||
| 441 | * |
||
| 442 | * @since 1.8.12 |
||
| 443 | * @access public |
||
| 444 | */ |
||
| 445 | public function __flush_resume_updates() { |
||
| 453 | |||
| 454 | |||
| 455 | /** |
||
| 456 | * Initialize updates |
||
| 457 | * |
||
| 458 | * @since 2.0 |
||
| 459 | * @access public |
||
| 460 | * |
||
| 461 | * @return void |
||
| 462 | */ |
||
| 463 | public function __give_start_updating() { |
||
| 482 | |||
| 483 | |||
| 484 | /** |
||
| 485 | * This function handle ajax query for dn update status. |
||
| 486 | * |
||
| 487 | * @since 2.0 |
||
| 488 | * @access public |
||
| 489 | * |
||
| 490 | * @return string |
||
| 491 | */ |
||
| 492 | public function __give_db_updates_info() { |
||
| 509 | |||
| 510 | /** |
||
| 511 | * Send ajax response |
||
| 512 | * |
||
| 513 | * @since 1.8.12 |
||
| 514 | * @access public |
||
| 515 | * |
||
| 516 | * @param $data |
||
| 517 | * @param string $type |
||
| 518 | */ |
||
| 519 | public function send_ajax_response( $data, $type = '' ) { |
||
| 550 | |||
| 551 | /** |
||
| 552 | * Set current update percentage. |
||
| 553 | * |
||
| 554 | * @since 1.8.12 |
||
| 555 | * @access public |
||
| 556 | * |
||
| 557 | * @param $total |
||
| 558 | * @param $current_total |
||
| 559 | */ |
||
| 560 | public function set_percentage( $total, $current_total ) { |
||
| 567 | |||
| 568 | /** |
||
| 569 | * Check if parent update completed or not. |
||
| 570 | * |
||
| 571 | * @since 2.0 |
||
| 572 | * @access private |
||
| 573 | * |
||
| 574 | * @param array $update |
||
| 575 | * |
||
| 576 | * @return bool|null |
||
| 577 | */ |
||
| 578 | public function is_parent_updates_completed( $update ) { |
||
| 601 | |||
| 602 | /** |
||
| 603 | * Flag to check if DB updates running or not. |
||
| 604 | * |
||
| 605 | * @since 2.0 |
||
| 606 | * @access public |
||
| 607 | * @return bool |
||
| 608 | */ |
||
| 609 | public function is_doing_updates() { |
||
| 612 | |||
| 613 | |||
| 614 | /** |
||
| 615 | * Check if update has valid dependency or not. |
||
| 616 | * |
||
| 617 | * @since 2.0 |
||
| 618 | * @access public |
||
| 619 | * |
||
| 620 | * @param $update |
||
| 621 | * |
||
| 622 | * @return bool |
||
| 623 | */ |
||
| 624 | public function has_valid_dependency( $update ) { |
||
| 638 | |||
| 639 | /** |
||
| 640 | * Get updates. |
||
| 641 | * |
||
| 642 | * @since 1.8.12 |
||
| 643 | * @access public |
||
| 644 | * |
||
| 645 | * @param string $update_type Tye of update. |
||
| 646 | * @param string $status Tye of update. |
||
| 647 | * |
||
| 648 | * @return array |
||
| 649 | */ |
||
| 650 | public function get_updates( $update_type = '', $status = 'all' ) { |
||
| 683 | |||
| 684 | /** |
||
| 685 | * Get addon update count. |
||
| 686 | * |
||
| 687 | * @since 1.8.12 |
||
| 688 | * @access public |
||
| 689 | * @return int |
||
| 690 | */ |
||
| 691 | public function get_total_plugin_update_count() { |
||
| 694 | |||
| 695 | /** |
||
| 696 | * Get total update count |
||
| 697 | * |
||
| 698 | * @since 1.8.12 |
||
| 699 | * @access public |
||
| 700 | * |
||
| 701 | * @return int |
||
| 702 | */ |
||
| 703 | public function get_total_update_count() { |
||
| 709 | |||
| 710 | /** |
||
| 711 | * Get total pending updates count |
||
| 712 | * |
||
| 713 | * @since 1.8.12 |
||
| 714 | * @access public |
||
| 715 | * |
||
| 716 | * @return int |
||
| 717 | */ |
||
| 718 | public function get_pending_db_update_count() { |
||
| 721 | |||
| 722 | /** |
||
| 723 | * Get total updates count |
||
| 724 | * |
||
| 725 | * @since 1.8.18 |
||
| 726 | * @access public |
||
| 727 | * |
||
| 728 | * @return int |
||
| 729 | */ |
||
| 730 | public function get_total_db_update_count() { |
||
| 733 | |||
| 734 | /** |
||
| 735 | * Get total new updates count |
||
| 736 | * |
||
| 737 | * @since 2.0 |
||
| 738 | * @access public |
||
| 739 | * |
||
| 740 | * @return int |
||
| 741 | */ |
||
| 742 | public function get_total_new_db_update_count() { |
||
| 747 | |||
| 748 | /** |
||
| 749 | * Get total new updates count |
||
| 750 | * |
||
| 751 | * @since 2.0 |
||
| 752 | * @access public |
||
| 753 | * |
||
| 754 | * @return int |
||
| 755 | */ |
||
| 756 | public function get_running_db_update() { |
||
| 763 | |||
| 764 | /** |
||
| 765 | * Get database update processing percentage. |
||
| 766 | * |
||
| 767 | * @since 2.0 |
||
| 768 | * @access public |
||
| 769 | * @return float|int |
||
| 770 | */ |
||
| 771 | public function get_db_update_processing_percentage() { |
||
| 791 | } |
||
| 792 | |||
| 794 |