| Conditions | 17 |
| Paths | 10 |
| Total Lines | 65 |
| 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 |
||
| 177 | protected function set_items( $invoice, $data ) { |
||
| 178 | if ( !empty( $data['items'] ) && is_array( $data['items'] ) ) { |
||
| 179 | $items_array = array(); |
||
| 180 | |||
| 181 | if ( !empty( $invoice->country ) ) { |
||
| 182 | $country = $invoice->country; |
||
| 183 | } else if ( !empty( $data['billing_details']['country'] ) ) { |
||
| 184 | $country = $data['billing_details']['country']; |
||
| 185 | } else { |
||
| 186 | $country = wpinv_default_billing_country( '', $invoice->get_user_id() ); |
||
| 187 | } |
||
| 188 | |||
| 189 | if ( !empty( $invoice->state ) ) { |
||
| 190 | $state = $invoice->state; |
||
| 191 | } else if ( !empty( $data['billing_details']['state'] ) ) { |
||
| 192 | $state = $data['billing_details']['state']; |
||
| 193 | } else { |
||
| 194 | $state = wpinv_get_default_state(); |
||
| 195 | } |
||
| 196 | |||
| 197 | $_POST['country'] = $country; |
||
| 198 | $_POST['state'] = $state; |
||
| 199 | |||
| 200 | $rate = wpinv_get_tax_rate( $country, $state, 'global' ); |
||
| 201 | |||
| 202 | $total_tax = 0; |
||
| 203 | foreach ( $data['items'] as $item ) { |
||
| 204 | $id = isset( $item['id'] ) ? sanitize_text_field( $item['id'] ) : ''; |
||
| 205 | $title = isset( $item['title'] ) ? sanitize_text_field( $item['title'] ) : ''; |
||
| 206 | $desc = isset( $item['description'] ) ? sanitize_text_field( $item['description'] ) : ''; |
||
| 207 | $amount = isset( $item['amount'] ) ? wpinv_round_amount( $item['amount'] ) : 0; |
||
| 208 | |||
| 209 | if ( !empty( $item['vat_rates_class'] ) ) { |
||
| 210 | $vat_rates_class = $item['vat_rates_class']; |
||
| 211 | } else { |
||
| 212 | $vat_rates_class = '_standard'; |
||
| 213 | } |
||
| 214 | $vat_rate = wpinv_get_tax_rate( $country, $state, $id ); |
||
| 215 | |||
| 216 | $tax = $amount > 0 ? ( $amount * 0.01 * (float)$vat_rate ) : 0; |
||
| 217 | $total_tax += $tax; |
||
| 218 | |||
| 219 | $items_array[] = array( |
||
| 220 | 'id' => $id, |
||
| 221 | 'title' => esc_html( $title ), |
||
| 222 | 'description' => esc_html( $desc ), |
||
| 223 | 'amount' => $amount > 0 ? wpinv_round_amount( $amount ) : 0, |
||
| 224 | 'subtotal' => $amount > 0 ? wpinv_round_amount( $amount ) : 0, |
||
| 225 | 'vat_rates_class' => $vat_rates_class, |
||
| 226 | 'vat_rate' => $vat_rate, |
||
| 227 | 'tax' => $tax > 0 ? wpinv_round_amount( $tax ) : 0, |
||
| 228 | ); |
||
| 229 | } |
||
| 230 | |||
| 231 | update_post_meta( $invoice->ID, '_wpinv_tax', wpinv_round_amount( $total_tax ) ); |
||
| 232 | $invoice->set( 'tax', wpinv_round_amount( $total_tax ) ); |
||
| 233 | |||
| 234 | $items_array = apply_filters( 'wpinv_save_invoice_items', $items_array, $data['items'], $invoice ); |
||
| 235 | |||
| 236 | $invoice->set( 'items', $items_array ); |
||
| 237 | update_post_meta( $invoice->ID, '_wpinv_items', $items_array ); |
||
| 238 | } |
||
| 239 | |||
| 240 | return $invoice; |
||
| 241 | } |
||
| 242 | |||
| 265 | } |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.