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

WC_Order_Item_Shipping   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 220
Duplicated Lines 5 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 11
loc 220
rs 10
wmc 26
lcom 1
cbo 1

19 Methods

Rating   Name   Duplication   Size   Complexity  
A offsetGet() 0 6 2
A offsetSet() 0 6 2
A offsetExists() 0 6 2
A read() 0 9 2
A save() 0 11 2
A get_internal_meta_keys() 0 3 1
A set_name() 0 3 1
A set_method_title() 0 3 1
A set_method_id() 0 3 1
A set_total() 0 3 1
A set_total_tax() 0 3 1
A set_taxes() 11 11 2
A get_type() 0 3 1
A get_name() 0 3 1
A get_method_title() 0 3 2
A get_method_id() 0 3 1
A get_total() 0 3 1
A get_total_tax() 0 3 1
A get_taxes() 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 (shipping).
8
 *
9
 * @version     2.7.0
10
 * @since       2.7.0
11
 * @package     WooCommerce/Classes
12
 * @author      WooThemes
13
 */
14
class WC_Order_Item_Shipping 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
		'method_title'  => '',
25
		'method_id'     => '',
26
		'total'         => 0,
27
		'total_tax'     => 0,
28
		'taxes'         => array(
29
			'total' => array()
30
		),
31
	);
32
33
	/**
34
	 * offsetGet for ArrayAccess/Backwards compatibility.
35
	 * @deprecated Add deprecation notices in future release.
36
	 * @param string $offset
37
	 * @return mixed
38
	 */
39
	public function offsetGet( $offset ) {
40
		if ( 'cost' === $offset ) {
41
			$offset = 'total';
42
		}
43
		return parent::offsetGet( $offset );
44
	}
45
46
	/**
47
	 * offsetSet for ArrayAccess/Backwards compatibility.
48
	 * @deprecated Add deprecation notices in future release.
49
	 * @param string $offset
50
	 * @param mixed $value
51
	 */
52
	public function offsetSet( $offset, $value ) {
53
		if ( 'cost' === $offset ) {
54
			$offset = 'total';
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( 'cost' ) ) ) {
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_method_id( get_metadata( 'order_item', $this->get_id(), 'method_id', true ) );
78
			$this->set_total( get_metadata( 'order_item', $this->get_id(), 'cost', true ) );
79
			$this->set_total_tax( get_metadata( 'order_item', $this->get_id(), 'total_tax', true ) );
80
			$this->set_taxes( get_metadata( 'order_item', $this->get_id(), 'taxes', true ) );
81
		}
82
	}
83
84
	/**
85
	 * Save properties specific to this order item.
86
	 * @return int Item ID
87
	 */
88
	public function save() {
89
		parent::save();
90
		if ( $this->get_id() ) {
91
			wc_update_order_item_meta( $this->get_id(), 'method_id', $this->get_method_id() );
92
			wc_update_order_item_meta( $this->get_id(), 'cost', $this->get_total() );
93
			wc_update_order_item_meta( $this->get_id(), 'total_tax', $this->get_total_tax() );
94
			wc_update_order_item_meta( $this->get_id(), 'taxes', $this->get_taxes() );
95
		}
96
97
		return $this->get_id();
98
	}
99
100
	/**
101
	 * Internal meta keys we don't want exposed as part of meta_data.
102
	 * @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...
103
	 */
104
	protected function get_internal_meta_keys() {
105
		return array( 'method_id', 'cost', 'total_tax', 'taxes' );
106
	}
107
108
	/*
109
	|--------------------------------------------------------------------------
110
	| Setters
111
	|--------------------------------------------------------------------------
112
	*/
113
114
	/**
115
	 * Set order item name.
116
	 * @param string $value
117
	 */
118
	public function set_name( $value ) {
119
		$this->set_method_title( $value );
120
	}
121
122
	/**
123
	 * Set code.
124
	 * @param string $value
125
	 */
126
	public function set_method_title( $value ) {
127
		$this->_data['method_title'] = wc_clean( $value );
128
	}
129
130
	/**
131
	 * Set shipping method id.
132
	 * @param string $value
133
	 */
134
	public function set_method_id( $value ) {
135
		$this->_data['method_id'] = wc_clean( $value );
136
	}
137
138
	/**
139
	 * Set total.
140
	 * @param string $value
141
	 */
142
	public function set_total( $value ) {
143
		$this->_data['total'] = wc_format_decimal( $value );
144
	}
145
146
	/**
147
	 * Set total tax.
148
	 * @param string $value
149
	 */
150
	public function set_total_tax( $value ) {
151
		$this->_data['total_tax'] = wc_format_decimal( $value );
152
	}
153
154
	/**
155
	 * Set taxes.
156
	 *
157
	 * This is an array of tax ID keys with total amount values.
158
	 * @param array $raw_tax_data
159
	 */
160 View Code Duplication
	public function set_taxes( $raw_tax_data ) {
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...
161
		$raw_tax_data = maybe_unserialize( $raw_tax_data );
162
		$tax_data     = array(
163
			'total'    => array()
164
		);
165
		if ( ! empty( $raw_tax_data['total'] ) ) {
166
			$tax_data['total']    = array_map( 'wc_format_decimal', $raw_tax_data['total'] );
167
		}
168
		$this->_data['taxes'] = $tax_data;
169
		$this->set_total_tax( array_sum( $tax_data['total'] ) );
170
	}
171
172
	/*
173
	|--------------------------------------------------------------------------
174
	| Getters
175
	|--------------------------------------------------------------------------
176
	*/
177
178
	/**
179
	 * Get order item type.
180
	 * @return string
181
	 */
182
	public function get_type() {
183
		return 'shipping';
184
	}
185
186
	/**
187
	 * Get order item name.
188
	 * @return string
189
	 */
190
	public function get_name() {
191
		return $this->get_method_title();
192
	}
193
194
	/**
195
	 * Get title.
196
	 * @return string
197
	 */
198
	public function get_method_title() {
199
		return $this->_data['method_title'] ? $this->_data['method_title'] : __( 'Shipping', 'woocommerce' );
200
	}
201
202
	/**
203
	 * Get method ID.
204
	 * @return string
205
	 */
206
	public function get_method_id() {
207
		return $this->_data['method_id'];
208
	}
209
210
	/**
211
	 * Get total cost.
212
	 * @return string
213
	 */
214
	public function get_total() {
215
		return wc_format_decimal( $this->_data['total'] );
216
	}
217
218
	/**
219
	 * Get total tax.
220
	 * @return string
221
	 */
222
	public function get_total_tax() {
223
		return wc_format_decimal( $this->_data['total_tax'] );
224
	}
225
226
	/**
227
	 * Get taxes.
228
	 * @return array
229
	 */
230
	public function get_taxes() {
231
		return $this->_data['taxes'];
232
	}
233
}
234