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_Tax 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_Tax, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | class WC_Tax { |
||
| 17 | |||
| 18 | /** |
||
| 19 | * Precision. |
||
| 20 | * |
||
| 21 | * @var int |
||
| 22 | */ |
||
| 23 | public static $precision; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Round at subtotal. |
||
| 27 | * |
||
| 28 | * @var bool |
||
| 29 | */ |
||
| 30 | public static $round_at_subtotal; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Load options. |
||
| 34 | * |
||
| 35 | * @access public |
||
| 36 | */ |
||
| 37 | public static function init() { |
||
| 42 | |||
| 43 | /** |
||
| 44 | * When the woocommerce_tax_classes option is changed, remove any orphan rates. |
||
| 45 | * @param string $old_value |
||
| 46 | * @param string $value |
||
| 47 | */ |
||
| 48 | public static function maybe_remove_tax_class_rates( $old_value, $value ) { |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Calculate tax for a line. |
||
| 67 | * @param float $price Price to calc tax on |
||
| 68 | * @param array $rates Rates to apply |
||
| 69 | * @param boolean $price_includes_tax Whether the passed price has taxes included |
||
| 70 | * @param boolean $suppress_rounding Whether to suppress any rounding from taking place |
||
| 71 | * @return array Array of rates + prices after tax |
||
| 72 | */ |
||
| 73 | public static function calc_tax( $price, $rates, $price_includes_tax = false, $suppress_rounding = false ) { |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Calculate the shipping tax using a passed array of rates. |
||
| 97 | * |
||
| 98 | * @param float Price |
||
| 99 | * @param array Taxation Rate |
||
| 100 | * @return array |
||
| 101 | */ |
||
| 102 | public static function calc_shipping_tax( $price, $rates ) { |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Multiply cost by pow precision. |
||
| 108 | * @param float $price |
||
| 109 | * @return float |
||
| 110 | */ |
||
| 111 | private static function precision( $price ) { |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Divide cost by pow precision. |
||
| 117 | * @param float $price |
||
| 118 | * @return float |
||
| 119 | */ |
||
| 120 | private static function remove_precision( $price ) { |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Round to precision. |
||
| 126 | * |
||
| 127 | * Filter example: to return rounding to .5 cents you'd use: |
||
| 128 | * |
||
| 129 | * function euro_5cent_rounding( $in ) { |
||
| 130 | * return round( $in / 5, 2 ) * 5; |
||
| 131 | * } |
||
| 132 | * add_filter( 'woocommerce_tax_round', 'euro_5cent_rounding' ); |
||
| 133 | * @return double |
||
| 134 | */ |
||
| 135 | public static function round( $in ) { |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Calc tax from inclusive price. |
||
| 141 | * |
||
| 142 | * @param float $price |
||
| 143 | * @param array $rates |
||
| 144 | * @return array |
||
| 145 | */ |
||
| 146 | public static function calc_inclusive_tax( $price, $rates ) { |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Calc tax from exclusive price. |
||
| 185 | * |
||
| 186 | * @param float $price |
||
| 187 | * @param array $rates |
||
| 188 | * @return array |
||
| 189 | */ |
||
| 190 | public static function calc_exclusive_tax( $price, $rates ) { |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Searches for all matching country/state/postcode tax rates. |
||
| 240 | * |
||
| 241 | * @param array $args |
||
| 242 | * @return array |
||
| 243 | */ |
||
| 244 | public static function find_rates( $args = array() ) { |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Searches for all matching country/state/postcode tax rates. |
||
| 273 | * |
||
| 274 | * @param array $args |
||
| 275 | * @return array |
||
| 276 | */ |
||
| 277 | public static function find_shipping_rates( $args = array() ) { |
||
| 291 | |||
| 292 | /** |
||
| 293 | * Does the sort comparison. |
||
| 294 | */ |
||
| 295 | private static function sort_rates_callback( $rate1, $rate2 ) { |
||
| 318 | |||
| 319 | /** |
||
| 320 | * Logical sort order for tax rates based on the following in order of priority: |
||
| 321 | * - Priority |
||
| 322 | * - County code |
||
| 323 | * - State code |
||
| 324 | * @param array $rates |
||
| 325 | * @return array |
||
| 326 | * @todo remove tax_rate_order column |
||
| 327 | */ |
||
| 328 | private static function sort_rates( $rates ) { |
||
| 336 | |||
| 337 | /** |
||
| 338 | * Loop through a set of tax rates and get the matching rates (1 per priority). |
||
| 339 | * |
||
| 340 | * @param string $country |
||
| 341 | * @param string $state |
||
| 342 | * @param string $postcode |
||
| 343 | * @param string $city |
||
| 344 | * @param string $tax_class |
||
| 345 | * @return array |
||
| 346 | */ |
||
| 347 | private static function get_matched_tax_rates( $country, $state, $postcode, $city, $tax_class ) { |
||
| 433 | |||
| 434 | /** |
||
| 435 | * Get the customer tax location based on their status and the current page. |
||
| 436 | * |
||
| 437 | * Used by get_rates(), get_shipping_rates(). |
||
| 438 | * |
||
| 439 | * @param $tax_class string Optional, passed to the filter for advanced tax setups. |
||
| 440 | * @return array |
||
| 441 | */ |
||
| 442 | public static function get_tax_location( $tax_class = '' ) { |
||
| 458 | |||
| 459 | /** |
||
| 460 | * Get's an array of matching rates for a tax class. |
||
| 461 | * @param string $tax_class |
||
| 462 | * @return array |
||
| 463 | */ |
||
| 464 | public static function get_rates( $tax_class = '' ) { |
||
| 483 | |||
| 484 | /** |
||
| 485 | * Get's an array of matching rates for the shop's base country. |
||
| 486 | * |
||
| 487 | * @param string Tax Class |
||
| 488 | * @return array |
||
| 489 | */ |
||
| 490 | public static function get_base_tax_rates( $tax_class = '' ) { |
||
| 499 | |||
| 500 | /** |
||
| 501 | * Alias for get_base_tax_rates(). |
||
| 502 | * |
||
| 503 | * @deprecated 2.3 |
||
| 504 | * @param string Tax Class |
||
| 505 | * @return array |
||
| 506 | */ |
||
| 507 | public static function get_shop_base_rate( $tax_class = '' ) { |
||
| 510 | |||
| 511 | /** |
||
| 512 | * Gets an array of matching shipping tax rates for a given class. |
||
| 513 | * |
||
| 514 | * @param string Tax Class |
||
| 515 | * @return mixed |
||
| 516 | */ |
||
| 517 | public static function get_shipping_tax_rates( $tax_class = null ) { |
||
| 587 | |||
| 588 | /** |
||
| 589 | * Return true/false depending on if a rate is a compound rate. |
||
| 590 | * |
||
| 591 | * @param int key |
||
| 592 | * @return bool |
||
| 593 | */ |
||
| 594 | public static function is_compound( $key ) { |
||
| 598 | |||
| 599 | /** |
||
| 600 | * Return a given rates label. |
||
| 601 | * |
||
| 602 | * @param mixed $key_or_rate Tax rate ID, or the db row itself in object format |
||
| 603 | * @return string |
||
| 604 | */ |
||
| 605 | public static function get_rate_label( $key_or_rate ) { |
||
| 622 | |||
| 623 | /** |
||
| 624 | * Return a given rates percent. |
||
| 625 | * |
||
| 626 | * @param mixed $key_or_rate Tax rate ID, or the db row itself in object format |
||
| 627 | * @return string |
||
| 628 | */ |
||
| 629 | public static function get_rate_percent( $key_or_rate ) { |
||
| 642 | |||
| 643 | /** |
||
| 644 | * Get a rates code. Code is made up of COUNTRY-STATE-NAME-Priority. E.g GB-VAT-1, US-AL-TAX-1. |
||
| 645 | * |
||
| 646 | * @access public |
||
| 647 | * @param mixed $key_or_rate Tax rate ID, or the db row itself in object format |
||
| 648 | * @return string |
||
| 649 | */ |
||
| 650 | public static function get_rate_code( $key_or_rate ) { |
||
| 674 | |||
| 675 | /** |
||
| 676 | * Round tax lines and return the sum. |
||
| 677 | * |
||
| 678 | * @param array |
||
| 679 | * @return float |
||
| 680 | */ |
||
| 681 | public static function get_tax_total( $taxes ) { |
||
| 684 | |||
| 685 | /** |
||
| 686 | * Get store tax classes. |
||
| 687 | * @return array |
||
| 688 | */ |
||
| 689 | public static function get_tax_classes() { |
||
| 692 | |||
| 693 | /** |
||
| 694 | * format the city. |
||
| 695 | * @param string $city |
||
| 696 | * @return string |
||
| 697 | */ |
||
| 698 | private static function format_tax_rate_city( $city ) { |
||
| 701 | |||
| 702 | /** |
||
| 703 | * format the state. |
||
| 704 | * @param string $state |
||
| 705 | * @return string |
||
| 706 | */ |
||
| 707 | private static function format_tax_rate_state( $state ) { |
||
| 711 | |||
| 712 | /** |
||
| 713 | * format the country. |
||
| 714 | * @param string $country |
||
| 715 | * @return string |
||
| 716 | */ |
||
| 717 | private static function format_tax_rate_country( $country ) { |
||
| 721 | |||
| 722 | /** |
||
| 723 | * format the tax rate name. |
||
| 724 | * @param string $name |
||
| 725 | * @return string |
||
| 726 | */ |
||
| 727 | private static function format_tax_rate_name( $name ) { |
||
| 730 | |||
| 731 | /** |
||
| 732 | * format the rate. |
||
| 733 | * @param double $rate |
||
| 734 | * @return string |
||
| 735 | */ |
||
| 736 | private static function format_tax_rate( $rate ) { |
||
| 739 | |||
| 740 | /** |
||
| 741 | * format the priority. |
||
| 742 | * @param string $priority |
||
| 743 | * @return int |
||
| 744 | */ |
||
| 745 | private static function format_tax_rate_priority( $priority ) { |
||
| 748 | |||
| 749 | /** |
||
| 750 | * format the class. |
||
| 751 | * @param string $class |
||
| 752 | * @return string |
||
| 753 | */ |
||
| 754 | public static function format_tax_rate_class( $class ) { |
||
| 762 | |||
| 763 | /** |
||
| 764 | * Prepare and format tax rate for DB insertion. |
||
| 765 | * @param array $tax_rate |
||
| 766 | * @return array |
||
| 767 | */ |
||
| 768 | private static function prepare_tax_rate( $tax_rate ) { |
||
| 776 | |||
| 777 | /** |
||
| 778 | * Insert a new tax rate. |
||
| 779 | * |
||
| 780 | * Internal use only. |
||
| 781 | * |
||
| 782 | * @since 2.3.0 |
||
| 783 | * @access private |
||
| 784 | * |
||
| 785 | * @param array $tax_rate |
||
| 786 | * |
||
| 787 | * @return int tax rate id |
||
| 788 | */ |
||
| 789 | public static function _insert_tax_rate( $tax_rate ) { |
||
| 800 | |||
| 801 | /** |
||
| 802 | * Get tax rate. |
||
| 803 | * |
||
| 804 | * Internal use only. |
||
| 805 | * |
||
| 806 | * @since 2.5.0 |
||
| 807 | * @access private |
||
| 808 | * |
||
| 809 | * @param int $tax_rate_id |
||
| 810 | * @param string $output_type |
||
| 811 | * |
||
| 812 | * @return array |
||
| 813 | */ |
||
| 814 | public static function _get_tax_rate( $tax_rate_id, $output_type = ARRAY_A ) { |
||
| 823 | |||
| 824 | /** |
||
| 825 | * Update a tax rate. |
||
| 826 | * |
||
| 827 | * Internal use only. |
||
| 828 | * |
||
| 829 | * @since 2.3.0 |
||
| 830 | * @access private |
||
| 831 | * |
||
| 832 | * @param int $tax_rate_id |
||
| 833 | * @param array $tax_rate |
||
| 834 | */ |
||
| 835 | public static function _update_tax_rate( $tax_rate_id, $tax_rate ) { |
||
| 852 | |||
| 853 | /** |
||
| 854 | * Delete a tax rate from the database. |
||
| 855 | * |
||
| 856 | * Internal use only. |
||
| 857 | * |
||
| 858 | * @since 2.3.0 |
||
| 859 | * @access private |
||
| 860 | * |
||
| 861 | * @param int $tax_rate_id |
||
| 862 | */ |
||
| 863 | public static function _delete_tax_rate( $tax_rate_id ) { |
||
| 873 | |||
| 874 | /** |
||
| 875 | * Update postcodes for a tax rate in the DB. |
||
| 876 | * |
||
| 877 | * Internal use only. |
||
| 878 | * |
||
| 879 | * @since 2.3.0 |
||
| 880 | * @access private |
||
| 881 | * |
||
| 882 | * @param int $tax_rate_id |
||
| 883 | * @param string $postcodes String of postcodes separated by ; characters |
||
| 884 | * @return string |
||
| 885 | */ |
||
| 886 | public static function _update_tax_rate_postcodes( $tax_rate_id, $postcodes ) { |
||
| 896 | |||
| 897 | /** |
||
| 898 | * Update cities for a tax rate in the DB. |
||
| 899 | * |
||
| 900 | * Internal use only. |
||
| 901 | * |
||
| 902 | * @since 2.3.0 |
||
| 903 | * @access private |
||
| 904 | * |
||
| 905 | * @param int $tax_rate_id |
||
| 906 | * @param string $cities |
||
| 907 | * @return string |
||
| 908 | */ |
||
| 909 | public static function _update_tax_rate_cities( $tax_rate_id, $cities ) { |
||
| 917 | |||
| 918 | /** |
||
| 919 | * Updates locations (postcode and city). |
||
| 920 | * |
||
| 921 | * Internal use only. |
||
| 922 | * |
||
| 923 | * @since 2.3.0 |
||
| 924 | * @access private |
||
| 925 | * |
||
| 926 | * @param int $tax_rate_id |
||
| 927 | * @param string $type |
||
| 928 | * @return string |
||
| 929 | */ |
||
| 930 | private static function _update_tax_rate_locations( $tax_rate_id, $values, $type ) { |
||
| 952 | |||
| 953 | /** |
||
| 954 | * Used by admin settings page. |
||
| 955 | * |
||
| 956 | * @param string $tax_class |
||
| 957 | * |
||
| 958 | * @return array|null|object |
||
| 959 | */ |
||
| 960 | public static function get_rates_for_tax_class( $tax_class ) { |
||
| 987 | } |
||
| 988 | WC_Tax::init(); |
||
| 989 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.