| Conditions | 18 |
| Paths | 21 |
| Total Lines | 73 |
| Code Lines | 39 |
| 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 |
||
| 26 | public function __construct( $submission ) { |
||
| 27 | |||
| 28 | $data = $submission->get_data(); |
||
| 29 | $payment_form = $submission->get_payment_form(); |
||
| 30 | $invoice = $submission->get_invoice(); |
||
| 31 | $force_prices = array(); |
||
| 32 | |||
| 33 | // Prepare the selected items. |
||
| 34 | $selected_items = array(); |
||
| 35 | if ( ! empty( $data['getpaid-items'] ) ) { |
||
| 36 | $selected_items = wpinv_clean( $data['getpaid-items'] ); |
||
| 37 | |||
| 38 | if ( ! empty( $invoice ) && $submission->is_initial_fetch() ) { |
||
| 39 | foreach ( $invoice->get_items() as $invoice_item ) { |
||
| 40 | if ( isset( $selected_items[ $invoice_item->get_id() ] ) ) { |
||
| 41 | $selected_items[ $invoice_item->get_id() ]['quantity'] = $invoice_item->get_quantity(); |
||
| 42 | $selected_items[ $invoice_item->get_id() ]['price'] = $invoice_item->get_price(); |
||
| 43 | |||
| 44 | $force_prices[ $invoice_item->get_id() ] = $invoice_item->get_price(); |
||
| 45 | } |
||
| 46 | } |
||
| 47 | } |
||
| 48 | } |
||
| 49 | |||
| 50 | // (Maybe) set form items. |
||
| 51 | if ( isset( $data['getpaid-form-items'] ) ) { |
||
| 52 | |||
| 53 | // Confirm items key. |
||
| 54 | $form_items = wpinv_clean( $data['getpaid-form-items'] ); |
||
| 55 | if ( ! isset( $data['getpaid-form-items-key'] ) || md5( NONCE_KEY . AUTH_KEY . $form_items ) !== $data['getpaid-form-items-key'] ) { |
||
|
|
|||
| 56 | throw new Exception( __( 'We could not validate the form items. Please reload the page and try again.', 'invoicing' ) ); |
||
| 57 | } |
||
| 58 | |||
| 59 | $items = array(); |
||
| 60 | $item_ids = array(); |
||
| 61 | |||
| 62 | foreach ( getpaid_convert_items_to_array( $form_items ) as $item_id => $qty ) { |
||
| 63 | if ( ! in_array( $item_id, $item_ids ) ) { |
||
| 64 | $item = new GetPaid_Form_Item( $item_id ); |
||
| 65 | $item->set_quantity( $qty ); |
||
| 66 | |||
| 67 | if ( empty( $qty ) ) { |
||
| 68 | $item->set_allow_quantities( true ); |
||
| 69 | $item->set_is_required( false ); |
||
| 70 | } |
||
| 71 | |||
| 72 | if ( ! $item->user_can_set_their_price() && isset( $force_prices[ $item_id ] ) ) { |
||
| 73 | $item->set_is_dynamic_pricing( true ); |
||
| 74 | $item->set_minimum_price( 0 ); |
||
| 75 | } |
||
| 76 | |||
| 77 | $item_ids[] = $item->get_id(); |
||
| 78 | $items[] = $item; |
||
| 79 | } |
||
| 80 | } |
||
| 81 | |||
| 82 | if ( ! $payment_form->is_default() ) { |
||
| 83 | |||
| 84 | foreach ( $payment_form->get_items() as $item ) { |
||
| 85 | if ( ! in_array( $item->get_id(), $item_ids ) ) { |
||
| 86 | $item_ids[] = $item->get_id(); |
||
| 87 | $items[] = $item; |
||
| 88 | } |
||
| 89 | } |
||
| 90 | } |
||
| 91 | |||
| 92 | $payment_form->set_items( $items ); |
||
| 93 | |||
| 94 | } |
||
| 95 | |||
| 96 | // Process each individual item. |
||
| 97 | foreach ( $payment_form->get_items() as $item ) { |
||
| 98 | $this->process_item( $item, $selected_items, $submission ); |
||
| 99 | } |
||
| 148 |