Conditions | 40 |
Paths | 5059 |
Total Lines | 108 |
Code Lines | 66 |
Lines | 12 |
Ratio | 11.11 % |
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 if ( !defined( 'ABSPATH' ) ) exit; |
||
95 | function get_shipping_cost( $nb_of_items, $total_cart, $total_shipping_cost, $total_weight, $selected_method = '' ) { |
||
96 | global $wpdb; |
||
97 | |||
98 | $shipping_cost = 0; |
||
99 | |||
100 | $shipping_mode_state = get_option( 'wpshop_shipping_address_choice' ); |
||
101 | if ( ! empty( $shipping_mode_state ) && isset( $shipping_mode_state['activate'] ) && ( 'on' === $shipping_mode_state['activate'] ) ) { |
||
102 | $shipping_mode_option = get_option( 'wps_shipping_mode' ); |
||
103 | if( ( !empty($selected_method) || isset( $_SESSION['shipping_address'] ) && empty( $_SESSION['shipping_method'] ) ) ) { |
||
104 | if( !empty($selected_method) ) { |
||
105 | $chosen_shipping_mode = $selected_method; |
||
106 | } else { |
||
107 | $ship_mod = new wps_shipping_mode_ctr(); |
||
108 | $shipping_modes = $ship_mod->get_shipping_mode_for_address( $_SESSION['shipping_address'] ); |
||
109 | if( !empty( $shipping_modes ) ) { |
||
110 | $shipping_modes = $shipping_modes['modes']; |
||
111 | foreach( $shipping_modes as $key => $shipping_mode ) { |
||
112 | $chosen_shipping_mode = $key; |
||
113 | break; |
||
114 | } |
||
115 | } else { |
||
116 | $chosen_shipping_mode = 'default_choice'; |
||
117 | } |
||
118 | } |
||
119 | } |
||
120 | else { |
||
121 | if( !empty( $_SESSION['shipping_method'] ) ) { |
||
122 | $chosen_shipping_mode = wpshop_tools::varSanitizer( $_SESSION['shipping_method'] ); |
||
123 | } else { |
||
124 | $chosen_shipping_mode = 'default_choice'; |
||
125 | } |
||
126 | } |
||
127 | |||
128 | $default_weight_unity = get_option( 'wpshop_shop_default_weight_unity' ); |
||
129 | if ( !empty($default_weight_unity) ) { |
||
130 | $query = $wpdb->prepare('SELECT unit FROM ' .WPSHOP_DBT_ATTRIBUTE_UNIT. ' WHERE id = %d', $default_weight_unity); |
||
131 | $weight_unity = $wpdb->get_var( $query ); |
||
132 | |||
133 | if ( !empty($weight_unity) && $weight_unity == 'kg' ) { |
||
134 | |||
135 | $total_weight = $total_weight * 1000; |
||
136 | } |
||
137 | } |
||
138 | |||
139 | if ( ( !empty($_SESSION['shipping_method']) && $_SESSION['shipping_method'] == 'shipping-partners' ) || !empty( $_SESSION['wps-pos-addon']) ) { |
||
140 | return 0; |
||
141 | } |
||
142 | |||
143 | /** Take the selected shipping mode **/ |
||
144 | if( $chosen_shipping_mode == 'default_choice' ) { |
||
145 | View Code Duplication | if( !empty( $shipping_mode_option['modes'][ $shipping_mode_option['default_choice'] ]) ) { |
|
146 | $selected_shipping_mode_config = $shipping_mode_option['modes'][ $shipping_mode_option['default_choice'] ]; |
||
147 | } else { |
||
148 | $selected_shipping_mode_config = ''; |
||
149 | } |
||
150 | View Code Duplication | } else { |
|
151 | if( !empty( $shipping_mode_option['modes'][$chosen_shipping_mode]) ) { |
||
152 | $selected_shipping_mode_config = $shipping_mode_option['modes'][$chosen_shipping_mode]; |
||
153 | } else { |
||
154 | $selected_shipping_mode_config = ''; |
||
155 | } |
||
156 | } |
||
157 | $shipping_cost = $total_shipping_cost; |
||
158 | |||
159 | /** Free Shipping **/ |
||
160 | if ( ( !empty($selected_shipping_mode_config) && !empty($selected_shipping_mode_config['free_shipping']) ) || ( $selected_method == 'is_downloadable_' ) ) { |
||
161 | $shipping_cost = 0; |
||
162 | } |
||
163 | /** Free Shipping From **/ |
||
164 | elseif( !empty($selected_shipping_mode_config) && !empty($selected_shipping_mode_config['free_from']) && $selected_shipping_mode_config['free_from'] >= 0 && $selected_shipping_mode_config['free_from'] <= number_format( $total_cart, 2, '.', '') ) { |
||
165 | $shipping_cost = 0; |
||
166 | } |
||
167 | else { |
||
168 | /** Check Custom Shipping Cost **/ |
||
169 | |||
170 | if ( !empty($selected_shipping_mode_config['custom_shipping_rules']) && !empty($selected_shipping_mode_config['custom_shipping_rules']['active']) ) { |
||
171 | $address_infos = ''; |
||
172 | if( !empty( $_SESSION['shipping_address'] ) ) { |
||
173 | $address_infos = get_post_meta($_SESSION['shipping_address'],'_wpshop_address_metadata', true); |
||
174 | } |
||
175 | $country = ( !empty($address_infos['country']) ) ? $address_infos['country'] : ''; |
||
176 | /** Check Active Postcode option **/ |
||
177 | if ( !empty($selected_shipping_mode_config['custom_shipping_rules']['active_cp']) ) { |
||
178 | $postcode = $address_infos['postcode']; |
||
179 | if ( array_key_exists($country.'-'.$postcode, $selected_shipping_mode_config['custom_shipping_rules']['fees']) ) { |
||
180 | $country = $country.'-'.$postcode; |
||
181 | } |
||
182 | elseif( array_key_exists($country.'-OTHERS', $selected_shipping_mode_config['custom_shipping_rules']['fees']) ) { |
||
183 | $country = $country.'-OTHERS'; |
||
184 | } |
||
185 | } |
||
186 | $shipping_cost += $this->calculate_custom_shipping_cost($country, array('weight'=>$total_weight,'price'=> $total_cart), $selected_shipping_mode_config['custom_shipping_rules']['fees'], $chosen_shipping_mode); |
||
187 | } |
||
188 | |||
189 | /** Min- Max config **/ |
||
190 | if ( !empty($selected_shipping_mode_config['min_max']) && !empty($selected_shipping_mode_config['min_max']['activate']) ) { |
||
191 | if ( !empty($selected_shipping_mode_config['min_max']['min']) && $shipping_cost < $selected_shipping_mode_config['min_max']['min'] ) { |
||
192 | $shipping_cost = $selected_shipping_mode_config['min_max']['min']; |
||
193 | } |
||
194 | elseif( !empty($selected_shipping_mode_config['min_max']['max']) &&$shipping_cost > $selected_shipping_mode_config['min_max']['max']) { |
||
195 | $shipping_cost = $selected_shipping_mode_config['min_max']['max']; |
||
196 | } |
||
197 | |||
198 | } |
||
199 | } |
||
200 | } |
||
201 | return $shipping_cost; |
||
202 | } |
||
203 | |||
358 |
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.