Complex classes like EE_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 EE_Cart, and based on these observations, apply Extract Interface, too.
1 | <?php if ( ! defined('EVENT_ESPRESSO_VERSION')) { |
||
19 | class EE_Cart |
||
20 | { |
||
21 | |||
22 | /** |
||
23 | * instance of the EE_Cart object |
||
24 | * |
||
25 | * @access private |
||
26 | * @var EE_Cart $_instance |
||
27 | */ |
||
28 | private static $_instance; |
||
29 | |||
30 | /** |
||
31 | * instance of the EE_Session object |
||
32 | * |
||
33 | * @access protected |
||
34 | * @var EE_Session $_session |
||
35 | */ |
||
36 | protected $_session; |
||
37 | |||
38 | /** |
||
39 | * The total Line item which comprises all the children line-item subtotals, |
||
40 | * which in turn each have their line items. |
||
41 | * Typically, the line item structure will look like: |
||
42 | * grand total |
||
43 | * -tickets-sub-total |
||
44 | * --ticket1 |
||
45 | * --ticket2 |
||
46 | * --... |
||
47 | * -taxes-sub-total |
||
48 | * --tax1 |
||
49 | * --tax2 |
||
50 | * |
||
51 | * @var EE_Line_Item |
||
52 | */ |
||
53 | private $_grand_total; |
||
54 | |||
55 | |||
56 | |||
57 | /** |
||
58 | * @singleton method used to instantiate class object |
||
59 | * @access public |
||
60 | * @param EE_Line_Item $grand_total |
||
61 | * @param EE_Session $session |
||
62 | * @return \EE_Cart |
||
63 | * @throws \EE_Error |
||
64 | */ |
||
65 | public static function instance(EE_Line_Item $grand_total = null, EE_Session $session = null) |
||
86 | |||
87 | |||
88 | |||
89 | /** |
||
90 | * private constructor to prevent direct creation |
||
91 | * |
||
92 | * @Constructor |
||
93 | * @access private |
||
94 | * @param EE_Line_Item $grand_total |
||
95 | * @param EE_Session $session |
||
96 | */ |
||
97 | private function __construct(EE_Line_Item $grand_total = null, EE_Session $session = null) |
||
105 | |||
106 | |||
107 | |||
108 | /** |
||
109 | * Resets the cart completely (whereas empty_cart |
||
110 | * |
||
111 | * @param EE_Line_Item $grand_total |
||
112 | * @param EE_Session $session |
||
113 | * @return EE_Cart |
||
114 | * @throws \EE_Error |
||
115 | */ |
||
116 | public static function reset(EE_Line_Item $grand_total = null, EE_Session $session = null) |
||
125 | |||
126 | |||
127 | |||
128 | /** |
||
129 | * @return \EE_Session |
||
130 | */ |
||
131 | public function session() |
||
138 | |||
139 | |||
140 | |||
141 | /** |
||
142 | * @param EE_Session $session |
||
143 | */ |
||
144 | public function set_session(EE_Session $session = null) |
||
148 | |||
149 | |||
150 | |||
151 | /** |
||
152 | * Sets the cart to match the line item. Especially handy for loading an old cart where you |
||
153 | * know the grand total line item on it |
||
154 | * |
||
155 | * @param EE_Line_Item $line_item |
||
156 | */ |
||
157 | public function set_grand_total_line_item(EE_Line_Item $line_item) |
||
161 | |||
162 | |||
163 | |||
164 | /** |
||
165 | * get_cart_from_reg_url_link |
||
166 | * |
||
167 | * @access public |
||
168 | * @param EE_Transaction $transaction |
||
169 | * @param EE_Session $session |
||
170 | * @return \EE_Cart |
||
171 | * @throws \EE_Error |
||
172 | */ |
||
173 | public static function get_cart_from_txn(EE_Transaction $transaction, EE_Session $session = null) |
||
180 | |||
181 | |||
182 | |||
183 | /** |
||
184 | * Creates the total line item, and ensures it has its 'tickets' and 'taxes' sub-items |
||
185 | * |
||
186 | * @return EE_Line_Item |
||
187 | * @throws \EE_Error |
||
188 | */ |
||
189 | private function _create_grand_total() |
||
194 | |||
195 | |||
196 | |||
197 | /** |
||
198 | * Gets all the line items of object type Ticket |
||
199 | * |
||
200 | * @access public |
||
201 | * @return \EE_Line_Item[] |
||
202 | */ |
||
203 | public function get_tickets() |
||
207 | |||
208 | |||
209 | |||
210 | /** |
||
211 | * returns the total quantity of tickets in the cart |
||
212 | * |
||
213 | * @access public |
||
214 | * @return int |
||
215 | * @throws \EE_Error |
||
216 | */ |
||
217 | public function all_ticket_quantity_count() |
||
229 | |||
230 | |||
231 | |||
232 | /** |
||
233 | * Gets all the tax line items |
||
234 | * |
||
235 | * @return \EE_Line_Item[] |
||
236 | * @throws \EE_Error |
||
237 | */ |
||
238 | public function get_taxes() |
||
242 | |||
243 | |||
244 | |||
245 | /** |
||
246 | * Gets the total line item (which is a parent of all other line items) on this cart |
||
247 | * |
||
248 | * @return EE_Line_Item |
||
249 | * @throws \EE_Error |
||
250 | */ |
||
251 | public function get_grand_total() |
||
255 | |||
256 | |||
257 | |||
258 | /** |
||
259 | * @process items for adding to cart |
||
260 | * @access public |
||
261 | * @param EE_Ticket $ticket |
||
262 | * @param int $qty |
||
263 | * @return TRUE on success, FALSE on fail |
||
264 | * @throws \EE_Error |
||
265 | */ |
||
266 | public function add_ticket_to_cart(EE_Ticket $ticket, $qty = 1) |
||
271 | |||
272 | |||
273 | |||
274 | /** |
||
275 | * get_cart_total_before_tax |
||
276 | * |
||
277 | * @access public |
||
278 | * @return float |
||
279 | * @throws \EE_Error |
||
280 | */ |
||
281 | public function get_cart_total_before_tax() |
||
285 | |||
286 | |||
287 | |||
288 | /** |
||
289 | * gets the total amount of tax paid for items in this cart |
||
290 | * |
||
291 | * @access public |
||
292 | * @return float |
||
293 | * @throws \EE_Error |
||
294 | */ |
||
295 | public function get_applied_taxes() |
||
299 | |||
300 | |||
301 | |||
302 | /** |
||
303 | * Gets the total amount to be paid for the items in the cart, including taxes and other modifiers |
||
304 | * |
||
305 | * @access public |
||
306 | * @return float |
||
307 | * @throws \EE_Error |
||
308 | */ |
||
309 | public function get_cart_grand_total() |
||
314 | |||
315 | |||
316 | |||
317 | /** |
||
318 | * Gets the total amount to be paid for the items in the cart, including taxes and other modifiers |
||
319 | * |
||
320 | * @access public |
||
321 | * @return float |
||
322 | * @throws \EE_Error |
||
323 | */ |
||
324 | public function recalculate_all_cart_totals() |
||
332 | |||
333 | |||
334 | |||
335 | /** |
||
336 | * deletes an item from the cart |
||
337 | * |
||
338 | * @access public |
||
339 | * @param array|bool|string $line_item_codes |
||
340 | * @return int on success, FALSE on fail |
||
341 | * @throws \EE_Error |
||
342 | */ |
||
343 | public function delete_items($line_item_codes = false) |
||
348 | |||
349 | |||
350 | |||
351 | /** |
||
352 | * @remove ALL items from cart and zero ALL totals |
||
353 | * @access public |
||
354 | * @return bool |
||
355 | * @throws \EE_Error |
||
356 | */ |
||
357 | public function empty_cart() |
||
363 | |||
364 | |||
365 | |||
366 | /** |
||
367 | * @remove ALL items from cart and delete total as well |
||
368 | * @access public |
||
369 | * @return bool |
||
370 | * @throws \EE_Error |
||
371 | */ |
||
372 | public function delete_cart() |
||
381 | |||
382 | |||
383 | |||
384 | /** |
||
385 | * @save cart to session |
||
386 | * @access public |
||
387 | * @param bool $apply_taxes |
||
388 | * @return TRUE on success, FALSE on fail |
||
389 | * @throws \EE_Error |
||
390 | */ |
||
391 | public function save_cart($apply_taxes = true) |
||
408 | |||
409 | |||
410 | |||
411 | public function __wakeup() |
||
418 | |||
419 | |||
420 | |||
421 | /** |
||
422 | * @return array |
||
423 | */ |
||
424 | public function __sleep() |
||
431 | |||
432 | |||
433 | } |
||
434 | /* End of file EE_Cart.core.php */ |
||
436 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.