Completed
Pull Request — master (#9826)
by Mike
15:37
created

WC_Shipping_Legacy_Free_Shipping   A

Complexity

Total Complexity 29

Size/Duplication

Total Lines 202
Duplicated Lines 26.24 %

Coupling/Cohesion

Components 2
Dependencies 4
Metric Value
wmc 29
lcom 2
cbo 4
dl 53
loc 202
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 6 6 1
A get_option_key() 0 3 1
A init() 17 17 1
A init_form_fields() 0 59 1
D is_available() 21 71 24
A calculate_shipping() 9 9 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
1 ignored issue
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 17 and the first side effect is on line 4.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
3
if ( ! defined( 'ABSPATH' ) ) {
4
	exit; // Exit if accessed directly
5
}
6
7
/**
8
 * Free Shipping Method.
9
 *
10
 * This class is here for backwards commpatility for methods existing before zones existed.
11
 *
12
 * @deprecated  2.6.0
13
 * @version 2.4.0
14
 * @package WooCommerce/Classes/Shipping
15
 * @author  WooThemes
16
 */
17
class WC_Shipping_Legacy_Free_Shipping extends WC_Shipping_Method {
18
19
	/** @var float Min amount to be valid */
20
	public $min_amount;
21
22
	/** @var string Requires option */
23
	public $requires;
24
25
	/**
26
	 * Constructor.
27
	 */
28 View Code Duplication
	public function __construct() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
29
		$this->id 			= 'legacy_free_shipping';
30
		$this->method_title = __( 'Free Shipping (Legacy)', 'woocommerce' );
31
		$this->method_description = sprintf( __( '<strong>This method is deprecated in 2.6.0 and will be removed in future versions - we recommend disabling it and instead setting up a new rate within your <a href="%s">Shipping Zones</a>.</strong>', 'woocommerce' ), admin_url( 'admin.php?page=wc-shipping' ) );
32
		$this->init();
33
	}
34
35
	/**
36
	 * Return the name of the option in the WP DB.
37
	 * @since 2.6.0
38
	 * @return string
39
	 */
40
	public function get_option_key() {
41
		return $this->plugin_id . 'free_shipping' . '_settings';
42
	}
43
44
	/**
45
	 * init function.
46
	 */
47 View Code Duplication
	public function init() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
48
49
		// Load the settings.
50
		$this->init_form_fields();
51
		$this->init_settings();
52
53
		// Define user set variables
54
		$this->enabled		= $this->get_option( 'enabled' );
55
		$this->title 		= $this->get_option( 'title' );
56
		$this->min_amount 	= $this->get_option( 'min_amount', 0 );
57
		$this->availability = $this->get_option( 'availability' );
0 ignored issues
show
Deprecated Code introduced by
The property WC_Shipping_Method::$availability has been deprecated with message: 2.6.0

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
58
		$this->countries 	= $this->get_option( 'countries' );
0 ignored issues
show
Deprecated Code introduced by
The property WC_Shipping_Method::$countries has been deprecated with message: 2.6.0

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
59
		$this->requires		= $this->get_option( 'requires' );
60
61
		// Actions
62
		add_action( 'woocommerce_update_options_shipping_' . $this->id, array( $this, 'process_admin_options' ) );
63
	}
64
65
	/**
66
	 * Initialise Gateway Settings Form Fields.
67
	 */
