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 |
||
| 15 | class LaraCart implements LaraCartContract |
||
| 16 | { |
||
| 17 | const QTY = 'qty'; |
||
| 18 | const HASH = 'generateCartHash'; |
||
| 19 | const PRICE = 'price'; |
||
| 20 | const SERVICE = 'laracart'; |
||
| 21 | const RANHASH = 'generateRandomCartItemHash'; |
||
| 22 | |||
| 23 | protected $events; |
||
| 24 | protected $session; |
||
| 25 | |||
| 26 | public $cart; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * LaraCart constructor. |
||
| 30 | * |
||
| 31 | * @param SessionManager $session |
||
| 32 | * @param Dispatcher $events |
||
| 33 | */ |
||
| 34 | public function __construct(SessionManager $session, Dispatcher $events) |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Sets and Gets the instance of the cart in the session we should be using |
||
| 44 | * |
||
| 45 | * @param string $instance |
||
| 46 | * |
||
| 47 | * @return LaraCart |
||
| 48 | */ |
||
| 49 | public function setInstance($instance = 'default') |
||
| 50 | { |
||
| 51 | $this->get($instance); |
||
| 52 | |||
| 53 | $this->session->set('laracart.instance', $instance); |
||
| 54 | |||
| 55 | $this->events->fire('laracart.new'); |
||
| 56 | |||
| 57 | return $this; |
||
| 58 | } |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Gets the instance in the session |
||
| 62 | * |
||
| 63 | * @param string $instance |
||
| 64 | * |
||
| 65 | * @return $this cart instance |
||
| 66 | */ |
||
| 67 | public function get($instance = 'default') |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Gets an an attribute from the cart |
||
| 78 | * |
||
| 79 | * @param $attribute |
||
| 80 | * @param $defaultValue |
||
| 81 | * |
||
| 82 | * @return mixed |
||
| 83 | */ |
||
| 84 | public function getAttribute($attribute, $defaultValue = null) |
||
| 85 | { |
||
| 86 | return array_get($this->cart->attributes, $attribute, $defaultValue); |
||
| 87 | } |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Gets all the carts attributes |
||
| 91 | * |
||
| 92 | * @return mixed |
||
| 93 | */ |
||
| 94 | public function getAttributes() |
||
| 95 | { |
||
| 96 | return $this->cart->attributes; |
||
| 97 | } |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Adds an Attribute to the cart |
||
| 101 | * |
||
| 102 | * @param $attribute |
||
| 103 | * @param $value |
||
| 104 | */ |
||
| 105 | public function setAttribute($attribute, $value) |
||
| 106 | { |
||
| 107 | array_set($this->cart->attributes, $attribute, $value); |
||
| 108 | |||
| 109 | $this->update(); |
||
| 110 | } |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Updates cart session |
||
| 114 | */ |
||
| 115 | public function update() |
||
| 116 | { |
||
| 117 | $this->session->set(config('laracart.cache_prefix', 'laracart') . '.' . $this->cart->instance, $this->cart); |
||
| 118 | |||
| 119 | $this->events->fire('laracart.update', $this->cart); |
||
| 120 | } |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Removes an attribute from the cart |
||
| 124 | * |
||
| 125 | * @param $attribute |
||
| 126 | */ |
||
| 127 | public function removeAttribute($attribute) |
||
| 128 | { |
||
| 129 | array_forget($this->cart->attributes, $attribute); |
||
| 130 | |||
| 131 | $this->update(); |
||
| 132 | } |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Creates a CartItem and then adds it to cart |
||
| 136 | * |
||
| 137 | * @param string|int $itemID |
||
| 138 | * @param null $name |
||
| 139 | * @param int $qty |
||
| 140 | * @param string $price |
||
| 141 | * @param array $options |
||
| 142 | * @param bool|true $taxable |
||
| 143 | * |
||
| 144 | * @return CartItem |
||
| 145 | */ |
||
| 146 | public function addLine($itemID, $name = null, $qty = 1, $price = '0.00', $options = [], $taxable = true) |
||
| 147 | { |
||
| 148 | return $this->add($itemID, $name, $qty, $price, $options, $taxable, true); |
||
| 149 | } |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Creates a CartItem and then adds it to cart |
||
| 153 | * |
||
| 154 | * @param $itemID |
||
| 155 | * @param null $name |
||
| 156 | * @param int $qty |
||
| 157 | * @param string $price |
||
| 158 | * @param array $options |
||
| 159 | * @param bool|false $taxable |
||
| 160 | * @param bool|false $lineItem |
||
| 161 | * |
||
| 162 | * @return CartItem |
||
| 163 | */ |
||
| 164 | public function add( |
||
| 165 | $itemID, |
||
| 166 | $name = null, |
||
| 167 | $qty = 1, |
||
| 168 | $price = '0.00', |
||
| 169 | $options = [], |
||
| 170 | $taxable = true, |
||
| 171 | $lineItem = false |
||
| 172 | ) { |
||
| 173 | $item = $this->addItem( |
||
| 174 | new CartItem( |
||
| 175 | $itemID, |
||
| 176 | $name, |
||
| 177 | $qty, |
||
| 178 | $price, |
||
| 179 | $options, |
||
| 180 | $taxable, |
||
| 181 | $lineItem |
||
| 182 | ) |
||
| 183 | ); |
||
| 184 | |||
| 185 | return $this->getItem($item->getHash()); |
||
| 186 | } |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Adds the cartItem into the cart session |
||
| 190 | * |
||
| 191 | * @param CartItem $cartItem |
||
| 192 | * |
||
| 193 | * @return CartItem |
||
| 194 | */ |
||
| 195 | public function addItem(CartItem $cartItem) |
||
| 196 | { |
||
| 197 | $itemHash = $cartItem->generateHash(); |
||
| 198 | |||
| 199 | if ($this->getItem($itemHash)) { |
||
| 200 | $this->getItem($itemHash)->qty += $cartItem->qty; |
||
|
|
|||
| 201 | } else { |
||
| 202 | $this->cart->items[] = $cartItem; |
||
| 203 | } |
||
| 204 | |||
| 205 | $this->events->fire('laracart.addItem', $cartItem); |
||
| 206 | |||
| 207 | $this->update(); |
||
| 208 | |||
| 209 | return $cartItem; |
||
| 210 | } |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Finds a cartItem based on the itemHash |
||
| 214 | * |
||
| 215 | * @param $itemHash |
||
| 216 | * |
||
| 217 | * @return CartItem | null |
||
| 218 | */ |
||
| 219 | public function getItem($itemHash) |
||
| 220 | { |
||
| 221 | return array_get($this->getItems(), $itemHash); |
||
| 222 | } |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Gets all the items within the cart |
||
| 226 | * |
||
| 227 | * @return array |
||
| 228 | */ |
||
| 229 | public function getItems() |
||
| 230 | { |
||
| 231 | $items = []; |
||
| 232 | if (isset($this->cart->items) === true) { |
||
| 233 | foreach ($this->cart->items as $item) { |
||
| 234 | $items[$item->getHash()] = $item; |
||
| 235 | } |
||
| 236 | } |
||
| 237 | |||
| 238 | return $items; |
||
| 239 | } |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Updates an items attributes |
||
| 243 | * |
||
| 244 | * @param $itemHash |
||
| 245 | * @param $key |
||
| 246 | * @param $value |
||
| 247 | * |
||
| 248 | * @return CartItem |
||
| 249 | * |
||
| 250 | * @throws Exceptions\InvalidPrice |
||
| 251 | * @throws Exceptions\InvalidQuantity |
||
| 252 | */ |
||
| 253 | public function updateItem($itemHash, $key, $value) |
||
| 254 | { |
||
| 255 | if (empty($item = $this->getItem($itemHash)) === false) { |
||
| 256 | $item->$key = $value; |
||
| 257 | } |
||
| 258 | |||
| 259 | $item->generateHash(); |
||
| 260 | |||
| 261 | return $item; |
||
| 262 | } |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Removes a CartItem based on the itemHash |
||
| 266 | * |
||
| 267 | * @param $itemHash |
||
| 268 | */ |
||
| 269 | public function removeItem($itemHash) |
||
| 270 | { |
||
| 271 | foreach ($this->cart->items as $itemKey => $item) { |
||
| 272 | if ($item->getHash() == $itemHash) { |
||
| 273 | unset($this->cart->items[$itemKey]); |
||
| 274 | break; |
||
| 275 | } |
||
| 276 | } |
||
| 277 | |||
| 278 | $this->events->fire('laracart.removeItem', $itemHash); |
||
| 279 | } |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Empties the carts items |
||
| 283 | */ |
||
| 284 | public function emptyCart() |
||
| 285 | { |
||
| 286 | unset($this->cart->items); |
||
| 287 | |||
| 288 | $this->update(); |
||
| 289 | |||
| 290 | $this->events->fire('laracart.empty', $this->cart->instance); |
||
| 291 | } |
||
| 292 | |||
| 293 | /** |
||
| 294 | * Completely destroys cart and anything associated with it |
||
| 295 | */ |
||
| 296 | public function destroyCart() |
||
| 297 | { |
||
| 298 | $instance = $this->cart->instance; |
||
| 299 | |||
| 300 | $this->session->forget(config('laracart.cache_prefix', 'laracart') . '.' . $instance); |
||
| 301 | |||
| 302 | $this->setInstance('default'); |
||
| 303 | |||
| 304 | $this->events->fire('laracart.destroy', $instance); |
||
| 305 | } |
||
| 306 | |||
| 307 | /** |
||
| 308 | * Gets the coupons for the current cart |
||
| 309 | * |
||
| 310 | * @return array |
||
| 311 | */ |
||
| 312 | public function getCoupons() |
||
| 313 | { |
||
| 314 | return $this->cart->coupons; |
||
| 315 | } |
||
| 316 | |||
| 317 | /** |
||
| 318 | * Finds a specific coupon in the cart |
||
| 319 | * |
||
| 320 | * @param $code |
||
| 321 | * @return mixed |
||
| 322 | */ |
||
| 323 | public function findCoupon($code) |
||
| 324 | { |
||
| 325 | return array_get($this->cart->coupons, $code); |
||
| 326 | } |
||
| 327 | |||
| 328 | /** |
||
| 329 | * Applies a coupon to the cart |
||
| 330 | * |
||
| 331 | * @param CouponContract $coupon |
||
| 332 | */ |
||
| 333 | public function addCoupon(CouponContract $coupon) |
||
| 343 | |||
| 344 | /** |
||
| 345 | * Removes a coupon in the cart |
||
| 346 | * |
||
| 347 | * @param $code |
||
| 348 | */ |
||
| 349 | public function removeCoupon($code) |
||
| 350 | { |
||
| 351 | foreach ($this->getItems() as $item) { |
||
| 352 | if (isset($item->code) && $item->code == $code) { |
||
| 353 | $item->code = null; |
||
| 354 | $item->discount = null; |
||
| 355 | $item->couponInfo = []; |
||
| 356 | } |
||
| 357 | } |
||
| 358 | |||
| 359 | array_forget($this->cart->coupons, $code); |
||
| 360 | |||
| 361 | $this->update(); |
||
| 362 | } |
||
| 363 | |||
| 364 | /** |
||
| 365 | * Gets a speific fee from the fees array |
||
| 366 | * |
||
| 367 | * @param $name |
||
| 368 | * |
||
| 369 | * @return mixed |
||
| 370 | */ |
||
| 371 | public function getFee($name) |
||
| 372 | { |
||
| 373 | return array_get($this->cart->fees, $name, new CartFee(null, false)); |
||
| 374 | } |
||
| 375 | |||
| 376 | /** |
||
| 377 | * Allows to charge for additional fees that may or may not be taxable |
||
| 378 | * ex - service fee , delivery fee, tips |
||
| 379 | * |
||
| 380 | * @param $name |
||
| 381 | * @param $amount |
||
| 382 | * @param bool|false $taxable |
||
| 383 | * @param array $options |
||
| 384 | */ |
||
| 385 | public function addFee($name, $amount, $taxable = false, Array $options = []) |
||
| 386 | { |
||
| 387 | array_set($this->cart->fees, $name, new CartFee($amount, $taxable, $options)); |
||
| 388 | |||
| 389 | $this->update(); |
||
| 390 | } |
||
| 391 | |||
| 392 | /** |
||
| 393 | * Reemoves a fee from the fee array |
||
| 394 | * |
||
| 395 | * @param $name |
||
| 396 | */ |
||
| 397 | public function removeFee($name) |
||
| 398 | { |
||
| 399 | array_forget($this->cart->fees, $name); |
||
| 400 | |||
| 401 | $this->update(); |
||
| 402 | } |
||
| 403 | |||
| 404 | /** |
||
| 405 | * Gets the total tax for the cart |
||
| 406 | * |
||
| 407 | * @param bool|true $format |
||
| 408 | * |
||
| 409 | * @return string |
||
| 410 | */ |
||
| 411 | public function taxTotal($format = true) |
||
| 412 | { |
||
| 413 | $totalTax = 0; |
||
| 414 | $discounted = 0; |
||
| 415 | $totalDiscount = $this->totalDiscount(false); |
||
| 416 | |||
| 417 | if ($this->count() != 0) { |
||
| 418 | foreach ($this->getItems() as $item) { |
||
| 419 | if ($discounted >= $totalDiscount) { |
||
| 420 | $totalTax += $item->tax(); |
||
| 421 | } else { |
||
| 422 | $itemPrice = $item->subTotal(false); |
||
| 423 | |||
| 424 | if (($discounted + $itemPrice) > $totalDiscount) { |
||
| 425 | $totalTax += $item->tax($totalDiscount - $discounted); |
||
| 426 | } |
||
| 427 | |||
| 428 | $discounted += $itemPrice; |
||
| 429 | } |
||
| 430 | } |
||
| 431 | } |
||
| 432 | |||
| 433 | return $this->formatMoney($totalTax, null, null, $format); |
||
| 434 | } |
||
| 435 | |||
| 436 | /** |
||
| 437 | * Gets the total of the cart with or without tax |
||
| 438 | * |
||
| 439 | * @param boolean $format |
||
| 440 | * @param boolean $withDiscount |
||
| 441 | * |
||
| 442 | * @return string |
||
| 443 | */ |
||
| 444 | public function total($format = true, $withDiscount = true) |
||
| 445 | { |
||
| 446 | $total = $this->subTotal(false) + $this->feeTotals(false); |
||
| 447 | |||
| 448 | if ($withDiscount) { |
||
| 449 | $total -= $this->totalDiscount(false); |
||
| 450 | } |
||
| 451 | |||
| 452 | $total += $this->taxTotal(false); |
||
| 453 | |||
| 454 | return $this->formatMoney($total, null, null, $format); |
||
| 455 | } |
||
| 456 | |||
| 457 | /** |
||
| 458 | * Gets the subtotal of the cart with or without tax |
||
| 459 | * |
||
| 460 | * @param boolean $format |
||
| 461 | * @param boolean $withDiscount |
||
| 462 | * |
||
| 463 | * @return string |
||
| 464 | */ |
||
| 465 | public function subTotal($format = true, $withDiscount = true) |
||
| 466 | { |
||
| 467 | $total = 0; |
||
| 468 | |||
| 469 | if ($this->count() != 0) { |
||
| 470 | foreach ($this->getItems() as $item) { |
||
| 471 | $total += $item->subTotal(false, $withDiscount); |
||
| 472 | } |
||
| 473 | } |
||
| 474 | |||
| 475 | return $this->formatMoney($total, null, null, $format); |
||
| 476 | } |
||
| 477 | |||
| 478 | /** |
||
| 479 | * Get the count based on qty, or number of unique items |
||
| 480 | * |
||
| 481 | * @param bool $withItemQty |
||
| 482 | * |
||
| 483 | * @return int |
||
| 484 | */ |
||
| 485 | public function count($withItemQty = true) |
||
| 486 | { |
||
| 487 | $count = 0; |
||
| 488 | |||
| 489 | foreach ($this->getItems() as $item) { |
||
| 490 | if ($withItemQty) { |
||
| 491 | $count += $item->qty; |
||
| 492 | } else { |
||
| 493 | $count++; |
||
| 494 | } |
||
| 495 | } |
||
| 496 | |||
| 497 | return $count; |
||
| 498 | } |
||
| 499 | |||
| 500 | /** |
||
| 501 | * |
||
| 502 | * Formats the number into a money format based on the locale and international formats |
||
| 503 | * |
||
| 504 | * @param $number |
||
| 505 | * @param $locale |
||
| 506 | * @param $internationalFormat |
||
| 507 | * @param $format |
||
| 508 | * |
||
| 509 | * @return string |
||
| 510 | */ |
||
| 511 | public static function formatMoney($number, $locale = null, $internationalFormat = null, $format = true) |
||
| 512 | { |
||
| 513 | $number = number_format($number, 2, '.', ''); |
||
| 514 | |||
| 515 | if ($format) { |
||
| 516 | setlocale(LC_MONETARY, empty($locale) ? config('laracart.locale', 'en_US.UTF-8') : $locale); |
||
| 517 | |||
| 518 | if (empty($internationalFormat) === true) { |
||
| 519 | $internationalFormat = config('laracart.international_format', false); |
||
| 520 | } |
||
| 521 | |||
| 522 | $number = money_format($internationalFormat ? '%i' : '%n', $number); |
||
| 523 | } |
||
| 524 | |||
| 525 | return $number; |
||
| 526 | } |
||
| 527 | |||
| 528 | /** |
||
| 529 | * Gets all the fee totals |
||
| 530 | * |
||
| 531 | * @param boolean $format |
||
| 532 | * |
||
| 533 | * @return string |
||
| 534 | */ |
||
| 535 | public function feeTotals($format = true) |
||
| 548 | |||
| 549 | /** |
||
| 550 | * Gets all the fees on the cart object |
||
| 551 | * |
||
| 552 | * @return mixed |
||
| 553 | */ |
||
| 554 | public function getFees() |
||
| 555 | { |
||
| 556 | return $this->cart->fees; |
||
| 557 | } |
||
| 558 | |||
| 559 | /** |
||
| 560 | * Gets the total amount discounted |
||
| 561 | * |
||
| 562 | * @param boolean $format |
||
| 563 | * |
||
| 564 | * @return string |
||
| 565 | */ |
||
| 566 | public function totalDiscount($format = true) |
||
| 567 | { |
||
| 568 | $total = 0; |
||
| 569 | |||
| 570 | foreach ($this->cart->coupons as $coupon) { |
||
| 571 | if($coupon->appliedToCart) { |
||
| 572 | $total += $coupon->discount(); |
||
| 573 | } |
||
| 578 | } |
||
| 579 |
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.