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_Tools_Import_Donors 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_Tools_Import_Donors, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
24 | class Give_Tools_Import_Donors extends Give_Batch_Export { |
||
25 | |||
26 | /** |
||
27 | * Form Data passed in batch processing. |
||
28 | * |
||
29 | * @var $request |
||
30 | */ |
||
31 | var $request; |
||
32 | |||
33 | /** |
||
34 | * Used to store form id's that are going to get recount. |
||
35 | * |
||
36 | * @var $form_key |
||
37 | * |
||
38 | * @since 1.8.13 |
||
39 | */ |
||
40 | var $form_key = 'give_temp_delete_form_ids'; |
||
41 | |||
42 | /** |
||
43 | * Used to store donation id's that are going to get deleted. |
||
44 | * |
||
45 | * @var $donation_key |
||
46 | * |
||
47 | * @since 1.8.12 |
||
48 | */ |
||
49 | var $donation_key = 'give_temp_delete_donation_ids'; |
||
50 | |||
51 | /** |
||
52 | * Used to store the step where the step will be. ( 'count', 'donations', 'donors' ). |
||
53 | * |
||
54 | * @var $step_key |
||
55 | * |
||
56 | * @since 1.8.12 |
||
57 | */ |
||
58 | var $step_key = 'give_temp_delete_step'; |
||
59 | |||
60 | /** |
||
61 | * Used to store donors id's that are going to get deleted. |
||
62 | * |
||
63 | * @var $donor_key |
||
64 | * |
||
65 | * @since 1.8.12 |
||
66 | */ |
||
67 | var $donor_key = 'give_temp_delete_donor_ids'; |
||
68 | |||
69 | /** |
||
70 | * Used to store to get the page count in the loop. |
||
71 | * |
||
72 | * @var $step_on_key |
||
73 | * |
||
74 | * @since 1.8.12 |
||
75 | */ |
||
76 | var $step_on_key = 'give_temp_delete_step_on'; |
||
77 | |||
78 | /** |
||
79 | * Contain total number of step. |
||
80 | * |
||
81 | * @var $total_step |
||
82 | * |
||
83 | * @since 1.8.12 |
||
84 | */ |
||
85 | var $total_step; |
||
86 | |||
87 | /** |
||
88 | * Counting contain total number of step that completed. |
||
89 | * |
||
90 | * @var $step_completed |
||
91 | * |
||
92 | * @since 1.8.12 |
||
93 | */ |
||
94 | var $step_completed; |
||
95 | |||
96 | /** |
||
97 | * Our export type. Used for export-type specific filters/actions. |
||
98 | * |
||
99 | * @var $export_type |
||
100 | * |
||
101 | * @since 1.8.12 |
||
102 | */ |
||
103 | public $export_type = ''; |
||
104 | |||
105 | /** |
||
106 | * Allows for a non-form batch processing to be run. |
||
107 | * |
||
108 | * @var $is_void |
||
109 | * |
||
110 | * @since 1.8.12 |
||
111 | */ |
||
112 | public $is_void = true; |
||
113 | |||
114 | /** |
||
115 | * Sets the number of items to pull on each step |
||
116 | * |
||
117 | * @var $per_step |
||
118 | * |
||
119 | * @since 1.8.12 |
||
120 | */ |
||
121 | public $per_step = 10; |
||
122 | |||
123 | /** |
||
124 | * Set's all the donors id's |
||
125 | * |
||
126 | * @var $donor_ids |
||
127 | * |
||
128 | * @since 1.8.12 |
||
129 | */ |
||
130 | public $donor_ids = array(); |
||
131 | |||
132 | /** |
||
133 | * Give_Tools_Import_Donors constructor. |
||
134 | * |
||
135 | * @param int $_step Steps. |
||
136 | */ |
||
137 | public function __construct( $_step = 1 ) { |
||
142 | |||
143 | /** |
||
144 | * Get the Export Data |
||
145 | * |
||
146 | * @access public |
||
147 | * @since 1.8.12 |
||
148 | * @global object $wpdb Used to query the database using the WordPress Database API |
||
149 | * |
||
150 | * @return void |
||
151 | */ |
||
152 | View Code Duplication | public function pre_fetch() { |
|
191 | |||
192 | /** |
||
193 | * Will Update or Add the donation and donors ids in the with option table for there respected key. |
||
194 | * |
||
195 | * @param string $step On which the current ajax is running. |
||
196 | * @param array $donation_ids Contain the list of all the donation id's that has being add before this. |
||
197 | * @param array $donor_ids Contain the list of all the donors id's that has being add before this. |
||
198 | */ |
||
199 | private function count( $step, $donation_ids = array(), $donor_ids = array() ) { |
||
287 | |||
288 | /** |
||
289 | * Return the calculated completion percentage. |
||
290 | * |
||
291 | * @since 1.8.12 |
||
292 | * |
||
293 | * @return int |
||
294 | */ |
||
295 | public function get_percentage_complete() { |
||
298 | |||
299 | /** |
||
300 | * Process Steps |
||
301 | * |
||
302 | * @return bool |
||
303 | */ |
||
304 | public function process_step() { |
||
334 | |||
335 | /** |
||
336 | * Get the Export Data |
||
337 | * |
||
338 | * @access public |
||
339 | * @since 1.8.12 |
||
340 | * @global object $wpdb Used to query the database using the WordPress Database API |
||
341 | * |
||
342 | * @return array|bool $data The data for the CSV file |
||
343 | */ |
||
344 | public function get_data() { |
||
477 | |||
478 | /** |
||
479 | * This function will get list of donation ids ready for deletion. |
||
480 | * |
||
481 | * @param array $donation_ids List of donation ids. |
||
482 | * @param string $page Ajax on Page. |
||
483 | * |
||
484 | * @return mixed |
||
485 | */ |
||
486 | View Code Duplication | public function get_delete_ids( $donation_ids, $page ) { |
|
505 | |||
506 | /** |
||
507 | * Given a key, get the information from the Database Directly |
||
508 | * |
||
509 | * @since 1.8.12 |
||
510 | * |
||
511 | * @param string $key Option Key. |
||
512 | * @param bool $default_value True, if default value, else false. |
||
513 | * |
||
514 | * @return mixed Returns the data from the database |
||
515 | */ |
||
516 | public function get_option( $key, $default_value = false ) { |
||
519 | |||
520 | /** |
||
521 | * Give a key, store the value |
||
522 | * |
||
523 | * @since 1.8.12 |
||
524 | * |
||
525 | * @param string $key Option Key. |
||
526 | * @param mixed $value Option Value. |
||
527 | * |
||
528 | * @return void |
||
529 | */ |
||
530 | public function update_option( $key, $value ) { |
||
533 | |||
534 | /** |
||
535 | * Delete an option |
||
536 | * |
||
537 | * @since 1.8.12 |
||
538 | * |
||
539 | * @param string $key Option Key. |
||
540 | * |
||
541 | * @return void |
||
542 | */ |
||
543 | public function delete_option( $key ) { |
||
546 | |||
547 | /** |
||
548 | * Get the current step in number. |
||
549 | * |
||
550 | * There are three step to delete the total donor first counting, second deleting donotion and third deleting donors. |
||
551 | * |
||
552 | * @return int|string |
||
553 | */ |
||
554 | View Code Duplication | private function get_step() { |
|
566 | |||
567 | /** |
||
568 | * Get the current $page value in the ajax. |
||
569 | */ |
||
570 | private function get_step_page() { |
||
573 | } |
||
574 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.