| Conditions | 35 |
| Paths | 60 |
| Total Lines | 90 |
| Code Lines | 50 |
| 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 |
||
| 280 | public static function save( $post_id, $posted ) { |
||
| 281 | |||
| 282 | // Prepare the invoice. |
||
| 283 | $invoice = new WPInv_Invoice( $post_id ); |
||
| 284 | |||
| 285 | // Load new data. |
||
| 286 | $invoice->set_props( |
||
| 287 | array( |
||
| 288 | 'template' => isset( $posted['wpinv_template'] ) ? wpinv_clean( $posted['wpinv_template'] ) : null, |
||
| 289 | 'email_cc' => isset( $posted['wpinv_cc'] ) ? wpinv_clean( $posted['wpinv_cc'] ) : null, |
||
| 290 | 'disable_taxes' => ! empty( $posted['disable_taxes'] ), |
||
| 291 | 'currency' => isset( $posted['wpinv_currency'] ) ? wpinv_clean( $posted['wpinv_currency'] ) : null, |
||
| 292 | 'gateway' => ( $invoice->needs_payment() && isset( $posted['wpinv_gateway'] ) ) ? wpinv_clean( $posted['wpinv_gateway'] ) : null, |
||
| 293 | 'address' => isset( $posted['wpinv_address'] ) ? wpinv_clean( $posted['wpinv_address'] ) : null, |
||
| 294 | 'vat_number' => isset( $posted['wpinv_vat_number'] ) ? wpinv_clean( $posted['wpinv_vat_number'] ) : null, |
||
| 295 | 'company' => isset( $posted['wpinv_company'] ) ? wpinv_clean( $posted['wpinv_company'] ) : null, |
||
| 296 | 'company_id' => isset( $posted['wpinv_company_id'] ) ? wpinv_clean( $posted['wpinv_company_id'] ) : null, |
||
| 297 | 'zip' => isset( $posted['wpinv_zip'] ) ? wpinv_clean( $posted['wpinv_zip'] ) : null, |
||
| 298 | 'state' => isset( $posted['wpinv_state'] ) ? wpinv_clean( $posted['wpinv_state'] ) : null, |
||
| 299 | 'city' => isset( $posted['wpinv_city'] ) ? wpinv_clean( $posted['wpinv_city'] ) : null, |
||
| 300 | 'country' => isset( $posted['wpinv_country'] ) ? wpinv_clean( $posted['wpinv_country'] ) : null, |
||
| 301 | 'phone' => isset( $posted['wpinv_phone'] ) ? wpinv_clean( $posted['wpinv_phone'] ) : null, |
||
| 302 | 'first_name' => isset( $posted['wpinv_first_name'] ) ? wpinv_clean( $posted['wpinv_first_name'] ) : null, |
||
| 303 | 'last_name' => isset( $posted['wpinv_last_name'] ) ? wpinv_clean( $posted['wpinv_last_name'] ) : null, |
||
| 304 | 'author' => isset( $posted['post_author_override'] ) ? wpinv_clean( $posted['post_author_override'] ) : null, |
||
| 305 | 'date_created' => isset( $posted['date_created'] ) ? wpinv_clean( $posted['date_created'] ) : null, |
||
| 306 | 'date_completed' => isset( $posted['wpinv_date_completed'] ) ? wpinv_clean( $posted['wpinv_date_completed'] ) : null, |
||
| 307 | 'due_date' => isset( $posted['wpinv_due_date'] ) ? wpinv_clean( $posted['wpinv_due_date'] ) : null, |
||
| 308 | 'number' => isset( $posted['wpinv_number'] ) ? wpinv_clean( $posted['wpinv_number'] ) : null, |
||
| 309 | 'status' => isset( $posted['wpinv_status'] ) ? wpinv_clean( $posted['wpinv_status'] ) : null, |
||
| 310 | ) |
||
| 311 | ); |
||
| 312 | |||
| 313 | // Discount code. |
||
| 314 | if ( ! $invoice->is_paid() && ! $invoice->is_refunded() ) { |
||
| 315 | |||
| 316 | if ( isset( $posted['wpinv_discount_code'] ) ) { |
||
| 317 | $invoice->set_discount_code( wpinv_clean( $posted['wpinv_discount_code'] ) ); |
||
|
|
|||
| 318 | } |
||
| 319 | |||
| 320 | $discount = new WPInv_Discount( $invoice->get_discount_code() ); |
||
| 321 | if ( $discount->exists() ) { |
||
| 322 | $invoice->add_discount( getpaid_calculate_invoice_discount( $invoice, $discount ) ); |
||
| 323 | } else { |
||
| 324 | $invoice->remove_discount( 'discount_code' ); |
||
| 325 | } |
||
| 326 | |||
| 327 | // Recalculate totals. |
||
| 328 | $invoice->recalculate_total(); |
||
| 329 | |||
| 330 | } |
||
| 331 | |||
| 332 | // If we're creating a new user... |
||
| 333 | if ( ! empty( $posted['wpinv_new_user'] ) && is_email( stripslashes( $posted['wpinv_email'] ) ) ) { |
||
| 334 | |||
| 335 | // Attempt to create the user. |
||
| 336 | $user = wpinv_create_user( sanitize_email( stripslashes( $posted['wpinv_email'] ) ), $invoice->get_first_name() . $invoice->get_last_name() ); |
||
| 337 | |||
| 338 | // If successful, update the invoice author. |
||
| 339 | if ( is_numeric( $user ) ) { |
||
| 340 | $invoice->set_author( $user ); |
||
| 341 | } else { |
||
| 342 | wpinv_error_log( $user->get_error_message(), __( 'Invoice add new user', 'invoicing' ), __FILE__, __LINE__ ); |
||
| 343 | } |
||
| 344 | } |
||
| 345 | |||
| 346 | // Do not send new invoice notifications. |
||
| 347 | $GLOBALS['wpinv_skip_invoice_notification'] = true; |
||
| 348 | |||
| 349 | // Save the invoice. |
||
| 350 | $invoice->save(); |
||
| 351 | |||
| 352 | // Save the user address. |
||
| 353 | getpaid_save_invoice_user_address( $invoice ); |
||
| 354 | |||
| 355 | // Undo do not send new invoice notifications. |
||
| 356 | $GLOBALS['wpinv_skip_invoice_notification'] = false; |
||
| 357 | |||
| 358 | // (Maybe) send new user notification. |
||
| 359 | $should_send_notification = wpinv_get_option( 'disable_new_user_emails' ); |
||
| 360 | if ( ! empty( $user ) && is_numeric( $user ) && apply_filters( 'getpaid_send_new_user_notification', empty( $should_send_notification ) ) ) { |
||
| 361 | wp_send_new_user_notifications( $user, 'user' ); |
||
| 362 | } |
||
| 363 | |||
| 364 | if ( ! empty( $posted['send_to_customer'] ) && ! $invoice->is_draft() ) { |
||
| 365 | getpaid()->get( 'invoice_emails' )->user_invoice( $invoice, true ); |
||
| 366 | } |
||
| 367 | |||
| 368 | // Fires after an invoice is saved. |
||
| 369 | do_action( 'wpinv_invoice_metabox_saved', $invoice ); |
||
| 370 | } |
||
| 372 |