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 |
||
18 | class Cart |
||
19 | { |
||
20 | /** |
||
21 | * Session manager. |
||
22 | * |
||
23 | * @var \Illuminate\Session\SessionManager |
||
24 | */ |
||
25 | protected $session; |
||
26 | |||
27 | /** |
||
28 | * Event dispatcher. |
||
29 | * |
||
30 | * @var \Illuminate\Contracts\Events\Dispatcher |
||
31 | */ |
||
32 | protected $event; |
||
33 | |||
34 | /** |
||
35 | * Current cart name. |
||
36 | * |
||
37 | * @var string |
||
38 | */ |
||
39 | protected $name = 'shopping_cart.default'; |
||
40 | |||
41 | /** |
||
42 | * Associated model name. |
||
43 | * |
||
44 | * @var string |
||
45 | */ |
||
46 | protected $model; |
||
47 | |||
48 | /** |
||
49 | * Constructor. |
||
50 | * |
||
51 | * @param \Illuminate\Session\SessionManager $session Session class name |
||
52 | * @param \Illuminate\Contracts\Events\Dispatcher $event Event class name |
||
53 | */ |
||
54 | 13 | public function __construct(SessionManager $session, Dispatcher $event) |
|
59 | |||
60 | public function dispatchEvent($event, $payload = [], $halt = false) |
||
68 | |||
69 | 1 | /** |
|
70 | * Set the current cart name. |
||
71 | 1 | * |
|
72 | * @param string $name Cart name name |
||
73 | * |
||
74 | * @return Cart |
||
75 | */ |
||
76 | public function name($name) |
||
82 | |||
83 | 2 | /** |
|
84 | 1 | * Associated model. |
|
85 | * |
||
86 | 1 | * @param string $model The name of the model |
|
87 | * |
||
88 | 1 | * @return Cart |
|
89 | */ |
||
90 | public function associate($model) |
||
99 | |||
100 | /** |
||
101 | * Get all items. |
||
102 | * |
||
103 | * @return \Illuminate\Support\Collection |
||
104 | */ |
||
105 | public function all() |
||
109 | |||
110 | /** |
||
111 | * Add a row to the cart. |
||
112 | 10 | * |
|
113 | * @param int|string $id Unique ID of the item |
||
114 | 10 | * @param string $name Name of the item |
|
115 | * @param int $qty Item qty to add to the cart |
||
116 | 10 | * @param float $price Price of one item |
|
117 | * @param array $attributes Array of additional attributes, such as 'size' or 'color'... |
||
118 | 10 | * |
|
119 | * @return string |
||
120 | 8 | */ |
|
121 | public function add($id, $name = null, $qty = null, $price = null, array $attributes = []) |
||
135 | 3 | ||
136 | /** |
||
137 | 3 | * Update the quantity of one row of the cart. |
|
138 | 1 | * |
|
139 | * @param string $rawId The __raw_id of the item you want to update |
||
140 | * @param int|array $attribute New quantity of the item|Array of attributes to update |
||
141 | 2 | * |
|
142 | * @return Item|bool |
||
143 | 2 | */ |
|
144 | public function update($rawId, $attribute) |
||
164 | |||
165 | 3 | /** |
|
166 | 1 | * Remove a row from the cart. |
|
167 | * |
||
168 | * @param string $rawId The __raw_id of the item |
||
169 | 3 | * |
|
170 | * @return bool |
||
171 | 3 | */ |
|
172 | public function remove($rawId) |
||
190 | |||
191 | 5 | /** |
|
192 | * Get a row of the cart by its ID. |
||
193 | 5 | * |
|
194 | * @param string $rawId The ID of the row to fetch |
||
195 | * |
||
196 | * @return Item |
||
197 | */ |
||
198 | public function get($rawId) |
||
204 | |||
205 | 5 | /** |
|
206 | * Clean the cart. |
||
207 | 5 | * |
|
208 | * @return bool |
||
209 | 5 | */ |
|
210 | public function destroy() |
||
222 | 1 | ||
223 | /** |
||
224 | * Alias of destory(). |
||
225 | * |
||
226 | * @return bool |
||
227 | */ |
||
228 | public function clean() |
||
232 | |||
233 | /** |
||
234 | * Get the price total. |
||
235 | * |
||
236 | * @return float |
||
237 | */ |
||
238 | public function total() |
||
242 | |||
243 | 2 | /** |
|
244 | * Return total price of cart. |
||
245 | 2 | * |
|
246 | 1 | * @return |
|
247 | */ |
||
248 | public function totalPrice() |
||
264 | |||
265 | 2 | /** |
|
266 | * Get the number of items in the cart. |
||
267 | 2 | * |
|
268 | 2 | * @param bool $totalItems Get all the items (when false, will return the number of rows) |
|
269 | * |
||
270 | * @return int |
||
271 | 2 | */ |
|
272 | public function count($totalItems = true) |
||
288 | |||
289 | /** |
||
290 | * Get rows count. |
||
291 | * |
||
292 | * @return int |
||
293 | */ |
||
294 | public function countRows() |
||
298 | |||
299 | 1 | /** |
|
300 | * Search if the cart has a item. |
||
301 | 1 | * |
|
302 | 1 | * @param array $search An array with the item ID and optional options |
|
303 | * |
||
304 | * @return array|Collection |
||
305 | 1 | */ |
|
306 | 1 | public function search(array $search) |
|
322 | |||
323 | /** |
||
324 | * Get current cart name. |
||
325 | * |
||
326 | * @return string |
||
327 | */ |
||
328 | public function getName() |
||
332 | |||
333 | /** |
||
334 | * Get current associated model. |
||
335 | * |
||
336 | * @return string |
||
337 | */ |
||
338 | public function getModel() |
||
342 | |||
343 | /** |
||
344 | * Return whether the shopping cart is empty. |
||
345 | * |
||
346 | * @return bool |
||
347 | */ |
||
348 | public function isEmpty() |
||
352 | |||
353 | /** |
||
354 | * Add row to the cart. |
||
355 | 10 | * |
|
356 | * @param string $id Unique ID of the item |
||
357 | 10 | * @param string $name Name of the item |
|
358 | 1 | * @param int $qty Item qty to add to the cart |
|
359 | * @param float $price Price of one item |
||
360 | * @param array $attributes Array of additional options, such as 'size' or 'color' |
||
361 | 9 | * |
|
362 | 1 | * @return string |
|
363 | */ |
||
364 | protected function addRow($id, $name, $qty, $price, array $attributes = []) |
||
386 | 8 | ||
387 | /** |
||
388 | 8 | * Generate a unique id for the new row. |
|
389 | * |
||
390 | 8 | * @param string $id Unique ID of the item |
|
391 | * @param array $attributes Array of additional options, such as 'size' or 'color' |
||
392 | * |
||
393 | * @return string |
||
394 | */ |
||
395 | protected function generateRawId($id, $attributes) |
||
401 | |||
402 | 8 | /** |
|
403 | * Sync the cart to session. |
||
404 | 8 | * |
|
405 | * @param \Illuminate\Support\Collection|null $cart The new cart content |
||
406 | * |
||
407 | * @return \Illuminate\Support\Collection |
||
408 | */ |
||
409 | protected function save($cart) |
||
415 | |||
416 | 11 | /** |
|
417 | * Get the carts content. |
||
418 | * |
||
419 | * @return \Illuminate\Support\Collection |
||
420 | */ |
||
421 | protected function getCart() |
||
427 | 3 | ||
428 | /** |
||
429 | 3 | * Update a row if the rawId already exists. |
|
430 | * |
||
431 | 3 | * @param string $rawId The ID of the row to update |
|
432 | * @param array $attributes The quantity to add to the row |
||
433 | 3 | * |
|
434 | 3 | * @return Item |
|
435 | */ |
||
436 | protected function updateRow($rawId, array $attributes) |
||
454 | |||
455 | /** |
||
456 | * Create a new row Object. |
||
457 | * |
||
458 | 8 | * @param string $rawId The ID of the new row |
|
459 | * @param string $id Unique ID of the item |
||
460 | 8 | * @param string $name Name of the item |
|
461 | * @param int $qty Item qty to add to the cart |
||
462 | 8 | * @param float $price Price of one item |
|
463 | * @param array $attributes Array of additional options, such as 'size' or 'color' |
||
464 | 8 | * |
|
465 | * @return Item |
||
466 | 8 | */ |
|
467 | protected function insertRow($rawId, $id, $name, $qty, $price, $attributes = []) |
||
479 | |||
480 | /** |
||
481 | * Make a row item. |
||
482 | * |
||
483 | 8 | * @param string $rawId raw id |
|
484 | * @param mixed $id item id |
||
485 | 8 | * @param string $name item name |
|
486 | 8 | * @param int $qty quantity |
|
487 | 8 | * @param float $price price |
|
488 | 8 | * @param array $attributes other attributes |
|
489 | 8 | * |
|
490 | 8 | * @return Item |
|
491 | 8 | */ |
|
492 | 8 | protected function makeRow($rawId, $id, $name, $qty, $price, array $attributes = []) |
|
504 | 3 | ||
505 | /** |
||
506 | 3 | * Update the quantity of a row. |
|
507 | 1 | * |
|
508 | * @param string $rawId The ID of the row |
||
509 | * @param int $qty The qty to add |
||
510 | 3 | * |
|
511 | * @return Item|bool |
||
512 | */ |
||
513 | protected function updateQty($rawId, $qty) |
||
521 | 1 | ||
522 | /** |
||
523 | 1 | * Update an attribute of the row. |
|
524 | * |
||
525 | * @param string $rawId The ID of the row |
||
526 | * @param array $attributes An array of attributes to update |
||
527 | * |
||
528 | * @return Item |
||
529 | */ |
||
530 | protected function updateAttribute($rawId, $attributes) |
||
534 | } |
||
535 |
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.