| Conditions | 9 |
| Paths | 28 |
| Total Lines | 63 |
| Code Lines | 35 |
| Lines | 19 |
| Ratio | 30.16 % |
| Changes | 3 | ||
| Bugs | 0 | Features | 1 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 63 | function wpbo_failsafe_add_subscriber( $data = array(), $wp_error = true ) { |
||
| 64 | |||
| 65 | global $wpdb; |
||
| 66 | |||
| 67 | $table_name = wpbo_failsafe_table; |
||
| 68 | |||
| 69 | $defaults = array( |
||
| 70 | 'ID' => false, |
||
| 71 | 'conversion_id' => 0, |
||
| 72 | 'time' => '', |
||
| 73 | 'first_name' => '', |
||
| 74 | 'last_name' => '', |
||
| 75 | 'email' => '', |
||
| 76 | 'provider' => '', |
||
| 77 | 'status' => '', |
||
| 78 | ); |
||
| 79 | |||
| 80 | $data = array_merge( $defaults, $data ); |
||
| 81 | |||
| 82 | View Code Duplication | if ( empty( $data['time'] ) || '0000-00-00 00:00:00' == $data['time'] ) { |
|
| 83 | $data['time'] = current_time( 'mysql' ); |
||
| 84 | } |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Validate the date |
||
| 88 | */ |
||
| 89 | $valid_date = wpbo_check_date( $data['time'] ); |
||
| 90 | |||
| 91 | View Code Duplication | if ( ! $valid_date ) { |
|
| 92 | if ( $wp_error ) { |
||
| 93 | return new WP_Error( 'invalid_date', __( 'Whoops, the provided date is invalid.' ) ); |
||
| 94 | } else { |
||
| 95 | return false; |
||
| 96 | } |
||
| 97 | } |
||
| 98 | |||
| 99 | // Make sure we have a provider |
||
| 100 | if ( empty( $data['provider'] ) ) { |
||
| 101 | $provider = str_replace( ' ', '', ucwords( str_replace( array( '-', '_' ), ' ', sanitize_text_field( wpbo_get_option( 'mailing_provider', '' ) ) ) ) ); |
||
| 102 | $data['provider'] = $provider; |
||
| 103 | } |
||
| 104 | |||
| 105 | // Set the status as failed by default |
||
| 106 | if ( empty( $data['status'] ) ) { |
||
| 107 | $data['status'] = 'failed'; |
||
| 108 | } |
||
| 109 | |||
| 110 | /* Sanitize all data values */ |
||
| 111 | $data = array_map( 'sanitize_text_field', $data ); |
||
| 112 | |||
| 113 | $insert = $wpdb->insert( $table_name, $data, array( '%s', '%d', '%d', '%s', '%s', '%s', '%s', '%s' ) ); |
||
| 114 | |||
| 115 | View Code Duplication | if ( false === $insert ) { |
|
| 116 | if ( $wp_error ) { |
||
| 117 | return new WP_Error( 'insert_failed', __( 'Whoops, we could not insert the data in the database.' ) ); |
||
| 118 | } else { |
||
| 119 | return false; |
||
| 120 | } |
||
| 121 | } else { |
||
| 122 | return $wpdb->insert_id; |
||
| 123 | } |
||
| 124 | |||
| 125 | } |
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.