| Conditions | 24 |
| Paths | > 20000 |
| Total Lines | 137 |
| Code Lines | 135 |
| Lines | 18 |
| Ratio | 13.14 % |
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 |
||
| 104 | public function get_order( $id, $fields = null ) { |
||
| 105 | |||
| 106 | // ensure order ID is valid & user has permission to read |
||
| 107 | $id = $this->validate_request( $id, 'shop_order', 'read' ); |
||
| 108 | |||
| 109 | if ( is_wp_error( $id ) ) |
||
| 110 | return $id; |
||
| 111 | |||
| 112 | $order = wc_get_order( $id ); |
||
| 113 | |||
| 114 | $order_post = get_post( $id ); |
||
| 115 | |||
| 116 | $order_data = array( |
||
| 117 | 'id' => $order->id, |
||
| 118 | 'order_number' => $order->get_order_number(), |
||
| 119 | 'created_at' => $this->server->format_datetime( $order_post->post_date_gmt ), |
||
| 120 | 'updated_at' => $this->server->format_datetime( $order_post->post_modified_gmt ), |
||
| 121 | 'completed_at' => $this->server->format_datetime( $order->completed_date, true ), |
||
| 122 | 'status' => $order->get_status(), |
||
| 123 | 'currency' => $order->order_currency, |
||
| 124 | 'total' => wc_format_decimal( $order->get_total(), 2 ), |
||
| 125 | 'subtotal' => wc_format_decimal( $this->get_order_subtotal( $order ), 2 ), |
||
| 126 | 'total_line_items_quantity' => $order->get_item_count(), |
||
| 127 | 'total_tax' => wc_format_decimal( $order->get_total_tax(), 2 ), |
||
| 128 | 'total_shipping' => wc_format_decimal( $order->get_total_shipping(), 2 ), |
||
| 129 | 'cart_tax' => wc_format_decimal( $order->get_cart_tax(), 2 ), |
||
| 130 | 'shipping_tax' => wc_format_decimal( $order->get_shipping_tax(), 2 ), |
||
| 131 | 'total_discount' => wc_format_decimal( $order->get_total_discount(), 2 ), |
||
| 132 | 'cart_discount' => wc_format_decimal( $order->get_cart_discount(), 2 ), |
||
| 133 | 'order_discount' => wc_format_decimal( $order->get_order_discount(), 2 ), |
||
| 134 | 'shipping_methods' => $order->get_shipping_method(), |
||
| 135 | 'payment_details' => array( |
||
| 136 | 'method_id' => $order->payment_method, |
||
| 137 | 'method_title' => $order->payment_method_title, |
||
| 138 | 'paid' => isset( $order->paid_date ), |
||
| 139 | ), |
||
| 140 | 'billing_address' => array( |
||
| 141 | 'first_name' => $order->billing_first_name, |
||
| 142 | 'last_name' => $order->billing_last_name, |
||
| 143 | 'company' => $order->billing_company, |
||
| 144 | 'address_1' => $order->billing_address_1, |
||
| 145 | 'address_2' => $order->billing_address_2, |
||
| 146 | 'city' => $order->billing_city, |
||
| 147 | 'state' => $order->billing_state, |
||
| 148 | 'postcode' => $order->billing_postcode, |
||
| 149 | 'country' => $order->billing_country, |
||
| 150 | 'email' => $order->billing_email, |
||
| 151 | 'phone' => $order->billing_phone, |
||
| 152 | ), |
||
| 153 | 'shipping_address' => array( |
||
| 154 | 'first_name' => $order->shipping_first_name, |
||
| 155 | 'last_name' => $order->shipping_last_name, |
||
| 156 | 'company' => $order->shipping_company, |
||
| 157 | 'address_1' => $order->shipping_address_1, |
||
| 158 | 'address_2' => $order->shipping_address_2, |
||
| 159 | 'city' => $order->shipping_city, |
||
| 160 | 'state' => $order->shipping_state, |
||
| 161 | 'postcode' => $order->shipping_postcode, |
||
| 162 | 'country' => $order->shipping_country, |
||
| 163 | ), |
||
| 164 | 'note' => $order->customer_note, |
||
| 165 | 'customer_ip' => $order->customer_ip_address, |
||
| 166 | 'customer_user_agent' => $order->customer_user_agent, |
||
| 167 | 'customer_id' => $order->customer_user, |
||
| 168 | 'view_order_url' => $order->get_view_order_url(), |
||
| 169 | 'line_items' => array(), |
||
| 170 | 'shipping_lines' => array(), |
||
| 171 | 'tax_lines' => array(), |
||
| 172 | 'fee_lines' => array(), |
||
| 173 | 'coupon_lines' => array(), |
||
| 174 | ); |
||
| 175 | |||
| 176 | // add line items |
||
| 177 | foreach( $order->get_items() as $item_id => $item ) { |
||
| 178 | |||
| 179 | $product = $order->get_product_from_item( $item ); |
||
| 180 | |||
| 181 | $order_data['line_items'][] = array( |
||
| 182 | 'id' => $item_id, |
||
| 183 | 'subtotal' => wc_format_decimal( $order->get_line_subtotal( $item ), 2 ), |
||
| 184 | 'total' => wc_format_decimal( $order->get_line_total( $item ), 2 ), |
||
| 185 | 'total_tax' => wc_format_decimal( $order->get_line_tax( $item ), 2 ), |
||
| 186 | 'price' => wc_format_decimal( $order->get_item_total( $item ), 2 ), |
||
| 187 | 'quantity' => (int) $item['qty'], |
||
| 188 | 'tax_class' => ( ! empty( $item['tax_class'] ) ) ? $item['tax_class'] : null, |
||
| 189 | 'name' => $item['name'], |
||
| 190 | 'product_id' => ( isset( $product->variation_id ) ) ? $product->variation_id : $product->id, |
||
| 191 | 'sku' => is_object( $product ) ? $product->get_sku() : null, |
||
| 192 | ); |
||
| 193 | } |
||
| 194 | |||
| 195 | // add shipping |
||
| 196 | foreach ( $order->get_shipping_methods() as $shipping_item_id => $shipping_item ) { |
||
| 197 | |||
| 198 | $order_data['shipping_lines'][] = array( |
||
| 199 | 'id' => $shipping_item_id, |
||
| 200 | 'method_id' => $shipping_item['method_id'], |
||
| 201 | 'method_title' => $shipping_item['name'], |
||
| 202 | 'total' => wc_format_decimal( $shipping_item['cost'], 2 ), |
||
| 203 | ); |
||
| 204 | } |
||
| 205 | |||
| 206 | // add taxes |
||
| 207 | foreach ( $order->get_tax_totals() as $tax_code => $tax ) { |
||
| 208 | |||
| 209 | $order_data['tax_lines'][] = array( |
||
| 210 | 'code' => $tax_code, |
||
| 211 | 'title' => $tax->label, |
||
| 212 | 'total' => wc_format_decimal( $tax->amount, 2 ), |
||
| 213 | 'compound' => (bool) $tax->is_compound, |
||
| 214 | ); |
||
| 215 | } |
||
| 216 | |||
| 217 | // add fees |
||
| 218 | foreach ( $order->get_fees() as $fee_item_id => $fee_item ) { |
||
| 219 | |||
| 220 | $order_data['fee_lines'][] = array( |
||
| 221 | 'id' => $fee_item_id, |
||
| 222 | 'title' => $fee_item['name'], |
||
| 223 | 'tax_class' => ( ! empty( $fee_item['tax_class'] ) ) ? $fee_item['tax_class'] : null, |
||
| 224 | 'total' => wc_format_decimal( $order->get_line_total( $fee_item ), 2 ), |
||
| 225 | 'total_tax' => wc_format_decimal( $order->get_line_tax( $fee_item ), 2 ), |
||
| 226 | ); |
||
| 227 | } |
||
| 228 | |||
| 229 | // add coupons |
||
| 230 | foreach ( $order->get_items( 'coupon' ) as $coupon_item_id => $coupon_item ) { |
||
| 231 | |||
| 232 | $order_data['coupon_lines'][] = array( |
||
| 233 | 'id' => $coupon_item_id, |
||
| 234 | 'code' => $coupon_item['name'], |
||
| 235 | 'amount' => wc_format_decimal( $coupon_item['discount_amount'], 2 ), |
||
| 236 | ); |
||
| 237 | } |
||
| 238 | |||
| 239 | return array( 'order' => apply_filters( 'woocommerce_api_order_response', $order_data, $order, $fields, $this->server ) ); |
||
| 240 | } |
||
| 241 | |||
| 401 |