Completed
Pull Request — master (#11208)
by Mike
09:06
created

WC_Order_Factory::get_order_item()   C

Complexity

Conditions 13
Paths 32

Size

Total Lines 39
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 13
eloc 33
c 1
b 0
f 0
nc 32
nop 1
dl 0
loc 39
rs 5.1234

How to fix   Complexity   

Long Method

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:

1
<?php
2
3
if ( ! defined( 'ABSPATH' ) ) {
4
	exit; // Exit if accessed directly
5
}
6
7
/**
8
 * Order Factory Class
9
 *
10
 * The WooCommerce order factory creating the right order objects.
11
 *
12
 * @class 		WC_Order_Factory
13
 * @version		2.7.0
14
 * @package		WooCommerce/Classes
15
 * @category	Class
16
 * @author 		WooThemes
17
 */
18
class WC_Order_Factory {
19
20
	/**
21
	 * Get order.
22
	 *
23
	 * @param bool $the_order (default: false)
24
	 * @return WC_Order|bool
25
	 */
26
	public static function get_order( $the_order = false ) {
27
		global $post;
28
29
		if ( false === $the_order ) {
30
			$the_order = $post;
31
		} elseif ( is_numeric( $the_order ) ) {
32
			$the_order = get_post( $the_order );
33
		} elseif ( $the_order instanceof WC_Order ) {
34
			$the_order = get_post( $the_order->get_id() );
35
		}
36
37
		if ( ! $the_order || ! is_object( $the_order ) ) {
38
			return false;
39
		}
40
41
		$order_id  = absint( $the_order->ID );
42
		$post_type = $the_order->post_type;
43
44
		if ( $order_type = wc_get_order_type( $post_type ) ) {
45
			$classname = $order_type['class_name'];
46
		} else {
47
			$classname = false;
48
		}
49
50
		// Filter classname so that the class can be overridden if extended.
51
		$classname = apply_filters( 'woocommerce_order_class', $classname, $post_type, $order_id, $the_order );
52
53
		if ( ! class_exists( $classname ) ) {
54
			return false;
55
		}
56
57
		return new $classname( $the_order );
58
	}
59
60
	/**
61
	 * Get order item.
62
	 * @param int
63
	 * @return WC_Order_Item
64
	 */
65
	public static function get_order_item( $item_id = 0 ) {
66
		global $wpdb;
67
68
		if ( is_numeric( $item_id ) ) {
69
			$item_data = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}woocommerce_order_items WHERE order_item_id = %d LIMIT 1;", $item_id ) );
70
			$item_type = $item_data->order_item_type;
71
		} elseif ( $item_id instanceof WC_Order_Item ) {
72
			$item_data = $item_id->get_data();
73
			$item_type = $item_data->get_type();
74
		} elseif( is_object( $item_id ) && ! empty( $item_id->order_item_type ) ) {
75
			$item_data = $item_id;
76
			$item_type = $item_id->order_item_type;
77
		} else {
78
			$item_data = false;
79
			$item_type = false;
80
		}
81
82
		if ( $item_data && $item_type ) {
83
			switch ( $item_type ) {
84
				case 'line_item' :
85
				case 'product' :
86
					return new WC_Order_Item_Product( $item_data );
87
				break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
88
				case 'coupon' :
89
					return new WC_Order_Item_Coupon( $item_data );
90
				break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
91
				case 'fee' :
92
					return new WC_Order_Item_Fee( $item_data );
93
				break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
94
				case 'shipping' :
95
					return new WC_Order_Item_Shipping( $item_data );
96
				break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
97
				case 'tax' :
98
					return new WC_Order_Item_Tax( $item_data );
99
				break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
100
			}
101
		}
102
		return new WC_Order_Item();
103
	}
104
}
105