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_Cart_Totals 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_Cart_Totals, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | class WC_Cart_Totals { |
||
| 17 | |||
| 18 | /** |
||
| 19 | * Should tax be calculated? |
||
| 20 | * @var boolean |
||
| 21 | */ |
||
| 22 | private $calculate_tax = true; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Stores totals. |
||
| 26 | * @var array |
||
| 27 | */ |
||
| 28 | private $totals = null; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Coupons to calculate. |
||
| 32 | * @var array |
||
| 33 | */ |
||
| 34 | private $coupons = array(); |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Line items to calculate. |
||
| 38 | * @var array |
||
| 39 | */ |
||
| 40 | private $items = array(); |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Fees to calculate. |
||
| 44 | * @var array |
||
| 45 | */ |
||
| 46 | private $fees = array(); |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Shipping to calculate. |
||
| 50 | * @var array |
||
| 51 | */ |
||
| 52 | private $shipping_lines = array(); |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Constructor. |
||
| 56 | */ |
||
| 57 | public function __construct() { |
||
| 60 | |||
| 61 | /* |
||
| 62 | |-------------------------------------------------------------------------- |
||
| 63 | | Default item props. |
||
| 64 | |-------------------------------------------------------------------------- |
||
| 65 | */ |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Get default totals. |
||
| 69 | * @return array |
||
| 70 | */ |
||
| 71 | private function get_default_totals() { |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Get default blank set of props used per item. |
||
| 93 | * @return array |
||
| 94 | */ |
||
| 95 | private function get_default_item_props() { |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Get default blank set of props used per coupon. |
||
| 110 | * @return array |
||
| 111 | */ |
||
| 112 | private function get_default_coupon_props() { |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Get default blank set of props used per fee. |
||
| 122 | * @return array |
||
| 123 | */ |
||
| 124 | private function get_default_fee_props() { |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Get default blank set of props used per shipping row. |
||
| 133 | * @return array |
||
| 134 | */ |
||
| 135 | private function get_default_shipping_props() { |
||
| 142 | |||
| 143 | /* |
||
| 144 | |-------------------------------------------------------------------------- |
||
| 145 | | Calculation methods. |
||
| 146 | |-------------------------------------------------------------------------- |
||
| 147 | */ |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Removes data we don't want to expose in the item totals. |
||
| 151 | * @param object $item |
||
| 152 | * @return array |
||
| 153 | */ |
||
| 154 | private function format_item_totals( $item ) { |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Totals are costs after discounts. |
||
| 162 | */ |
||
| 163 | public function calculate_item_totals() { |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Subtotals are costs before discounts. |
||
| 193 | * |
||
| 194 | * To prevent rounding issues we need to work with the inclusive price where possible. |
||
| 195 | * otherwise we'll see errors such as when working with a 9.99 inc price, 20% VAT which would. |
||
| 196 | * be 8.325 leading to totals being 1p off. |
||
| 197 | * |
||
| 198 | * Pre tax coupons come off the price the customer thinks they are paying - tax is calculated. |
||
| 199 | * afterwards. |
||
| 200 | * |
||
| 201 | * e.g. $100 bike with $10 coupon = customer pays $90 and tax worked backwards from that. |
||
| 202 | */ |
||
| 203 | private function calculate_item_subtotals() { |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Calculate any fees taxes. |
||
| 228 | */ |
||
| 229 | private function calculate_fee_totals() { |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Main cart totals. |
||
| 244 | */ |
||
| 245 | public function calculate_totals() { |
||
| 265 | |||
| 266 | /** |
||
| 267 | * Sort items by the subtotal. |
||
| 268 | */ |
||
| 269 | private function sort_items_callback( $a, $b ) { |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Should discounts be applied sequentially? |
||
| 280 | * @return bool |
||
| 281 | */ |
||
| 282 | private function calc_discounts_sequentially() { |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Get an items price to discount (undiscounted price). |
||
| 288 | * @param object $item |
||
| 289 | * @return float |
||
| 290 | */ |
||
| 291 | private function get_undiscounted_price( $item ) { |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Get an individual items price after discounts are applied. |
||
| 301 | * @param object $item |
||
| 302 | * @return float |
||
| 303 | */ |
||
| 304 | private function get_discounted_price( $item ) { |
||
| 361 | |||
| 362 | /** |
||
| 363 | * Only ran if woocommerce_adjust_non_base_location_prices is true. |
||
| 364 | * |
||
| 365 | * If the customer is outside of the base location, this removes the base taxes. |
||
| 366 | * |
||
| 367 | * Pre 2.7, this was on by default. From 2.7 onwards this is off by default meaning |
||
| 368 | * that if a product costs 10 including tax, all users will pay 10 regardless of location and taxes. This was experiemental from 2.4.7. |
||
| 369 | */ |
||
| 370 | private function adjust_non_base_location_price( $item ) { |
||
| 385 | |||
| 386 | /* |
||
| 387 | |-------------------------------------------------------------------------- |
||
| 388 | | Setters. |
||
| 389 | |-------------------------------------------------------------------------- |
||
| 390 | */ |
||
| 391 | |||
| 392 | /** |
||
| 393 | * Should we calc tax? |
||
| 394 | * @param bool |
||
| 395 | */ |
||
| 396 | public function set_calculate_tax( $value ) { |
||
| 399 | |||
| 400 | /** |
||
| 401 | * Set all totals. |
||
| 402 | * @param array $value |
||
| 403 | */ |
||
| 404 | public function set_totals( $value ) { |
||
| 408 | |||
| 409 | /** |
||
| 410 | * Sets items and adds precision which lets us work with integers. |
||
| 411 | * @param array $items |
||
| 412 | */ |
||
| 413 | public function set_items( $items ) { |
||
| 426 | |||
| 427 | /** |
||
| 428 | * Set coupons. |
||
| 429 | * @param array $coupons |
||
| 430 | */ |
||
| 431 | public function set_coupons( $coupons ) { |
||
| 438 | |||
| 439 | /** |
||
| 440 | * Set fees. |
||
| 441 | * @param array $fees |
||
| 442 | */ |
||
| 443 | public function set_fees( $fees ) { |
||
| 452 | |||
| 453 | /** |
||
| 454 | * Set shipping lines. |
||
| 455 | * @param array |
||
| 456 | */ |
||
| 457 | public function set_shipping( $shipping_objects ) { |
||
| 470 | |||
| 471 | /** |
||
| 472 | * Set taxes. |
||
| 473 | * @param array $value |
||
| 474 | */ |
||
| 475 | private function set_taxes( $value ) { |
||
| 478 | |||
| 479 | /** |
||
| 480 | * Set tax total. |
||
| 481 | * @param float $value |
||
| 482 | */ |
||
| 483 | private function set_tax_total( $value ) { |
||
| 486 | |||
| 487 | /** |
||
| 488 | * Set shipping total. |
||
| 489 | * @param float $value |
||
| 490 | */ |
||
| 491 | private function set_shipping_total( $value ) { |
||
| 494 | |||
| 495 | /** |
||
| 496 | * Set shipping tax total. |
||
| 497 | * @param float $value |
||
| 498 | */ |
||
| 499 | private function set_shipping_tax_total( $value ) { |
||
| 502 | |||
| 503 | /** |
||
| 504 | * Set item totals. |
||
| 505 | * @param array $value |
||
| 506 | */ |
||
| 507 | private function set_item_totals( $value ) { |
||
| 510 | |||
| 511 | /** |
||
| 512 | * Set items subtotal. |
||
| 513 | * @param float $value |
||
| 514 | */ |
||
| 515 | private function set_items_subtotal( $value ) { |
||
| 518 | |||
| 519 | /** |
||
| 520 | * Set items subtotal tax. |
||
| 521 | * @param float $value |
||
| 522 | */ |
||
| 523 | private function set_items_subtotal_tax( $value ) { |
||
| 526 | |||
| 527 | /** |
||
| 528 | * Set items total. |
||
| 529 | * @param float $value |
||
| 530 | */ |
||
| 531 | private function set_items_total( $value ) { |
||
| 534 | |||
| 535 | /** |
||
| 536 | * Set items total tax. |
||
| 537 | * @param float $value |
||
| 538 | */ |
||
| 539 | private function set_items_total_tax( $value ) { |
||
| 542 | |||
| 543 | /** |
||
| 544 | * Set discount total. |
||
| 545 | * @param array $value |
||
| 546 | */ |
||
| 547 | private function set_coupon_totals( $value ) { |
||
| 550 | |||
| 551 | /** |
||
| 552 | * Set discount total tax. |
||
| 553 | * @param array $value |
||
| 554 | */ |
||
| 555 | private function set_coupon_tax_totals( $value ) { |
||
| 558 | |||
| 559 | /** |
||
| 560 | * Set counts. |
||
| 561 | * @param array $value |
||
| 562 | */ |
||
| 563 | private function set_coupon_counts( $value ) { |
||
| 566 | |||
| 567 | /** |
||
| 568 | * Set fees total. |
||
| 569 | * @param float $value |
||
| 570 | */ |
||
| 571 | private function set_fees_total( $value ) { |
||
| 574 | |||
| 575 | /** |
||
| 576 | * Set fees total tax. |
||
| 577 | * @param float $value |
||
| 578 | */ |
||
| 579 | private function set_fees_total_tax( $value ) { |
||
| 582 | |||
| 583 | /** |
||
| 584 | * Set total. |
||
| 585 | * @param float $value |
||
| 586 | */ |
||
| 587 | private function set_total( $value ) { |
||
| 590 | |||
| 591 | /* |
||
| 592 | |-------------------------------------------------------------------------- |
||
| 593 | | Getters. |
||
| 594 | |-------------------------------------------------------------------------- |
||
| 595 | */ |
||
| 596 | |||
| 597 | /** |
||
| 598 | * Get all totals. |
||
| 599 | * @return array. |
||
|
|
|||
| 600 | */ |
||
| 601 | public function get_totals() { |
||
| 604 | |||
| 605 | /** |
||
| 606 | * Get shipping and item taxes. |
||
| 607 | * @return array |
||
| 608 | */ |
||
| 609 | public function get_taxes() { |
||
| 612 | |||
| 613 | /** |
||
| 614 | * Get tax total. |
||
| 615 | * @return float |
||
| 616 | */ |
||
| 617 | public function get_tax_total() { |
||
| 620 | |||
| 621 | /** |
||
| 622 | * Get shipping total. |
||
| 623 | * @return float |
||
| 624 | */ |
||
| 625 | public function get_shipping_total() { |
||
| 628 | |||
| 629 | /** |
||
| 630 | * Get shipping tax total. |
||
| 631 | * @return float |
||
| 632 | */ |
||
| 633 | public function get_shipping_tax_total() { |
||
| 636 | |||
| 637 | /** |
||
| 638 | * Get the items subtotal. |
||
| 639 | * @return float |
||
| 640 | */ |
||
| 641 | public function get_items_subtotal() { |
||
| 644 | |||
| 645 | /** |
||
| 646 | * Get the items subtotal tax. |
||
| 647 | * @return float |
||
| 648 | */ |
||
| 649 | public function get_items_subtotal_tax() { |
||
| 652 | |||
| 653 | /** |
||
| 654 | * Get the items total. |
||
| 655 | * @return float |
||
| 656 | */ |
||
| 657 | public function get_items_total() { |
||
| 660 | |||
| 661 | /** |
||
| 662 | * Get the items total tax. |
||
| 663 | * @return float |
||
| 664 | */ |
||
| 665 | public function get_items_total_tax() { |
||
| 668 | |||
| 669 | /** |
||
| 670 | * Get the total discount amount. |
||
| 671 | * @return array |
||
| 672 | */ |
||
| 673 | public function get_coupon_totals() { |
||
| 676 | |||
| 677 | /** |
||
| 678 | * Get the total discount amount. |
||
| 679 | * @return array |
||
| 680 | */ |
||
| 681 | public function get_coupon_tax_totals() { |
||
| 684 | |||
| 685 | /** |
||
| 686 | * Get couppon counts. |
||
| 687 | * @return array |
||
| 688 | */ |
||
| 689 | public function get_coupon_counts() { |
||
| 692 | |||
| 693 | /** |
||
| 694 | * Get the total fees amount. |
||
| 695 | * @return float |
||
| 696 | */ |
||
| 697 | public function get_fees_total() { |
||
| 700 | |||
| 701 | /** |
||
| 702 | * Get the total fee tax amount. |
||
| 703 | * @return float |
||
| 704 | */ |
||
| 705 | public function get_fees_total_tax() { |
||
| 708 | |||
| 709 | /** |
||
| 710 | * Get the total. |
||
| 711 | * @return float |
||
| 712 | */ |
||
| 713 | public function get_total() { |
||
| 716 | |||
| 717 | /** |
||
| 718 | * Returns an array of item totals. |
||
| 719 | * @return array |
||
| 720 | */ |
||
| 721 | public function get_item_totals() { |
||
| 724 | |||
| 725 | /** |
||
| 726 | * Should we calc tax? |
||
| 727 | * @param bool |
||
| 728 | */ |
||
| 729 | private function get_calculate_tax() { |
||
| 732 | |||
| 733 | /** |
||
| 734 | * Get tax rates for an item. Caches rates in class to avoid multiple look ups. |
||
| 735 | * @param object $item |
||
| 736 | * @return array of taxes |
||
| 737 | */ |
||
| 738 | private function get_item_tax_rates( $item ) { |
||
| 742 | |||
| 743 | /** |
||
| 744 | * Get all tax rows for a set of items and shipping methods. |
||
| 745 | * @return array |
||
| 746 | */ |
||
| 747 | private function get_merged_taxes() { |
||
| 772 | } |
||
| 773 |
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.