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 | |||
| 28 | public $cart; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * LaraCart constructor. |
||
| 32 | * |
||
| 33 | * @param SessionManager $session |
||
| 34 | * @param Dispatcher $events |
||
| 35 | * @param AuthManager $authManager |
||
| 36 | */ |
||
| 37 | public function __construct(SessionManager $session, Dispatcher $events, AuthManager $authManager) |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Sets and Gets the instance of the cart in the session we should be using |
||
| 48 | * |
||
| 49 | * @param string $instance |
||
| 50 | * |
||
| 51 | * @return LaraCart |
||
| 52 | */ |
||
| 53 | public function setInstance($instance = 'default') |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Gets the instance in the session |
||
| 66 | * |
||
| 67 | * @param string $instance |
||
| 68 | * |
||
| 69 | * @return $this cart instance |
||
| 70 | */ |
||
| 71 | public function get($instance = 'default') |
||
| 72 | { |
||
| 73 | if (config('laracart.cross_devices', false)) { |
||
| 74 | if (!empty($cartSessionID = $this->authManager->user()->cart_session_id)) { |
||
| 75 | $this->session->setId($cartSessionID); |
||
| 76 | $this->session->start(); |
||
| 77 | } |
||
| 78 | } |
||
| 79 | |||
| 80 | if (empty($this->cart = $this->session->get(config('laracart.cache_prefix', 'laracart') . '.' . $instance))) { |
||
| 81 | $this->cart = new Cart($instance); |
||
| 82 | } |
||
| 83 | |||
| 84 | return $this; |
||
| 85 | } |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Gets an an attribute from the cart |
||
| 89 | * |
||
| 90 | * @param $attribute |
||
| 91 | * @param $defaultValue |
||
| 92 | * |
||
| 93 | * @return mixed |
||
| 94 | */ |
||
| 95 | public function getAttribute($attribute, $defaultValue = null) |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Gets all the carts attributes |
||
| 102 | * |
||
| 103 | * @return mixed |
||
| 104 | */ |
||
| 105 | public function getAttributes() |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Adds an Attribute to the cart |
||
| 112 | * |
||
| 113 | * @param $attribute |
||
| 114 | * @param $value |
||
| 115 | */ |
||
| 116 | public function setAttribute($attribute, $value) |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Updates cart session |
||
| 125 | */ |
||
| 126 | public function update() |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Removes an attribute from the cart |
||
| 142 | * |
||
| 143 | * @param $attribute |
||
| 144 | */ |
||
| 145 | public function removeAttribute($attribute) |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Creates a CartItem and then adds it to cart |
||
| 154 | * |
||
| 155 | * @param string|int $itemID |
||
| 156 | * @param null $name |
||
| 157 | * @param int $qty |
||
| 158 | * @param string $price |
||
| 159 | * @param array $options |
||
| 160 | * @param bool|true $taxable |
||
| 161 | * |
||
| 162 | * @return CartItem |
||
| 163 | */ |
||
| 164 | public function addLine($itemID, $name = null, $qty = 1, $price = '0.00', $options = [], $taxable = true) |
||
| 165 | { |
||
| 166 | return $this->add($itemID, $name, $qty, $price, $options, $taxable, true); |
||
| 167 | } |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Creates a CartItem and then adds it to cart |
||
| 171 | * |
||
| 172 | * @param $itemID |
||
| 173 | * @param null $name |
||
| 174 | * @param int $qty |
||
| 175 | * @param string $price |
||
| 176 | * @param array $options |
||
| 177 | * @param bool|false $taxable |
||
| 178 | * @param bool|false $lineItem |
||
| 179 | * |
||
| 180 | * @return CartItem |
||
| 181 | */ |
||
| 182 | public function add( |
||
| 183 | $itemID, |
||
| 184 | $name = null, |
||
| 185 | $qty = 1, |
||
| 186 | $price = '0.00', |
||
| 187 | $options = [], |
||
| 188 | $taxable = true, |
||
| 189 | $lineItem = false |
||
| 190 | ) { |
||
| 191 | $item = $this->addItem( |
||
| 192 | new CartItem( |
||
| 193 | $itemID, |
||
| 194 | $name, |
||
| 195 | $qty, |
||
| 196 | $price, |
||
| 197 | $options, |
||
| 198 | $taxable, |
||
| 199 | $lineItem |
||
| 200 | ) |
||
| 201 | ); |
||
| 202 | |||
| 203 | $this->update(); |
||
| 204 | |||
| 205 | return $this->getItem($item->getHash()); |
||
| 206 | } |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Adds the cartItem into the cart session |
||
| 210 | * |
||
| 211 | * @param CartItem $cartItem |
||
| 212 | * |
||
| 213 | * @return CartItem |
||
| 214 | */ |
||
| 215 | public function addItem(CartItem $cartItem) |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Increment the quantity of a cartItem based on the itemHash |
||
| 232 | * |
||
| 233 | * @param $itemHash |
||
| 234 | * |
||
| 235 | * @return CartItem | null |
||
| 236 | */ |
||
| 237 | public function increment($itemHash) |
||
| 245 | |||
| 246 | /** |
||
| 247 | * Decrement the quantity of a cartItem based on the itemHash |
||
| 248 | * |
||
| 249 | * @param $itemHash |
||
| 250 | * |
||
| 251 | * @return CartItem | null |
||
| 252 | */ |
||
| 253 | public function decrement($itemHash) |
||
| 267 | |||
| 268 | /* |
||
| 269 | * Find items in the cart matching a data set |
||
| 270 | * |
||
| 271 | * @return array |
||
| 272 | */ |
||
| 273 | public function find($data) |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Finds a cartItem based on the itemHash |
||
| 288 | * |
||
| 289 | * @param $itemHash |
||
| 290 | * |
||
| 291 | * @return CartItem | null |
||
| 292 | */ |
||
| 293 | public function getItem($itemHash) |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Gets all the items within the cart |
||
| 300 | * |
||
| 301 | * @return array |
||
| 302 | */ |
||
| 303 | public function getItems() |
||
| 314 | |||
| 315 | /** |
||
| 316 | * Updates an items attributes |
||
| 317 | * |
||
| 318 | * @param $itemHash |
||
| 319 | * @param $key |
||
| 320 | * @param $value |
||
| 321 | * |
||
| 322 | * @return CartItem |
||
| 323 | * |
||
| 324 | * @throws Exceptions\InvalidPrice |
||
| 325 | * @throws Exceptions\InvalidQuantity |
||
| 326 | */ |
||
| 327 | public function updateItem($itemHash, $key, $value) |
||
| 339 | |||
| 340 | /** |
||
| 341 | * Removes a CartItem based on the itemHash |
||
| 342 | * |
||
| 343 | * @param $itemHash |
||
| 344 | */ |
||
| 345 | public function removeItem($itemHash) |
||
| 358 | |||
| 359 | /** |
||
| 360 | * Empties the carts items |
||
| 361 | */ |
||
| 362 | public function emptyCart() |
||
| 370 | |||
| 371 | /** |
||
| 372 | * Completely destroys cart and anything associated with it |
||
| 373 | */ |
||
| 374 | public function destroyCart() |
||
| 386 | |||
| 387 | /** |
||
| 388 | * Gets the coupons for the current cart |
||
| 389 | * |
||
| 390 | * @return array |
||
| 391 | */ |
||
| 392 | public function getCoupons() |
||
| 396 | |||
| 397 | /** |
||
| 398 | * Finds a specific coupon in the cart |
||
| 399 | * |
||
| 400 | * @param $code |
||
| 401 | * @return mixed |
||
| 402 | */ |
||
| 403 | public function findCoupon($code) |
||
| 407 | |||
| 408 | /** |
||
| 409 | * Applies a coupon to the cart |
||
| 410 | * |
||
| 411 | * @param CouponContract $coupon |
||
| 412 | */ |
||
| 413 | public function addCoupon(CouponContract $coupon) |
||
| 423 | |||
| 424 | /** |
||
| 425 | * Removes a coupon in the cart |
||
| 426 | * |
||
| 427 | * @param $code |
||
| 428 | */ |
||
| 429 | public function removeCoupon($code) |
||
| 443 | |||
| 444 | /** |
||
| 445 | * Gets a speific fee from the fees array |
||
| 446 | * |
||
| 447 | * @param $name |
||
| 448 | * |
||
| 449 | * @return mixed |
||
| 450 | */ |
||
| 451 | public function getFee($name) |
||
| 455 | |||
| 456 | /** |
||
| 457 | * Allows to charge for additional fees that may or may not be taxable |
||
| 458 | * ex - service fee , delivery fee, tips |
||
| 459 | * |
||
| 460 | * @param $name |
||
| 461 | * @param $amount |
||
| 462 | * @param bool|false $taxable |
||
| 463 | * @param array $options |
||
| 464 | */ |
||
| 465 | public function addFee($name, $amount, $taxable = false, array $options = []) |
||
| 471 | |||
| 472 | /** |
||
| 473 | * Reemoves a fee from the fee array |
||
| 474 | * |
||
| 475 | * @param $name |
||
| 476 | */ |
||
| 477 | public function removeFee($name) |
||
| 483 | |||
| 484 | /** |
||
| 485 | * Gets the total tax for the cart |
||
| 486 | * |
||
| 487 | * @param bool|true $format |
||
| 488 | * |
||
| 489 | * @return string |
||
| 490 | */ |
||
| 491 | public function taxTotal($format = true) |
||
| 521 | |||
| 522 | /** |
||
| 523 | * Gets the total of the cart with or without tax |
||
| 524 | * |
||
| 525 | * @param boolean $format |
||
| 526 | * @param boolean $withDiscount |
||
| 527 | * |
||
| 528 | * @return string |
||
| 529 | */ |
||
| 530 | public function total($format = true, $withDiscount = true, $withTax = true) |
||
| 544 | |||
| 545 | /** |
||
| 546 | * Gets the subtotal of the cart with or without tax |
||
| 547 | * |
||
| 548 | * @param boolean $format |
||
| 549 | * @param boolean $withDiscount |
||
| 550 | * |
||
| 551 | * @return string |
||
| 552 | */ |
||
| 553 | public function subTotal($format = true, $withDiscount = true) |
||
| 565 | |||
| 566 | /** |
||
| 567 | * Get the count based on qty, or number of unique items |
||
| 568 | * |
||
| 569 | * @param bool $withItemQty |
||
| 570 | * |
||
| 571 | * @return int |
||
| 572 | */ |
||
| 573 | public function count($withItemQty = true) |
||
| 587 | |||
| 588 | /** |
||
| 589 | * |
||
| 590 | * Formats the number into a money format based on the locale and international formats |
||
| 591 | * |
||
| 592 | * @param $number |
||
| 593 | * @param $locale |
||
| 594 | * @param $internationalFormat |
||
| 595 | * @param $format |
||
| 596 | * |
||
| 597 | * @return string |
||
| 598 | */ |
||
| 599 | public static function formatMoney($number, $locale = null, $internationalFormat = null, $format = true) |
||
| 616 | |||
| 617 | /** |
||
| 618 | * Gets all the fee totals |
||
| 619 | * |
||
| 620 | * @param boolean $format |
||
| 621 | * |
||
| 622 | * @return string |
||
| 623 | */ |
||
| 624 | public function feeTotals($format = true, $withTax = false) |
||
| 638 | |||
| 639 | /** |
||
| 640 | * Gets all the fees on the cart object |
||
| 641 | * |
||
| 642 | * @return mixed |
||
| 643 | */ |
||
| 644 | public function getFees() |
||
| 648 | |||
| 649 | /** |
||
| 650 | * Gets the total amount discounted |
||
| 651 | * |
||
| 652 | * @param boolean $format |
||
| 653 | * |
||
| 654 | * @return string |
||
| 655 | */ |
||
| 656 | public function totalDiscount($format = true) |
||
| 668 | } |
||
| 669 |
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.