Completed
Pull Request — master (#11613)
by Mike
11:14
created

WC_Legacy_Coupon::__get()   C

Complexity

Conditions 26
Paths 26

Size

Total Lines 77
Code Lines 70

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 26
eloc 70
nc 26
nop 1
dl 0
loc 77
rs 5.1159
c 0
b 0
f 0

How to fix   Long Method    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
if ( ! defined( 'ABSPATH' ) ) {
3
	exit;
4
}
5
6
/**
7
 * Legacy Coupon.
8
 *
9
 * Legacy and deprecated functions are here to keep the WC_Legacy_Coupon class clean.
10
 * This class will be removed in future versions.
11
 *
12
 * @class       WC_Legacy_Coupon
13
 * @version     2.7.0
14
 * @package     WooCommerce/Classes
15
 * @category    Class
16
 * @author      WooThemes
17
 */
18
abstract class WC_Legacy_Coupon extends WC_Data {
19
20
	/**
21
	 * Magic __isset method for backwards compatibility. Legacy properties which could be accessed directly in the past.
22
	 * @param  string $key
23
	 * @return bool
24
	 */
25
	public function __isset( $key ) {
26
		$legacy_keys = array(
27
			'id', 'exists', 'coupon_custom_fields', 'type', 'discount_type', 'amount', 'code',
28
			'individual_use', 'product_ids', 'exclude_product_ids', 'usage_limit', 'usage_limit_per_user',
29
			'limit_usage_to_x_items', 'usage_count', 'expiry_date', 'product_categories',
30
			'exclude_product_categories', 'minimum_amount', 'maximum_amount', 'customer_email',
31
		);
32
		if ( in_array( $key, $legacy_keys ) ) {
33
			return true;
34
		}
35
		return false;
36
	}
37
38
	/**
39
	 * Magic __get method for backwards compatibility. Maps legacy vars to new getters.
40
	 * @param  string $key
41
	 * @return mixed
42
	 */
43
	public function __get( $key ) {
44
		_doing_it_wrong( $key, 'Coupon properties should not be accessed directly.', '2.7' );
45
46
		switch( $key ) {
47
			case 'id' :
48
				$value = $this->get_id();
49
			break;
50
			case 'exists' :
51
				$value = ( $this->get_id() > 0 ) ? true : false;
52
			break;
53
			case 'coupon_custom_fields' :
54
				$legacy_custom_fields = array();
55
				$custom_fields = $this->get_id() ? $this->get_meta_data() : array();
56
				if ( ! empty( $custom_fields ) ) {
57
					foreach ( $custom_fields as  $cf_value ) {
58
						// legacy only supports 1 key
59
						$legacy_custom_fields[ $cf_value->key ][0] = $cf_value->value;
60
					}
61
				}
62
				$value = $legacy_custom_fields;
63
			break;
64
			case 'type' :
65
			case 'discount_type' :
66
				$value = $this->get_discount_type();
67
			break;
68
			case 'amount' :
69
				$value = $this->get_amount();
70
			break;
71
			case 'code' :
72
				$value = $this->get_code();
73
			break;
74
			case 'individual_use' :
75
				$value = ( true === $this->get_individual_use() ) ? 'yes' : 'no';
76
			break;
77
			case 'product_ids' :
78
				$value = $this->get_product_ids();
79
			break;
80
			case 'exclude_product_ids' :
81
				$value = $this->get_excluded_product_ids();
82
			break;
83
			case 'usage_limit' :
84
				$value = $this->get_usage_limit();
85
			break;
86
			case 'usage_limit_per_user' :
87
				$value = $this->get_usage_limit_per_user();
88
			break;
89
			case 'limit_usage_to_x_items' :
90
				$value = $this->get_limit_usage_to_x_items();
91
			break;
92
			case 'usage_count' :
93
				$value = $this->get_usage_count();
94
			break;
95
			case 'expiry_date' :
96
				$value = $this->get_expiry_date();
97
			break;
98
			case 'product_categories' :
99
				$value = $this->get_product_categories();
100
			break;
101
			case 'exclude_product_categories' :
102
				$value = $this->get_excluded_product_categories();
103
			break;
104
			case 'minimum_amount' :
105
				$value = $this->get_minimum_amount();
106
			break;
107
			case 'maximum_amount' :
108
				$value = $this->get_maximum_amount();
109
			break;
110
			case 'customer_email' :
111
				$value = $this->get_email_restrictions();
112
			break;
113
			default :
114
				$value = '';
115
			break;
116
		}
117
118
		return $value;
119
	}
120
121
	/**
122
	 * Format loaded data as array.
123
	 * @param  string|array $array
124
	 * @return array
125
	 */
126
	public function format_array( $array ) {
127
		_deprecated_function( 'format_array', '2.7', '' );
128
		if ( ! is_array( $array ) ) {
129
			if ( is_serialized( $array ) ) {
130
				$array = maybe_unserialize( $array );
131
			} else {
132
				$array = explode( ',', $array );
133
			}
134
		}
135
		return array_filter( array_map( 'trim', array_map( 'strtolower', $array ) ) );
136
	}
137
138
139
	/**
140
	 * Check if coupon needs applying before tax.
141
	 *
142
	 * @return bool
143
	 */
144
	public function apply_before_tax() {
145
		_deprecated_function( 'apply_before_tax', '2.7', '' );
146
		return true;
147
	}
148
149
	/**
150
	 * Check if a coupon enables free shipping.
151
	 *
152
	 * @return bool
153
	 */
154
	public function enable_free_shipping() {
155
		_deprecated_function( 'enable_free_shipping', '2.7', 'get_free_shipping' );
156
		return $this->get_free_shipping();
157
	}
158
159
	/**
160
	 * Check if a coupon excludes sale items.
161
	 *
162
	 * @return bool
163
	 */
164
	public function exclude_sale_items() {
165
		_deprecated_function( 'exclude_sale_items', '2.7', 'get_exclude_sale_items' );
166
		return $this->get_exclude_sale_items();
0 ignored issues
show
Bug introduced by
The method get_exclude_sale_items() does not exist on WC_Legacy_Coupon. Did you maybe mean exclude_sale_items()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
167
	}
168
169
}
170