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() { |
||
| 277 | |||
| 278 | |||
| 279 | /** |
||
| 280 | * Show update related notices |
||
| 281 | * |
||
| 282 | * @since 2.0 |
||
| 283 | * @access public |
||
| 284 | */ |
||
| 285 | public function __redirect_admin() { |
||
| 299 | |||
| 300 | |||
| 301 | /** |
||
| 302 | * Show update related notices |
||
| 303 | * |
||
| 304 | * @since 2.0 |
||
| 305 | * @access public |
||
| 306 | */ |
||
| 307 | public function __show_notice() { |
||
| 375 | |||
| 376 | /** |
||
| 377 | * Render Give Updates Completed page |
||
| 378 | * |
||
| 379 | * @since 1.8.12 |
||
| 380 | * @access public |
||
| 381 | */ |
||
| 382 | public function render_complete_page() { |
||
| 385 | |||
| 386 | /** |
||
| 387 | * Render Give Updates page |
||
| 388 | * |
||
| 389 | * @since 1.8.12 |
||
| 390 | * @access public |
||
| 391 | */ |
||
| 392 | public function render_page() { |
||
| 395 | |||
| 396 | /** |
||
| 397 | * Run database upgrades |
||
| 398 | * |
||
| 399 | * @since 2.0 |
||
| 400 | * @access private |
||
| 401 | */ |
||
| 402 | private function run_db_update() { |
||
| 426 | |||
| 427 | |||
| 428 | /** |
||
| 429 | * Delete resume updates |
||
| 430 | * |
||
| 431 | * @since 1.8.12 |
||
| 432 | * @access public |
||
| 433 | */ |
||
| 434 | public function __flush_resume_updates() { |
||
| 442 | |||
| 443 | |||
| 444 | /** |
||
| 445 | * Initialize updates |
||
| 446 | * |
||
| 447 | * @since 2.0 |
||
| 448 | * @access public |
||
| 449 | * |
||
| 450 | * @return void |
||
| 451 | */ |
||
| 452 | public function __give_start_updating() { |
||
| 471 | |||
| 472 | |||
| 473 | /** |
||
| 474 | * This function handle ajax query for dn update status. |
||
| 475 | * |
||
| 476 | * @since 2.0 |
||
| 477 | * @access public |
||
| 478 | * |
||
| 479 | * @return string |
||
| 480 | */ |
||
| 481 | public function __give_db_updates_info() { |
||
| 498 | |||
| 499 | /** |
||
| 500 | * Send ajax response |
||
| 501 | * |
||
| 502 | * @since 1.8.12 |
||
| 503 | * @access public |
||
| 504 | * |
||
| 505 | * @param $data |
||
| 506 | * @param string $type |
||
| 507 | */ |
||
| 508 | public function send_ajax_response( $data, $type = '' ) { |
||
| 539 | |||
| 540 | /** |
||
| 541 | * Set current update percentage. |
||
| 542 | * |
||
| 543 | * @since 1.8.12 |
||
| 544 | * @access public |
||
| 545 | * |
||
| 546 | * @param $total |
||
| 547 | * @param $current_total |
||
| 548 | */ |
||
| 549 | public function set_percentage( $total, $current_total ) { |
||
| 556 | |||
| 557 | /** |
||
| 558 | * Check if parent update completed or not. |
||
| 559 | * |
||
| 560 | * @since 2.0 |
||
| 561 | * @access private |
||
| 562 | * |
||
| 563 | * @param array $update |
||
| 564 | * |
||
| 565 | * @return bool|null |
||
| 566 | */ |
||
| 567 | public function is_parent_updates_completed( $update ) { |
||
| 590 | |||
| 591 | /** |
||
| 592 | * Flag to check if DB updates running or not. |
||
| 593 | * |
||
| 594 | * @since 2.0 |
||
| 595 | * @access public |
||
| 596 | * @return bool |
||
| 597 | */ |
||
| 598 | public function is_doing_updates() { |
||
| 601 | |||
| 602 | |||
| 603 | /** |
||
| 604 | * Check if update has valid dependency or not. |
||
| 605 | * |
||
| 606 | * @since 2.0 |
||
| 607 | * @access public |
||
| 608 | * |
||
| 609 | * @param $update |
||
| 610 | * |
||
| 611 | * @return bool |
||
| 612 | */ |
||
| 613 | public function has_valid_dependency( $update ) { |
||
| 627 | |||
| 628 | /** |
||
| 629 | * Get updates. |
||
| 630 | * |
||
| 631 | * @since 1.8.12 |
||
| 632 | * @access public |
||
| 633 | * |
||
| 634 | * @param string $update_type Tye of update. |
||
| 635 | * @param string $status Tye of update. |
||
| 636 | * |
||
| 637 | * @return array |
||
| 638 | */ |
||
| 639 | public function get_updates( $update_type = '', $status = 'all' ) { |
||
| 672 | |||
| 673 | /** |
||
| 674 | * Get addon update count. |
||
| 675 | * |
||
| 676 | * @since 1.8.12 |
||
| 677 | * @access public |
||
| 678 | * @return int |
||
| 679 | */ |
||
| 680 | public function get_total_plugin_update_count() { |
||
| 683 | |||
| 684 | /** |
||
| 685 | * Get total update count |
||
| 686 | * |
||
| 687 | * @since 1.8.12 |
||
| 688 | * @access public |
||
| 689 | * |
||
| 690 | * @return int |
||
| 691 | */ |
||
| 692 | public function get_total_update_count() { |
||
| 698 | |||
| 699 | /** |
||
| 700 | * Get total pending updates count |
||
| 701 | * |
||
| 702 | * @since 1.8.12 |
||
| 703 | * @access public |
||
| 704 | * |
||
| 705 | * @return int |
||
| 706 | */ |
||
| 707 | public function get_pending_db_update_count() { |
||
| 710 | |||
| 711 | /** |
||
| 712 | * Get total updates count |
||
| 713 | * |
||
| 714 | * @since 1.8.18 |
||
| 715 | * @access public |
||
| 716 | * |
||
| 717 | * @return int |
||
| 718 | */ |
||
| 719 | public function get_total_db_update_count() { |
||
| 722 | |||
| 723 | /** |
||
| 724 | * Get total new updates count |
||
| 725 | * |
||
| 726 | * @since 2.0 |
||
| 727 | * @access public |
||
| 728 | * |
||
| 729 | * @return int |
||
| 730 | */ |
||
| 731 | public function get_total_new_db_update_count() { |
||
| 736 | |||
| 737 | /** |
||
| 738 | * Get total new updates count |
||
| 739 | * |
||
| 740 | * @since 2.0 |
||
| 741 | * @access public |
||
| 742 | * |
||
| 743 | * @return int |
||
| 744 | */ |
||
| 745 | public function get_running_db_update() { |
||
| 752 | |||
| 753 | /** |
||
| 754 | * Get database update processing percentage. |
||
| 755 | * |
||
| 756 | * @since 2.0 |
||
| 757 | * @access public |
||
| 758 | * @return float|int |
||
| 759 | */ |
||
| 760 | public function get_db_update_processing_percentage() { |
||
| 780 | } |
||
| 781 | |||
| 783 |