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_Abstract_Legacy_Order 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_Abstract_Legacy_Order, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
17 | abstract class WC_Abstract_Legacy_Order { |
||
18 | |||
19 | /** |
||
20 | * Update a line item for the order. |
||
21 | * |
||
22 | * Note this does not update order totals. |
||
23 | * |
||
24 | * @since 2.2 |
||
25 | * @param object|int $item order item ID or item object. |
||
26 | * @param WC_Product $product |
||
27 | * @param array $args data to update. |
||
28 | * @return int updated order item ID |
||
29 | */ |
||
30 | public function update_product( $item, $product, $args ) { |
||
71 | |||
72 | /** |
||
73 | * Update coupon for order. Note this does not update order totals. |
||
74 | * @since 2.2 |
||
75 | * @param object|int $item |
||
76 | * @param array $args |
||
77 | * @return int updated order item ID |
||
78 | */ |
||
79 | public function update_coupon( $item, $args ) { |
||
107 | |||
108 | /** |
||
109 | * Update shipping method for order. |
||
110 | * |
||
111 | * Note this does not update the order total. |
||
112 | * |
||
113 | * @since 2.2 |
||
114 | * @param object|int $item |
||
115 | * @param array $args |
||
116 | * @return int updated order item ID |
||
117 | */ |
||
118 | public function update_shipping( $item, $args ) { |
||
144 | |||
145 | /** |
||
146 | * Update fee for order. |
||
147 | * |
||
148 | * Note this does not update order totals. |
||
149 | * |
||
150 | * @since 2.2 |
||
151 | * @param object|int $item |
||
152 | * @param array $args |
||
153 | * @return int updated order item ID |
||
154 | */ |
||
155 | View Code Duplication | public function update_fee( $item, $args ) { |
|
175 | |||
176 | /** |
||
177 | * Update tax line on order. |
||
178 | * Note this does not update order totals. |
||
179 | * |
||
180 | * @since 2.6 |
||
181 | * @param object|int $item |
||
182 | * @param array $args |
||
183 | * @return int updated order item ID |
||
184 | */ |
||
185 | View Code Duplication | public function update_tax( $item, $args ) { |
|
205 | |||
206 | /** |
||
207 | * Get a product (either product or variation). |
||
208 | * @deprecated Add deprecation notices in future release. Replaced with $item->get_product() |
||
209 | * @param object $item |
||
210 | * @return WC_Product|bool |
||
211 | */ |
||
212 | public function get_product_from_item( $item ) { |
||
220 | |||
221 | /** |
||
222 | * Set the customer address. |
||
223 | * @since 2.2.0 |
||
224 | * @param array $address Address data. |
||
225 | * @param string $type billing or shipping. |
||
226 | */ |
||
227 | public function set_address( $address, $type = 'billing' ) { |
||
235 | |||
236 | /** |
||
237 | * Set an order total. |
||
238 | * @since 2.2.0 |
||
239 | * @param float $amount |
||
240 | * @param string $total_type |
||
241 | * @return bool |
||
242 | */ |
||
243 | public function legacy_set_total( $amount, $total_type = 'total' ) { |
||
283 | |||
284 | /** |
||
285 | * Magic __isset method for backwards compatibility. |
||
286 | * @param string $key |
||
287 | * @return bool |
||
288 | */ |
||
289 | public function __isset( $key ) { |
||
294 | |||
295 | /** |
||
296 | * Magic __get method for backwards compatibility. |
||
297 | * @param string $key |
||
298 | * @return mixed |
||
299 | */ |
||
300 | public function __get( $key ) { |
||
351 | |||
352 | /** |
||
353 | * has_meta function for order items. |
||
354 | * |
||
355 | * @param string $order_item_id |
||
356 | * @return array of meta data. |
||
357 | */ |
||
358 | public function has_meta( $order_item_id ) { |
||
367 | |||
368 | /** |
||
369 | * Display meta data belonging to an item. |
||
370 | * @param array $item |
||
371 | */ |
||
372 | public function display_item_meta( $item ) { |
||
378 | |||
379 | /** |
||
380 | * Display download links for an order item. |
||
381 | * @param array $item |
||
382 | */ |
||
383 | public function display_item_downloads( $item ) { |
||
400 | |||
401 | /** |
||
402 | * Get the Download URL. |
||
403 | * |
||
404 | * @param int $product_id |
||
405 | * @param int $download_id |
||
406 | * @return string |
||
407 | */ |
||
408 | public function get_download_url( $product_id, $download_id ) { |
||
416 | |||
417 | /** |
||
418 | * Get the downloadable files for an item in this order. |
||
419 | * |
||
420 | * @param array $item |
||
421 | * @return array |
||
422 | */ |
||
423 | public function get_item_downloads( $item ) { |
||
456 | |||
457 | /** |
||
458 | * Gets shipping total. Alias of WC_Order::get_shipping_total(). |
||
459 | * @deprecated 2.6.0 since this is an alias only. |
||
460 | * @return float |
||
461 | */ |
||
462 | public function get_total_shipping() { |
||
465 | |||
466 | /** |
||
467 | * Get order item meta. |
||
468 | * @deprecated 2.6.0 |
||
469 | * @param mixed $order_item_id |
||
470 | * @param string $key (default: '') |
||
471 | * @param bool $single (default: false) |
||
472 | * @return array|string |
||
473 | */ |
||
474 | public function get_item_meta( $order_item_id, $key = '', $single = false ) { |
||
478 | |||
479 | /** |
||
480 | * Get all item meta data in array format in the order it was saved. Does not group meta by key like get_item_meta(). |
||
481 | * |
||
482 | * @param mixed $order_item_id |
||
483 | * @return array of objects |
||
484 | */ |
||
485 | public function get_item_meta_array( $order_item_id ) { |
||
490 | |||
491 | /** |
||
492 | * Expand item meta into the $item array. |
||
493 | * @deprecated 2.6.0 Item meta no longer expanded due to new order item |
||
494 | * classes. This function now does nothing to avoid data breakage. |
||
495 | * @since 2.4.0 |
||
496 | * @param array $item before expansion. |
||
497 | * @return array |
||
498 | */ |
||
499 | public function expand_item_meta( $item ) { |
||
503 | |||
504 | /** |
||
505 | * Load the order object. Called from the constructor. |
||
506 | * @deprecated 2.6.0 Logic moved to constructor |
||
507 | * @param int|object|WC_Order $order Order to init. |
||
508 | */ |
||
509 | View Code Duplication | protected function init( $order ) { |
|
519 | |||
520 | /** |
||
521 | * Gets an order from the database. |
||
522 | * @deprecated 2.6 |
||
523 | * @param int $id (default: 0). |
||
524 | * @return bool |
||
525 | */ |
||
526 | View Code Duplication | public function get_order( $id = 0 ) { |
|
537 | |||
538 | /** |
||
539 | * Populates an order from the loaded post data. |
||
540 | * @deprecated 2.6 |
||
541 | * @param mixed $result |
||
542 | */ |
||
543 | public function populate( $result ) { |
||
547 | |||
548 | /** |
||
549 | * Cancel the order and restore the cart (before payment). |
||
550 | * @deprecated 2.6.0 Moved to event handler. |
||
551 | * @param string $note (default: '') Optional note to add. |
||
552 | */ |
||
553 | public function cancel_order( $note = '' ) { |
||
558 | |||
559 | /** |
||
560 | * Record sales. |
||
561 | * @deprecated 2.6.0 |
||
562 | */ |
||
563 | public function record_product_sales() { |
||
567 | |||
568 | /** |
||
569 | * Increase applied coupon counts. |
||
570 | * @deprecated 2.6.0 |
||
571 | */ |
||
572 | public function increase_coupon_usage_counts() { |
||
576 | |||
577 | /** |
||
578 | * Decrease applied coupon counts. |
||
579 | * @deprecated 2.6.0 |
||
580 | */ |
||
581 | public function decrease_coupon_usage_counts() { |
||
585 | |||
586 | /** |
||
587 | * Reduce stock levels for all line items in the order. |
||
588 | * @deprecated 2.6.0 |
||
589 | */ |
||
590 | public function reduce_order_stock() { |
||
594 | |||
595 | /** |
||
596 | * Send the stock notifications. |
||
597 | * @deprecated 2.6.0 No longer needs to be called directly. |
||
598 | */ |
||
599 | public function send_stock_notifications( $product, $new_stock, $qty_ordered ) { |
||
602 | |||
603 | /** |
||
604 | * Output items for display in html emails. |
||
605 | * @deprecated 2.6.0 Moved to template functions. |
||
606 | * @param array $args Items args. |
||
607 | * @return string |
||
608 | */ |
||
609 | public function email_order_items_table( $args = array() ) { |
||
612 | } |
||
613 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.