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 extends WC_Data { |
||
| 18 | |||
| 19 | /** |
||
| 20 | * Update a line item for the order. |
||
| 21 | * |
||
| 22 | * Note this does not update order totals. |
||
| 23 | * |
||
| 24 | * @param object|int $item order item ID or item object. |
||
| 25 | * @param WC_Product $product |
||
| 26 | * @param array $args data to update. |
||
| 27 | * @return int updated order item ID |
||
| 28 | */ |
||
| 29 | public function update_product( $item, $product, $args ) { |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Update coupon for order. Note this does not update order totals. |
||
| 73 | * @param object|int $item |
||
| 74 | * @param array $args |
||
| 75 | * @return int updated order item ID |
||
| 76 | */ |
||
| 77 | public function update_coupon( $item, $args ) { |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Update shipping method for order. |
||
| 108 | * |
||
| 109 | * Note this does not update the order total. |
||
| 110 | * |
||
| 111 | * @param object|int $item |
||
| 112 | * @param array $args |
||
| 113 | * @return int updated order item ID |
||
| 114 | */ |
||
| 115 | public function update_shipping( $item, $args ) { |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Update fee for order. |
||
| 144 | * |
||
| 145 | * Note this does not update order totals. |
||
| 146 | * |
||
| 147 | * @param object|int $item |
||
| 148 | * @param array $args |
||
| 149 | * @return int updated order item ID |
||
| 150 | */ |
||
| 151 | View Code Duplication | public function update_fee( $item, $args ) { |
|
| 171 | |||
| 172 | /** |
||
| 173 | * Update tax line on order. |
||
| 174 | * Note this does not update order totals. |
||
| 175 | * |
||
| 176 | * @since 2.7 |
||
| 177 | * @param object|int $item |
||
| 178 | * @param array $args |
||
| 179 | * @return int updated order item ID |
||
| 180 | */ |
||
| 181 | View Code Duplication | public function update_tax( $item, $args ) { |
|
| 201 | |||
| 202 | /** |
||
| 203 | * Get a product (either product or variation). |
||
| 204 | * @deprecated Add deprecation notices in future release. Replaced with $item->get_product() |
||
| 205 | * @param object $item |
||
| 206 | * @return WC_Product|bool |
||
| 207 | */ |
||
| 208 | public function get_product_from_item( $item ) { |
||
| 216 | |||
| 217 | /** |
||
| 218 | * Set the customer address. |
||
| 219 | * @param array $address Address data. |
||
| 220 | * @param string $type billing or shipping. |
||
| 221 | */ |
||
| 222 | public function set_address( $address, $type = 'billing' ) { |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Set an order total. |
||
| 233 | * @param float $amount |
||
| 234 | * @param string $total_type |
||
| 235 | * @return bool |
||
| 236 | */ |
||
| 237 | public function legacy_set_total( $amount, $total_type = 'total' ) { |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Magic __isset method for backwards compatibility. |
||
| 280 | * @param string $key |
||
| 281 | * @return bool |
||
| 282 | */ |
||
| 283 | public function __isset( $key ) { |
||
| 288 | |||
| 289 | /** |
||
| 290 | * Magic __get method for backwards compatibility. |
||
| 291 | * @param string $key |
||
| 292 | * @return mixed |
||
| 293 | */ |
||
| 294 | public function __get( $key ) { |
||
| 345 | |||
| 346 | /** |
||
| 347 | * has_meta function for order items. |
||
| 348 | * |
||
| 349 | * @param string $order_item_id |
||
| 350 | * @return array of meta data. |
||
| 351 | */ |
||
| 352 | public function has_meta( $order_item_id ) { |
||
| 361 | |||
| 362 | /** |
||
| 363 | * Display meta data belonging to an item. |
||
| 364 | * @param array $item |
||
| 365 | */ |
||
| 366 | public function display_item_meta( $item ) { |
||
| 372 | |||
| 373 | /** |
||
| 374 | * Display download links for an order item. |
||
| 375 | * @param array $item |
||
| 376 | */ |
||
| 377 | public function display_item_downloads( $item ) { |
||
| 395 | |||
| 396 | /** |
||
| 397 | * Get the Download URL. |
||
| 398 | * |
||
| 399 | * @param int $product_id |
||
| 400 | * @param int $download_id |
||
| 401 | * @return string |
||
| 402 | */ |
||
| 403 | public function get_download_url( $product_id, $download_id ) { |
||
| 412 | |||
| 413 | /** |
||
| 414 | * Get the downloadable files for an item in this order. |
||
| 415 | * |
||
| 416 | * @param array $item |
||
| 417 | * @return array |
||
| 418 | */ |
||
| 419 | public function get_item_downloads( $item ) { |
||
| 423 | |||
| 424 | /** |
||
| 425 | * Gets shipping total. Alias of WC_Order::get_shipping_total(). |
||
| 426 | * @deprecated 2.7.0 since this is an alias only. |
||
| 427 | * @return float |
||
| 428 | */ |
||
| 429 | public function get_total_shipping() { |
||
| 432 | |||
| 433 | /** |
||
| 434 | * Get order item meta. |
||
| 435 | * @deprecated 2.7.0 |
||
| 436 | * @param mixed $order_item_id |
||
| 437 | * @param string $key (default: '') |
||
| 438 | * @param bool $single (default: false) |
||
| 439 | * @return array|string |
||
| 440 | */ |
||
| 441 | public function get_item_meta( $order_item_id, $key = '', $single = false ) { |
||
| 445 | |||
| 446 | /** |
||
| 447 | * Get all item meta data in array format in the order it was saved. Does not group meta by key like get_item_meta(). |
||
| 448 | * |
||
| 449 | * @param mixed $order_item_id |
||
| 450 | * @return array of objects |
||
| 451 | */ |
||
| 452 | public function get_item_meta_array( $order_item_id ) { |
||
| 464 | |||
| 465 | /** |
||
| 466 | * Expand item meta into the $item array. |
||
| 467 | * @deprecated 2.7.0 Item meta no longer expanded due to new order item |
||
| 468 | * classes. This function now does nothing to avoid data breakage. |
||
| 469 | * @param array $item before expansion. |
||
| 470 | * @return array |
||
| 471 | */ |
||
| 472 | public function expand_item_meta( $item ) { |
||
| 476 | |||
| 477 | /** |
||
| 478 | * Load the order object. Called from the constructor. |
||
| 479 | * @deprecated 2.7.0 Logic moved to constructor |
||
| 480 | * @param int|object|WC_Order $order Order to init. |
||
| 481 | */ |
||
| 482 | View Code Duplication | protected function init( $order ) { |
|
| 492 | |||
| 493 | /** |
||
| 494 | * Gets an order from the database. |
||
| 495 | * @deprecated 2.7 |
||
| 496 | * @param int $id (default: 0). |
||
| 497 | * @return bool |
||
| 498 | */ |
||
| 499 | View Code Duplication | public function get_order( $id = 0 ) { |
|
| 510 | |||
| 511 | /** |
||
| 512 | * Populates an order from the loaded post data. |
||
| 513 | * @deprecated 2.7 |
||
| 514 | * @param mixed $result |
||
| 515 | */ |
||
| 516 | public function populate( $result ) { |
||
| 520 | |||
| 521 | /** |
||
| 522 | * Cancel the order and restore the cart (before payment). |
||
| 523 | * @deprecated 2.7.0 Moved to event handler. |
||
| 524 | * @param string $note (default: '') Optional note to add. |
||
| 525 | */ |
||
| 526 | public function cancel_order( $note = '' ) { |
||
| 531 | |||
| 532 | /** |
||
| 533 | * Record sales. |
||
| 534 | * @deprecated 2.7.0 |
||
| 535 | */ |
||
| 536 | public function record_product_sales() { |
||
| 540 | |||
| 541 | /** |
||
| 542 | * Increase applied coupon counts. |
||
| 543 | * @deprecated 2.7.0 |
||
| 544 | */ |
||
| 545 | public function increase_coupon_usage_counts() { |
||
| 549 | |||
| 550 | /** |
||
| 551 | * Decrease applied coupon counts. |
||
| 552 | * @deprecated 2.7.0 |
||
| 553 | */ |
||
| 554 | public function decrease_coupon_usage_counts() { |
||
| 558 | |||
| 559 | /** |
||
| 560 | * Reduce stock levels for all line items in the order. |
||
| 561 | * @deprecated 2.7.0 |
||
| 562 | */ |
||
| 563 | public function reduce_order_stock() { |
||
| 567 | |||
| 568 | /** |
||
| 569 | * Send the stock notifications. |
||
| 570 | * @deprecated 2.7.0 No longer needs to be called directly. |
||
| 571 | */ |
||
| 572 | public function send_stock_notifications( $product, $new_stock, $qty_ordered ) { |
||
| 575 | |||
| 576 | /** |
||
| 577 | * Output items for display in html emails. |
||
| 578 | * @deprecated 2.7.0 Moved to template functions. |
||
| 579 | * @param array $args Items args. |
||
| 580 | * @return string |
||
| 581 | */ |
||
| 582 | public function email_order_items_table( $args = array() ) { |
||
| 586 | |||
| 587 | /** |
||
| 588 | * Get currency. |
||
| 589 | * @deprecated 2.7.0 |
||
| 590 | */ |
||
| 591 | public function get_order_currency() { |
||
| 595 | } |
||
| 596 |
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.