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 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, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
18 | class WC_Order extends WC_Abstract_Order { |
||
19 | |||
20 | /** |
||
21 | * When a payment is complete this function is called. |
||
22 | * |
||
23 | * Most of the time this should mark an order as 'processing' so that admin can process/post the items. |
||
24 | * If the cart contains only downloadable items then the order is 'completed' since the admin needs to take no action. |
||
25 | * Stock levels are reduced at this point. |
||
26 | * Sales are also recorded for products. |
||
27 | * Finally, record the date of payment. |
||
28 | * |
||
29 | * @param string $transaction_id Optional transaction id to store in post meta. |
||
30 | */ |
||
31 | public function payment_complete( $transaction_id = '' ) { |
||
67 | |||
68 | /* |
||
69 | |-------------------------------------------------------------------------- |
||
70 | | Conditionals |
||
71 | |-------------------------------------------------------------------------- |
||
72 | | |
||
73 | | Checks if a condition is true or false. |
||
74 | | |
||
75 | */ |
||
76 | |||
77 | /** |
||
78 | * Checks if an order can be edited, specifically for use on the Edit Order screen. |
||
79 | * @return bool |
||
80 | */ |
||
81 | public function is_editable() { |
||
84 | |||
85 | /** |
||
86 | * Returns if an order has been paid for based on the order status. |
||
87 | * @since 2.5.0 |
||
88 | * @return bool |
||
89 | */ |
||
90 | public function is_paid() { |
||
93 | |||
94 | /** |
||
95 | * Checks if product download is permitted. |
||
96 | * |
||
97 | * @return bool |
||
98 | */ |
||
99 | public function is_download_permitted() { |
||
102 | |||
103 | /** |
||
104 | * Checks if an order needs display the shipping address, based on shipping method. |
||
105 | * @return bool |
||
106 | */ |
||
107 | public function needs_shipping_address() { |
||
124 | |||
125 | /** |
||
126 | * Returns true if the order contains a downloadable product. |
||
127 | * @return bool |
||
128 | */ |
||
129 | public function has_downloadable_item() { |
||
137 | |||
138 | /** |
||
139 | * Checks if an order needs payment, based on status and order total. |
||
140 | * |
||
141 | * @return bool |
||
142 | */ |
||
143 | public function needs_payment() { |
||
147 | |||
148 | /* |
||
149 | |-------------------------------------------------------------------------- |
||
150 | | Downloadable file permissions |
||
151 | |-------------------------------------------------------------------------- |
||
152 | */ |
||
153 | |||
154 | /** |
||
155 | * Get the Download URL. |
||
156 | * |
||
157 | * @param int $product_id |
||
158 | * @param int $download_id |
||
159 | * @return string |
||
160 | */ |
||
161 | public function get_download_url( $product_id, $download_id ) { |
||
169 | |||
170 | /** |
||
171 | * Get the downloadable files for an item in this order. |
||
172 | * |
||
173 | * @param array $item |
||
174 | * @return array |
||
175 | */ |
||
176 | public function get_item_downloads( $item ) { |
||
209 | |||
210 | /** |
||
211 | * Display download links for an order item. |
||
212 | * @param array $item |
||
213 | */ |
||
214 | public function display_item_downloads( $item ) { |
||
231 | |||
232 | /** |
||
233 | * Gets order total - formatted for display. |
||
234 | * @return string |
||
235 | */ |
||
236 | public function get_formatted_order_total( $tax_display = '', $display_refunded = true ) { |
||
268 | |||
269 | /* |
||
270 | |-------------------------------------------------------------------------- |
||
271 | | URLs and Endpoints |
||
272 | |-------------------------------------------------------------------------- |
||
273 | */ |
||
274 | |||
275 | /** |
||
276 | * Generates a URL so that a customer can pay for their (unpaid - pending) order. Pass 'true' for the checkout version which doesn't offer gateway choices. |
||
277 | * |
||
278 | * @param bool $on_checkout |
||
279 | * @return string |
||
280 | */ |
||
281 | public function get_checkout_payment_url( $on_checkout = false ) { |
||
296 | |||
297 | /** |
||
298 | * Generates a URL for the thanks page (order received). |
||
299 | * |
||
300 | * @return string |
||
301 | */ |
||
302 | public function get_checkout_order_received_url() { |
||
313 | |||
314 | /** |
||
315 | * Generates a URL so that a customer can cancel their (unpaid - pending) order. |
||
316 | * |
||
317 | * @param string $redirect |
||
318 | * |
||
319 | * @return string |
||
320 | */ |
||
321 | View Code Duplication | public function get_cancel_order_url( $redirect = '' ) { |
|
329 | |||
330 | /** |
||
331 | * Generates a raw (unescaped) cancel-order URL for use by payment gateways. |
||
332 | * |
||
333 | * @param string $redirect |
||
334 | * |
||
335 | * @return string The unescaped cancel-order URL. |
||
336 | */ |
||
337 | View Code Duplication | public function get_cancel_order_url_raw( $redirect = '' ) { |
|
346 | |||
347 | /** |
||
348 | * Helper method to return the cancel endpoint. |
||
349 | * |
||
350 | * @return string the cancel endpoint; either the cart page or the home page. |
||
351 | */ |
||
352 | public function get_cancel_endpoint() { |
||
364 | |||
365 | /** |
||
366 | * Generates a URL to view an order from the my account page. |
||
367 | * |
||
368 | * @return string |
||
369 | */ |
||
370 | public function get_view_order_url() { |
||
373 | |||
374 | /* |
||
375 | |-------------------------------------------------------------------------- |
||
376 | | Order notes. |
||
377 | |-------------------------------------------------------------------------- |
||
378 | */ |
||
379 | |||
380 | /** |
||
381 | * Adds a note (comment) to the order. |
||
382 | * |
||
383 | * @param string $note Note to add. |
||
384 | * @param int $is_customer_note (default: 0) Is this a note for the customer? |
||
385 | * @param bool added_by_user Was the note added by a user? |
||
386 | * @return int Comment ID. |
||
387 | */ |
||
388 | public function add_order_note( $note, $is_customer_note = 0, $added_by_user = false ) { |
||
419 | |||
420 | /** |
||
421 | * List order notes (public) for the customer. |
||
422 | * |
||
423 | * @return array |
||
424 | */ |
||
425 | public function get_customer_order_notes() { |
||
449 | |||
450 | /* |
||
451 | |-------------------------------------------------------------------------- |
||
452 | | Refunds |
||
453 | |-------------------------------------------------------------------------- |
||
454 | */ |
||
455 | |||
456 | /** |
||
457 | * Get order refunds. |
||
458 | * @since 2.2 |
||
459 | * @return array of WC_Order_Refund objects |
||
460 | */ |
||
461 | public function get_refunds() { |
||
479 | |||
480 | /** |
||
481 | * Get amount already refunded. |
||
482 | * |
||
483 | * @since 2.2 |
||
484 | * @return string |
||
485 | */ |
||
486 | View Code Duplication | public function get_total_refunded() { |
|
499 | |||
500 | /** |
||
501 | * Get the total tax refunded. |
||
502 | * |
||
503 | * @since 2.3 |
||
504 | * @return float |
||
505 | */ |
||
506 | View Code Duplication | public function get_total_tax_refunded() { |
|
520 | |||
521 | /** |
||
522 | * Get the total shipping refunded. |
||
523 | * |
||
524 | * @since 2.4 |
||
525 | * @return float |
||
526 | */ |
||
527 | View Code Duplication | public function get_total_shipping_refunded() { |
|
541 | |||
542 | /** |
||
543 | * Gets the count of order items of a certain type that have been refunded. |
||
544 | * @since 2.4.0 |
||
545 | * @param string $item_type |
||
546 | * @return string |
||
547 | */ |
||
548 | public function get_item_count_refunded( $item_type = '' ) { |
||
565 | |||
566 | /** |
||
567 | * Get the total number of items refunded. |
||
568 | * |
||
569 | * @since 2.4.0 |
||
570 | * @param string $item_type type of the item we're checking, if not a line_item |
||
571 | * @return integer |
||
572 | */ |
||
573 | public function get_total_qty_refunded( $item_type = 'line_item' ) { |
||
582 | |||
583 | /** |
||
584 | * Get the refunded amount for a line item. |
||
585 | * |
||
586 | * @param int $item_id ID of the item we're checking |
||
587 | * @param string $item_type type of the item we're checking, if not a line_item |
||
588 | * @return integer |
||
589 | */ |
||
590 | public function get_qty_refunded_for_item( $item_id, $item_type = 'line_item' ) { |
||
601 | |||
602 | /** |
||
603 | * Get the refunded amount for a line item. |
||
604 | * |
||
605 | * @param int $item_id ID of the item we're checking |
||
606 | * @param string $item_type type of the item we're checking, if not a line_item |
||
607 | * @return integer |
||
608 | */ |
||
609 | public function get_total_refunded_for_item( $item_id, $item_type = 'line_item' ) { |
||
627 | |||
628 | /** |
||
629 | * Get the refunded amount for a line item. |
||
630 | * |
||
631 | * @param int $item_id ID of the item we're checking |
||
632 | * @param int $tax_id ID of the tax we're checking |
||
633 | * @param string $item_type type of the item we're checking, if not a line_item |
||
634 | * @return double |
||
635 | */ |
||
636 | public function get_tax_refunded_for_item( $item_id, $tax_id, $item_type = 'line_item' ) { |
||
660 | |||
661 | /** |
||
662 | * Get total tax refunded by rate ID. |
||
663 | * |
||
664 | * @param int $rate_id |
||
665 | * |
||
666 | * @return float |
||
667 | */ |
||
668 | public function get_total_tax_refunded_by_rate_id( $rate_id ) { |
||
680 | |||
681 | /** |
||
682 | * How much money is left to refund? |
||
683 | * @return string |
||
684 | */ |
||
685 | public function get_remaining_refund_amount() { |
||
688 | |||
689 | /** |
||
690 | * How many items are left to refund? |
||
691 | * @return int |
||
692 | */ |
||
693 | public function get_remaining_refund_items() { |
||
696 | } |
||
697 |
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.