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_Legacy_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_Legacy_Cart, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 18 | abstract class WC_Legacy_Cart { |
||
| 19 | /** |
||
| 20 | * Handle unset props. |
||
| 21 | * @param string $key |
||
| 22 | * @return mixed |
||
| 23 | */ |
||
| 24 | public function __get( $key ) { |
||
| 116 | |||
| 117 | /** |
||
| 118 | * @deprecated 2.7.0 |
||
| 119 | */ |
||
| 120 | public function add_discount( $coupon_code ) { |
||
| 124 | |||
| 125 | /** |
||
| 126 | * @deprecated 2.7.0 |
||
| 127 | */ |
||
| 128 | public function has_discount( $coupon_code = '' ) { |
||
| 132 | |||
| 133 | /** |
||
| 134 | * @deprecated 2.7.0 |
||
| 135 | */ |
||
| 136 | public function get_applied_coupons() { |
||
| 140 | |||
| 141 | /** |
||
| 142 | * @deprecated 2.7.0 |
||
| 143 | */ |
||
| 144 | public function check_cart_coupons() { |
||
| 148 | |||
| 149 | /** |
||
| 150 | * @deprecated 2.7.0 |
||
| 151 | */ |
||
| 152 | public function check_customer_coupons( $posted ) { |
||
| 157 | |||
| 158 | /** |
||
| 159 | * @deprecated 2.7.0 |
||
| 160 | */ |
||
| 161 | public function show_shipping() { |
||
| 165 | |||
| 166 | /** |
||
| 167 | * @deprecated 2.7.0 |
||
| 168 | */ |
||
| 169 | public function calculate_fees() { |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Add a product to the cart. |
||
| 176 | * @deprecated 2.7.0 |
||
| 177 | * @param int $product_id contains the id of the product to add to the cart |
||
| 178 | * @param int $quantity contains the quantity of the item to add |
||
| 179 | * @param int $variation_id |
||
| 180 | * @param array $variation attribute values |
||
| 181 | * @param array $cart_item_data extra cart item data we want to pass into the item |
||
| 182 | * @return string|bool $cart_item_key |
||
| 183 | */ |
||
| 184 | public function add_to_cart( $product_id = 0, $quantity = 1, $variation_id = 0, $variation = array(), $cart_item_data = array() ) { |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Get cart items quantities - merged so we can do accurate stock checks on items across multiple lines. |
||
| 201 | * @return array |
||
| 202 | */ |
||
| 203 | public function get_cart_item_quantities() { |
||
| 207 | |||
| 208 | /** |
||
| 209 | * @deprecated 2.7.0 |
||
| 210 | */ |
||
| 211 | public function check_cart_items() { |
||
| 215 | |||
| 216 | /** |
||
| 217 | * @deprecated 2.7.0 |
||
| 218 | */ |
||
| 219 | public function check_cart_item_stock() { |
||
| 222 | |||
| 223 | /** |
||
| 224 | * @deprecated 2.7.0 |
||
| 225 | */ |
||
| 226 | public function persistent_cart_update() { |
||
| 229 | |||
| 230 | /** |
||
| 231 | * @deprecated 2.7.0 |
||
| 232 | */ |
||
| 233 | public function persistent_cart_destroy() { |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Determines the value that the customer spent and the subtotal |
||
| 239 | * displayed, used for things like coupon validation. |
||
| 240 | * |
||
| 241 | * Since the coupon lines are displayed based on the TAX DISPLAY value |
||
| 242 | * of cart, this is used to determine the spend. |
||
| 243 | * |
||
| 244 | * If cart totals are shown including tax, use the subtotal. |
||
| 245 | * If cart totals are shown excluding tax, use the subtotal ex tax |
||
| 246 | * (tax is shown after coupons). |
||
| 247 | * @deprecated 2.7.0 |
||
| 248 | * @since 2.6.0 |
||
| 249 | * @return string |
||
| 250 | */ |
||
| 251 | public function get_displayed_subtotal() { |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Get the product row price per item. |
||
| 258 | * |
||
| 259 | * @param WC_Product $product |
||
| 260 | * @return string formatted price |
||
| 261 | */ |
||
| 262 | public function get_product_price( $product ) { |
||
| 266 | |||
| 267 | /** |
||
| 268 | * Gets the sub total (after calculation). |
||
| 269 | * @deprecated 2.7.0 |
||
| 270 | * @param bool $compound whether to include compound taxes |
||
| 271 | * @return string formatted price |
||
| 272 | */ |
||
| 273 | public function get_cart_subtotal( $compound = false ) { |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Get the product row subtotal. |
||
| 280 | * |
||
| 281 | * Gets the tax etc to avoid rounding issues. |
||
| 282 | * |
||
| 283 | * When on the checkout (review order), this will get the subtotal based on the customer's tax rate rather than the base rate. |
||
| 284 | * |
||
| 285 | * @param WC_Product $product |
||
| 286 | * @param int $quantity |
||
| 287 | * @return string formatted price |
||
| 288 | */ |
||
| 289 | public function get_product_subtotal( $product, $quantity ) { |
||
| 293 | |||
| 294 | /** |
||
| 295 | * Gets the total discount amount. |
||
| 296 | * @deprecated 2.7.0 in favor to get_cart_discount_total() |
||
| 297 | */ |
||
| 298 | public function get_total_discount() { |
||
| 302 | |||
| 303 | /** |
||
| 304 | * Gets the url to the cart page. |
||
| 305 | * |
||
| 306 | * @deprecated 2.5.0 in favor to wc_get_cart_url() |
||
| 307 | * @return string url to page |
||
| 308 | */ |
||
| 309 | public function get_cart_url() { |
||
| 313 | |||
| 314 | /** |
||
| 315 | * Gets the url to the checkout page. |
||
| 316 | * |
||
| 317 | * @deprecated 2.5.0 in favor to wc_get_checkout_url() |
||
| 318 | * @return string url to page |
||
| 319 | */ |
||
| 320 | public function get_checkout_url() { |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Coupons enabled function. Filterable. |
||
| 327 | * |
||
| 328 | * @deprecated 2.5.0 in favor to wc_coupons_enabled() |
||
| 329 | * @return bool |
||
| 330 | */ |
||
| 331 | public function coupons_enabled() { |
||
| 335 | |||
| 336 | /** |
||
| 337 | * Sees if we need a shipping address. |
||
| 338 | * |
||
| 339 | * @deprecated 2.5.0 in favor to wc_ship_to_billing_address_only() |
||
| 340 | * @return bool |
||
| 341 | */ |
||
| 342 | public function ship_to_billing_address_only() { |
||
| 346 | |||
| 347 | /** |
||
| 348 | * @deprecated 2.7.0 |
||
| 349 | */ |
||
| 350 | public function get_cross_sells() { |
||
| 354 | |||
| 355 | /** |
||
| 356 | * @deprecated 2.7.0 |
||
| 357 | */ |
||
| 358 | public function get_remove_url( $cart_item_key ) { |
||
| 362 | |||
| 363 | /** |
||
| 364 | * @deprecated 2.7.0 |
||
| 365 | */ |
||
| 366 | public function get_undo_url( $cart_item_key ) { |
||
| 370 | |||
| 371 | /** |
||
| 372 | * @deprecated 2.7.0 |
||
| 373 | */ |
||
| 374 | public function get_discounted_price( $values, $price, $add_totals = false ) { |
||
| 378 | |||
| 379 | /** |
||
| 380 | * @deprecated 2.7.0 |
||
| 381 | */ |
||
| 382 | public function get_item_data( $cart_item, $flat = false ) { |
||
| 386 | |||
| 387 | /** |
||
| 388 | * @deprecated 2.7.0 Unused method. |
||
| 389 | */ |
||
| 390 | public function get_total_ex_tax() { |
||
| 394 | |||
| 395 | /** |
||
| 396 | * @deprecated 2.7.0 Unused method. |
||
| 397 | */ |
||
| 398 | public function init() { |
||
| 401 | |||
| 402 | /** |
||
| 403 | * @deprecated 2.7.0 Taxes are just not calculated when not needed, so no need to remove them. |
||
| 404 | */ |
||
| 405 | public function remove_taxes() { |
||
| 408 | |||
| 409 | /** |
||
| 410 | * @deprecated 2.7.0 |
||
| 411 | */ |
||
| 412 | public function find_product_in_cart( $cart_id = false ) { |
||
| 421 | |||
| 422 | /** |
||
| 423 | * Generate a unique ID for the cart item being added. |
||
| 424 | * @deprecated 2.7.0 |
||
| 425 | * @param int $product_id - id of the product the key is being generated for |
||
| 426 | * @param int $variation_id of the product the key is being generated for |
||
| 427 | * @param array $variation data for the cart item |
||
| 428 | * @param array $cart_item_data other cart item data passed which affects this items uniqueness in the cart |
||
| 429 | * @return string cart item key |
||
| 430 | */ |
||
| 431 | public function generate_cart_id( $product_id, $variation_id = 0, $variation = array(), $cart_item_data = array() ) { |
||
| 435 | |||
| 436 | /** |
||
| 437 | * @deprecated 2.7.0 |
||
| 438 | */ |
||
| 439 | public function check_cart_item_validity() { |
||
| 443 | |||
| 444 | /** |
||
| 445 | * @deprecated 2.7.0 Unused |
||
| 446 | */ |
||
| 447 | public function get_cart_shipping_total() { |
||
| 480 | |||
| 481 | /** |
||
| 482 | * @deprecated 2.7.0 Unused |
||
| 483 | */ |
||
| 484 | public function get_tax_amount( $tax_rate_id ) { |
||
| 489 | |||
| 490 | /** |
||
| 491 | * @deprecated 2.7.0 Unused |
||
| 492 | */ |
||
| 493 | public function get_shipping_tax_amount( $tax_rate_id ) { |
||
| 498 | |||
| 499 | /** |
||
| 500 | * @deprecated 2.7.0 Unused |
||
| 501 | */ |
||
| 502 | public function get_cart_total() { |
||
| 511 | |||
| 512 | /** |
||
| 513 | * @deprecated 2.7.0 Unused |
||
| 514 | */ |
||
| 515 | public function get_cart_tax() { |
||
| 520 | } |
||
| 521 |
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.