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_Product_Variation 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_Product_Variation, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
18 | class WC_Product_Variation extends WC_Product { |
||
19 | |||
20 | /** @public int ID of the variation itself. */ |
||
21 | public $variation_id; |
||
22 | |||
23 | /** @public object Parent Variable product object. */ |
||
24 | public $parent; |
||
25 | |||
26 | /** @public string Stores the shipping class of the variation. */ |
||
27 | public $variation_shipping_class = false; |
||
28 | |||
29 | /** @public int Stores the shipping class ID of the variation. */ |
||
30 | public $variation_shipping_class_id = false; |
||
31 | |||
32 | /** @public unused vars @deprecated in 2.2 */ |
||
33 | public $variation_has_sku = true; |
||
34 | public $variation_has_length = true; |
||
35 | public $variation_has_width = true; |
||
36 | public $variation_has_height = true; |
||
37 | public $variation_has_weight = true; |
||
38 | public $variation_has_tax_class = true; |
||
39 | public $variation_has_downloadable_files = true; |
||
40 | |||
41 | /** @private array Data which is only at variation level - no inheritance plus their default values if left blank. */ |
||
42 | protected $variation_level_meta_data = array( |
||
43 | 'downloadable' => 'no', |
||
44 | 'virtual' => 'no', |
||
45 | 'manage_stock' => 'no', |
||
46 | 'sale_price_dates_from' => '', |
||
47 | 'sale_price_dates_to' => '', |
||
48 | 'price' => '', |
||
49 | 'regular_price' => '', |
||
50 | 'sale_price' => '', |
||
51 | 'stock' => 0, |
||
52 | 'stock_status' => 'instock', |
||
53 | 'downloadable_files' => array() |
||
54 | ); |
||
55 | |||
56 | /** @private array Data which can be at variation level, otherwise fallback to parent if not set. */ |
||
57 | protected $variation_inherited_meta_data = array( |
||
58 | 'tax_class' => '', |
||
59 | 'backorders' => 'no', |
||
60 | 'sku' => '', |
||
61 | 'weight' => '', |
||
62 | 'length' => '', |
||
63 | 'width' => '', |
||
64 | 'height' => '' |
||
65 | ); |
||
66 | |||
67 | /** |
||
68 | * Loads required variation data. |
||
69 | * |
||
70 | * @param int $variation ID of the variation to load |
||
71 | * @param array $args Array of the arguments containing parent product data |
||
72 | */ |
||
73 | public function __construct( $variation, $args = array() ) { |
||
74 | if ( is_object( $variation ) ) { |
||
75 | $this->variation_id = absint( $variation->ID ); |
||
76 | } else { |
||
77 | $this->variation_id = absint( $variation ); |
||
78 | } |
||
79 | |||
80 | /* Get main product data from parent (args) */ |
||
81 | $this->id = ! empty( $args['parent_id'] ) ? intval( $args['parent_id'] ) : wp_get_post_parent_id( $this->variation_id ); |
||
82 | |||
83 | // The post doesn't have a parent id, therefore its invalid and we should prevent this being created. |
||
84 | if ( empty( $this->id ) ) { |
||
85 | throw new Exception( sprintf( 'No parent product set for variation #%d', $this->variation_id ), 422 ); |
||
86 | } |
||
87 | |||
88 | $this->product_type = 'variation'; |
||
89 | $this->parent = ! empty( $args['parent'] ) ? $args['parent'] : wc_get_product( $this->id ); |
||
90 | $this->post = ! empty( $this->parent->post ) ? $this->parent->post : array(); |
||
91 | } |
||
92 | |||
93 | /** |
||
94 | * __isset function. |
||
95 | * |
||
96 | * @param mixed $key |
||
97 | * @return bool |
||
98 | */ |
||
99 | public function __isset( $key ) { |
||
108 | |||
109 | /** |
||
110 | * Get method returns variation meta data if set, otherwise in most cases the data from the parent. |
||
111 | * |
||
112 | * @param string $key |
||
113 | * @return mixed |
||
114 | */ |
||
115 | public function __get( $key ) { |
||
149 | |||
150 | /** |
||
151 | * Return the variation ID |
||
152 | * |
||
153 | * @since 2.5.0 |
||
154 | * @return int variation (post) ID |
||
155 | */ |
||
156 | public function get_id() { |
||
159 | |||
160 | /** |
||
161 | * Returns whether or not the product post exists. |
||
162 | * |
||
163 | * @return bool |
||
164 | */ |
||
165 | public function exists() { |
||
168 | |||
169 | /** |
||
170 | * Wrapper for get_permalink. Adds this variations attributes to the URL. |
||
171 | * |
||
172 | * @param $item_object item array If a cart or order item is passed, we can get a link containing the exact attributes selected for the variation, rather than the default attributes. |
||
173 | * @return string |
||
174 | */ |
||
175 | public function get_permalink( $item_object = null ) { |
||
176 | if ( ! empty( $item_object['variation'] ) ) { |
||
177 | $data = $item_object['variation']; |
||
178 | } elseif ( ! empty( $item_object['item_meta_array'] ) ) { |
||
179 | $data_keys = array_map( 'wc_variation_attribute_name', wp_list_pluck( $item_object['item_meta_array'], 'key' ) ); |
||
180 | $data_values = wp_list_pluck( $item_object['item_meta_array'], 'value' ); |
||
181 | $data = array_intersect_key( array_combine( $data_keys, $data_values ), $this->variation_data ); |
||
182 | } else { |
||
183 | $data = $this->variation_data; |
||
184 | } |
||
185 | return add_query_arg( array_map( 'urlencode', array_filter( $data ) ), get_permalink( $this->id ) ); |
||
186 | } |
||
187 | |||
188 | /** |
||
189 | * Get the add to url used mainly in loops. |
||
190 | * |
||
191 | * @return string |
||
192 | */ |
||
193 | public function add_to_cart_url() { |
||
199 | |||
200 | /** |
||
201 | * Get the add to cart button text. |
||
202 | * |
||
203 | * @return string |
||
204 | */ |
||
205 | public function add_to_cart_text() { |
||
210 | |||
211 | /** |
||
212 | * Checks if this particular variation is visible. Invisible variations are enabled and can be selected, but no price / stock info is displayed. |
||
213 | * Instead, a suitable 'unavailable' message is displayed. |
||
214 | * Invisible by default: Disabled variations and variations with an empty price. |
||
215 | * |
||
216 | * @return bool |
||
217 | */ |
||
218 | public function variation_is_visible() { |
||
233 | |||
234 | /** |
||
235 | * Controls whether this particular variation will appear greyed-out (inactive) or not (active). |
||
236 | * Used by extensions to make incompatible variations appear greyed-out, etc. |
||
237 | * Other possible uses: prevent out-of-stock variations from being selected. |
||
238 | * |
||
239 | * @return bool |
||
240 | */ |
||
241 | public function variation_is_active() { |
||
244 | |||
245 | /** |
||
246 | * Returns false if the product cannot be bought. |
||
247 | * Override abstract method so that: i) Disabled variations are not be purchasable by admins. ii) Enabled variations are not purchasable if the parent product is not purchasable. |
||
248 | * |
||
249 | * @return bool |
||
250 | */ |
||
251 | public function is_purchasable() { |
||
260 | |||
261 | /** |
||
262 | * Returns whether or not the variations parent is visible. |
||
263 | * |
||
264 | * @return bool |
||
265 | */ |
||
266 | public function parent_is_visible() { |
||
269 | |||
270 | /** |
||
271 | * Get variation ID. |
||
272 | * |
||
273 | * @return int |
||
274 | */ |
||
275 | public function get_variation_id() { |
||
278 | |||
279 | /** |
||
280 | * Get variation attribute values. |
||
281 | * |
||
282 | * @return array of attributes and their values for this variation |
||
283 | */ |
||
284 | public function get_variation_attributes() { |
||
287 | |||
288 | /** |
||
289 | * Check if all variation's attributes are set. |
||
290 | * |
||
291 | * @return boolean |
||
292 | */ |
||
293 | public function has_all_attributes_set() { |
||
308 | |||
309 | /** |
||
310 | * Get variation price HTML. Prices are not inherited from parents. |
||
311 | * |
||
312 | * @return string containing the formatted price |
||
313 | */ |
||
314 | public function get_price_html( $price = '' ) { |
||
334 | |||
335 | /** |
||
336 | * Gets the main product image ID. |
||
337 | * |
||
338 | * @return int |
||
339 | */ |
||
340 | public function get_image_id() { |
||
352 | |||
353 | /** |
||
354 | * Gets the main product image. |
||
355 | * |
||
356 | * @param string $size (default: 'shop_thumbnail') |
||
357 | * @param bool True to return $placeholder if no image is found, or false to return an empty string. |
||
358 | * @return string |
||
359 | */ |
||
360 | public function get_image( $size = 'shop_thumbnail', $attr = array(), $placeholder = true ) { |
||
374 | |||
375 | /** |
||
376 | * Returns whether or not the product (or variation) is stock managed. |
||
377 | * |
||
378 | * @return bool|string Bool if managed at variation level, 'parent' if managed by the parent. |
||
379 | */ |
||
380 | public function managing_stock() { |
||
392 | |||
393 | /** |
||
394 | * Returns number of items available for sale from the variation, or parent. |
||
395 | * |
||
396 | * @return int |
||
397 | */ |
||
398 | public function get_stock_quantity() { |
||
401 | |||
402 | /** |
||
403 | * Returns the tax status. Always use parent data. |
||
404 | * |
||
405 | * @return string |
||
406 | */ |
||
407 | public function get_tax_status() { |
||
410 | |||
411 | /** |
||
412 | * Returns whether or not the product is in stock. |
||
413 | * |
||
414 | * @return bool |
||
415 | */ |
||
416 | View Code Duplication | public function is_in_stock() { |
|
432 | |||
433 | /** |
||
434 | * Set stock level of the product variation. |
||
435 | * |
||
436 | * Uses queries rather than update_post_meta so we can do this in one query (to avoid stock issues). |
||
437 | * We cannot rely on the original loaded value in case another order was made since then. |
||
438 | * |
||
439 | * @param int $amount |
||
440 | * @param string $mode can be set, add, or subtract |
||
441 | * @return int new stock level |
||
442 | */ |
||
443 | public function set_stock( $amount = null, $mode = 'set' ) { |
||
485 | |||
486 | /** |
||
487 | * Set stock status. |
||
488 | * |
||
489 | * @param string $status |
||
490 | */ |
||
491 | public function set_stock_status( $status ) { |
||
511 | |||
512 | /** |
||
513 | * Reduce stock level of the product. |
||
514 | * |
||
515 | * @param int $amount (default: 1) Amount to reduce by |
||
516 | * @return int stock level |
||
517 | */ |
||
518 | public function reduce_stock( $amount = 1 ) { |
||
525 | |||
526 | /** |
||
527 | * Increase stock level of the product. |
||
528 | * |
||
529 | * @param int $amount (default: 1) Amount to increase by |
||
530 | * @return int stock level |
||
531 | */ |
||
532 | public function increase_stock( $amount = 1 ) { |
||
539 | |||
540 | /** |
||
541 | * Returns the availability of the product. |
||
542 | * |
||
543 | * @return string |
||
544 | */ |
||
545 | public function get_availability() { |
||
546 | // Default to in-stock |
||
547 | $availability = __( 'In stock', 'woocommerce' ); |
||
548 | $class = 'in-stock'; |
||
549 | |||
550 | // If out of stock, this takes priority over all other settings. |
||
551 | if ( ! $this->is_in_stock() ) { |
||
552 | $availability = __( 'Out of stock', 'woocommerce' ); |
||
553 | $class = 'out-of-stock'; |
||
554 | |||
555 | // Any further we can assume status is set to in stock. |
||
556 | } elseif ( $this->managing_stock() && $this->is_on_backorder( 1 ) ) { |
||
557 | $availability = __( 'Available on backorder', 'woocommerce' ); |
||
558 | $class = 'available-on-backorder'; |
||
559 | |||
560 | View Code Duplication | } elseif ( true === $this->managing_stock() ) { |
|
561 | switch ( get_option( 'woocommerce_stock_format' ) ) { |
||
562 | case 'no_amount' : |
||
563 | $availability = __( 'In stock', 'woocommerce' ); |
||
564 | break; |
||
565 | case 'low_amount' : |
||
566 | if ( $this->get_stock_quantity() <= get_option( 'woocommerce_notify_low_stock_amount' ) ) { |
||
567 | $availability = sprintf( __( 'Only %s left in stock', 'woocommerce' ), $this->get_stock_quantity() ); |
||
568 | |||
569 | if ( $this->backorders_allowed() && $this->backorders_require_notification() ) { |
||
570 | $availability .= ' ' . __( '(also available on backorder)', 'woocommerce' ); |
||
571 | } |
||
572 | } else { |
||
573 | $availability = __( 'In stock', 'woocommerce' ); |
||
574 | } |
||
575 | break; |
||
576 | default : |
||
577 | $availability = sprintf( __( '%s in stock', 'woocommerce' ), $this->get_stock_quantity() ); |
||
578 | |||
579 | if ( $this->backorders_allowed() && $this->backorders_require_notification() ) { |
||
580 | $availability .= ' ' . __( '(also available on backorder)', 'woocommerce' ); |
||
581 | } |
||
582 | break; |
||
583 | } |
||
584 | } elseif ( 'parent' === $this->managing_stock() ) { |
||
585 | return parent::get_availability(); |
||
586 | } |
||
587 | |||
588 | return apply_filters( 'woocommerce_get_availability', array( 'availability' => $availability, 'class' => $class ), $this ); |
||
589 | } |
||
590 | |||
591 | /** |
||
592 | * Returns whether or not the product needs to notify the customer on backorder. |
||
593 | * |
||
594 | * @return bool |
||
595 | */ |
||
596 | public function backorders_require_notification() { |
||
603 | |||
604 | /** |
||
605 | * Is on backorder? |
||
606 | * |
||
607 | * @param int $qty_in_cart (default: 0) |
||
608 | * @return bool |
||
609 | */ |
||
610 | public function is_on_backorder( $qty_in_cart = 0 ) { |
||
617 | |||
618 | /** |
||
619 | * Returns whether or not the product has enough stock for the order. |
||
620 | * |
||
621 | * @param mixed $quantity |
||
622 | * @return bool |
||
623 | */ |
||
624 | public function has_enough_stock( $quantity ) { |
||
631 | |||
632 | /** |
||
633 | * Get the shipping class, and if not set, get the shipping class of the parent. |
||
634 | * |
||
635 | * @return string |
||
636 | */ |
||
637 | View Code Duplication | public function get_shipping_class() { |
|
649 | |||
650 | /** |
||
651 | * Returns the product shipping class ID. |
||
652 | * |
||
653 | * @return int |
||
654 | */ |
||
655 | View Code Duplication | public function get_shipping_class_id() { |
|
667 | |||
668 | /** |
||
669 | * Get formatted variation data with WC < 2.4 back compat and proper formatting of text-based attribute names. |
||
670 | * |
||
671 | * @return string |
||
672 | */ |
||
673 | public function get_formatted_variation_attributes( $flat = false ) { |
||
747 | |||
748 | /** |
||
749 | * Get product name with extra details such as SKU, price and attributes. Used within admin. |
||
750 | * |
||
751 | * @return string Formatted product name, including attributes and price |
||
752 | */ |
||
753 | public function get_formatted_name() { |
||
765 | |||
766 | /** |
||
767 | * Get product variation description. |
||
768 | * |
||
769 | * @return string |
||
770 | */ |
||
771 | public function get_variation_description() { |
||
774 | } |
||
775 |
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.