| Conditions | 22 |
| Paths | 35 |
| Total Lines | 86 |
| 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 |
||
| 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 ( is_array( $submission->get_field( 'getpaid-variable-items' ) ) ) { |
||
| 39 | $selected_prices = $submission->get_field( 'getpaid-variable-items' ); |
||
| 40 | |||
| 41 | $selected_items = array_filter( |
||
| 42 | $selected_items, |
||
| 43 | function ( $item ) use ( $selected_prices ) { |
||
| 44 | return isset( $item['price_id'] ) && in_array( (int) $item['price_id'], $selected_prices ); |
||
| 45 | } |
||
| 46 | ); |
||
| 47 | } |
||
| 48 | |||
| 49 | if ( ! empty( $invoice ) && $submission->is_initial_fetch() ) { |
||
| 50 | foreach ( $invoice->get_items() as $invoice_item ) { |
||
| 51 | if ( ! $invoice_item->has_variable_pricing() && isset( $selected_items[ $invoice_item->get_id() ] ) ) { |
||
| 52 | $selected_items[ $invoice_item->get_id() ]['quantity'] = $invoice_item->get_quantity(); |
||
| 53 | $selected_items[ $invoice_item->get_id() ]['price'] = $invoice_item->get_price(); |
||
| 54 | |||
| 55 | $force_prices[ $invoice_item->get_id() ] = $invoice_item->get_price(); |
||
| 56 | } |
||
| 57 | } |
||
| 58 | } |
||
| 59 | } |
||
| 60 | |||
| 61 | // (Maybe) set form items. |
||
| 62 | if ( isset( $data['getpaid-form-items'] ) ) { |
||
| 63 | |||
| 64 | // Confirm items key. |
||
| 65 | $form_items = wpinv_clean( $data['getpaid-form-items'] ); |
||
| 66 | if ( ! isset( $data['getpaid-form-items-key'] ) || md5( NONCE_KEY . AUTH_KEY . $form_items ) !== $data['getpaid-form-items-key'] ) { |
||
| 67 | throw new Exception( __( 'We could not validate the form items. Please reload the page and try again.', 'invoicing' ) ); |
||
| 68 | } |
||
| 69 | |||
| 70 | $items = array(); |
||
| 71 | $item_ids = array(); |
||
| 72 | |||
| 73 | foreach ( getpaid_convert_items_to_array( $form_items ) as $item_id => $qty ) { |
||
| 74 | if ( ! in_array( $item_id, $item_ids ) ) { |
||
| 75 | $item = new GetPaid_Form_Item( $item_id ); |
||
| 76 | |||
| 77 | if ( ! $item->has_variable_pricing() ) { |
||
| 78 | $item->set_quantity( $qty ); |
||
| 79 | |||
| 80 | if ( empty( $qty ) ) { |
||
| 81 | $item->set_allow_quantities( true ); |
||
| 82 | $item->set_is_required( false ); |
||
| 83 | } |
||
| 84 | |||
| 85 | if ( ! $item->user_can_set_their_price() && isset( $force_prices[ $item_id ] ) ) { |
||
| 86 | $item->set_is_dynamic_pricing( true ); |
||
| 87 | $item->set_minimum_price( 0 ); |
||
| 88 | } |
||
| 89 | } |
||
| 90 | |||
| 91 | $item_ids[] = $item->get_id(); |
||
| 92 | $items[] = $item; |
||
| 93 | } |
||
| 94 | } |
||
| 95 | |||
| 96 | if ( ! $payment_form->is_default() ) { |
||
| 97 | |||
| 98 | foreach ( $payment_form->get_items() as $item ) { |
||
| 99 | if ( ! in_array( $item->get_id(), $item_ids ) ) { |
||
| 100 | $item_ids[] = $item->get_id(); |
||
| 101 | $items[] = $item; |
||
| 102 | } |
||
| 103 | } |
||
| 104 | } |
||
| 105 | |||
| 106 | $payment_form->set_items( $items ); |
||
| 107 | } |
||
| 108 | |||
| 109 | // Process each individual item. |
||
| 110 | foreach ( $payment_form->get_items() as $item ) { |
||
| 111 | $this->process_item( $item, $selected_items, $submission ); |
||
| 112 | } |
||
| 202 |