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 |
||
| 7 | class Give_Updates { |
||
|
|
|||
| 8 | |||
| 9 | /** |
||
| 10 | * Instance. |
||
| 11 | * |
||
| 12 | * @since |
||
| 13 | * @access static |
||
| 14 | * @var |
||
| 15 | */ |
||
| 16 | static private $instance; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * Updates |
||
| 20 | * |
||
| 21 | * @since 1.8.12 |
||
| 22 | * @access private |
||
| 23 | * @var array |
||
| 24 | */ |
||
| 25 | private $updates = array(); |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Current update percentage number |
||
| 29 | * |
||
| 30 | * @since 1.8.12 |
||
| 31 | * @access private |
||
| 32 | * @var array |
||
| 33 | */ |
||
| 34 | public $percentage = 0; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Current update step number |
||
| 38 | * |
||
| 39 | * @since 1.8.12 |
||
| 40 | * @access private |
||
| 41 | * @var array |
||
| 42 | */ |
||
| 43 | public $step = 1; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Current update number |
||
| 47 | * |
||
| 48 | * @since 1.8.12 |
||
| 49 | * @access private |
||
| 50 | * @var array |
||
| 51 | */ |
||
| 52 | public $update = 1; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Singleton pattern. |
||
| 56 | * |
||
| 57 | * @since 1.8.12 |
||
| 58 | * @access private |
||
| 59 | * |
||
| 60 | * @param Give_Updates . |
||
| 61 | */ |
||
| 62 | private function __construct() { |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Register updates |
||
| 67 | * |
||
| 68 | * @since 1.8.12 |
||
| 69 | * @access public |
||
| 70 | * |
||
| 71 | * @param array $args |
||
| 72 | */ |
||
| 73 | public function register( $args ) { |
||
| 92 | |||
| 93 | |||
| 94 | /** |
||
| 95 | * Get updates. |
||
| 96 | * |
||
| 97 | * @since 1.8.12 |
||
| 98 | * @access public |
||
| 99 | * |
||
| 100 | * @param string $update_type Tye of update. |
||
| 101 | * @param string $status Tye of update. |
||
| 102 | * |
||
| 103 | * @return array |
||
| 104 | */ |
||
| 105 | public function get_updates( $update_type = '', $status = 'all' ) { |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Get instance. |
||
| 141 | * |
||
| 142 | * @since |
||
| 143 | * @access static |
||
| 144 | * @return static |
||
| 145 | */ |
||
| 146 | static function get_instance() { |
||
| 153 | |||
| 154 | /** |
||
| 155 | * |
||
| 156 | * Setup hook |
||
| 157 | * |
||
| 158 | * @since 1.8.12 |
||
| 159 | * @access public |
||
| 160 | */ |
||
| 161 | public function setup() { |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Register plugin add-on updates. |
||
| 179 | * |
||
| 180 | * @since 1.8.12 |
||
| 181 | * @access public |
||
| 182 | */ |
||
| 183 | public function __register_plugin_addon_updates() { |
||
| 195 | |||
| 196 | |||
| 197 | /** |
||
| 198 | * Fire custom action hook to register updates |
||
| 199 | * |
||
| 200 | * @since 1.8.12 |
||
| 201 | * @access public |
||
| 202 | */ |
||
| 203 | public function __register_upgrade() { |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Rename `Donations` menu title if updates exists |
||
| 218 | * |
||
| 219 | * @since 1.8.12 |
||
| 220 | * @access public |
||
| 221 | */ |
||
| 222 | function __change_donations_label() { |
||
| 244 | |||
| 245 | /** |
||
| 246 | * Register updates menu |
||
| 247 | * |
||
| 248 | * @since 1.8.12 |
||
| 249 | * @access public |
||
| 250 | */ |
||
| 251 | public function __register_menu() { |
||
| 288 | |||
| 289 | /** |
||
| 290 | * Get total updates count |
||
| 291 | * |
||
| 292 | * @since 1.8.12 |
||
| 293 | * @access public |
||
| 294 | * @return int |
||
| 295 | */ |
||
| 296 | public function get_db_update_count() { |
||
| 299 | |||
| 300 | /** |
||
| 301 | * Render Give Updates Completed page |
||
| 302 | * |
||
| 303 | * @since 1.8.12 |
||
| 304 | * @access public |
||
| 305 | */ |
||
| 306 | public function render_complete_page() { |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Render Give Updates page |
||
| 312 | * |
||
| 313 | * @since 1.8.12 |
||
| 314 | * @access public |
||
| 315 | */ |
||
| 316 | public function render_page() { |
||
| 319 | |||
| 320 | /** |
||
| 321 | * Get addon update count. |
||
| 322 | * |
||
| 323 | * @since 1.8.12 |
||
| 324 | * @access public |
||
| 325 | * @return int |
||
| 326 | */ |
||
| 327 | public function get_plugin_update_count() { |
||
| 330 | |||
| 331 | /** |
||
| 332 | * Get total update count |
||
| 333 | * |
||
| 334 | * @since 1.8.12 |
||
| 335 | * @access public |
||
| 336 | * |
||
| 337 | * @return int |
||
| 338 | */ |
||
| 339 | public function get_update_count() { |
||
| 345 | |||
| 346 | |||
| 347 | /** |
||
| 348 | * Delete resume updates |
||
| 349 | * |
||
| 350 | * @since 1.8.12 |
||
| 351 | * @access public |
||
| 352 | */ |
||
| 353 | public function __flush_resume_updates() { |
||
| 361 | |||
| 362 | /** |
||
| 363 | * Process give updates. |
||
| 364 | * |
||
| 365 | * @since 1.8.12 |
||
| 366 | * @access public |
||
| 367 | */ |
||
| 368 | public function __give_ajax_updates() { |
||
| 458 | |||
| 459 | /** |
||
| 460 | * Send ajax response |
||
| 461 | * |
||
| 462 | * @since 1.8.12 |
||
| 463 | * @access public |
||
| 464 | * |
||
| 465 | * @param $data |
||
| 466 | * @param string $type |
||
| 467 | */ |
||
| 468 | public function send_ajax_response( $data, $type = '' ) { |
||
| 496 | |||
| 497 | |||
| 498 | /** |
||
| 499 | * Resume updates |
||
| 500 | * |
||
| 501 | * @since 1.8.12 |
||
| 502 | * @access public |
||
| 503 | * |
||
| 504 | * @return bool|int |
||
| 505 | */ |
||
| 506 | public function resume_updates() { |
||
| 515 | |||
| 516 | |||
| 517 | /** |
||
| 518 | * Set current update percentage. |
||
| 519 | * |
||
| 520 | * @since 1.8.12 |
||
| 521 | * @access public |
||
| 522 | * |
||
| 523 | * @param $total |
||
| 524 | * @param $current_total |
||
| 525 | */ |
||
| 526 | public function set_percentage( $total, $current_total ) { |
||
| 533 | } |
||
| 534 | |||
| 536 |