Complex classes like 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 Cart, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 21 | class Cart |
||
| 22 | { |
||
| 23 | /** |
||
| 24 | * Session manager. |
||
| 25 | * |
||
| 26 | * @var \iBrand\Shoppingcart\Storage\Storage |
||
| 27 | */ |
||
| 28 | protected $storage; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Event dispatcher. |
||
| 32 | * |
||
| 33 | * @var \Illuminate\Contracts\Events\Dispatcher |
||
| 34 | */ |
||
| 35 | protected $event; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Current cart name. |
||
| 39 | * |
||
| 40 | * @var string |
||
| 41 | */ |
||
| 42 | protected $name = 'cart.default'; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Associated model name. |
||
| 46 | * |
||
| 47 | * @var string |
||
| 48 | */ |
||
| 49 | protected $model; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Constructor. |
||
| 53 | * |
||
| 54 | * @param \iBrand\Shoppingcart\Storage\Storage $storage $storage class name |
||
| 55 | * @param \Illuminate\Contracts\Events\Dispatcher $event Event class name |
||
| 56 | */ |
||
| 57 | public function __construct(Storage $storage, Dispatcher $event) |
||
| 62 | |||
| 63 | public function setStorage(Storage $storage) |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Set the current cart name. |
||
| 70 | * |
||
| 71 | * @param string $name Cart name name |
||
| 72 | * |
||
| 73 | * @return Cart |
||
| 74 | */ |
||
| 75 | public function name($name) |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Associated model. |
||
| 84 | * |
||
| 85 | * @param string $model The name of the model |
||
| 86 | * |
||
| 87 | * @return Cart |
||
| 88 | */ |
||
| 89 | public function associate($model) |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Get all items. |
||
| 101 | * |
||
| 102 | * @return \Illuminate\Support\Collection |
||
| 103 | */ |
||
| 104 | public function all() |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Add a row to the cart. |
||
| 111 | * |
||
| 112 | * @param int|string $id Unique ID of the item |
||
| 113 | * @param string $name Name of the item |
||
| 114 | * @param int $qty Item qty to add to the cart |
||
| 115 | * @param float $price Price of one item |
||
| 116 | * @param array $attributes Array of additional attributes, such as 'size' or 'color'... |
||
| 117 | * |
||
| 118 | * @return string |
||
| 119 | */ |
||
| 120 | public function add($id, $name = null, $qty = null, $price = null, array $attributes = []) |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Update the quantity of one row of the cart. |
||
| 135 | * |
||
| 136 | * @param string $rawId The __raw_id of the item you want to update |
||
| 137 | * @param int|array $attribute New quantity of the item|Array of attributes to update |
||
| 138 | * |
||
| 139 | * @return Item|bool |
||
| 140 | */ |
||
| 141 | public function update($rawId, $attribute) |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Remove a row from the cart. |
||
| 163 | * |
||
| 164 | * @param string $rawId The __raw_id of the item |
||
| 165 | * |
||
| 166 | * @return bool |
||
| 167 | */ |
||
| 168 | public function remove($rawId) |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Get a row of the cart by its ID. |
||
| 189 | * |
||
| 190 | * @param string $rawId The ID of the row to fetch |
||
| 191 | * |
||
| 192 | * @return Item |
||
| 193 | */ |
||
| 194 | public function get($rawId) |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Clean the cart. |
||
| 203 | * |
||
| 204 | * @return bool |
||
| 205 | */ |
||
| 206 | public function destroy() |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Alias of destory(). |
||
| 221 | * |
||
| 222 | * @return bool |
||
| 223 | */ |
||
| 224 | public function clean() |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Get the price total. |
||
| 231 | * |
||
| 232 | * @return float |
||
| 233 | */ |
||
| 234 | public function total() |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Return total price of cart. |
||
| 241 | * |
||
| 242 | * @return |
||
| 243 | */ |
||
| 244 | public function totalPrice() |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Get the number of items in the cart. |
||
| 263 | * |
||
| 264 | * @param bool $totalItems Get all the items (when false, will return the number of rows) |
||
| 265 | * |
||
| 266 | * @return int |
||
| 267 | */ |
||
| 268 | public function count($totalItems = true) |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Get rows count. |
||
| 287 | * |
||
| 288 | * @return int |
||
| 289 | */ |
||
| 290 | public function countRows() |
||
| 294 | |||
| 295 | /** |
||
| 296 | * Search if the cart has a item. |
||
| 297 | * |
||
| 298 | * @param array $search An array with the item ID and optional options |
||
| 299 | * |
||
| 300 | * @return array |
||
| 301 | */ |
||
| 302 | public function search(array $search) |
||
| 318 | |||
| 319 | /** |
||
| 320 | * Get current cart name. |
||
| 321 | * |
||
| 322 | * @return string |
||
| 323 | */ |
||
| 324 | public function getName() |
||
| 328 | |||
| 329 | /** |
||
| 330 | * Get current associated model. |
||
| 331 | * |
||
| 332 | * @return string |
||
| 333 | */ |
||
| 334 | public function getModel() |
||
| 338 | |||
| 339 | /** |
||
| 340 | * Return whether the shopping cart is empty. |
||
| 341 | * |
||
| 342 | * @return bool |
||
| 343 | */ |
||
| 344 | public function isEmpty() |
||
| 348 | |||
| 349 | /** |
||
| 350 | * Add row to the cart. |
||
| 351 | * |
||
| 352 | * @param string $id Unique ID of the item |
||
| 353 | * @param string $name Name of the item |
||
| 354 | * @param int $qty Item qty to add to the cart |
||
| 355 | * @param float $price Price of one item |
||
| 356 | * @param array $attributes Array of additional options, such as 'size' or 'color' |
||
| 357 | * |
||
| 358 | * @return string |
||
| 359 | */ |
||
| 360 | protected function addRow($id, $name, $qty, $price, array $attributes = []) |
||
| 382 | |||
| 383 | /** |
||
| 384 | * Generate a unique id for the new row. |
||
| 385 | * |
||
| 386 | * @param string $id Unique ID of the item |
||
| 387 | * @param array $attributes Array of additional options, such as 'size' or 'color' |
||
| 388 | * |
||
| 389 | * @return string |
||
| 390 | */ |
||
| 391 | protected function generateRawId($id, $attributes) |
||
| 397 | |||
| 398 | /** |
||
| 399 | * Sync the cart to strage. |
||
| 400 | * |
||
| 401 | * @param \Illuminate\Support\Collection|null $cart The new cart content |
||
| 402 | * |
||
| 403 | * @return \Illuminate\Support\Collection |
||
| 404 | */ |
||
| 405 | protected function save($cart) |
||
| 411 | |||
| 412 | public function saveFromSession() |
||
| 421 | |||
| 422 | /** |
||
| 423 | * Get the carts content. |
||
| 424 | * |
||
| 425 | * @return \Illuminate\Support\Collection |
||
| 426 | */ |
||
| 427 | protected function getCart() |
||
| 433 | |||
| 434 | /** |
||
| 435 | * Update a row if the rawId already exists. |
||
| 436 | * |
||
| 437 | * @param string $rawId The ID of the row to update |
||
| 438 | * @param array $attributes The quantity to add to the row |
||
| 439 | * |
||
| 440 | * @return Item |
||
| 441 | */ |
||
| 442 | protected function updateRow($rawId, array $attributes) |
||
| 462 | |||
| 463 | /** |
||
| 464 | * Create a new row Object. |
||
| 465 | * |
||
| 466 | * @param string $rawId The ID of the new row |
||
| 467 | * @param string $id Unique ID of the item |
||
| 468 | * @param string $name Name of the item |
||
| 469 | * @param int $qty Item qty to add to the cart |
||
| 470 | * @param float $price Price of one item |
||
| 471 | * @param array $attributes Array of additional options, such as 'size' or 'color' |
||
| 472 | * |
||
| 473 | * @return Item |
||
| 474 | */ |
||
| 475 | protected function insertRow($rawId, $id, $name, $qty, $price, $attributes = []) |
||
| 487 | |||
| 488 | /** |
||
| 489 | * Make a row item. |
||
| 490 | * |
||
| 491 | * @param string $rawId raw id |
||
| 492 | * @param mixed $id item id |
||
| 493 | * @param string $name item name |
||
| 494 | * @param int $qty quantity |
||
| 495 | * @param float $price price |
||
| 496 | * @param array $attributes other attributes |
||
| 497 | * |
||
| 498 | * @return Item |
||
| 499 | */ |
||
| 500 | protected function makeRow($rawId, $id, $name, $qty, $price, array $attributes = []) |
||
| 512 | |||
| 513 | /** |
||
| 514 | * Update the quantity of a row. |
||
| 515 | * |
||
| 516 | * @param string $rawId The ID of the row |
||
| 517 | * @param int $qty The qty to add |
||
| 518 | * |
||
| 519 | * @return Item|bool |
||
| 520 | */ |
||
| 521 | protected function updateQty($rawId, $qty) |
||
| 529 | |||
| 530 | /** |
||
| 531 | * Update an attribute of the row. |
||
| 532 | * |
||
| 533 | * @param string $rawId The ID of the row |
||
| 534 | * @param array $attributes An array of attributes to update |
||
| 535 | * |
||
| 536 | * @return Item |
||
| 537 | */ |
||
| 538 | protected function updateAttribute($rawId, $attributes) |
||
| 542 | } |
||
| 543 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.