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 | * Updates |
||
| 21 | * |
||
| 22 | * @since 1.8.12 |
||
| 23 | * @access private |
||
| 24 | * @var array |
||
| 25 | */ |
||
| 26 | private $updates = array(); |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Current update percentage number |
||
| 30 | * |
||
| 31 | * @since 1.8.12 |
||
| 32 | * @access private |
||
| 33 | * @var array |
||
| 34 | */ |
||
| 35 | public $percentage = 0; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Current update step number |
||
| 39 | * |
||
| 40 | * @since 1.8.12 |
||
| 41 | * @access private |
||
| 42 | * @var array |
||
| 43 | */ |
||
| 44 | public $step = 1; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Current update number |
||
| 48 | * |
||
| 49 | * @since 1.8.12 |
||
| 50 | * @access private |
||
| 51 | * @var array |
||
| 52 | */ |
||
| 53 | public $update = 1; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Singleton pattern. |
||
| 57 | * |
||
| 58 | * @since 1.8.12 |
||
| 59 | * @access private |
||
| 60 | * |
||
| 61 | * @param Give_Updates . |
||
| 62 | */ |
||
| 63 | private function __construct() { |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Register updates |
||
| 68 | * |
||
| 69 | * @since 1.8.12 |
||
| 70 | * @access public |
||
| 71 | * |
||
| 72 | * @param array $args |
||
| 73 | */ |
||
| 74 | public function register( $args ) { |
||
| 93 | |||
| 94 | |||
| 95 | /** |
||
| 96 | * Get updates. |
||
| 97 | * |
||
| 98 | * @since 1.8.12 |
||
| 99 | * @access public |
||
| 100 | * |
||
| 101 | * @param string $update_type Tye of update. |
||
| 102 | * @param string $status Tye of update. |
||
| 103 | * |
||
| 104 | * @return array |
||
| 105 | */ |
||
| 106 | public function get_updates( $update_type = '', $status = 'all' ) { |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Get instance. |
||
| 142 | * |
||
| 143 | * @since |
||
| 144 | * @access static |
||
| 145 | * @return static |
||
| 146 | */ |
||
| 147 | static function get_instance() { |
||
| 154 | |||
| 155 | /** |
||
| 156 | * |
||
| 157 | * Setup hook |
||
| 158 | * |
||
| 159 | * @since 1.8.12 |
||
| 160 | * @access public |
||
| 161 | */ |
||
| 162 | public function setup() { |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Register plugin add-on updates. |
||
| 180 | * |
||
| 181 | * @since 1.8.12 |
||
| 182 | * @access public |
||
| 183 | */ |
||
| 184 | public function __register_plugin_addon_updates() { |
||
| 196 | |||
| 197 | |||
| 198 | /** |
||
| 199 | * Fire custom action hook to register updates |
||
| 200 | * |
||
| 201 | * @since 1.8.12 |
||
| 202 | * @access public |
||
| 203 | */ |
||
| 204 | public function __register_upgrade() { |
||
| 216 | |||
| 217 | /** |
||
| 218 | * Rename `Donations` menu title if updates exists |
||
| 219 | * |
||
| 220 | * @since 1.8.12 |
||
| 221 | * @access public |
||
| 222 | */ |
||
| 223 | function __change_donations_label() { |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Register updates menu |
||
| 251 | * |
||
| 252 | * @since 1.8.12 |
||
| 253 | * @access public |
||
| 254 | */ |
||
| 255 | public function __register_menu() { |
||
| 292 | |||
| 293 | /** |
||
| 294 | * Get total updates count |
||
| 295 | * |
||
| 296 | * @since 1.8.12 |
||
| 297 | * @access public |
||
| 298 | * @return int |
||
| 299 | */ |
||
| 300 | public function get_db_update_count() { |
||
| 303 | |||
| 304 | /** |
||
| 305 | * Render Give Updates Completed page |
||
| 306 | * |
||
| 307 | * @since 1.8.12 |
||
| 308 | * @access public |
||
| 309 | */ |
||
| 310 | public function render_complete_page() { |
||
| 313 | |||
| 314 | /** |
||
| 315 | * Render Give Updates page |
||
| 316 | * |
||
| 317 | * @since 1.8.12 |
||
| 318 | * @access public |
||
| 319 | */ |
||
| 320 | public function render_page() { |
||
| 323 | |||
| 324 | /** |
||
| 325 | * Get addon update count. |
||
| 326 | * |
||
| 327 | * @since 1.8.12 |
||
| 328 | * @access public |
||
| 329 | * @return int |
||
| 330 | */ |
||
| 331 | public function get_plugin_update_count() { |
||
| 334 | |||
| 335 | /** |
||
| 336 | * Get total update count |
||
| 337 | * |
||
| 338 | * @since 1.8.12 |
||
| 339 | * @access public |
||
| 340 | * |
||
| 341 | * @return int |
||
| 342 | */ |
||
| 343 | public function get_update_count() { |
||
| 349 | |||
| 350 | |||
| 351 | /** |
||
| 352 | * Delete resume updates |
||
| 353 | * |
||
| 354 | * @since 1.8.12 |
||
| 355 | * @access public |
||
| 356 | */ |
||
| 357 | public function __flush_resume_updates() { |
||
| 365 | |||
| 366 | /** |
||
| 367 | * Process give updates. |
||
| 368 | * |
||
| 369 | * @since 1.8.12 |
||
| 370 | * @access public |
||
| 371 | */ |
||
| 372 | public function __give_ajax_updates() { |
||
| 468 | |||
| 469 | /** |
||
| 470 | * Send ajax response |
||
| 471 | * |
||
| 472 | * @since 1.8.12 |
||
| 473 | * @access public |
||
| 474 | * |
||
| 475 | * @param $data |
||
| 476 | * @param string $type |
||
| 477 | */ |
||
| 478 | public function send_ajax_response( $data, $type = '' ) { |
||
| 506 | |||
| 507 | |||
| 508 | /** |
||
| 509 | * Resume updates |
||
| 510 | * |
||
| 511 | * @since 1.8.12 |
||
| 512 | * @access public |
||
| 513 | * |
||
| 514 | * @return bool|int |
||
| 515 | */ |
||
| 516 | public function resume_updates() { |
||
| 525 | |||
| 526 | |||
| 527 | /** |
||
| 528 | * Set current update percentage. |
||
| 529 | * |
||
| 530 | * @since 1.8.12 |
||
| 531 | * @access public |
||
| 532 | * |
||
| 533 | * @param $total |
||
| 534 | * @param $current_total |
||
| 535 | */ |
||
| 536 | public function set_percentage( $total, $current_total ) { |
||
| 543 | |||
| 544 | /** |
||
| 545 | * Check if parent update completed or not. |
||
| 546 | * |
||
| 547 | * @since 2.0 |
||
| 548 | * @access private |
||
| 549 | * |
||
| 550 | * @param array $update |
||
| 551 | * |
||
| 552 | * @return bool |
||
| 553 | */ |
||
| 554 | private function is_parent_updates_completed( $update ) { |
||
| 576 | } |
||
| 577 | |||
| 579 |