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 = 25; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Singleton pattern. |
||
| 55 | * |
||
| 56 | * @since |
||
| 57 | * @access private |
||
| 58 | */ |
||
| 59 | private function __construct() { |
||
| 60 | self::$per_page = ! empty( $_GET['per_page'] ) ? absint( $_GET['per_page'] ) : self::$per_page; |
||
|
|
|||
| 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 | View Code Duplication | 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 | View Code Duplication | public function update_notices( $messages ) { |
|
| 132 | if ( ! empty( $_GET['tab'] ) && 'import' === give_clean( $_GET['tab'] ) ) { |
||
| 133 | unset( $messages['give-setting-updated'] ); |
||
| 134 | } |
||
| 135 | |||
| 136 | return $messages; |
||
| 137 | } |
||
| 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() { |
||
| 451 | |||
| 452 | /** |
||
| 453 | * @param $option_value |
||
| 454 | * @param $value |
||
| 455 | * |
||
| 456 | * @return string |
||
| 457 | */ |
||
| 458 | public function selected( $option_value, $value ) { |
||
| 471 | |||
| 472 | /** |
||
| 473 | * Print the columns from the CSV. |
||
| 474 | * |
||
| 475 | * @since 1.8.14 |
||
| 476 | * @access private |
||
| 477 | * |
||
| 478 | * @param string $index |
||
| 479 | * @param bool $value |
||
| 480 | * @param array $mapto |
||
| 481 | * |
||
| 482 | * @return void |
||
| 483 | */ |
||
| 484 | private function get_columns( $index, $value = false, $mapto = array() ) { |
||
| 521 | |||
| 522 | /** |
||
| 523 | * Print the option html for select in importer |
||
| 524 | * |
||
| 525 | * @since 1.8.15 |
||
| 526 | * @access public |
||
| 527 | * |
||
| 528 | * @param array $options |
||
| 529 | * @param string $current_mapto |
||
| 530 | * @param bool $value |
||
| 531 | * |
||
| 532 | * @return void |
||
| 533 | */ |
||
| 534 | public function get_dropdown_option_html( $options, $current_mapto, $value = false ) { |
||
| 557 | |||
| 558 | /** |
||
| 559 | * Get column count of csv file. |
||
| 560 | * |
||
| 561 | * @since 1.8.14 |
||
| 562 | * |
||
| 563 | * @param $file_id |
||
| 564 | * |
||
| 565 | * @return bool|int |
||
| 566 | */ |
||
| 567 | public function get_csv_total( $file_id ) { |
||
| 580 | |||
| 581 | /** |
||
| 582 | * Get the CSV fields title from the CSV. |
||
| 583 | * |
||
| 584 | * @since 1.8.14 |
||
| 585 | * |
||
| 586 | * @param (int) $file_id |
||
| 587 | * @param int $index |
||
| 588 | * @param string $delimiter |
||
| 589 | * |
||
| 590 | * @return array|bool $raw_data title of the CSV file fields |
||
| 591 | */ |
||
| 592 | public function get_importer( $file_id, $index = 0, $delimiter = 'csv' ) { |
||
| 616 | |||
| 617 | /** |
||
| 618 | * Remove UTF-8 BOM signature. |
||
| 619 | * |
||
| 620 | * @since 1.8.14 |
||
| 621 | * |
||
| 622 | * @param string $string String to handle. |
||
| 623 | * |
||
| 624 | * @return string |
||
| 625 | */ |
||
| 626 | public function remove_utf8_bom( $string ) { |
||
| 633 | |||
| 634 | |||
| 635 | /** |
||
| 636 | * Is used to show the process when user upload the donor form. |
||
| 637 | * |
||
| 638 | * @since 1.8.14 |
||
| 639 | */ |
||
| 640 | public function render_progress() { |
||
| 659 | |||
| 660 | /** |
||
| 661 | * Will return the import step. |
||
| 662 | * |
||
| 663 | * @since 1.8.14 |
||
| 664 | * |
||
| 665 | * @return int $step on which step doest the import is on. |
||
| 666 | */ |
||
| 667 | View Code Duplication | public function get_step() { |
|
| 668 | $step = (int) ( isset( $_REQUEST['step'] ) ? give_clean( $_REQUEST['step'] ) : 0 ); |
||
| 669 | $on_step = 1; |
||
| 670 | |||
| 671 | if ( empty( $step ) || 1 === $step ) { |
||
| 672 | $on_step = 1; |
||
| 673 | } elseif ( $this->check_for_dropdown_or_import() ) { |
||
| 674 | $on_step = 3; |
||
| 675 | } elseif ( 2 === $step ) { |
||
| 676 | $on_step = 2; |
||
| 677 | } elseif ( 4 === $step ) { |
||
| 678 | $on_step = 4; |
||
| 679 | } |
||
| 680 | |||
| 681 | return $on_step; |
||
| 682 | } |
||
| 683 | |||
| 684 | /** |
||
| 685 | * Render donations import page |
||
| 686 | * |
||
| 687 | * @since 1.8.14 |
||
| 688 | */ |
||
| 689 | public function render_page() { |
||
| 692 | |||
| 693 | /** |
||
| 694 | * Add CSV upload HTMl |
||
| 695 | * |
||
| 696 | * Print the html of the file upload from which CSV will be uploaded. |
||
| 697 | * |
||
| 698 | * @since 1.8.14 |
||
| 699 | * @return void |
||
| 700 | */ |
||
| 701 | public function render_media_csv() { |
||
| 802 | |||
| 803 | /** |
||
| 804 | * Run when user click on the submit button. |
||
| 805 | * |
||
| 806 | * @since 1.8.14 |
||
| 807 | */ |
||
| 808 | public function save() { |
||
| 842 | |||
| 843 | /** |
||
| 844 | * Check if user uploaded csv is valid or not. |
||
| 845 | * |
||
| 846 | * @since 1.8.14 |
||
| 847 | * @access public |
||
| 848 | * |
||
| 849 | * @param int|bool $csv ID of the CSV files. |
||
| 850 | * @param string $match_url ID of the CSV files. |
||
| 851 | * |
||
| 852 | * @return bool $has_error CSV is valid or not. |
||
| 853 | */ |
||
| 854 | private function is_valid_csv( $csv = false, $match_url = '' ) { |
||
| 877 | |||
| 878 | |||
| 879 | /** |
||
| 880 | * Render report import field |
||
| 881 | * |
||
| 882 | * @since 1.8.14 |
||
| 883 | * @access public |
||
| 884 | * |
||
| 885 | * @param $field |
||
| 886 | * @param $option_value |
||
| 887 | */ |
||
| 888 | public function render_import_field( $field, $option_value ) { |
||
| 891 | |||
| 892 | /** |
||
| 893 | * Get if current page import donations page or not |
||
| 894 | * |
||
| 895 | * @since 1.8.14 |
||
| 896 | * @return bool |
||
| 897 | */ |
||
| 898 | private function is_donations_import_page() { |
||
| 903 | } |
||
| 904 | |||
| 907 |