Completed
Pull Request — master (#11208)
by Mike
09:50
created

WC_Order_Item_Coupon   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 174
Duplicated Lines 9.2 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 16
loc 174
rs 10
wmc 22
lcom 1
cbo 1

15 Methods

Rating   Name   Duplication   Size   Complexity  
A offsetGet() 8 8 3
A offsetSet() 8 8 3
A offsetExists() 0 6 2
A read() 0 7 2
A save() 0 9 2
A get_internal_meta_keys() 0 3 1
A set_name() 0 3 1
A set_code() 0 3 1
A set_discount() 0 3 1
A set_discount_tax() 0 3 1
A get_type() 0 3 1
A get_name() 0 3 1
A get_code() 0 3 1
A get_discount() 0 3 1
A get_discount_tax() 0 3 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
2
if ( ! defined( 'ABSPATH' ) ) {
3
	exit;
4
}
5
6
/**
7
 * Order Line Item (coupon).
8
 *
9
 * @version     2.7.0
10
 * @since       2.7.0
11
 * @package     WooCommerce/Classes
12
 * @author      WooThemes
13
 */
14
class WC_Order_Item_Coupon extends WC_Order_Item {
15
16
	/**
17
	 * Data properties of this order item object.
18
	 * @since 2.7.0
19
	 * @var array
20
	 */
21
	protected $_data = array(
22
		'order_id'      => 0,
23
		'order_item_id' => 0,
24
		'code'          => '',
25
		'discount'      => 0,
26
		'discount_tax'  => 0,
27
	);
28
29
	/**
30
	 * offsetGet for ArrayAccess/Backwards compatibility.
31
	 * @deprecated Add deprecation notices in future release.
32
	 * @param string $offset
33
	 * @return mixed
34
	 */
35 View Code Duplication
	public function offsetGet( $offset ) {
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...
36
		if ( 'discount_amount' === $offset ) {
37
			$offset = 'discount';
38
		} elseif ( 'discount_amount_tax' === $offset ) {
39
			$offset = 'discount_tax';
40
		}
41
		return parent::offsetGet( $offset );
42
	}
43
44
	/**
45
	 * offsetSet for ArrayAccess/Backwards compatibility.
46
	 * @deprecated Add deprecation notices in future release.
47
	 * @param string $offset
48
	 * @param mixed $value
49
	 */
50 View Code Duplication
	public function offsetSet( $offset, $value ) {
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...
51
		if ( 'discount_amount' === $offset ) {
52
			$offset = 'discount';
53
		} elseif ( 'discount_amount_tax' === $offset ) {
54
			$offset = 'discount_tax';
55
		}
56
		parent::offsetSet( $offset, $value );
57
	}
58
59
	/**
60
	 * offsetExists for ArrayAccess
61
	 * @param string $offset
62
	 * @return bool
63
	 */
64
	public function offsetExists( $offset ) {
65
		if ( in_array( $offset, array( 'discount_amount', 'discount_amount_tax' ) ) ) {
66
			return true;
67
		}
68
		return parent::offsetExists( $offset );
69
	}
70
71
	/**
72
	 * Read/populate data properties specific to this order item.
73
	 */
74
	public function read( $id ) {
75
		parent::read( $id );
76
		if ( $this->get_id() ) {
77
			$this->set_discount( get_metadata( 'order_item', $this->get_id(), 'discount_amount', true ) );
78
			$this->set_discount_tax( get_metadata( 'order_item', $this->get_id(), 'discount_amount_tax', true ) );
79
		}
80
	}
81
82
	/**
83
	 * Save properties specific to this order item.
84
	 * @return int Item ID
85
	 */
86
	public function save() {
87
		parent::save();
88
		if ( $this->get_id() ) {
89
			wc_update_order_item_meta( $this->get_id(), 'discount_amount', $this->get_discount() );
90
			wc_update_order_item_meta( $this->get_id(), 'discount_amount_tax', $this->get_discount_tax() );
91
		}
92
93
		return $this->get_id();
94
	}
95
96
	/**
97
	 * Internal meta keys we don't want exposed as part of meta_data.
98
	 * @return array()
0 ignored issues
show
Documentation introduced by
The doc-type array() could not be parsed: Expected "|" or "end of type", but got "(" at position 5. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
99
	 */
100
	protected function get_internal_meta_keys() {
101
		return array( 'discount_amount', 'discount_amount_tax' );
102
	}
103
104
	/*
105
	|--------------------------------------------------------------------------
106
	| Setters
107
	|--------------------------------------------------------------------------
108
	*/
109
110
	/**
111
	 * Set order item name.
112
	 * @param string $value
113
	 */
114
	public function set_name( $value ) {
115
		$this->set_code( $value );
116
	}
117
118
	/**
119
	 * Set code.
120
	 * @param string $value
121
	 */
122
	public function set_code( $value ) {
123
		$this->_data['code'] = wc_clean( $value );
124
	}
125
126
	/**
127
	 * Set discount amount.
128
	 * @param string $value
129
	 */
130
	public function set_discount( $value ) {
131
		$this->_data['discount'] =  wc_format_decimal( $value );
132
	}
133
134
	/**
135
	 * Set discounted tax amount.
136
	 * @param string $value
137
	 */
138
	public function set_discount_tax( $value ) {
139
		$this->_data['discount_tax'] = wc_format_decimal( $value );
140
	}
141
142
	/*
143
	|--------------------------------------------------------------------------
144
	| Getters
145
	|--------------------------------------------------------------------------
146
	*/
147
148
	/**
149
	 * Get order item type.
150
	 * @return string
151
	 */
152
	public function get_type() {
153
		return 'coupon';
154
	}
155
156
	/**
157
	 * Get order item name.
158
	 * @return string
159
	 */
160
	public function get_name() {
161
		return $this->get_code();
162
	}
163
164
	/**
165
	 * Get coupon code.
166
	 * @return string
167
	 */
168
	public function get_code() {
169
		return $this->_data['code'];
170
	}
171
172
	/**
173
	 * Get discount amount.
174
	 * @return string
175
	 */
176
	public function get_discount() {
177
		return wc_format_decimal( $this->_data['discount'] );
178
	}
179
180
	/**
181
	 * Get discounted tax amount.
182
	 * @return string
183
	 */
184
	public function get_discount_tax() {
185
		return wc_format_decimal( $this->_data['discount_tax'] );
186
	}
187
}
188