| Conditions | 17 |
| Paths | 10 |
| Total Lines | 65 |
| Code Lines | 46 |
| 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 |
||
| 118 | protected function set_items( $invoice, $data ) { |
||
| 119 | if ( !empty( $data['items'] ) && is_array( $data['items'] ) ) { |
||
| 120 | $items_array = array(); |
||
| 121 | |||
| 122 | if ( !empty( $invoice->country ) ) { |
||
| 123 | $country = $invoice->country; |
||
| 124 | } else if ( !empty( $data['billing_details']['country'] ) ) { |
||
| 125 | $country = $data['billing_details']['country']; |
||
| 126 | } else { |
||
| 127 | $country = wpinv_default_billing_country( '', $invoice->get_user_id() ); |
||
| 128 | } |
||
| 129 | |||
| 130 | if ( !empty( $invoice->state ) ) { |
||
| 131 | $state = $invoice->state; |
||
| 132 | } else if ( !empty( $data['billing_details']['state'] ) ) { |
||
| 133 | $state = $data['billing_details']['state']; |
||
| 134 | } else { |
||
| 135 | $state = wpinv_get_default_state(); |
||
| 136 | } |
||
| 137 | |||
| 138 | $_POST['country'] = $country; |
||
| 139 | $_POST['state'] = $state; |
||
| 140 | |||
| 141 | $rate = wpinv_get_tax_rate( $country, $state, 'global' ); |
||
| 142 | |||
| 143 | $total_tax = 0; |
||
| 144 | foreach ( $data['items'] as $item ) { |
||
| 145 | $id = isset( $item['id'] ) ? sanitize_text_field( $item['id'] ) : ''; |
||
| 146 | $title = isset( $item['title'] ) ? sanitize_text_field( $item['title'] ) : ''; |
||
| 147 | $desc = isset( $item['description'] ) ? sanitize_text_field( $item['description'] ) : ''; |
||
| 148 | $amount = isset( $item['amount'] ) ? wpinv_round_amount( $item['amount'] ) : 0; |
||
| 149 | |||
| 150 | if ( !empty( $item['vat_rates_class'] ) ) { |
||
| 151 | $vat_rates_class = $item['vat_rates_class']; |
||
| 152 | } else { |
||
| 153 | $vat_rates_class = '_standard'; |
||
| 154 | } |
||
| 155 | $vat_rate = wpinv_get_tax_rate( $country, $state, $id ); |
||
| 156 | |||
| 157 | $tax = $amount > 0 ? ( $amount * 0.01 * (float)$vat_rate ) : 0; |
||
| 158 | $total_tax += $tax; |
||
| 159 | |||
| 160 | $items_array[] = array( |
||
| 161 | 'id' => $id, |
||
| 162 | 'title' => esc_html( $title ), |
||
| 163 | 'description' => esc_html( $desc ), |
||
| 164 | 'amount' => $amount > 0 ? wpinv_round_amount( $amount ) : 0, |
||
| 165 | 'subtotal' => $amount > 0 ? wpinv_round_amount( $amount ) : 0, |
||
| 166 | 'vat_rates_class' => $vat_rates_class, |
||
| 167 | 'vat_rate' => $vat_rate, |
||
| 168 | 'tax' => $tax > 0 ? wpinv_round_amount( $tax ) : 0, |
||
| 169 | ); |
||
| 170 | } |
||
| 171 | |||
| 172 | update_post_meta( $invoice->ID, '_wpinv_tax', wpinv_round_amount( $total_tax ) ); |
||
| 173 | $invoice->set( 'tax', wpinv_round_amount( $total_tax ) ); |
||
| 174 | |||
| 175 | $items_array = apply_filters( 'wpinv_save_invoice_items', $items_array, $data['items'], $invoice ); |
||
| 176 | |||
| 177 | $invoice->set( 'items', $items_array ); |
||
| 178 | update_post_meta( $invoice->ID, '_wpinv_items', $items_array ); |
||
| 179 | } |
||
| 180 | |||
| 181 | return $invoice; |
||
| 182 | } |
||
| 183 | |||
| 206 | } |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.