Complex classes like LaraCart 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 LaraCart, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | class LaraCart implements LaraCartContract |
||
| 17 | { |
||
| 18 | const QTY = 'qty'; |
||
| 19 | const HASH = 'generateCartHash'; |
||
| 20 | const PRICE = 'price'; |
||
| 21 | const SERVICE = 'laracart'; |
||
| 22 | const RANHASH = 'generateRandomCartItemHash'; |
||
| 23 | |||
| 24 | protected $events; |
||
| 25 | protected $session; |
||
| 26 | protected $authManager; |
||
| 27 | protected $prefix; |
||
| 28 | |||
| 29 | public $cart; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * LaraCart constructor. |
||
| 33 | * |
||
| 34 | * @param SessionManager $session |
||
| 35 | * @param Dispatcher $events |
||
| 36 | * @param AuthManager $authManager |
||
| 37 | */ |
||
| 38 | public function __construct(SessionManager $session, Dispatcher $events, AuthManager $authManager) |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Gets all current instances inside the session |
||
| 50 | * |
||
| 51 | * @return mixed |
||
| 52 | */ |
||
| 53 | public function getInstances() |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Sets and Gets the instance of the cart in the session we should be using |
||
| 62 | * |
||
| 63 | * @param string $instance |
||
| 64 | * |
||
| 65 | * @return LaraCart |
||
| 66 | */ |
||
| 67 | public function setInstance($instance = 'default') |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Gets the instance in the session |
||
| 80 | * |
||
| 81 | * @param string $instance |
||
| 82 | * |
||
| 83 | * @return $this cart instance |
||
| 84 | */ |
||
| 85 | public function get($instance = 'default') |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Gets an an attribute from the cart |
||
| 104 | * |
||
| 105 | * @param $attribute |
||
| 106 | * @param $defaultValue |
||
| 107 | * |
||
| 108 | * @return mixed |
||
| 109 | */ |
||
| 110 | public function getAttribute($attribute, $defaultValue = null) |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Gets all the carts attributes |
||
| 117 | * |
||
| 118 | * @return mixed |
||
| 119 | */ |
||
| 120 | public function getAttributes() |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Adds an Attribute to the cart |
||
| 127 | * |
||
| 128 | * @param $attribute |
||
| 129 | * @param $value |
||
| 130 | */ |
||
| 131 | public function setAttribute($attribute, $value) |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Updates cart session |
||
| 140 | */ |
||
| 141 | public function update() |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Removes an attribute from the cart |
||
| 155 | * |
||
| 156 | * @param $attribute |
||
| 157 | */ |
||
| 158 | public function removeAttribute($attribute) |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Creates a CartItem and then adds it to cart |
||
| 167 | * |
||
| 168 | * @param string|int $itemID |
||
| 169 | * @param null $name |
||
| 170 | * @param int $qty |
||
| 171 | * @param string $price |
||
| 172 | * @param array $options |
||
| 173 | * @param bool|true $taxable |
||
| 174 | * |
||
| 175 | * @return CartItem |
||
| 176 | */ |
||
| 177 | public function addLine($itemID, $name = null, $qty = 1, $price = '0.00', $options = [], $taxable = true) |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Creates a CartItem and then adds it to cart |
||
| 184 | * |
||
| 185 | * @param $itemID |
||
| 186 | * @param null $name |
||
| 187 | * @param int $qty |
||
| 188 | * @param string $price |
||
| 189 | * @param array $options |
||
| 190 | * @param bool|false $taxable |
||
| 191 | * @param bool|false $lineItem |
||
| 192 | * |
||
| 193 | * @return CartItem |
||
| 194 | */ |
||
| 195 | public function add( |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Adds the cartItem into the cart session |
||
| 223 | * |
||
| 224 | * @param CartItem $cartItem |
||
| 225 | * |
||
| 226 | * @return CartItem |
||
| 227 | */ |
||
| 228 | public function addItem(CartItem $cartItem) |
||
| 242 | |||
| 243 | /** |
||
| 244 | * Increment the quantity of a cartItem based on the itemHash |
||
| 245 | * |
||
| 246 | * @param $itemHash |
||
| 247 | * |
||
| 248 | * @return CartItem | null |
||
| 249 | */ |
||
| 250 | public function increment($itemHash) |
||
| 258 | |||
| 259 | /** |
||
| 260 | * Decrement the quantity of a cartItem based on the itemHash |
||
| 261 | * |
||
| 262 | * @param $itemHash |
||
| 263 | * |
||
| 264 | * @return CartItem | null |
||
| 265 | */ |
||
| 266 | public function decrement($itemHash) |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Find items in the cart matching a data set |
||
| 283 | * |
||
| 284 | * |
||
| 285 | * param $data |
||
| 286 | * @return array |
||
| 287 | */ |
||
| 288 | public function find($data) |
||
| 300 | |||
| 301 | /** |
||
| 302 | * Finds a cartItem based on the itemHash |
||
| 303 | * |
||
| 304 | * @param $itemHash |
||
| 305 | * |
||
| 306 | * @return CartItem | null |
||
| 307 | */ |
||
| 308 | public function getItem($itemHash) |
||
| 312 | |||
| 313 | /** |
||
| 314 | * Gets all the items within the cart |
||
| 315 | * |
||
| 316 | * @return array |
||
| 317 | */ |
||
| 318 | public function getItems() |
||
| 329 | |||
| 330 | /** |
||
| 331 | * Updates an items attributes |
||
| 332 | * |
||
| 333 | * @param $itemHash |
||
| 334 | * @param $key |
||
| 335 | * @param $value |
||
| 336 | * |
||
| 337 | * @return CartItem |
||
| 338 | * |
||
| 339 | * @throws Exceptions\InvalidPrice |
||
| 340 | * @throws Exceptions\InvalidQuantity |
||
| 341 | */ |
||
| 342 | public function updateItem($itemHash, $key, $value) |
||
| 354 | |||
| 355 | /** |
||
| 356 | * Removes a CartItem based on the itemHash |
||
| 357 | * |
||
| 358 | * @param $itemHash |
||
| 359 | */ |
||
| 360 | public function removeItem($itemHash) |
||
| 373 | |||
| 374 | /** |
||
| 375 | * Empties the carts items |
||
| 376 | */ |
||
| 377 | public function emptyCart() |
||
| 385 | |||
| 386 | /** |
||
| 387 | * Completely destroys cart and anything associated with it |
||
| 388 | */ |
||
| 389 | public function destroyCart() |
||
| 401 | |||
| 402 | /** |
||
| 403 | * Gets the coupons for the current cart |
||
| 404 | * |
||
| 405 | * @return array |
||
| 406 | */ |
||
| 407 | public function getCoupons() |
||
| 411 | |||
| 412 | /** |
||
| 413 | * Finds a specific coupon in the cart |
||
| 414 | * |
||
| 415 | * @param $code |
||
| 416 | * @return mixed |
||
| 417 | */ |
||
| 418 | public function findCoupon($code) |
||
| 422 | |||
| 423 | /** |
||
| 424 | * Applies a coupon to the cart |
||
| 425 | * |
||
| 426 | * @param CouponContract $coupon |
||
| 427 | */ |
||
| 428 | public function addCoupon(CouponContract $coupon) |
||
| 438 | |||
| 439 | /** |
||
| 440 | * Removes a coupon in the cart |
||
| 441 | * |
||
| 442 | * @param $code |
||
| 443 | */ |
||
| 444 | public function removeCoupon($code) |
||
| 458 | |||
| 459 | /** |
||
| 460 | * Gets a speific fee from the fees array |
||
| 461 | * |
||
| 462 | * @param $name |
||
| 463 | * |
||
| 464 | * @return mixed |
||
| 465 | */ |
||
| 466 | public function getFee($name) |
||
| 470 | |||
| 471 | /** |
||
| 472 | * Allows to charge for additional fees that may or may not be taxable |
||
| 473 | * ex - service fee , delivery fee, tips |
||
| 474 | * |
||
| 475 | * @param $name |
||
| 476 | * @param $amount |
||
| 477 | * @param bool|false $taxable |
||
| 478 | * @param array $options |
||
| 479 | */ |
||
| 480 | public function addFee($name, $amount, $taxable = false, array $options = []) |
||
| 486 | |||
| 487 | /** |
||
| 488 | * Reemoves a fee from the fee array |
||
| 489 | * |
||
| 490 | * @param $name |
||
| 491 | */ |
||
| 492 | public function removeFee($name) |
||
| 498 | |||
| 499 | /** |
||
| 500 | * Gets the total tax for the cart |
||
| 501 | * |
||
| 502 | * @param bool|true $format |
||
| 503 | * |
||
| 504 | * @return string |
||
| 505 | */ |
||
| 506 | public function taxTotal($format = true) |
||
| 536 | |||
| 537 | /** |
||
| 538 | * Gets the total of the cart with or without tax |
||
| 539 | * |
||
| 540 | * @param boolean $format |
||
| 541 | * @param boolean $withDiscount |
||
| 542 | * |
||
| 543 | * @return string |
||
| 544 | */ |
||
| 545 | public function total($format = true, $withDiscount = true, $withTax = true) |
||
| 559 | |||
| 560 | /** |
||
| 561 | * Gets the subtotal of the cart with or without tax |
||
| 562 | * |
||
| 563 | * @param boolean $format |
||
| 564 | * @param boolean $withDiscount |
||
| 565 | * |
||
| 566 | * @return string |
||
| 567 | */ |
||
| 568 | public function subTotal($format = true, $withDiscount = true) |
||
| 580 | |||
| 581 | /** |
||
| 582 | * Get the count based on qty, or number of unique items |
||
| 583 | * |
||
| 584 | * @param bool $withItemQty |
||
| 585 | * |
||
| 586 | * @return int |
||
| 587 | */ |
||
| 588 | public function count($withItemQty = true) |
||
| 602 | |||
| 603 | /** |
||
| 604 | * |
||
| 605 | * Formats the number into a money format based on the locale and international formats |
||
| 606 | * |
||
| 607 | * @param $number |
||
| 608 | * @param $locale |
||
| 609 | * @param $internationalFormat |
||
| 610 | * @param $format |
||
| 611 | * |
||
| 612 | * @return string |
||
| 613 | */ |
||
| 614 | public static function formatMoney($number, $locale = null, $internationalFormat = null, $format = true) |
||
| 631 | |||
| 632 | /** |
||
| 633 | * Gets all the fee totals |
||
| 634 | * |
||
| 635 | * @param boolean $format |
||
| 636 | * |
||
| 637 | * @return string |
||
| 638 | */ |
||
| 639 | public function feeTotals($format = true, $withTax = false) |
||
| 653 | |||
| 654 | /** |
||
| 655 | * Gets all the fees on the cart object |
||
| 656 | * |
||
| 657 | * @return mixed |
||
| 658 | */ |
||
| 659 | public function getFees() |
||
| 663 | |||
| 664 | /** |
||
| 665 | * Gets the total amount discounted |
||
| 666 | * |
||
| 667 | * @param boolean $format |
||
| 668 | * |
||
| 669 | * @return string |
||
| 670 | */ |
||
| 671 | public function totalDiscount($format = true) |
||
| 683 | } |
||
| 684 |
Since your code implements the magic setter
_set, this function will be called for any write access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.Since the property has write access only, you can use the @property-write annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.