| Conditions | 4 |
| Paths | 4 |
| Total Lines | 55 |
| Code Lines | 34 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
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 |
||
| 54 | public function save_profile( Profile $profile, $data = array(), $format = array() ) { |
||
| 55 | global $wpdb; |
||
| 56 | |||
| 57 | $id = $wpdb->get_var( $wpdb->prepare( "SELECT id FROM $wpdb->pronamic_pay_mollie_profiles WHERE mollie_id = %s", $profile->get_id() ) ); |
||
| 58 | |||
| 59 | $data['email'] = $profile->email; |
||
|
|
|||
| 60 | $format['email'] = '%s'; |
||
| 61 | |||
| 62 | $data['name'] = $profile->name; |
||
| 63 | $format['name'] = '%s'; |
||
| 64 | |||
| 65 | if ( null === $id ) { |
||
| 66 | $data['mollie_id'] = $profile->get_id(); |
||
| 67 | $foramt['mollie_id'] = '%s'; |
||
| 68 | |||
| 69 | $result = $wpdb->insert( |
||
| 70 | $wpdb->pronamic_pay_mollie_profiles, |
||
| 71 | $data, |
||
| 72 | $format |
||
| 73 | ); |
||
| 74 | |||
| 75 | if ( false === $result ) { |
||
| 76 | \WP_CLI::error( |
||
| 77 | sprintf( |
||
| 78 | 'Database error: %s.', |
||
| 79 | $wpdb->last_error |
||
| 80 | ) |
||
| 81 | ); |
||
| 82 | } |
||
| 83 | |||
| 84 | $id = $wpdb->insert_id; |
||
| 85 | } else { |
||
| 86 | $result = $wpdb->update( |
||
| 87 | $wpdb->pronamic_pay_mollie_profiles, |
||
| 88 | $data, |
||
| 89 | array( |
||
| 90 | 'id' => $id, |
||
| 91 | ), |
||
| 92 | $format, |
||
| 93 | array( |
||
| 94 | 'id' => '%d', |
||
| 95 | ) |
||
| 96 | ); |
||
| 97 | |||
| 98 | if ( false === $result ) { |
||
| 99 | \WP_CLI::error( |
||
| 100 | sprintf( |
||
| 101 | 'Database error: %s.', |
||
| 102 | $wpdb->last_error |
||
| 103 | ) |
||
| 104 | ); |
||
| 105 | } |
||
| 106 | } |
||
| 107 | |||
| 108 | return $id; |
||
| 109 | } |
||
| 111 |