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() { |
||
247 | |||
248 | /** |
||
249 | * Register updates menu |
||
250 | * |
||
251 | * @since 1.8.12 |
||
252 | * @access public |
||
253 | */ |
||
254 | public function __register_menu() { |
||
291 | |||
292 | /** |
||
293 | * Get total updates count |
||
294 | * |
||
295 | * @since 1.8.12 |
||
296 | * @access public |
||
297 | * @return int |
||
298 | */ |
||
299 | public function get_db_update_count() { |
||
302 | |||
303 | /** |
||
304 | * Render Give Updates Completed page |
||
305 | * |
||
306 | * @since 1.8.12 |
||
307 | * @access public |
||
308 | */ |
||
309 | public function render_complete_page() { |
||
312 | |||
313 | /** |
||
314 | * Render Give Updates page |
||
315 | * |
||
316 | * @since 1.8.12 |
||
317 | * @access public |
||
318 | */ |
||
319 | public function render_page() { |
||
322 | |||
323 | /** |
||
324 | * Get addon update count. |
||
325 | * |
||
326 | * @since 1.8.12 |
||
327 | * @access public |
||
328 | * @return int |
||
329 | */ |
||
330 | public function get_plugin_update_count() { |
||
333 | |||
334 | /** |
||
335 | * Get total update count |
||
336 | * |
||
337 | * @since 1.8.12 |
||
338 | * @access public |
||
339 | * |
||
340 | * @return int |
||
341 | */ |
||
342 | public function get_update_count() { |
||
348 | |||
349 | |||
350 | /** |
||
351 | * Delete resume updates |
||
352 | * |
||
353 | * @since 1.8.12 |
||
354 | * @access public |
||
355 | */ |
||
356 | public function __flush_resume_updates() { |
||
364 | |||
365 | /** |
||
366 | * Process give updates. |
||
367 | * |
||
368 | * @since 1.8.12 |
||
369 | * @access public |
||
370 | */ |
||
371 | public function __give_ajax_updates() { |
||
455 | |||
456 | /** |
||
457 | * Send ajax response |
||
458 | * |
||
459 | * @since 1.8.12 |
||
460 | * @access public |
||
461 | * |
||
462 | * @param $data |
||
463 | * @param string $type |
||
464 | */ |
||
465 | public function send_ajax_response( $data, $type = '' ) { |
||
493 | |||
494 | |||
495 | /** |
||
496 | * Resume updates |
||
497 | * |
||
498 | * @since 1.8.12 |
||
499 | * @access public |
||
500 | * |
||
501 | * @return bool|int |
||
502 | */ |
||
503 | public function resume_updates() { |
||
512 | |||
513 | |||
514 | /** |
||
515 | * Set current update percentage. |
||
516 | * |
||
517 | * @since 1.8.12 |
||
518 | * @access public |
||
519 | * |
||
520 | * @param $total |
||
521 | * @param $current_total |
||
522 | */ |
||
523 | public function set_percentage( $total, $current_total ) { |
||
530 | } |
||
531 | |||
533 |