| Conditions | 12 |
| Paths | 14 |
| Total Lines | 56 |
| Code Lines | 28 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 1 | Features | 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 | |||
| 31 | // Prepare the selected items. |
||
| 32 | $selected_items = array(); |
||
| 33 | if ( ! empty( $data['getpaid-items'] ) ) { |
||
| 34 | $selected_items = wpinv_clean( $data['getpaid-items'] ); |
||
| 35 | } |
||
| 36 | |||
| 37 | // (Maybe) set form items. |
||
| 38 | if ( isset( $data['getpaid-form-items'] ) ) { |
||
| 39 | |||
| 40 | // Confirm items key. |
||
| 41 | $form_items = wpinv_clean( $data['getpaid-form-items'] ); |
||
| 42 | if ( ! isset( $data['getpaid-form-items-key'] ) || $data['getpaid-form-items-key'] !== md5( NONCE_KEY . AUTH_KEY . $form_items ) ) { |
||
|
|
|||
| 43 | throw new Exception( __( 'We could not validate the form items. Please reload the page and try again.', 'invoicing' ) ); |
||
| 44 | } |
||
| 45 | |||
| 46 | $items = array(); |
||
| 47 | $item_ids = array(); |
||
| 48 | |||
| 49 | foreach ( getpaid_convert_items_to_array( $form_items ) as $item_id => $qty ) { |
||
| 50 | if ( ! in_array( $item_id, $item_ids ) ) { |
||
| 51 | $item = new GetPaid_Form_Item( $item_id ); |
||
| 52 | $item->set_quantity( $qty ); |
||
| 53 | |||
| 54 | if ( 0 == $qty ) { |
||
| 55 | $item->set_allow_quantities( true ); |
||
| 56 | $item->set_is_required( false ); |
||
| 57 | } |
||
| 58 | |||
| 59 | $item_ids[] = $item->get_id(); |
||
| 60 | $items[] = $item; |
||
| 61 | } |
||
| 62 | } |
||
| 63 | |||
| 64 | if ( ! $payment_form->is_default() ) { |
||
| 65 | |||
| 66 | foreach ( $payment_form->get_items() as $item ) { |
||
| 67 | if ( ! in_array( $item->get_id(), $item_ids ) ) { |
||
| 68 | $item_ids[] = $item->get_id(); |
||
| 69 | $items[] = $item; |
||
| 70 | } |
||
| 71 | } |
||
| 72 | |||
| 73 | } |
||
| 74 | |||
| 75 | $payment_form->set_items( $items ); |
||
| 76 | |||
| 77 | } |
||
| 78 | |||
| 79 | // Process each individual item. |
||
| 80 | foreach ( $payment_form->get_items() as $item ) { |
||
| 81 | $this->process_item( $item, $selected_items, $submission ); |
||
| 82 | } |
||
| 132 |