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() { |
||
| 245 | |||
| 246 | /** |
||
| 247 | * Register updates menu |
||
| 248 | * |
||
| 249 | * @since 1.8.12 |
||
| 250 | * @access public |
||
| 251 | */ |
||
| 252 | public function __register_menu() { |
||
| 289 | |||
| 290 | /** |
||
| 291 | * Get total updates count |
||
| 292 | * |
||
| 293 | * @since 1.8.12 |
||
| 294 | * @access public |
||
| 295 | * @return int |
||
| 296 | */ |
||
| 297 | public function get_db_update_count() { |
||
| 300 | |||
| 301 | /** |
||
| 302 | * Render Give Updates Completed page |
||
| 303 | * |
||
| 304 | * @since 1.8.12 |
||
| 305 | * @access public |
||
| 306 | */ |
||
| 307 | public function render_complete_page() { |
||
| 310 | |||
| 311 | /** |
||
| 312 | * Render Give Updates page |
||
| 313 | * |
||
| 314 | * @since 1.8.12 |
||
| 315 | * @access public |
||
| 316 | */ |
||
| 317 | public function render_page() { |
||
| 320 | |||
| 321 | /** |
||
| 322 | * Get addon update count. |
||
| 323 | * |
||
| 324 | * @since 1.8.12 |
||
| 325 | * @access public |
||
| 326 | * @return int |
||
| 327 | */ |
||
| 328 | public function get_plugin_update_count() { |
||
| 331 | |||
| 332 | /** |
||
| 333 | * Get total update count |
||
| 334 | * |
||
| 335 | * @since 1.8.12 |
||
| 336 | * @access public |
||
| 337 | * |
||
| 338 | * @return int |
||
| 339 | */ |
||
| 340 | public function get_update_count() { |
||
| 346 | |||
| 347 | |||
| 348 | /** |
||
| 349 | * Delete resume updates |
||
| 350 | * |
||
| 351 | * @since 1.8.12 |
||
| 352 | * @access public |
||
| 353 | */ |
||
| 354 | public function __flush_resume_updates() { |
||
| 362 | |||
| 363 | /** |
||
| 364 | * Process give updates. |
||
| 365 | * |
||
| 366 | * @since 1.8.12 |
||
| 367 | * @access public |
||
| 368 | */ |
||
| 369 | public function __give_ajax_updates() { |
||
| 454 | |||
| 455 | /** |
||
| 456 | * Send ajax response |
||
| 457 | * |
||
| 458 | * @since 1.8.12 |
||
| 459 | * @access public |
||
| 460 | * |
||
| 461 | * @param $data |
||
| 462 | * @param string $type |
||
| 463 | */ |
||
| 464 | public function send_ajax_response( $data, $type = '' ) { |
||
| 492 | |||
| 493 | |||
| 494 | /** |
||
| 495 | * Resume updates |
||
| 496 | * |
||
| 497 | * @since 1.8.12 |
||
| 498 | * @access public |
||
| 499 | * |
||
| 500 | * @return bool|int |
||
| 501 | */ |
||
| 502 | public function resume_updates() { |
||
| 511 | |||
| 512 | |||
| 513 | /** |
||
| 514 | * Set current update percentage. |
||
| 515 | * |
||
| 516 | * @since 1.8.12 |
||
| 517 | * @access public |
||
| 518 | * |
||
| 519 | * @param $total |
||
| 520 | * @param $current_total |
||
| 521 | */ |
||
| 522 | public function set_percentage( $total, $current_total ) { |
||
| 529 | |||
| 530 | /** |
||
| 531 | * Check if parent update completed or not. |
||
| 532 | * |
||
| 533 | * @since 2.0 |
||
| 534 | * @access private |
||
| 535 | * |
||
| 536 | * @param array $update |
||
| 537 | * |
||
| 538 | * @return bool |
||
| 539 | */ |
||
| 540 | private function is_parent_updates_completed( $update ) { |
||
| 562 | } |
||
| 563 | |||
| 565 |