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 = '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) |
|
55 | { |
||
56 | 13 | $this->session = $session; |
|
57 | 13 | $this->event = $event; |
|
58 | 13 | } |
|
59 | |||
60 | /** |
||
61 | * Set the current cart name. |
||
62 | * |
||
63 | * @param string $name Cart name name |
||
64 | * |
||
65 | * @return Cart |
||
66 | */ |
||
67 | 1 | public function name($name) |
|
73 | |||
74 | /** |
||
75 | * Associated model. |
||
76 | * |
||
77 | * @param string $model The name of the model |
||
78 | * |
||
79 | * @return Cart |
||
80 | */ |
||
81 | 2 | public function associate($model) |
|
90 | |||
91 | /** |
||
92 | * Get all items. |
||
93 | * |
||
94 | * @return \Illuminate\Support\Collection |
||
95 | */ |
||
96 | 2 | public function all() |
|
100 | |||
101 | /** |
||
102 | * Add a row to the cart. |
||
103 | * |
||
104 | * @param int|string $id Unique ID of the item |
||
105 | * @param string $name Name of the item |
||
106 | * @param int $qty Item qty to add to the cart |
||
107 | * @param float $price Price of one item |
||
108 | * @param array $attributes Array of additional attributes, such as 'size' or 'color'... |
||
109 | * |
||
110 | * @return string |
||
111 | */ |
||
112 | 10 | public function add($id, $name = null, $qty = null, $price = null, array $attributes = []) |
|
124 | |||
125 | /** |
||
126 | * Update the quantity of one row of the cart. |
||
127 | * |
||
128 | * @param string $rawId The __raw_id of the item you want to update |
||
129 | * @param int|array $attribute New quantity of the item|Array of attributes to update |
||
130 | * |
||
131 | * @return Item|bool |
||
132 | */ |
||
133 | 3 | public function update($rawId, $attribute) |
|
153 | |||
154 | /** |
||
155 | * Remove a row from the cart. |
||
156 | * |
||
157 | * @param string $rawId The __raw_id of the item |
||
158 | * |
||
159 | * @return bool |
||
160 | */ |
||
161 | 3 | public function remove($rawId) |
|
179 | |||
180 | /** |
||
181 | * Get a row of the cart by its ID. |
||
182 | * |
||
183 | * @param string $rawId The ID of the row to fetch |
||
184 | * |
||
185 | * @return Item |
||
186 | */ |
||
187 | 5 | public function get($rawId) |
|
193 | |||
194 | /** |
||
195 | * Clean the cart. |
||
196 | * |
||
197 | * @return bool |
||
198 | */ |
||
199 | 5 | public function destroy() |
|
211 | |||
212 | /** |
||
213 | * Alias of destory(). |
||
214 | * |
||
215 | * @return bool |
||
216 | */ |
||
217 | 1 | public function clean() |
|
221 | |||
222 | /** |
||
223 | * Get the price total. |
||
224 | * |
||
225 | * @return float |
||
226 | */ |
||
227 | 2 | public function total() |
|
231 | |||
232 | /** |
||
233 | * Return total price of cart. |
||
234 | * |
||
235 | * @return |
||
236 | */ |
||
237 | 2 | public function totalPrice() |
|
253 | |||
254 | /** |
||
255 | * Get the number of items in the cart. |
||
256 | * |
||
257 | * @param bool $totalItems Get all the items (when false, will return the number of rows) |
||
258 | * |
||
259 | * @return int |
||
260 | */ |
||
261 | 2 | public function count($totalItems = true) |
|
277 | |||
278 | /** |
||
279 | * Get rows count. |
||
280 | * |
||
281 | * @return int |
||
282 | */ |
||
283 | 2 | public function countRows() |
|
287 | |||
288 | /** |
||
289 | * Search if the cart has a item. |
||
290 | * |
||
291 | * @param array $search An array with the item ID and optional options |
||
292 | * |
||
293 | * @return array |
||
294 | */ |
||
295 | 1 | public function search(array $search) |
|
311 | |||
312 | /** |
||
313 | * Get current cart name. |
||
314 | * |
||
315 | * @return string |
||
316 | */ |
||
317 | 1 | public function getName() |
|
321 | |||
322 | /** |
||
323 | * Get current associated model. |
||
324 | * |
||
325 | * @return string |
||
326 | */ |
||
327 | 1 | public function getModel() |
|
331 | |||
332 | /** |
||
333 | * Return whether the shopping cart is empty. |
||
334 | * |
||
335 | * @return bool |
||
336 | */ |
||
337 | 1 | public function isEmpty() |
|
338 | { |
||
339 | 1 | return $this->count() <= 0; |
|
340 | } |
||
341 | |||
342 | /** |
||
343 | * Add row to the cart. |
||
344 | * |
||
345 | * @param string $id Unique ID of the item |
||
346 | * @param string $name Name of the item |
||
347 | * @param int $qty Item qty to add to the cart |
||
348 | * @param float $price Price of one item |
||
349 | * @param array $attributes Array of additional options, such as 'size' or 'color' |
||
350 | * |
||
351 | * @return string |
||
352 | */ |
||
353 | 10 | protected function addRow($id, $name, $qty, $price, array $attributes = []) |
|
375 | |||
376 | /** |
||
377 | * Generate a unique id for the new row. |
||
378 | * |
||
379 | * @param string $id Unique ID of the item |
||
380 | * @param array $attributes Array of additional options, such as 'size' or 'color' |
||
381 | * |
||
382 | * @return string |
||
383 | */ |
||
384 | 8 | protected function generateRawId($id, $attributes) |
|
390 | |||
391 | /** |
||
392 | * Sync the cart to session. |
||
393 | * |
||
394 | * @param \Illuminate\Support\Collection|null $cart The new cart content |
||
395 | * |
||
396 | * @return \Illuminate\Support\Collection |
||
397 | */ |
||
398 | 8 | protected function save($cart) |
|
404 | |||
405 | /** |
||
406 | * Get the carts content. |
||
407 | * |
||
408 | * @return \Illuminate\Support\Collection |
||
409 | */ |
||
410 | 11 | protected function getCart() |
|
416 | |||
417 | /** |
||
418 | * Update a row if the rawId already exists. |
||
419 | * |
||
420 | * @param string $rawId The ID of the row to update |
||
421 | * @param array $attributes The quantity to add to the row |
||
422 | * |
||
423 | * @return Item |
||
424 | */ |
||
425 | 3 | protected function updateRow($rawId, array $attributes) |
|
443 | |||
444 | /** |
||
445 | * Create a new row Object. |
||
446 | * |
||
447 | * @param string $rawId The ID of the new row |
||
448 | * @param string $id Unique ID of the item |
||
449 | * @param string $name Name of the item |
||
450 | * @param int $qty Item qty to add to the cart |
||
451 | * @param float $price Price of one item |
||
452 | * @param array $attributes Array of additional options, such as 'size' or 'color' |
||
453 | * |
||
454 | * @return Item |
||
455 | */ |
||
456 | 8 | protected function insertRow($rawId, $id, $name, $qty, $price, $attributes = []) |
|
468 | |||
469 | /** |
||
470 | * Make a row item. |
||
471 | * |
||
472 | * @param string $rawId Raw id. |
||
473 | * @param mixed $id Item id. |
||
474 | * @param string $name Item name. |
||
475 | * @param int $qty Quantity. |
||
476 | * @param float $price Price. |
||
477 | * @param array $attributes Other attributes. |
||
478 | * |
||
479 | * @return Item |
||
480 | */ |
||
481 | 8 | protected function makeRow($rawId, $id, $name, $qty, $price, array $attributes = []) |
|
493 | |||
494 | /** |
||
495 | * Update the quantity of a row. |
||
496 | * |
||
497 | * @param string $rawId The ID of the row |
||
498 | * @param int $qty The qty to add |
||
499 | * |
||
500 | * @return Item|bool |
||
501 | */ |
||
502 | 3 | protected function updateQty($rawId, $qty) |
|
510 | |||
511 | /** |
||
512 | * Update an attribute of the row. |
||
513 | * |
||
514 | * @param string $rawId The ID of the row |
||
515 | * @param array $attributes An array of attributes to update |
||
516 | * |
||
517 | * @return Item |
||
518 | */ |
||
519 | 1 | protected function updateAttribute($rawId, $attributes) |
|
523 | } |
||
524 |