WC_Shortcode_Cart::calculate_shipping()   D
last analyzed

Complexity

Conditions 10
Paths 215

Size

Total Lines 35
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
dl 0
loc 35
rs 4.2666
c 0
b 0
f 0
eloc 23
nc 215
nop 0

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
 * Cart Shortcode
4
 *
5
 * Used on the cart page, the cart shortcode displays the cart contents and interface for coupon codes and other cart bits and pieces.
6
 *
7
 * @author 		WooThemes
8
 * @category 	Shortcodes
9
 * @package 	WooCommerce/Shortcodes/Cart
10
 * @version     2.3.0
11
 */
12
class WC_Shortcode_Cart {
13
14
	/**
15
	 * Calculate shipping for the cart.
16
	 */
17
	public static function calculate_shipping() {
18
		try {
19
			WC()->shipping->reset_shipping();
20
21
			$country  = wc_clean( $_POST['calc_shipping_country'] );
22
			$state    = wc_clean( isset( $_POST['calc_shipping_state'] ) ? $_POST['calc_shipping_state'] : '' );
23
			$postcode = apply_filters( 'woocommerce_shipping_calculator_enable_postcode', true ) ? wc_clean( $_POST['calc_shipping_postcode'] ) : '';
24
			$city     = apply_filters( 'woocommerce_shipping_calculator_enable_city', false ) ? wc_clean( $_POST['calc_shipping_city'] ) : '';
25
26
			if ( $postcode && ! WC_Validation::is_postcode( $postcode, $country ) ) {
27
				throw new Exception( __( 'Please enter a valid postcode/ZIP.', 'woocommerce' ) );
28
			} elseif ( $postcode ) {
29
				$postcode = wc_format_postcode( $postcode, $country );
0 ignored issues
show
Bug introduced by
It seems like $postcode can also be of type array; however, wc_format_postcode() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
30
			}
31
32
			if ( $country ) {
33
				WC()->customer->set_location( $country, $state, $postcode, $city );
34
				WC()->customer->set_shipping_location( $country, $state, $postcode, $city );
35
			} else {
36
				WC()->customer->set_to_base();
37
				WC()->customer->set_shipping_to_base();
38
			}
39
40
			WC()->customer->calculated_shipping( true );
41
42
			wc_add_notice( __( 'Shipping costs updated.', 'woocommerce' ), 'notice' );
43
44
			do_action( 'woocommerce_calculated_shipping' );
45
46
		} catch ( Exception $e ) {
47
			if ( ! empty( $e ) ) {
48
				wc_add_notice( $e->getMessage(), 'error' );
49
			}
50
		}
51
	}
52
53
	/**
54
	 * Output the cart shortcode.
55
	 */
56
	public static function output() {
57
		// Constants
58
		if ( ! defined( 'WOOCOMMERCE_CART' ) ) {
59
			define( 'WOOCOMMERCE_CART', true );
60
		}
61
62
		// Update Shipping
63
		if ( ! empty( $_POST['calc_shipping'] ) && wp_verify_nonce( $_POST['_wpnonce'], 'woocommerce-cart' ) ) {
64
			self::calculate_shipping();
65
		}
66
67
		// Check cart items are valid
68
		do_action( 'woocommerce_check_cart_items' );
69
70
		// Calc totals
71
		WC()->cart->calculate_totals();
72
73
		if ( WC()->cart->is_empty() ) {
74
			wc_get_template( 'cart/cart-empty.php' );
75
		} else {
76
			wc_get_template( 'cart/cart.php' );
77
		}
78
	}
79
}
80