Conditions | 10 |
Paths | 215 |
Total Lines | 35 |
Code Lines | 23 |
Lines | 0 |
Ratio | 0 % |
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 |
||
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 ); |
||
|
|||
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 | |||
80 |
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:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.