| Conditions | 3 |
| Paths | 4 |
| Total Lines | 75 |
| Code Lines | 52 |
| 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 |
||
| 53 | public static function create_order( $customer_id = 1, $product = null ) { |
||
| 54 | |||
| 55 | if ( ! is_a( $product, 'WC_Product' ) ) { |
||
| 56 | $product = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\ProductHelper::create_simple_product(); |
||
| 57 | } |
||
| 58 | |||
| 59 | ShippingHelper::create_simple_flat_rate(); |
||
| 60 | |||
| 61 | $order_data = array( |
||
| 62 | 'status' => 'pending', |
||
| 63 | 'customer_id' => $customer_id, |
||
| 64 | 'customer_note' => '', |
||
| 65 | 'total' => '', |
||
| 66 | ); |
||
| 67 | |||
| 68 | $_SERVER['REMOTE_ADDR'] = '127.0.0.1'; // Required, else wc_create_order throws an exception |
||
| 69 | $order = wc_create_order( $order_data ); |
||
| 70 | |||
| 71 | // Add order products |
||
| 72 | $item = new WC_Order_Item_Product(); |
||
| 73 | $item->set_props( |
||
| 74 | array( |
||
| 75 | 'product' => $product, |
||
| 76 | 'quantity' => 4, |
||
| 77 | 'subtotal' => wc_get_price_excluding_tax( $product, array( 'qty' => 4 ) ), |
||
| 78 | 'total' => wc_get_price_excluding_tax( $product, array( 'qty' => 4 ) ), |
||
| 79 | ) |
||
| 80 | ); |
||
| 81 | $item->save(); |
||
| 82 | $order->add_item( $item ); |
||
| 83 | |||
| 84 | // Set billing address |
||
| 85 | $order->set_billing_first_name( 'Jeroen' ); |
||
| 86 | $order->set_billing_last_name( 'Sormani' ); |
||
| 87 | $order->set_billing_company( 'WooCompany' ); |
||
| 88 | $order->set_billing_address_1( 'WooAddress' ); |
||
| 89 | $order->set_billing_address_2( '' ); |
||
| 90 | $order->set_billing_city( 'WooCity' ); |
||
| 91 | $order->set_billing_state( 'NY' ); |
||
| 92 | $order->set_billing_postcode( '123456' ); |
||
| 93 | $order->set_billing_country( 'US' ); |
||
| 94 | $order->set_billing_email( '[email protected]' ); |
||
| 95 | $order->set_billing_phone( '555-32123' ); |
||
| 96 | |||
| 97 | // Add shipping costs |
||
| 98 | $shipping_taxes = WC_Tax::calc_shipping_tax( '10', WC_Tax::get_shipping_tax_rates() ); |
||
| 99 | $rate = new WC_Shipping_Rate( 'flat_rate_shipping', 'Flat rate shipping', '10', $shipping_taxes, 'flat_rate' ); |
||
| 100 | $item = new WC_Order_Item_Shipping(); |
||
| 101 | $item->set_props( |
||
| 102 | array( |
||
| 103 | 'method_title' => $rate->label, |
||
| 104 | 'method_id' => $rate->id, |
||
| 105 | 'total' => wc_format_decimal( $rate->cost ), |
||
| 106 | 'taxes' => $rate->taxes, |
||
| 107 | ) |
||
| 108 | ); |
||
| 109 | foreach ( $rate->get_meta_data() as $key => $value ) { |
||
| 110 | $item->add_meta_data( $key, $value, true ); |
||
| 111 | } |
||
| 112 | $order->add_item( $item ); |
||
| 113 | |||
| 114 | // Set payment gateway |
||
| 115 | $payment_gateways = WC()->payment_gateways->payment_gateways(); |
||
| 116 | $order->set_payment_method( $payment_gateways['bacs'] ); |
||
| 117 | |||
| 118 | // Set totals |
||
| 119 | $order->set_shipping_total( 10 ); |
||
| 120 | $order->set_discount_total( 0 ); |
||
| 121 | $order->set_discount_tax( 0 ); |
||
| 122 | $order->set_cart_tax( 0 ); |
||
| 123 | $order->set_shipping_tax( 0 ); |
||
| 124 | $order->set_total( 50 ); // 4 x $10 simple helper product |
||
| 125 | $order->save(); |
||
| 126 | |||
| 127 | return $order; |
||
| 128 | } |
||
| 130 |