Completed
Pull Request — master (#11613)
by Mike
08:58
created

WC_Shortcode_Cart::calculate_shipping()   F

Complexity

Conditions 10
Paths 247

Size

Total Lines 37
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 24
nc 247
nop 0
dl 0
loc 37
rs 3.2542
c 0
b 0
f 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 );
0 ignored issues
show
Bug introduced by
The method set_location() does not seem to exist on object<WC_Customer>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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->set_calculated_shipping( true );
41
42
			WC()->customer->save_to_session();
43
44
			wc_add_notice( __( 'Shipping costs updated.', 'woocommerce' ), 'notice' );
45
46
			do_action( 'woocommerce_calculated_shipping' );
47
48
		} catch ( Exception $e ) {
49
			if ( ! empty( $e ) ) {
50
				wc_add_notice( $e->getMessage(), 'error' );
51
			}
52
		}
53
	}
54
55
	/**
56
	 * Output the cart shortcode.
57
	 */
58
	public static function output() {
59
		// Constants
60
		if ( ! defined( 'WOOCOMMERCE_CART' ) ) {
61
			define( 'WOOCOMMERCE_CART', true );
62
		}
63
64
		// Update Shipping
65
		if ( ! empty( $_POST['calc_shipping'] ) && wp_verify_nonce( $_POST['_wpnonce'], 'woocommerce-cart' ) ) {
66
			self::calculate_shipping();
67
68
			// Also calc totals before we check items so subtotals etc are up to date
69
			WC()->cart->calculate_totals();
70
		}
71
72
		// Check cart items are valid
73
		do_action( 'woocommerce_check_cart_items' );
74
75
		// Calc totals
76
		WC()->cart->calculate_totals();
77
78
		if ( WC()->cart->is_empty() ) {
79
			wc_get_template( 'cart/cart-empty.php' );
80
		} else {
81
			wc_get_template( 'cart/cart.php' );
82
		}
83
	}
84
}
85