68
	public function init_form_fields() {
69
		$this->form_fields = array(
70
			'enabled' => array(
71
				'title' 		=> __( 'Enable/Disable', 'woocommerce' ),
72
				'type' 			=> 'checkbox',
73
				'label' 		=> __( 'Enable Free Shipping', 'woocommerce' ),
74
				'default' 		=> 'no'
75
			),
76
			'title' => array(
77
				'title' 		=> __( 'Method Title', 'woocommerce' ),
78
				'type' 			=> 'text',
79
				'description' 	=> __( 'This controls the title which the user sees during checkout.', 'woocommerce' ),
80
				'default'		=> __( 'Free Shipping', 'woocommerce' ),
81
				'desc_tip'		=> true,
82
			),
83
			'availability' => array(
84
				'title' 		=> __( 'Method availability', 'woocommerce' ),
85
				'type' 			=> 'select',
86
				'default' 		=> 'all',
87
				'class'			=> 'availability wc-enhanced-select',
88
				'options'		=> array(
89
					'all' 		=> __( 'All allowed countries', 'woocommerce' ),
90
					'specific' 	=> __( 'Specific Countries', 'woocommerce' )
91
				)
92
			),
93
			'countries' => array(
94
				'title' 		=> __( 'Specific Countries', 'woocommerce' ),
95
				'type' 			=> 'multiselect',
96
				'class'			=> 'wc-enhanced-select',
97
				'css'			=> 'width: 450px;',
98
				'default' 		=> '',
99
				'options'		=> WC()->countries->get_shipping_countries(),
100
				'custom_attributes' => array(
101
					'data-placeholder' => __( 'Select some countries', 'woocommerce' )
102
				)
103
			),
104
			'requires' => array(
105
				'title' 		=> __( 'Free Shipping Requires...', 'woocommerce' ),
106
				'type' 			=> 'select',
107
				'class'         => 'wc-enhanced-select',
108
				'default' 		=> '',
109
				'options'		=> array(
110
					'' 				=> __( 'N/A', 'woocommerce' ),
111
					'coupon'		=> __( 'A valid free shipping coupon', 'woocommerce' ),
112
					'min_amount' 	=> __( 'A minimum order amount (defined below)', 'woocommerce' ),
113
					'either' 		=> __( 'A minimum order amount OR a coupon', 'woocommerce' ),
114
					'both' 			=> __( 'A minimum order amount AND a coupon', 'woocommerce' ),
115
				)
116
			),
117
			'min_amount' => array(
118
				'title' 		=> __( 'Minimum Order Amount', 'woocommerce' ),
119
				'type' 			=> 'price',
120
				'placeholder'	=> wc_format_localized_price( 0 ),
121
				'description' 	=> __( 'Users will need to spend this amount to get free shipping (if enabled above).', 'woocommerce' ),
122
				'default' 		=> '0',
123
				'desc_tip'		=> true
124
			)
125
		);
126
	}
127
128
	/**
129
	 * is_available function.
130
	 * @param array $package
131
	 * @return bool
132
	 */
133
	public function is_available( $package ) {
134
		if ( 'no' == $this->enabled ) {
135
			return false;
136
		}
137
138
		if ( 'specific' == $this->availability ) {
0 ignored issues
show
Deprecated Code introduced by
The property WC_Shipping_Method::$availability has been deprecated with message: 2.6.0

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
139
			$ship_to_countries = $this->countries;
0 ignored issues
show
Deprecated Code introduced by
The property WC_Shipping_Method::$countries has been deprecated with message: 2.6.0

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
140
		} else {
141
			$ship_to_countries = array_keys( WC()->countries->get_shipping_countries() );
142
		}
143
144
		if ( is_array( $ship_to_countries ) && ! in_array( $package['destination']['country'], $ship_to_countries ) ) {
145
			return false;
146
		}
147
148
		// Enabled logic
149
		$is_available       = false;
150
		$has_coupon         = false;
151
		$has_met_min_amount = false;
152
153 View Code Duplication
		if ( in_array( $this->requires, array( 'coupon', 'either', 'both' ) ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
154
155
			if ( $coupons = WC()->cart->get_coupons() ) {
156
				foreach ( $coupons as $code => $coupon ) {
157
					if ( $coupon->is_valid() && $coupon->enable_free_shipping() ) {
158
						$has_coupon = true;
159
					}
160
				}
161
			}
162
		}
163
164 View Code Duplication
		if ( in_array( $this->requires, array( 'min_amount', 'either', 'both' ) ) && isset( WC()->cart->cart_contents_total ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
165
			if ( WC()->cart->prices_include_tax ) {
166
				$total = WC()->cart->cart_contents_total + array_sum( WC()->cart->taxes );
167
			} else {
168
				$total = WC()->cart->cart_contents_total;
169
			}
170
171
			if ( $total >= $this->min_amount ) {
172
				$has_met_min_amount = true;
173
			}
174
		}
175
176
		switch ( $this->requires ) {
177
			case 'min_amount' :
178
				if ( $has_met_min_amount ) {
179
					$is_available = true;
180
				}
181
			break;
182
			case 'coupon' :
183
				if ( $has_coupon ) {
184
					$is_available = true;
185
				}
186
			break;
187
			case 'both' :
188
				if ( $has_met_min_amount && $has_coupon ) {
189
					$is_available = true;
190
				}
191
			break;
192
			case 'either' :
193
				if ( $has_met_min_amount || $has_coupon ) {
194
					$is_available = true;
195
				}
196
			break;
197
			default :
198
				$is_available = true;
199
			break;
200
		}
201
202
		return apply_filters( 'woocommerce_shipping_' . $this->id . '_is_available', $is_available, $package );
203
	}
204
205
	/**
206
	 * calculate_shipping function.
207
	 * @return array
208
	 */
209 View Code Duplication
	public function calculate_shipping() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
210
		$args = array(
211
			'id' 	=> $this->id,
212
			'label' => $this->title,
213
			'cost' 	=> 0,
214
			'taxes' => false
215
		);
216
		$this->add_rate( $args );
217
	}
218
}
219