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 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, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 31 | class WC_Cart extends WC_Cart_Session { |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Cart totals class. |
||
| 35 | * @var WC_Cart_Totals |
||
| 36 | */ |
||
| 37 | protected $totals; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * This stores the chosen shipping methods for the cart item packages. |
||
| 41 | * @var array |
||
| 42 | */ |
||
| 43 | protected $shipping_methods; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Constructor for the cart class. |
||
| 47 | */ |
||
| 48 | public function __construct() { |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Add an item to the cart. |
||
| 61 | */ |
||
| 62 | public function add_item( $args ) { |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Add additional fee to the cart. |
||
| 80 | * |
||
| 81 | * @param string $name Unique name for the fee. Multiple fees of the same name cannot be added. |
||
| 82 | * @param float $amount Fee amount. |
||
| 83 | * @param bool $taxable (default: false) Is the fee taxable? |
||
| 84 | * @param string $tax_class (default: '') The tax class for the fee if taxable. A blank string is standard tax class. |
||
| 85 | */ |
||
| 86 | public function add_fee( $name, $amount, $taxable = false, $tax_class = '' ) { |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Applies a coupon code. |
||
| 110 | * @since 2.7.0 |
||
| 111 | * @param string $coupon_code - The code to apply |
||
| 112 | * @return bool True if the coupon is applied, false if it does not exist or cannot be applied |
||
| 113 | */ |
||
| 114 | public function add_coupon( $coupon_code ) { |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Returns the contents of the cart in an array. |
||
| 148 | * @return array contents of the cart |
||
| 149 | */ |
||
| 150 | public function get_cart() { |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Get array of fees. |
||
| 162 | * @return array of fees |
||
| 163 | */ |
||
| 164 | public function get_fees() { |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Get array of applied coupon objects and codes. |
||
| 170 | * @return array of applied coupons |
||
| 171 | */ |
||
| 172 | public function get_coupons() { |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Checks if the cart is empty. |
||
| 178 | * @return bool |
||
| 179 | */ |
||
| 180 | public function is_empty() { |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Empties the cart and optionally the persistent cart too. |
||
| 186 | */ |
||
| 187 | public function empty_cart() { |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Set the quantity for an item in the cart. |
||
| 198 | * @param string $cart_item_key contains the id of the cart item |
||
| 199 | * @param int $quantity contains the quantity of the item |
||
| 200 | */ |
||
| 201 | public function set_quantity( $item_key, $quantity = 1, $deprecated = true ) { |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Remove item from cart. |
||
| 213 | * @param string $item_key Cart item key. |
||
| 214 | */ |
||
| 215 | public function remove_cart_item( $item_key ) { |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Remove a single coupon by code. |
||
| 221 | * @param string $coupon_code Code of the coupon to remove |
||
| 222 | * @return bool |
||
| 223 | */ |
||
| 224 | public function remove_coupon( $coupon_code ) { |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Remove all coupons. |
||
| 230 | */ |
||
| 231 | public function remove_coupons() { |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Returns a specific item in the cart. |
||
| 237 | * |
||
| 238 | * @param string $item_key Cart item key. |
||
| 239 | * @return array Item data |
||
| 240 | */ |
||
| 241 | public function get_cart_item( $item_key ) { |
||
| 244 | |||
| 245 | /** |
||
| 246 | * Restore item from cart. |
||
| 247 | * @param string $item_key Cart item key. |
||
| 248 | */ |
||
| 249 | public function restore_cart_item( $item_key ) { |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Get all tax classes for items in the cart. |
||
| 255 | * @return array |
||
| 256 | */ |
||
| 257 | public function get_cart_item_tax_classes() { |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Get weight of items in the cart. |
||
| 263 | * @return int |
||
| 264 | */ |
||
| 265 | public function get_cart_contents_weight() { |
||
| 268 | |||
| 269 | /** |
||
| 270 | * Get number of items in the cart. |
||
| 271 | * @return int |
||
| 272 | */ |
||
| 273 | public function get_cart_contents_count() { |
||
| 276 | |||
| 277 | /** |
||
| 278 | * Returns whether or not a coupon has been applied. |
||
| 279 | * @param string $coupon_code |
||
| 280 | * @return bool |
||
| 281 | */ |
||
| 282 | public function has_coupon( $coupon_code = '' ) { |
||
| 286 | |||
| 287 | /* |
||
| 288 | |-------------------------------------------------------------------------- |
||
| 289 | | Cart shipping calculation. |
||
| 290 | |-------------------------------------------------------------------------- |
||
| 291 | */ |
||
| 292 | |||
| 293 | /** |
||
| 294 | * Looks through the cart to see if shipping is actually required. |
||
| 295 | * @return bool whether or not the cart needs shipping |
||
| 296 | */ |
||
| 297 | public function needs_shipping() { |
||
| 312 | |||
| 313 | /** |
||
| 314 | * Should the shipping address form be shown. |
||
| 315 | * @return bool |
||
| 316 | */ |
||
| 317 | public function needs_shipping_address() { |
||
| 320 | |||
| 321 | /** |
||
| 322 | * Get packages to calculate shipping for. |
||
| 323 | * |
||
| 324 | * This lets us calculate costs for carts that are shipped to multiple locations. |
||
| 325 | * |
||
| 326 | * Shipping methods are responsible for looping through these packages. |
||
| 327 | * |
||
| 328 | * By default we pass the cart itself as a package - plugins can change this. |
||
| 329 | * through the filter and break it up. |
||
| 330 | * |
||
| 331 | * @since 1.5.4 |
||
| 332 | * @return array of cart items |
||
| 333 | */ |
||
| 334 | public function get_shipping_packages() { |
||
| 349 | |||
| 350 | /** |
||
| 351 | * Uses the shipping class to calculate shipping then gets the totals when its finished. |
||
| 352 | */ |
||
| 353 | public function calculate_shipping() { |
||
| 356 | |||
| 357 | /** |
||
| 358 | * Given a set of packages with rates, get the chosen ones only. |
||
| 359 | * @since 2.7.0 |
||
| 360 | * @param array $calculated_shipping_packages |
||
| 361 | * @return array |
||
| 362 | */ |
||
| 363 | protected function get_chosen_shipping_methods( $calculated_shipping_packages ) { |
||
| 376 | |||
| 377 | /* |
||
| 378 | |-------------------------------------------------------------------------- |
||
| 379 | | Cart Totals. |
||
| 380 | |-------------------------------------------------------------------------- |
||
| 381 | */ |
||
| 382 | |||
| 383 | /** |
||
| 384 | * Calculate totals for the items in the cart. |
||
| 385 | */ |
||
| 386 | public function calculate_totals() { |
||
| 406 | |||
| 407 | /** |
||
| 408 | * Looks at the totals to see if payment is actually required. |
||
| 409 | * |
||
| 410 | * @return bool |
||
| 411 | */ |
||
| 412 | public function needs_payment() { |
||
| 415 | |||
| 416 | /** |
||
| 417 | * Get item totals from totals class. |
||
| 418 | * @return array |
||
| 419 | */ |
||
| 420 | public function get_item_totals() { |
||
| 423 | |||
| 424 | /** |
||
| 425 | * Gets the order subtotal with or without tax. |
||
| 426 | * @param bool $including_tax |
||
| 427 | * @return string formatted price |
||
| 428 | */ |
||
| 429 | public function get_subtotal( $including_tax = false ) { |
||
| 432 | |||
| 433 | /** |
||
| 434 | * Gets the order total (after calculation). |
||
| 435 | * @return string formatted price |
||
| 436 | */ |
||
| 437 | public function get_total() { |
||
| 440 | |||
| 441 | /** |
||
| 442 | * Gets all taxes. |
||
| 443 | * @return array of taxes |
||
| 444 | */ |
||
| 445 | public function get_taxes() { |
||
| 448 | |||
| 449 | /** |
||
| 450 | * Gets all taxes which will be output. |
||
| 451 | * @return array |
||
| 452 | */ |
||
| 453 | View Code Duplication | public function get_tax_totals() { |
|
| 463 | |||
| 464 | /** |
||
| 465 | * Get tax row amounts with or without compound taxes includes. |
||
| 466 | * |
||
| 467 | * @param bool $compound True if getting compound taxes |
||
| 468 | * @param bool $round True if getting total to display |
||
| 469 | * @return float price |
||
| 470 | */ |
||
| 471 | public function get_taxes_total( $compound = true, $round = true ) { |
||
| 481 | |||
| 482 | /** |
||
| 483 | * Get the total tax on the cart. |
||
| 484 | * @return float |
||
| 485 | */ |
||
| 486 | public function get_tax_total() { |
||
| 489 | |||
| 490 | /** |
||
| 491 | * Get the total tax on shipping. |
||
| 492 | * @return float |
||
| 493 | */ |
||
| 494 | public function get_shipping_tax_total() { |
||
| 497 | |||
| 498 | /** |
||
| 499 | * Get the total cost of shipping, with or without taxes included. |
||
| 500 | * @param bool $including_tax |
||
| 501 | * @return float |
||
| 502 | */ |
||
| 503 | public function get_shipping_total( $including_tax = false ) { |
||
| 506 | |||
| 507 | /** |
||
| 508 | * Return the total amount of discount granted by coupons. |
||
| 509 | * @return float |
||
| 510 | */ |
||
| 511 | public function get_cart_discount_total() { |
||
| 514 | |||
| 515 | /** |
||
| 516 | * Return the tax on the total amount of discount granted by coupons. |
||
| 517 | * @return float |
||
| 518 | */ |
||
| 519 | public function get_cart_discount_tax_total() { |
||
| 522 | |||
| 523 | /** |
||
| 524 | * Get the total discount amount for a specific coupon code. |
||
| 525 | * @param string $code |
||
| 526 | * @param bool $ex_tax |
||
| 527 | * @return float |
||
| 528 | */ |
||
| 529 | public function get_coupon_discount_amount( $code, $ex_tax = true ) { |
||
| 540 | |||
| 541 | /** |
||
| 542 | * Get the total discount tax amount for a specific coupon code. |
||
| 543 | * @param string $code |
||
| 544 | * @return float |
||
| 545 | */ |
||
| 546 | public function get_coupon_discount_tax_amount( $code ) { |
||
| 553 | } |
||
| 554 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: