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 | * This stores the chosen shipping methods for the cart item packages. |
||
| 35 | * @var array |
||
| 36 | */ |
||
| 37 | protected $shipping_methods; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Constructor for the cart class. |
||
| 41 | */ |
||
| 42 | public function __construct() { |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Add an item to the cart. |
||
| 53 | */ |
||
| 54 | public function add_item( $args ) { |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Add additional fee to the cart. |
||
| 72 | * |
||
| 73 | * @param string $name Unique name for the fee. Multiple fees of the same name cannot be added. |
||
| 74 | * @param float $amount Fee amount. |
||
| 75 | * @param bool $taxable (default: false) Is the fee taxable? |
||
| 76 | * @param string $tax_class (default: '') The tax class for the fee if taxable. A blank string is standard tax class. |
||
| 77 | */ |
||
| 78 | public function add_fee( $name, $amount, $taxable = false, $tax_class = '' ) { |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Applies a coupon code. |
||
| 102 | * @since 2.7.0 |
||
| 103 | * @param string $coupon_code - The code to apply |
||
| 104 | * @return bool True if the coupon is applied, false if it does not exist or cannot be applied |
||
| 105 | */ |
||
| 106 | public function add_coupon( $coupon_code ) { |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Returns the contents of the cart in an array. |
||
| 140 | * @return array contents of the cart |
||
| 141 | */ |
||
| 142 | public function get_cart() { |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Get array of fees. |
||
| 154 | * @return array of fees |
||
| 155 | */ |
||
| 156 | public function get_fees() { |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Get array of applied coupon objects and codes. |
||
| 162 | * @return array of applied coupons |
||
| 163 | */ |
||
| 164 | public function get_coupons() { |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Checks if the cart is empty. |
||
| 170 | * @return bool |
||
| 171 | */ |
||
| 172 | public function is_empty() { |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Empties the cart and optionally the persistent cart too. |
||
| 178 | */ |
||
| 179 | public function empty_cart() { |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Set the quantity for an item in the cart. |
||
| 190 | * @param string $cart_item_key contains the id of the cart item |
||
| 191 | * @param int $quantity contains the quantity of the item |
||
| 192 | */ |
||
| 193 | public function set_quantity( $item_key, $quantity = 1, $deprecated = true ) { |
||
| 202 | |||
| 203 | /** |
||
| 204 | * Remove item from cart. |
||
| 205 | * @param string $item_key Cart item key. |
||
| 206 | */ |
||
| 207 | public function remove_cart_item( $item_key ) { |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Remove a single coupon by code. |
||
| 213 | * @param string $coupon_code Code of the coupon to remove |
||
| 214 | * @return bool |
||
| 215 | */ |
||
| 216 | public function remove_coupon( $coupon_code ) { |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Remove all coupons. |
||
| 222 | */ |
||
| 223 | public function remove_coupons() { |
||
| 226 | |||
| 227 | /** |
||
| 228 | * Returns a specific item in the cart. |
||
| 229 | * |
||
| 230 | * @param string $item_key Cart item key. |
||
| 231 | * @return array Item data |
||
| 232 | */ |
||
| 233 | public function get_cart_item( $item_key ) { |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Restore item from cart. |
||
| 239 | * @param string $item_key Cart item key. |
||
| 240 | */ |
||
| 241 | public function restore_cart_item( $item_key ) { |
||
| 244 | |||
| 245 | /** |
||
| 246 | * Get all tax classes for items in the cart. |
||
| 247 | * @return array |
||
| 248 | */ |
||
| 249 | public function get_cart_item_tax_classes() { |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Get weight of items in the cart. |
||
| 255 | * @return int |
||
| 256 | */ |
||
| 257 | public function get_cart_contents_weight() { |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Get number of items in the cart. |
||
| 263 | * @return int |
||
| 264 | */ |
||
| 265 | public function get_cart_contents_count() { |
||
| 268 | |||
| 269 | /** |
||
| 270 | * Returns whether or not a coupon has been applied. |
||
| 271 | * @param string $coupon_code |
||
| 272 | * @return bool |
||
| 273 | */ |
||
| 274 | public function has_coupon( $coupon_code = '' ) { |
||
| 278 | |||
| 279 | /* |
||
| 280 | |-------------------------------------------------------------------------- |
||
| 281 | | Cart shipping calculation. |
||
| 282 | |-------------------------------------------------------------------------- |
||
| 283 | */ |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Looks through the cart to see if shipping is actually required. |
||
| 287 | * @return bool whether or not the cart needs shipping |
||
| 288 | */ |
||
| 289 | public function needs_shipping() { |
||
| 304 | |||
| 305 | /** |
||
| 306 | * Should the shipping address form be shown. |
||
| 307 | * @return bool |
||
| 308 | */ |
||
| 309 | public function needs_shipping_address() { |
||
| 312 | |||
| 313 | /** |
||
| 314 | * Get packages to calculate shipping for. |
||
| 315 | * |
||
| 316 | * This lets us calculate costs for carts that are shipped to multiple locations. |
||
| 317 | * |
||
| 318 | * Shipping methods are responsible for looping through these packages. |
||
| 319 | * |
||
| 320 | * By default we pass the cart itself as a package - plugins can change this. |
||
| 321 | * through the filter and break it up. |
||
| 322 | * |
||
| 323 | * @since 1.5.4 |
||
| 324 | * @return array of cart items |
||
| 325 | */ |
||
| 326 | public function get_shipping_packages() { |
||
| 341 | |||
| 342 | /** |
||
| 343 | * Uses the shipping class to calculate shipping then gets the totals when its finished. |
||
| 344 | */ |
||
| 345 | public function calculate_shipping() { |
||
| 348 | |||
| 349 | /** |
||
| 350 | * Given a set of packages with rates, get the chosen ones only. |
||
| 351 | * @since 2.7.0 |
||
| 352 | * @param array $calculated_shipping_packages |
||
| 353 | * @return array |
||
| 354 | */ |
||
| 355 | protected function get_chosen_shipping_methods( $calculated_shipping_packages ) { |
||
| 368 | |||
| 369 | /* |
||
| 370 | |-------------------------------------------------------------------------- |
||
| 371 | | Cart Totals. |
||
| 372 | |-------------------------------------------------------------------------- |
||
| 373 | */ |
||
| 374 | |||
| 375 | /** |
||
| 376 | * Calculate totals for the items in the cart. |
||
| 377 | */ |
||
| 378 | public function calculate_totals() { |
||
| 398 | |||
| 399 | /** |
||
| 400 | * Looks at the totals to see if payment is actually required. |
||
| 401 | * |
||
| 402 | * @return bool |
||
| 403 | */ |
||
| 404 | public function needs_payment() { |
||
| 407 | |||
| 408 | /** |
||
| 409 | * Gets the order subtotal with or without tax. |
||
| 410 | * @param bool $including_tax |
||
| 411 | * @return string formatted price |
||
| 412 | */ |
||
| 413 | public function get_subtotal( $including_tax = false ) { |
||
| 420 | |||
| 421 | /** |
||
| 422 | * Gets the order total (after calculation). |
||
| 423 | * @return string formatted price |
||
| 424 | */ |
||
| 425 | public function get_total() { |
||
| 428 | |||
| 429 | /** |
||
| 430 | * Gets all taxes. |
||
| 431 | * @return array of taxes |
||
| 432 | */ |
||
| 433 | public function get_taxes() { |
||
| 436 | |||
| 437 | /** |
||
| 438 | * Gets all taxes which will be output. |
||
| 439 | * @return array |
||
| 440 | */ |
||
| 441 | View Code Duplication | public function get_tax_totals() { |
|
| 451 | |||
| 452 | /** |
||
| 453 | * Get tax row amounts with or without compound taxes includes. |
||
| 454 | * |
||
| 455 | * @param bool $compound True if getting compound taxes |
||
| 456 | * @param bool $round True if getting total to display |
||
| 457 | * @return float price |
||
| 458 | */ |
||
| 459 | public function get_taxes_total( $compound = true, $round = true ) { |
||
| 469 | |||
| 470 | /** |
||
| 471 | * Get the total tax on the cart. |
||
| 472 | * @return float |
||
| 473 | */ |
||
| 474 | public function get_tax_total() { |
||
| 477 | |||
| 478 | /** |
||
| 479 | * Get the total tax on shipping. |
||
| 480 | * @return float |
||
| 481 | */ |
||
| 482 | public function get_shipping_tax_total() { |
||
| 485 | |||
| 486 | /** |
||
| 487 | * Get the total cost of shipping, with or without taxes included. |
||
| 488 | * @param bool $including_tax |
||
| 489 | * @return float |
||
| 490 | */ |
||
| 491 | public function get_shipping_total( $including_tax = false ) { |
||
| 498 | |||
| 499 | /** |
||
| 500 | * Return the total amount of discount granted by coupons. |
||
| 501 | * @return float |
||
| 502 | */ |
||
| 503 | public function get_cart_discount_total() { |
||
| 506 | |||
| 507 | /** |
||
| 508 | * Return the tax on the total amount of discount granted by coupons. |
||
| 509 | * @return float |
||
| 510 | */ |
||
| 511 | public function get_cart_discount_tax_total() { |
||
| 514 | |||
| 515 | /** |
||
| 516 | * Get item totals from totals class. |
||
| 517 | * @return array |
||
| 518 | */ |
||
| 519 | public function get_item_totals() { |
||
| 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 ) { |
||
| 551 | } |
||
| 552 |
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: