| Conditions | 23 |
| Paths | 545 |
| Total Lines | 111 |
| Code Lines | 71 |
| Lines | 9 |
| Ratio | 8.11 % |
| Changes | 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 |
||
| 74 | function wpinv_convert_old_subscriptions() { |
||
| 75 | global $wpdb; |
||
| 76 | |||
| 77 | $query = "SELECT ". $wpdb->posts .".ID FROM ". $wpdb->posts ." INNER JOIN ". $wpdb->postmeta ." ON ( ". $wpdb->posts .".ID = ". $wpdb->postmeta .".post_id ) WHERE 1=1 AND ". $wpdb->postmeta .".meta_key = '_wpinv_subscr_status' AND (". $wpdb->postmeta .".meta_value = 'pending' OR ". $wpdb->postmeta .".meta_value = 'active' OR ". $wpdb->postmeta .".meta_value = 'cancelled' OR ". $wpdb->postmeta .".meta_value = 'completed' OR ". $wpdb->postmeta .".meta_value = 'expired' OR ". $wpdb->postmeta .".meta_value = 'trialling' OR ". $wpdb->postmeta .".meta_value = 'failing') AND ". $wpdb->posts .".post_type = 'wpi_invoice' GROUP BY ". $wpdb->posts .".ID ORDER BY ". $wpdb->posts .".ID ASC"; |
||
| 78 | |||
| 79 | $results = $wpdb->get_results( $query ); |
||
| 80 | |||
| 81 | if ( empty( $results ) ) { |
||
| 82 | return; |
||
| 83 | } |
||
| 84 | |||
| 85 | foreach ( $results as $row ) { |
||
| 86 | $invoice = new WPInv_Invoice( $row->ID ); |
||
| 87 | |||
| 88 | if ( empty( $invoice->ID ) ) { |
||
| 89 | continue; |
||
| 90 | } |
||
| 91 | |||
| 92 | if ( $invoice->has_status( 'wpi-renewal' ) ) { |
||
| 93 | continue; |
||
| 94 | } |
||
| 95 | |||
| 96 | $item = $invoice->get_recurring( true ); |
||
| 97 | |||
| 98 | if ( empty( $item ) ) { |
||
| 99 | continue; |
||
| 100 | } |
||
| 101 | |||
| 102 | $is_free_trial = $invoice->is_free_trial(); |
||
| 103 | $profile_id = get_post_meta( $invoice->ID, '_wpinv_subscr_profile_id', true ); |
||
| 104 | $subscription_status = get_post_meta( $invoice->ID, '_wpinv_subscr_status', true ); |
||
| 105 | $transaction_id = $invoice->get_transaction_id(); |
||
| 106 | |||
| 107 | // Last invoice |
||
| 108 | $query = "SELECT ID, post_date FROM ". $wpdb->posts ." WHERE post_type = 'wpi_invoice' AND post_parent = '" . $invoice->ID . "' ORDER BY ID DESC LIMIT 1"; |
||
| 109 | $last_payment = $wpdb->get_row( $query ); |
||
| 110 | |||
| 111 | if ( !empty( $last_payment ) ) { |
||
| 112 | $invoice_date = $last_payment->post_date; |
||
| 113 | |||
| 114 | $meta_profile_id = get_post_meta( $last_payment->ID, '_wpinv_subscr_profile_id', true ); |
||
| 115 | $meta_transaction_id = get_post_meta( $last_payment->ID, '_wpinv_transaction_id', true ); |
||
| 116 | |||
| 117 | if ( !empty( $meta_profile_id ) ) { |
||
| 118 | $profile_id = $meta_profile_id; |
||
| 119 | } |
||
| 120 | |||
| 121 | if ( !empty( $meta_transaction_id ) ) { |
||
| 122 | $transaction_id = $meta_transaction_id; |
||
| 123 | } |
||
| 124 | } else { |
||
| 125 | $invoice_date = $invoice->get_invoice_date( false ); |
||
| 126 | } |
||
| 127 | |||
| 128 | $profile_id = empty( $profile_id ) ? $invoice->ID : $profile_id; |
||
| 129 | $status = empty( $subscription_status ) ? 'pending' : $subscription_status; |
||
| 130 | |||
| 131 | $period = $item->get_recurring_period( true ); |
||
| 132 | $interval = $item->get_recurring_interval(); |
||
| 133 | $bill_times = (int)$item->get_recurring_limit(); |
||
| 134 | $add_period = $interval . ' ' . $period; |
||
| 135 | $trial_period = ''; |
||
| 136 | |||
| 137 | View Code Duplication | if ( $invoice->is_free_trial() ) { |
|
| 138 | $trial_period = $item->get_trial_period( true ); |
||
| 139 | $free_interval = $item->get_trial_interval(); |
||
| 140 | $trial_period = $free_interval . ' ' . $trial_period; |
||
| 141 | |||
| 142 | if ( empty( $last_payment ) ) { |
||
| 143 | $add_period = $trial_period; |
||
| 144 | } |
||
| 145 | } |
||
| 146 | |||
| 147 | $expiration = date_i18n( 'Y-m-d H:i:s', strtotime( '+' . $add_period . ' 23:59:59', strtotime( $invoice_date ) ) ); |
||
| 148 | if ( strtotime( $expiration ) < strtotime( date_i18n( 'Y-m-d' ) ) ) { |
||
| 149 | if ( $status == 'active' || $status == 'trialling' || $status == 'pending' ) { |
||
| 150 | $status = 'expired'; |
||
| 151 | } |
||
| 152 | } |
||
| 153 | |||
| 154 | $args = array( |
||
| 155 | 'product_id' => $item->ID, |
||
| 156 | 'customer_id' => $invoice->user_id, |
||
| 157 | 'parent_payment_id' => $invoice->ID, |
||
| 158 | 'status' => $status, |
||
| 159 | 'frequency' => $interval, |
||
| 160 | 'period' => $period, |
||
| 161 | 'initial_amount' => $invoice->get_total(), |
||
| 162 | 'recurring_amount' => $invoice->get_recurring_details( 'total' ), |
||
| 163 | 'bill_times' => $bill_times, |
||
| 164 | 'created' => $invoice_date, |
||
| 165 | 'expiration' => $expiration, |
||
| 166 | 'trial_period' => $trial_period, |
||
| 167 | 'profile_id' => $profile_id, |
||
| 168 | 'transaction_id' => $transaction_id, |
||
| 169 | ); |
||
| 170 | |||
| 171 | $subs_db = new WPInv_Subscriptions_DB; |
||
| 172 | $subs = $subs_db->get_subscriptions( array( 'parent_payment_id' => $invoice->ID, 'number' => 1 ) ); |
||
| 173 | $subscription = reset( $subs ); |
||
| 174 | |||
| 175 | if ( empty( $subscription ) || $subscription->id <= 0 ) { |
||
| 176 | $subscription = new WPInv_Subscription(); |
||
| 177 | $new_sub = $subscription->create( $args ); |
||
| 178 | |||
| 179 | if ( !empty( $bill_times ) && $new_sub->get_times_billed() >= $bill_times && ( 'active' == $new_sub->status || 'trialling' == $new_sub->status ) ) { |
||
| 180 | $new_sub->complete(); // Mark completed if all times billed |
||
| 181 | } |
||
| 182 | } |
||
| 183 | } |
||
| 184 | } |
||
| 185 | |||
| 219 | } |
If you suppress an error, we recommend checking for the error condition explicitly: