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:
Complex classes like WC_Order_Item_Product often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use WC_Order_Item_Product, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
14 | class WC_Order_Item_Product 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 | 'product_id' => 0, |
||
26 | 'variation_id' => 0, |
||
27 | 'quantity' => 0, |
||
28 | 'tax_class' => '', |
||
29 | 'subtotal' => 0, |
||
30 | 'subtotal_tax' => 0, |
||
31 | 'total' => 0, |
||
32 | 'total_tax' => 0, |
||
33 | 'taxes' => array( |
||
34 | 'subtotal' => array(), |
||
35 | 'total' => array() |
||
36 | ), |
||
37 | ); |
||
38 | |||
39 | /** |
||
40 | * offsetGet for ArrayAccess/Backwards compatibility. |
||
41 | * @deprecated Add deprecation notices in future release. |
||
42 | * @param string $offset |
||
43 | * @return mixed |
||
44 | */ |
||
45 | View Code Duplication | public function offsetGet( $offset ) { |
|
59 | |||
60 | /** |
||
61 | * offsetSet for ArrayAccess/Backwards compatibility. |
||
62 | * @deprecated Add deprecation notices in future release. |
||
63 | * @param string $offset |
||
64 | * @param mixed $value |
||
65 | */ |
||
66 | View Code Duplication | public function offsetSet( $offset, $value ) { |
|
80 | |||
81 | /** |
||
82 | * offsetExists for ArrayAccess |
||
83 | * @param string $offset |
||
84 | * @return bool |
||
85 | */ |
||
86 | public function offsetExists( $offset ) { |
||
92 | |||
93 | /** |
||
94 | * Read/populate data properties specific to this order item. |
||
95 | */ |
||
96 | public function read( $id ) { |
||
110 | |||
111 | /** |
||
112 | * Save properties specific to this order item. |
||
113 | * @return int Item ID |
||
114 | */ |
||
115 | public function save() { |
||
131 | |||
132 | /** |
||
133 | * Internal meta keys we don't want exposed as part of meta_data. |
||
134 | * @return array() |
||
135 | */ |
||
136 | protected function get_internal_meta_keys() { |
||
139 | |||
140 | /** |
||
141 | * Get the associated product. |
||
142 | * @return WC_Product|bool |
||
143 | */ |
||
144 | public function get_product() { |
||
158 | |||
159 | /** |
||
160 | * Get the Download URL. |
||
161 | * @param int $download_id |
||
162 | * @return string |
||
163 | */ |
||
164 | public function get_item_download_url( $download_id ) { |
||
165 | $order = $this->get_order(); |
||
166 | |||
167 | return $order ? add_query_arg( array( |
||
168 | 'download_file' => $this->get_variation_id() ? $this->get_variation_id() : $this->get_product_id(), |
||
169 | 'order' => $order->get_order_key(), |
||
170 | 'email' => urlencode( $order->get_billing_email() ), |
||
171 | 'key' => $download_id |
||
172 | ), trailingslashit( home_url() ) ) : ''; |
||
173 | } |
||
174 | |||
175 | /** |
||
176 | * Get any associated downloadable files. |
||
177 | * @return array |
||
178 | */ |
||
179 | public function get_item_downloads() { |
||
180 | global $wpdb; |
||
181 | |||
182 | $files = array(); |
||
183 | $product = $this->get_product(); |
||
184 | $order = $this->get_order(); |
||
185 | |||
186 | if ( $product && $order && $product->is_downloadable() && $order->is_download_permitted() ) { |
||
187 | $download_ids = $wpdb->get_col( |
||
188 | $wpdb->prepare( |
||
189 | "SELECT download_id FROM {$wpdb->prefix}woocommerce_downloadable_product_permissions WHERE user_email = %s AND order_key = %s AND product_id = %d ORDER BY permission_id", |
||
190 | $order->get_billing_email(), |
||
191 | $order->get_order_key(), |
||
192 | $this->get_variation_id() ? $this->get_variation_id() : $this->get_product_id() |
||
193 | ) |
||
194 | ); |
||
195 | |||
196 | foreach ( $download_ids as $download_id ) { |
||
197 | if ( $product->has_file( $download_id ) ) { |
||
198 | $files[ $download_id ] = $product->get_file( $download_id ); |
||
199 | $files[ $download_id ]['download_url'] = $this->get_item_download_url( $download_id ); |
||
200 | } |
||
201 | } |
||
202 | } |
||
203 | |||
204 | return apply_filters( 'woocommerce_get_item_downloads', $files, $this, $order ); |
||
205 | } |
||
206 | |||
207 | /** |
||
208 | * Get tax status. |
||
209 | * @return string |
||
210 | */ |
||
211 | public function get_tax_status() { |
||
215 | |||
216 | /** |
||
217 | * Set meta data for backordered products. |
||
218 | */ |
||
219 | public function set_backorder_meta() { |
||
224 | |||
225 | /* |
||
226 | |-------------------------------------------------------------------------- |
||
227 | | Setters |
||
228 | |-------------------------------------------------------------------------- |
||
229 | */ |
||
230 | |||
231 | /** |
||
232 | * Set quantity. |
||
233 | * @param int $value |
||
234 | */ |
||
235 | public function set_quantity( $value ) { |
||
241 | |||
242 | /** |
||
243 | * Set tax class. |
||
244 | * @param string $value |
||
245 | */ |
||
246 | public function set_tax_class( $value ) { |
||
252 | |||
253 | /** |
||
254 | * Set Product ID |
||
255 | * @param int $value |
||
256 | */ |
||
257 | public function set_product_id( $value ) { |
||
260 | |||
261 | /** |
||
262 | * Set variation ID. |
||
263 | * @param int $value |
||
264 | */ |
||
265 | public function set_variation_id( $value ) { |
||
268 | |||
269 | /** |
||
270 | * Line subtotal (before discounts). |
||
271 | * @param string $value |
||
272 | */ |
||
273 | public function set_subtotal( $value ) { |
||
276 | |||
277 | /** |
||
278 | * Line total (after discounts). |
||
279 | * @param string $value |
||
280 | */ |
||
281 | public function set_total( $value ) { |
||
284 | |||
285 | /** |
||
286 | * Line subtotal tax (before discounts). |
||
287 | * @param string $value |
||
288 | */ |
||
289 | public function set_subtotal_tax( $value ) { |
||
292 | |||
293 | /** |
||
294 | * Line total tax (after discounts). |
||
295 | * @param string $value |
||
296 | */ |
||
297 | public function set_total_tax( $value ) { |
||
300 | |||
301 | /** |
||
302 | * Set line taxes. |
||
303 | * @param array $raw_tax_data |
||
304 | */ |
||
305 | public function set_taxes( $raw_tax_data ) { |
||
317 | |||
318 | /** |
||
319 | * Set variation data (stored as meta data - write only). |
||
320 | * @param array $data Key/Value pairs |
||
321 | */ |
||
322 | public function set_variation( $data ) { |
||
327 | |||
328 | /** |
||
329 | * Set properties based on passed in product object. |
||
330 | * @param WC_Product $product |
||
331 | */ |
||
332 | public function set_product( $product ) { |
||
342 | |||
343 | /* |
||
344 | |-------------------------------------------------------------------------- |
||
345 | | Getters |
||
346 | |-------------------------------------------------------------------------- |
||
347 | */ |
||
348 | |||
349 | /** |
||
350 | * Get order item type. |
||
351 | * @return string |
||
352 | */ |
||
353 | public function get_type() { |
||
356 | |||
357 | /** |
||
358 | * Get product ID. |
||
359 | * @return int |
||
360 | */ |
||
361 | public function get_product_id() { |
||
364 | |||
365 | /** |
||
366 | * Get variation ID. |
||
367 | * @return int |
||
368 | */ |
||
369 | public function get_variation_id() { |
||
372 | |||
373 | /** |
||
374 | * Get quantity. |
||
375 | * @return int |
||
376 | */ |
||
377 | public function get_quantity() { |
||
380 | |||
381 | /** |
||
382 | * Get tax class. |
||
383 | * @return string |
||
384 | */ |
||
385 | public function get_tax_class() { |
||
388 | |||
389 | /** |
||
390 | * Get subtotal. |
||
391 | * @return string |
||
392 | */ |
||
393 | public function get_subtotal() { |
||
396 | |||
397 | /** |
||
398 | * Get subtotal tax. |
||
399 | * @return string |
||
400 | */ |
||
401 | public function get_subtotal_tax() { |
||
404 | |||
405 | /** |
||
406 | * Get total. |
||
407 | * @return string |
||
408 | */ |
||
409 | public function get_total() { |
||
412 | |||
413 | /** |
||
414 | * Get total tax. |
||
415 | * @return string |
||
416 | */ |
||
417 | public function get_total_tax() { |
||
420 | |||
421 | /** |
||
422 | * Get fee taxes. |
||
423 | * @return array |
||
424 | */ |
||
425 | public function get_taxes() { |
||
428 | } |
||
429 |
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.