| Conditions | 14 |
| Paths | 32 |
| Total Lines | 70 |
| Code Lines | 48 |
| Lines | 0 |
| Ratio | 0 % |
| 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 |
||
| 72 | function convert_old_subscriptions(){ |
||
| 73 | |||
| 74 | global $wpdb; |
||
| 75 | |||
| 76 | $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 = 'trialing' OR ". $wpdb->postmeta .".meta_value = 'failing') AND ". $wpdb->posts .".post_type = 'wpi_invoice' GROUP BY ". $wpdb->posts .".ID ORDER BY ". $wpdb->posts .".post_date ASC"; |
||
| 77 | |||
| 78 | $results = $wpdb->get_results($query); |
||
| 79 | |||
| 80 | foreach ( $results as $row ) { |
||
| 81 | |||
| 82 | $invoice = new WPInv_Invoice($row->ID); |
||
| 83 | |||
| 84 | $item_id = $invoice->get_meta( '_wpinv_item_ids', true ); |
||
| 85 | $item = new WPInv_Item( $item_id ); |
||
| 86 | |||
| 87 | if($invoice->has_status('wpi-renewal') || !$item->is_recurring()){ |
||
| 88 | continue; |
||
| 89 | } |
||
| 90 | |||
| 91 | $period = $item->get_recurring_period(true); |
||
| 92 | $interval = $item->get_recurring_interval(); |
||
| 93 | $bill_times = (int)$item->get_recurring_limit(); |
||
| 94 | $initial_amount = wpinv_sanitize_amount( $invoice->get_total(), 2 ); |
||
| 95 | $recurring_amount = wpinv_sanitize_amount( $invoice->get_recurring_details( 'total' ), 2 ); |
||
| 96 | $subscription_status = $invoice->get_meta( '_wpinv_subscr_status', true ); |
||
| 97 | $status = empty($subscription_status) ? 'pending' : $subscription_status; |
||
| 98 | $expiration = date( 'Y-m-d H:i:s', strtotime( '+' . $interval . ' ' . $period . ' 23:59:59', strtotime($invoice->date) ) ); |
||
| 99 | |||
| 100 | $trial_period = ''; |
||
| 101 | if ( $invoice->is_free_trial() && $item->has_free_trial() ) { |
||
| 102 | $trial_period = $item->get_trial_period(true); |
||
| 103 | $free_trial = $item->get_free_trial(); |
||
| 104 | $trial_period = ! empty( $invoice->is_free_trial() ) ? $free_trial . ' ' . $trial_period : ''; |
||
| 105 | $expiration = date( 'Y-m-d H:i:s', strtotime( '+' . $trial_period . ' 23:59:59', strtotime($invoice->date) ) ); |
||
| 106 | $status = "trialing" === $status ? 'trialling' : $status; |
||
| 107 | } |
||
| 108 | |||
| 109 | $args = array( |
||
| 110 | 'product_id' => $item_id, |
||
| 111 | 'customer_id' => $invoice->user_id, |
||
| 112 | 'parent_payment_id' => $invoice->ID, |
||
| 113 | 'status' => $status, |
||
| 114 | 'frequency' => $interval, |
||
| 115 | 'period' => $period, |
||
| 116 | 'initial_amount' => $initial_amount, |
||
| 117 | 'recurring_amount' => $recurring_amount, |
||
| 118 | 'bill_times' => $bill_times, |
||
| 119 | 'created' => date( 'Y-m-d H:i:s', strtotime($invoice->date) ), |
||
| 120 | 'expiration' => $expiration, |
||
| 121 | 'trial_period' => $trial_period, |
||
| 122 | 'profile_id' => $invoice->ID, |
||
| 123 | 'transaction_id' => $invoice->get_transaction_id(), |
||
| 124 | ); |
||
| 125 | |||
| 126 | $subs_db = new WPInv_Subscriptions_DB; |
||
| 127 | $subs = $subs_db->get_subscriptions( array( 'parent_payment_id' => $invoice->ID, 'number' => 1 ) ); |
||
| 128 | $subscription = reset( $subs ); |
||
| 129 | |||
| 130 | if( !$subscription || $subscription->id <= 0 ) { |
||
| 131 | |||
| 132 | $subscription = new WPInv_Subscription(); |
||
| 133 | $new_sub = $subscription->create( $args ); |
||
| 134 | |||
| 135 | if($new_sub->get_times_billed() >= $bill_times && 'active' == $new_sub->status || 'trialling' == $new_sub->status){ |
||
| 136 | $new_sub->complete(); // Mark completed if all times billed |
||
| 137 | } |
||
| 138 | } |
||
| 139 | |||
| 140 | } |
||
| 141 | } |
If you suppress an error, we recommend checking for the error condition explicitly: