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_Import_Donations 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_Import_Donations, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 25 | final class Give_Import_Donations { |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Importer type |
||
| 29 | * |
||
| 30 | * @since 1.8.13 |
||
| 31 | * @var string |
||
| 32 | */ |
||
| 33 | private $importer_type = 'import_donations'; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Instance. |
||
| 37 | * |
||
| 38 | * @since |
||
| 39 | * @access private |
||
| 40 | * @var |
||
| 41 | */ |
||
| 42 | static private $instance; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Importing donation per page. |
||
| 46 | * |
||
| 47 | * @since 1.8.14 |
||
| 48 | * |
||
| 49 | * @var int |
||
| 50 | */ |
||
| 51 | public static $per_page = 5; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Singleton pattern. |
||
| 55 | * |
||
| 56 | * @since |
||
| 57 | * @access private |
||
| 58 | */ |
||
| 59 | private function __construct() { |
||
| 61 | |||
| 62 | |||
| 63 | /** |
||
| 64 | * Get instance. |
||
| 65 | * |
||
| 66 | * @since |
||
| 67 | * @access public |
||
| 68 | * |
||
| 69 | * @return static |
||
| 70 | */ |
||
| 71 | public static function get_instance() { |
||
| 72 | if ( null === static::$instance ) { |
||
| 73 | self::$instance = new static(); |
||
| 74 | } |
||
| 75 | |||
| 76 | return self::$instance; |
||
| 77 | } |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Setup |
||
| 81 | * |
||
| 82 | * @since 1.8.14 |
||
| 83 | * |
||
| 84 | * @return void |
||
| 85 | */ |
||
| 86 | public function setup() { |
||
| 87 | $this->setup_hooks(); |
||
| 88 | } |
||
| 89 | |||
| 90 | |||
| 91 | /** |
||
| 92 | * Setup Hooks. |
||
| 93 | * |
||
| 94 | * @since 1.8.14 |
||
| 95 | * |
||
| 96 | * @return void |
||
| 97 | */ |
||
| 98 | private function setup_hooks() { |
||
| 99 | if ( ! $this->is_donations_import_page() ) { |
||
| 100 | return; |
||
| 101 | } |
||
| 102 | |||
| 103 | // Do not render main import tools page. |
||
| 104 | remove_action( 'give_admin_field_tools_import', array( 'Give_Settings_Import', 'render_import_field', ) ); |
||
| 105 | |||
|
|
|||
| 106 | |||
| 107 | // Render donation import page |
||
| 108 | add_action( 'give_admin_field_tools_import', array( $this, 'render_page' ) ); |
||
| 109 | |||
| 110 | // Print the HTML. |
||
| 111 | add_action( 'give_tools_import_donations_form_start', array( $this, 'html' ), 10 ); |
||
| 112 | |||
| 113 | // Run when form submit. |
||
| 114 | add_action( 'give-tools_save_import', array( $this, 'save' ) ); |
||
| 115 | |||
| 116 | add_action( 'give-tools_update_notices', array( $this, 'update_notices' ), 11, 1 ); |
||
| 117 | |||
| 118 | // Used to add submit button. |
||
| 119 | add_action( 'give_tools_import_donations_form_end', array( $this, 'submit' ), 10 ); |
||
| 120 | } |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Update notice |
||
| 124 | * |
||
| 125 | * @since 1.8.14 |
||
| 126 | * |
||
| 127 | * @param $messages |
||
| 128 | * |
||
| 129 | * @return mixed |
||
| 130 | */ |
||
| 131 | public function update_notices( $messages ) { |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Print submit and nonce button. |
||
| 141 | * |
||
| 142 | * @since 1.8.14 |
||
| 143 | */ |
||
| 144 | public function submit() { |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Print the HTML for importer. |
||
| 154 | * |
||
| 155 | * @since 1.8.14 |
||
| 156 | */ |
||
| 157 | public function html() { |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Show success notice |
||
| 206 | * |
||
| 207 | * @since 1.8.14 |
||
| 208 | */ |
||
| 209 | public function import_success() { |
||
| 300 | |||
| 301 | /** |
||
| 302 | * Will start Import |
||
| 303 | * |
||
| 304 | * @since 1.8.14 |
||
| 305 | */ |
||
| 306 | public function start_import() { |
||
| 307 | // Reset the donation form report. |
||
| 308 | give_import_donation_report_reset(); |
||
| 309 | |||
| 310 | $csv = (int) $_REQUEST['csv']; |
||
| 311 | $delimiter = ( ! empty( $_REQUEST['delimiter'] ) ? give_clean( $_REQUEST['delimiter'] ) : 'csv' ); |
||
| 312 | $index_start = 1; |
||
| 313 | $index_end = 1; |
||
| 314 | $next = true; |
||
| 315 | $total = self::get_csv_total( $csv ); |
||
| 316 | if ( self::$per_page < $total ) { |
||
| 317 | $total_ajax = ceil( $total / self::$per_page ); |
||
| 318 | $index_end = self::$per_page; |
||
| 319 | } else { |
||
| 320 | $total_ajax = 1; |
||
| 321 | $index_end = $total; |
||
| 322 | $next = false; |
||
| 323 | } |
||
| 324 | $current_percentage = 100 / ( $total_ajax + 1 ); |
||
| 325 | |||
| 326 | ?> |
||
| 327 | <tr valign="top" class="give-import-dropdown"> |
||
| 328 | <th colspan="2"> |
||
| 329 | <h2 id="give-import-title"><?php esc_html_e( 'Importing', 'give' ) ?></h2> |
||
| 330 | <p class="give-field-description"><?php esc_html_e( 'Your donations are now being imported...', 'give' ) ?></p> |
||
| 331 | </th> |
||
| 332 | </tr> |
||
| 333 | |||
| 334 | <tr valign="top" class="give-import-dropdown"> |
||
| 335 | <th colspan="2"> |
||
| 336 | <span class="spinner is-active"></span> |
||
| 337 | <div class="give-progress" |
||
| 338 | data-current="1" |
||
| 339 | data-total_ajax="<?php echo $total_ajax; ?>" |
||
| 340 | data-start="<?php echo $index_start; ?>" |
||
| 341 | data-end="<?php echo $index_end; ?>" |
||
| 342 | data-next="<?php echo $next; ?>" |
||
| 343 | data-total="<?php echo $total; ?>" |
||
| 344 | data-per_page="<?php echo self::$per_page; ?>"> |
||
| 345 | |||
| 346 | <div style="width: <?php echo $current_percentage; ?>%"></div> |
||
| 347 | </div> |
||
| 348 | <input type="hidden" value="3" name="step"> |
||
| 349 | <input type="hidden" value='<?php echo maybe_serialize( $_REQUEST['mapto'] ); ?>' name="mapto" |
||
| 350 | class="mapto"> |
||
| 351 | <input type="hidden" value="<?php echo $_REQUEST['csv']; ?>" name="csv" class="csv"> |
||
| 352 | <input type="hidden" value="<?php echo $_REQUEST['mode']; ?>" name="mode" class="mode"> |
||
| 353 | <input type="hidden" value="<?php echo $_REQUEST['create_user']; ?>" name="create_user" |
||
| 354 | class="create_user"> |
||
| 355 | <input type="hidden" value="<?php echo $_REQUEST['delete_csv']; ?>" name="delete_csv" |
||
| 356 | class="delete_csv"> |
||
| 357 | <input type="hidden" value="<?php echo $delimiter; ?>" name="delimiter"> |
||
| 358 | <input type="hidden" value='<?php echo maybe_serialize( self::get_importer( $csv, 0, $delimiter ) ); ?>' |
||
| 359 | name="main_key" |
||
| 360 | class="main_key"> |
||
| 361 | </th> |
||
| 362 | </tr> |
||
| 363 | |||
| 364 | <script type="text/javascript"> |
||
| 365 | jQuery(document).ready(function () { |
||
| 366 | give_on_donation_import_start(); |
||
| 367 | }); |
||
| 368 | </script> |
||
| 369 | <?php |
||
| 370 | } |
||
| 371 | |||
| 372 | /** |
||
| 373 | * Will return true if importing can be started or not else false. |
||
| 374 | * |
||
| 375 | * @since 1.8.14 |
||
| 376 | */ |
||
| 377 | public function check_for_dropdown_or_import() { |
||
| 401 | |||
| 402 | /** |
||
| 403 | * Print the Dropdown option for CSV. |
||
| 404 | * |
||
| 405 | * @since 1.8.14 |
||
| 406 | */ |
||
| 407 | public function render_dropdown() { |
||
| 454 | |||
| 455 | /** |
||
| 456 | * @param $option_value |
||
| 457 | * @param $value |
||
| 458 | * |
||
| 459 | * @return string |
||
| 460 | */ |
||
| 461 | public function selected( $option_value, $value ) { |
||
| 471 | |||
| 472 | /** |
||
| 473 | * Print the columns from the CSV. |
||
| 474 | * |
||
| 475 | * @since 1.8.14 |
||
| 476 | */ |
||
| 477 | public function get_columns( $index, $donations, $donors, $forms, $value = false, $mapto = array() ) { |
||
| 541 | |||
| 542 | /** |
||
| 543 | * Get column count of csv file. |
||
| 544 | * |
||
| 545 | * @since 1.8.14 |
||
| 546 | * |
||
| 547 | * @param $file_id |
||
| 548 | * |
||
| 549 | * @return bool|int |
||
| 550 | */ |
||
| 551 | public function get_csv_total( $file_id ) { |
||
| 564 | |||
| 565 | /** |
||
| 566 | * Get the CSV fields title from the CSV. |
||
| 567 | * |
||
| 568 | * @since 1.8.14 |
||
| 569 | * |
||
| 570 | * @param (int) $file_id |
||
| 571 | * @param int $index |
||
| 572 | * @param string $delimiter |
||
| 573 | * |
||
| 574 | * @return array|bool $raw_data title of the CSV file fields |
||
| 575 | */ |
||
| 576 | public function get_importer( $file_id, $index = 0, $delimiter = 'csv' ) { |
||
| 600 | |||
| 601 | /** |
||
| 602 | * Remove UTF-8 BOM signature. |
||
| 603 | * |
||
| 604 | * @since 1.8.14 |
||
| 605 | * |
||
| 606 | * @param string $string String to handle. |
||
| 607 | * |
||
| 608 | * @return string |
||
| 609 | */ |
||
| 610 | public function remove_utf8_bom( $string ) { |
||
| 617 | |||
| 618 | |||
| 619 | /** |
||
| 620 | * Is used to show the process when user upload the donor form. |
||
| 621 | * |
||
| 622 | * @since 1.8.14 |
||
| 623 | */ |
||
| 624 | public function render_progress() { |
||
| 643 | |||
| 644 | /** |
||
| 645 | * Will return the import step. |
||
| 646 | * |
||
| 647 | * @since 1.8.14 |
||
| 648 | * |
||
| 649 | * @return int $step on which step doest the import is on. |
||
| 650 | */ |
||
| 651 | public function get_step() { |
||
| 667 | |||
| 668 | /** |
||
| 669 | * Render donations import page |
||
| 670 | * |
||
| 671 | * @since 1.8.14 |
||
| 672 | */ |
||
| 673 | public function render_page() { |
||
| 676 | |||
| 677 | /** |
||
| 678 | * Add CSV upload HTMl |
||
| 679 | * |
||
| 680 | * Print the html of the file upload from which CSV will be uploaded. |
||
| 681 | * |
||
| 682 | * @since 1.8.14 |
||
| 683 | * @return void |
||
| 684 | */ |
||
| 685 | public function render_media_csv() { |
||
| 777 | |||
| 778 | /** |
||
| 779 | * Run when user click on the submit button. |
||
| 780 | * |
||
| 781 | * @since 1.8.14 |
||
| 782 | */ |
||
| 783 | public function save() { |
||
| 816 | |||
| 817 | /** |
||
| 818 | * Check if user uploaded csv is valid or not. |
||
| 819 | * |
||
| 820 | * @since 1.8.14 |
||
| 821 | * @access public |
||
| 822 | * |
||
| 823 | * @param int|bool $csv ID of the CSV files. |
||
| 824 | * @param string $match_url ID of the CSV files. |
||
| 825 | * |
||
| 826 | * @return bool $has_error CSV is valid or not. |
||
| 827 | */ |
||
| 828 | private function is_valid_csv( $csv = false, $match_url = '' ) { |
||
| 851 | |||
| 852 | |||
| 853 | /** |
||
| 854 | * Render report import field |
||
| 855 | * |
||
| 856 | * @since 1.8.14 |
||
| 857 | * @access public |
||
| 858 | * |
||
| 859 | * @param $field |
||
| 860 | * @param $option_value |
||
| 861 | */ |
||
| 862 | public function render_import_field( $field, $option_value ) { |
||
| 865 | |||
| 866 | /** |
||
| 867 | * Get if current page import donations page or not |
||
| 868 | * |
||
| 869 | * @since 1.8.14 |
||
| 870 | * @return bool |
||
| 871 | */ |
||
| 872 | private function is_donations_import_page() { |
||
| 877 | } |
||
| 878 | |||
| 881 |