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() { |
||
| 322 | |||
| 323 | /** |
||
| 324 | * Render Give Updates Completed page |
||
| 325 | * |
||
| 326 | * @since 1.8.12 |
||
| 327 | * @access public |
||
| 328 | */ |
||
| 329 | public function render_complete_page() { |
||
| 332 | |||
| 333 | /** |
||
| 334 | * Render Give Updates page |
||
| 335 | * |
||
| 336 | * @since 1.8.12 |
||
| 337 | * @access public |
||
| 338 | */ |
||
| 339 | public function render_page() { |
||
| 342 | |||
| 343 | |||
| 344 | /** |
||
| 345 | * Delete resume updates |
||
| 346 | * |
||
| 347 | * @since 1.8.12 |
||
| 348 | * @access public |
||
| 349 | */ |
||
| 350 | public function __flush_resume_updates() { |
||
| 358 | |||
| 359 | |||
| 360 | /** |
||
| 361 | * Initialize updates |
||
| 362 | * |
||
| 363 | * @since 2.0 |
||
| 364 | * @access public |
||
| 365 | * |
||
| 366 | * @return void |
||
| 367 | */ |
||
| 368 | public function __give_start_updating() { |
||
| 403 | |||
| 404 | |||
| 405 | /** |
||
| 406 | * This function handle ajax query for dn update status. |
||
| 407 | * |
||
| 408 | * @since 2.0 |
||
| 409 | * @access public |
||
| 410 | * |
||
| 411 | * @return string |
||
| 412 | */ |
||
| 413 | public function __give_db_updates_info() { |
||
| 430 | |||
| 431 | /** |
||
| 432 | * Send ajax response |
||
| 433 | * |
||
| 434 | * @since 1.8.12 |
||
| 435 | * @access public |
||
| 436 | * |
||
| 437 | * @param $data |
||
| 438 | * @param string $type |
||
| 439 | */ |
||
| 440 | public function send_ajax_response( $data, $type = '' ) { |
||
| 471 | |||
| 472 | /** |
||
| 473 | * Set current update percentage. |
||
| 474 | * |
||
| 475 | * @since 1.8.12 |
||
| 476 | * @access public |
||
| 477 | * |
||
| 478 | * @param $total |
||
| 479 | * @param $current_total |
||
| 480 | */ |
||
| 481 | public function set_percentage( $total, $current_total ) { |
||
| 488 | |||
| 489 | /** |
||
| 490 | * Check if parent update completed or not. |
||
| 491 | * |
||
| 492 | * @since 2.0 |
||
| 493 | * @access private |
||
| 494 | * |
||
| 495 | * @param array $update |
||
| 496 | * |
||
| 497 | * @return bool|null |
||
| 498 | */ |
||
| 499 | public function is_parent_updates_completed( $update ) { |
||
| 522 | |||
| 523 | /** |
||
| 524 | * Flag to check if DB updates running or not. |
||
| 525 | * |
||
| 526 | * @since 2.0 |
||
| 527 | * @access public |
||
| 528 | * @return bool |
||
| 529 | */ |
||
| 530 | public function is_doing_updates() { |
||
| 533 | |||
| 534 | |||
| 535 | /** |
||
| 536 | * Check if update has valid dependency or not. |
||
| 537 | * |
||
| 538 | * @since 2.0 |
||
| 539 | * @access public |
||
| 540 | * |
||
| 541 | * @param $update |
||
| 542 | * |
||
| 543 | * @return bool |
||
| 544 | */ |
||
| 545 | public function has_valid_dependency( $update ) { |
||
| 559 | |||
| 560 | /** |
||
| 561 | * Get updates. |
||
| 562 | * |
||
| 563 | * @since 1.8.12 |
||
| 564 | * @access public |
||
| 565 | * |
||
| 566 | * @param string $update_type Tye of update. |
||
| 567 | * @param string $status Tye of update. |
||
| 568 | * |
||
| 569 | * @return array |
||
| 570 | */ |
||
| 571 | public function get_updates( $update_type = '', $status = 'all' ) { |
||
| 604 | |||
| 605 | /** |
||
| 606 | * Get addon update count. |
||
| 607 | * |
||
| 608 | * @since 1.8.12 |
||
| 609 | * @access public |
||
| 610 | * @return int |
||
| 611 | */ |
||
| 612 | public function get_total_plugin_update_count() { |
||
| 615 | |||
| 616 | /** |
||
| 617 | * Get total update count |
||
| 618 | * |
||
| 619 | * @since 1.8.12 |
||
| 620 | * @access public |
||
| 621 | * |
||
| 622 | * @return int |
||
| 623 | */ |
||
| 624 | public function get_total_update_count() { |
||
| 630 | |||
| 631 | /** |
||
| 632 | * Get total pending updates count |
||
| 633 | * |
||
| 634 | * @since 1.8.12 |
||
| 635 | * @access public |
||
| 636 | * |
||
| 637 | * @return int |
||
| 638 | */ |
||
| 639 | public function get_pending_db_update_count() { |
||
| 642 | |||
| 643 | /** |
||
| 644 | * Get total updates count |
||
| 645 | * |
||
| 646 | * @since 1.8.18 |
||
| 647 | * @access public |
||
| 648 | * |
||
| 649 | * @return int |
||
| 650 | */ |
||
| 651 | public function get_total_db_update_count() { |
||
| 654 | |||
| 655 | /** |
||
| 656 | * Get total new updates count |
||
| 657 | * |
||
| 658 | * @since 2.0 |
||
| 659 | * @access public |
||
| 660 | * |
||
| 661 | * @return int |
||
| 662 | */ |
||
| 663 | public function get_total_new_db_update_count() { |
||
| 668 | |||
| 669 | /** |
||
| 670 | * Get total new updates count |
||
| 671 | * |
||
| 672 | * @since 2.0 |
||
| 673 | * @access public |
||
| 674 | * |
||
| 675 | * @return int |
||
| 676 | */ |
||
| 677 | public function get_running_db_update() { |
||
| 684 | |||
| 685 | /** |
||
| 686 | * Get database update processing percentage. |
||
| 687 | * |
||
| 688 | * @since 2.0 |
||
| 689 | * @access public |
||
| 690 | * @return float|int |
||
| 691 | */ |
||
| 692 | public function get_db_update_processing_percentage() { |
||
| 707 | } |
||
| 708 | |||
| 710 |