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 |
||
14 | class WC_Order_Item_Fee 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 | 'id' => 0, |
||
24 | 'name' => '', |
||
25 | 'tax_class' => '', |
||
26 | 'tax_status' => 'taxable', |
||
27 | 'total' => '', |
||
28 | 'total_tax' => '', |
||
29 | 'taxes' => array( |
||
30 | 'total' => array() |
||
31 | ) |
||
32 | ); |
||
33 | |||
34 | /** |
||
35 | * offsetGet for ArrayAccess/Backwards compatibility. |
||
36 | * @deprecated Add deprecation notices in future release. |
||
37 | * @param string $offset |
||
38 | * @return mixed |
||
39 | */ |
||
40 | View Code Duplication | public function offsetGet( $offset ) { |
|
50 | |||
51 | /** |
||
52 | * offsetSet for ArrayAccess/Backwards compatibility. |
||
53 | * @deprecated Add deprecation notices in future release. |
||
54 | * @param string $offset |
||
55 | * @param mixed $value |
||
56 | */ |
||
57 | View Code Duplication | public function offsetSet( $offset, $value ) { |
|
67 | |||
68 | /** |
||
69 | * offsetExists for ArrayAccess |
||
70 | * @param string $offset |
||
71 | * @return bool |
||
72 | */ |
||
73 | public function offsetExists( $offset ) { |
||
79 | |||
80 | /** |
||
81 | * Read/populate data properties specific to this order item. |
||
82 | */ |
||
83 | public function read( $id ) { |
||
93 | |||
94 | /** |
||
95 | * Save properties specific to this order item. |
||
96 | * @return int Item ID |
||
97 | */ |
||
98 | public function save() { |
||
110 | |||
111 | /** |
||
112 | * Internal meta keys we don't want exposed as part of meta_data. |
||
113 | * @return array() |
||
114 | */ |
||
115 | protected function get_internal_meta_keys() { |
||
118 | |||
119 | /* |
||
120 | |-------------------------------------------------------------------------- |
||
121 | | Setters |
||
122 | |-------------------------------------------------------------------------- |
||
123 | */ |
||
124 | |||
125 | /** |
||
126 | * Set tax class. |
||
127 | * @param string $value |
||
128 | */ |
||
129 | public function set_tax_class( $value ) { |
||
135 | |||
136 | /** |
||
137 | * Set tax_status. |
||
138 | * @param string $value |
||
139 | */ |
||
140 | public function set_tax_status( $value ) { |
||
147 | |||
148 | /** |
||
149 | * Set total. |
||
150 | * @param string $value |
||
151 | */ |
||
152 | public function set_total( $value ) { |
||
155 | |||
156 | /** |
||
157 | * Set total tax. |
||
158 | * @param string $value |
||
159 | */ |
||
160 | public function set_total_tax( $value ) { |
||
163 | |||
164 | /** |
||
165 | * Set taxes. |
||
166 | * |
||
167 | * This is an array of tax ID keys with total amount values. |
||
168 | * @param array $raw_tax_data |
||
169 | */ |
||
170 | View Code Duplication | public function set_taxes( $raw_tax_data ) { |
|
180 | |||
181 | /* |
||
182 | |-------------------------------------------------------------------------- |
||
183 | | Getters |
||
184 | |-------------------------------------------------------------------------- |
||
185 | */ |
||
186 | |||
187 | /** |
||
188 | * Get order item name. |
||
189 | * @return string |
||
190 | */ |
||
191 | public function get_name() { |
||
194 | |||
195 | /** |
||
196 | * Get order item type. |
||
197 | * @return string |
||
198 | */ |
||
199 | public function get_type() { |
||
202 | |||
203 | /** |
||
204 | * Get tax class. |
||
205 | * @return string |
||
206 | */ |
||
207 | public function get_tax_class() { |
||
210 | |||
211 | /** |
||
212 | * Get tax status. |
||
213 | * @return string |
||
214 | */ |
||
215 | public function get_tax_status() { |
||
218 | |||
219 | /** |
||
220 | * Get total fee. |
||
221 | * @return string |
||
222 | */ |
||
223 | public function get_total() { |
||
226 | |||
227 | /** |
||
228 | * Get total tax. |
||
229 | * @return string |
||
230 | */ |
||
231 | public function get_total_tax() { |
||
234 | |||
235 | /** |
||
236 | * Get fee taxes. |
||
237 | * @return array |
||
238 | */ |
||
239 | public function get_taxes() { |
||
242 | } |
||
243 |
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.