| Conditions | 25 |
| Paths | 388 |
| Total Lines | 100 |
| Code Lines | 56 |
| Lines | 0 |
| Ratio | 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 |
||
| 150 | function getQuote($for_display = false) { |
||
| 151 | global $wpdb, $wpsc_cart; |
||
| 152 | $quote_shipping_method = wpsc_get_customer_meta( 'quote_shipping_method' ); |
||
| 153 | $quote_shipping_option = wpsc_get_customer_meta( 'quote_shipping_option' ); |
||
| 154 | |||
| 155 | $country = ''; |
||
| 156 | |||
| 157 | if (isset($_POST['country'])) { |
||
| 158 | $country = sanitize_text_field( $_POST['country'] ); |
||
| 159 | wpsc_update_customer_meta( 'shipping_country', $country ); |
||
| 160 | } else { |
||
| 161 | $country = (string) wpsc_get_customer_meta( 'shipping_country' ); |
||
| 162 | } |
||
| 163 | |||
| 164 | if (is_object($wpsc_cart)) { |
||
| 165 | $cart_total = $wpsc_cart->calculate_subtotal(true); |
||
| 166 | } |
||
| 167 | |||
| 168 | if (get_option('base_country') != $country) { |
||
| 169 | |||
| 170 | $results = WPSC_Countries::get_continent( $country ); |
||
| 171 | |||
| 172 | $flatrates = get_option('flat_rates'); |
||
| 173 | |||
| 174 | if ($flatrates != '') { |
||
| 175 | if ( $quote_shipping_method == $this->internal_name && $quote_shipping_option != __( "Flat Rate", 'wp-e-commerce' ) ) |
||
| 176 | wpsc_delete_customer_meta( 'quote_shipping_option' ); |
||
| 177 | |||
| 178 | if ( isset ( $flatrates[$results] ) ) { |
||
| 179 | |||
| 180 | if (stristr($flatrates[$results],'%')) { |
||
| 181 | |||
| 182 | $shipping_percent = str_replace('%', '', $flatrates[$results]); |
||
| 183 | $shipping_amount = $cart_total * ( $shipping_percent / 100 ); |
||
| 184 | $flatrates[$results] = (float)$shipping_amount; |
||
| 185 | |||
| 186 | } |
||
| 187 | |||
| 188 | return array( __( "Flat Rate", 'wp-e-commerce' ) => (float) $flatrates[$results] ); |
||
| 189 | } |
||
| 190 | } |
||
| 191 | |||
| 192 | } else { |
||
| 193 | |||
| 194 | $flatrates = get_option( 'flat_rates' ); |
||
| 195 | $shipping_quotes = array(); |
||
| 196 | |||
| 197 | switch ( $country ) { |
||
| 198 | case 'NZ': |
||
| 199 | if ( isset( $flatrates['northisland'] ) && strlen( $flatrates['northisland'] ) > 0 ) { |
||
| 200 | $shipping_quotes[ __( 'North Island', 'wp-e-commerce' ) ] = esc_attr( $flatrates['northisland'] ); |
||
| 201 | } |
||
| 202 | if ( isset( $flatrates['southisland'] ) && strlen( $flatrates['southisland'] ) > 0 ) { |
||
| 203 | $shipping_quotes[ __( 'South Island', 'wp-e-commerce' ) ] = esc_attr( $flatrates['southisland'] ); |
||
| 204 | } |
||
| 205 | break; |
||
| 206 | |||
| 207 | case 'US': |
||
| 208 | if ( isset( $flatrates['continental'] ) && strlen( $flatrates['continental'] ) > 0 ) { |
||
| 209 | $shipping_quotes[ __( 'Continental 48 States', 'wp-e-commerce' ) ] = esc_attr( $flatrates['continental'] ); |
||
| 210 | } |
||
| 211 | if ( isset( $flatrates['all'] ) && strlen( $flatrates['all'] ) > 0 ) { |
||
| 212 | $shipping_quotes[ __( 'All 50 States', 'wp-e-commerce' ) ] = esc_attr( $flatrates['all'] ); |
||
| 213 | } |
||
| 214 | break; |
||
| 215 | |||
| 216 | default: |
||
| 217 | if ( isset( $flatrates['local'] ) && strlen( $flatrates['local'] ) > 0 ) { |
||
| 218 | $shipping_quotes[ __( 'Local Shipping', 'wp-e-commerce' ) ] = esc_attr( $flatrates['local'] ); |
||
| 219 | } |
||
| 220 | break; |
||
| 221 | } |
||
| 222 | |||
| 223 | // Deal with % shipping rates |
||
| 224 | foreach (array_keys($shipping_quotes) as $quote_name) { |
||
| 225 | |||
| 226 | if (stristr($shipping_quotes[$quote_name],'%')) { |
||
| 227 | $shipping_percent = str_replace('%', '', $shipping_quotes[$quote_name]); |
||
| 228 | $shipping_amount = $cart_total * ( $shipping_percent / 100 ); |
||
| 229 | $shipping_quotes[$quote_name] = (float)$shipping_amount; |
||
| 230 | } else { |
||
| 231 | $shipping_quotes[$quote_name] = (float)$shipping_quotes[$quote_name]; |
||
| 232 | } |
||
| 233 | |||
| 234 | } |
||
| 235 | |||
| 236 | if ( $quote_shipping_method == $this->internal_name ) { |
||
| 237 | |||
| 238 | $shipping_options = array_keys($shipping_quotes); |
||
| 239 | |||
| 240 | if ( array_search( $quote_shipping_option, $shipping_options ) === false) { |
||
| 241 | wpsc_delete_customer_meta( 'quote_shipping_option' ); |
||
| 242 | } |
||
| 243 | |||
| 244 | } |
||
| 245 | |||
| 246 | return $shipping_quotes; |
||
| 247 | } |
||
| 248 | |||
| 249 | } |
||
| 250 | |||
| 321 |