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:
1 | <?php |
||
24 | class Give_Tools_Delete_Donors extends Give_Batch_Export { |
||
25 | |||
26 | var $request; |
||
27 | |||
28 | /** |
||
29 | * Used to store donation id's that are going to get deleted. |
||
30 | * @var string |
||
31 | * @since 1.8.12 |
||
32 | */ |
||
33 | var $donation_key = 'give_temp_delete_donation_ids'; |
||
34 | |||
35 | /** |
||
36 | * Used to store donors id's that are going to get deleted. |
||
37 | * @var string |
||
38 | * @since 1.8.12 |
||
39 | */ |
||
40 | var $donor_key = 'give_temp_delete_donor_ids'; |
||
41 | |||
42 | /** |
||
43 | * Used to store the step where the step will be. ( 'count', 'donations', 'donors' ). |
||
44 | * @var string |
||
45 | * @since 1.8.12 |
||
46 | */ |
||
47 | var $step_key = 'give_temp_delete_step'; |
||
48 | |||
49 | /** |
||
50 | * Used to store to get the page count in the loop. |
||
51 | * @var string |
||
52 | * @since 1.8.12 |
||
53 | */ |
||
54 | var $step_on_key = 'give_temp_delete_step_on'; |
||
55 | |||
56 | /** |
||
57 | * Contain total number of step . |
||
58 | * @var string |
||
59 | * @since 1.8.12 |
||
60 | */ |
||
61 | var $total_step; |
||
62 | |||
63 | /** |
||
64 | * Counting contain total number of step that completed. |
||
65 | * @var int |
||
66 | * @since 1.8.12 |
||
67 | */ |
||
68 | var $step_completed; |
||
69 | |||
70 | /** |
||
71 | * Our export type. Used for export-type specific filters/actions |
||
72 | * @var string |
||
73 | * @since 1.8.12 |
||
74 | */ |
||
75 | public $export_type = ''; |
||
76 | |||
77 | /** |
||
78 | * Allows for a non-form batch processing to be run. |
||
79 | * @since 1.8.12 |
||
80 | * @var boolean |
||
81 | */ |
||
82 | public $is_void = true; |
||
83 | |||
84 | /** |
||
85 | * Sets the number of items to pull on each step |
||
86 | * @since 1.8.12 |
||
87 | * @var int |
||
88 | */ |
||
89 | public $per_step = 10; |
||
90 | |||
91 | /** |
||
92 | * Set's all the donors id's |
||
93 | * @since 1.8.12 |
||
94 | * @var array |
||
95 | */ |
||
96 | public $donor_ids = array(); |
||
97 | |||
98 | /** |
||
99 | * Constructor. |
||
100 | */ |
||
101 | public function __construct( $_step = 1 ) { |
||
106 | |||
107 | /** |
||
108 | * Get the Export Data |
||
109 | * |
||
110 | * @access public |
||
111 | * @since 1.8.12 |
||
112 | * @global object $wpdb Used to query the database using the WordPress Database API |
||
113 | * |
||
114 | * @return array|bool $data The data for the CSV file |
||
115 | */ |
||
116 | View Code Duplication | public function pre_fetch() { |
|
149 | |||
150 | /** |
||
151 | * Will Update or Add the donation and donors ids in the with option table for there respected key. |
||
152 | * |
||
153 | * @param string $step On which the current ajax is running. |
||
154 | * @param array $donation_ids Contain the list of all the donation id's that has being add before this |
||
155 | * @param array $donor_ids Contain the list of all the donors id's that has being add before this |
||
156 | */ |
||
157 | private function count( $step, $donation_ids = array(), $donor_ids = array() ) { |
||
223 | |||
224 | /** |
||
225 | * Return the calculated completion percentage. |
||
226 | * |
||
227 | * @since 1.8.12 |
||
228 | * @return int |
||
229 | */ |
||
230 | public function get_percentage_complete() { |
||
233 | |||
234 | View Code Duplication | public function process_step() { |
|
263 | |||
264 | /** |
||
265 | * Get the Export Data |
||
266 | * |
||
267 | * @access public |
||
268 | * @since 1.8.12 |
||
269 | * @global object $wpdb Used to query the database using the WordPress Database API |
||
270 | * |
||
271 | * @return array|bool $data The data for the CSV file |
||
272 | */ |
||
273 | public function get_data() { |
||
366 | |||
367 | View Code Duplication | public function get_delete_ids( $donation_ids, $page ) { |
|
386 | |||
387 | /** |
||
388 | * Given a key, get the information from the Database Directly |
||
389 | * |
||
390 | * @since 1.8.13 |
||
391 | * |
||
392 | * @param string $key The option_name |
||
393 | * |
||
394 | * @return mixed Returns the data from the database |
||
395 | */ |
||
396 | public function get_option( $key, $defalut_value = false ) { |
||
399 | |||
400 | /** |
||
401 | * Give a key, store the value |
||
402 | * |
||
403 | * @since 1.8.12s |
||
404 | * |
||
405 | * @param string $key The option_name |
||
406 | * @param mixed $value The value to store |
||
407 | * |
||
408 | * @return void |
||
409 | */ |
||
410 | public function update_option( $key, $value ) { |
||
413 | |||
414 | /** |
||
415 | * Delete an option |
||
416 | * |
||
417 | * @since 1.8.12 |
||
418 | * |
||
419 | * @param string $key The option_name to delete |
||
420 | * |
||
421 | * @return void |
||
422 | */ |
||
423 | public function delete_option( $key ) { |
||
426 | |||
427 | /** |
||
428 | * Get the current step in number. |
||
429 | * |
||
430 | * There are three step to delete the total donor first counting, second deleting donotion and third deleting donors. |
||
431 | * |
||
432 | * @return int|string |
||
433 | */ |
||
434 | View Code Duplication | private function get_step() { |
|
446 | |||
447 | /** |
||
448 | * Get the current $page value in the ajax. |
||
449 | */ |
||
450 | private function get_step_page() { |
||
453 | } |
||
454 |
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.