Conditions | 13 |
Paths | 32 |
Total Lines | 39 |
Code Lines | 33 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 0 | 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 |
||
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; |
||
88 | case 'coupon' : |
||
89 | return new WC_Order_Item_Coupon( $item_data ); |
||
90 | break; |
||
91 | case 'fee' : |
||
92 | return new WC_Order_Item_Fee( $item_data ); |
||
93 | break; |
||
94 | case 'shipping' : |
||
95 | return new WC_Order_Item_Shipping( $item_data ); |
||
96 | break; |
||
97 | case 'tax' : |
||
98 | return new WC_Order_Item_Tax( $item_data ); |
||
99 | break; |
||
100 | } |
||
101 | } |
||
102 | return new WC_Order_Item(); |
||
103 | } |
||
104 | } |
||
105 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.