Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like EE_Checkout 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_Checkout, and based on these observations, apply Extract Interface, too.
1 | <?php if ( ! defined('EVENT_ESPRESSO_VERSION')) { exit('No direct script access allowed'); } |
||
14 | class EE_Checkout { |
||
15 | |||
16 | /** |
||
17 | * whether current request originated from the EE admin |
||
18 | * @type bool |
||
19 | */ |
||
20 | public $admin_request = FALSE; |
||
21 | |||
22 | /** |
||
23 | * whether returning to edit attendee information or to retry a payment |
||
24 | * @type bool |
||
25 | */ |
||
26 | public $revisit = FALSE; |
||
27 | |||
28 | /** |
||
29 | * whether the primary registrant is returning to edit attendee information or to retry a payment |
||
30 | * @type bool |
||
31 | */ |
||
32 | public $primary_revisit = FALSE; |
||
33 | |||
34 | /** |
||
35 | * is registration allowed to progress or halted for some reason such as failing to pass recaptcha? |
||
36 | * @type bool |
||
37 | */ |
||
38 | public $continue_reg = TRUE; |
||
39 | |||
40 | /** |
||
41 | * redirect to thank you page ? |
||
42 | * @type bool |
||
43 | */ |
||
44 | public $redirect = FALSE; |
||
45 | |||
46 | /** |
||
47 | * generate the reg form or not ? |
||
48 | * @type bool |
||
49 | */ |
||
50 | public $generate_reg_form = TRUE; |
||
51 | |||
52 | /** |
||
53 | * process a reg form submission or not ? |
||
54 | * @type bool |
||
55 | */ |
||
56 | public $process_form_submission = FALSE; |
||
57 | |||
58 | /** |
||
59 | * tracks whether the TXN status modified during this checkout |
||
60 | * |
||
61 | * @type bool |
||
62 | */ |
||
63 | public $txn_status_updated = FALSE; |
||
64 | |||
65 | /** |
||
66 | * only triggered to true after absolutely everything has finished. |
||
67 | * |
||
68 | * @type bool |
||
69 | */ |
||
70 | protected $exit_spco = FALSE; |
||
71 | |||
72 | /** |
||
73 | * tracks whether any of the TXN's Registrations statuses modified during this checkout |
||
74 | * indexed by registration ID |
||
75 | * |
||
76 | * @type array |
||
77 | */ |
||
78 | protected $reg_status_updated = array(); |
||
79 | |||
80 | /** |
||
81 | * total number of tickets that were in the cart |
||
82 | * @type int |
||
83 | */ |
||
84 | public $total_ticket_count = 0; |
||
85 | |||
86 | /** |
||
87 | * corresponds loosely to EE_Transaction::remaining() |
||
88 | * but can be modified by SPCO |
||
89 | * |
||
90 | * @type float |
||
91 | */ |
||
92 | public $amount_owing = 0; |
||
93 | |||
94 | /** |
||
95 | * the reg step slug from the incoming request |
||
96 | * @type string |
||
97 | */ |
||
98 | public $step = ''; |
||
99 | |||
100 | /** |
||
101 | * the reg step slug for a step being edited |
||
102 | * @type string |
||
103 | */ |
||
104 | public $edit_step = ''; |
||
105 | |||
106 | /** |
||
107 | * the action being performed on the current step |
||
108 | * @type string |
||
109 | */ |
||
110 | public $action = ''; |
||
111 | |||
112 | /** |
||
113 | * reg_url_link for a previously saved registration |
||
114 | * @type string |
||
115 | */ |
||
116 | public $reg_url_link = ''; |
||
117 | |||
118 | /** |
||
119 | * string slug for the payment method that was selected during the payment options step |
||
120 | * @type string |
||
121 | */ |
||
122 | public $selected_method_of_payment = ''; |
||
123 | |||
124 | /** |
||
125 | * base url for the site's registration checkout page - additional url params will be added to this |
||
126 | * @type string |
||
127 | */ |
||
128 | public $reg_page_base_url = ''; |
||
129 | |||
130 | /** |
||
131 | * base url for the site's registration cancelled page - additional url params will be added to this |
||
132 | * @type string |
||
133 | */ |
||
134 | public $cancel_page_url = ''; |
||
135 | |||
136 | /** |
||
137 | * base url for the site's thank you page - additional url params will be added to this |
||
138 | * @type string |
||
139 | */ |
||
140 | public $thank_you_page_url = ''; |
||
141 | |||
142 | /** |
||
143 | * base url for any redirects - additional url params will be added to this |
||
144 | * @type string |
||
145 | */ |
||
146 | public $redirect_url = ''; |
||
147 | |||
148 | /** |
||
149 | * form of POST data for use with off-site gateways |
||
150 | * @type string |
||
151 | */ |
||
152 | public $redirect_form = ''; |
||
153 | |||
154 | /** |
||
155 | * array of query where params to use when retrieving cached registrations from $this->checkout->transaction |
||
156 | * @type array |
||
157 | */ |
||
158 | public $reg_cache_where_params = array(); |
||
159 | |||
160 | /** |
||
161 | * a class for managing and creating the JSON encoded array of data that gets passed back to the client during AJAX requests |
||
162 | * @type EE_SPCO_JSON_Response |
||
163 | */ |
||
164 | public $json_response; |
||
165 | |||
166 | /** |
||
167 | * where we are going next in the reg process |
||
168 | * @type EE_SPCO_Reg_Step |
||
169 | */ |
||
170 | public $next_step; |
||
171 | |||
172 | /** |
||
173 | * where we are in the reg process |
||
174 | * @type EE_SPCO_Reg_Step |
||
175 | */ |
||
176 | public $current_step; |
||
177 | |||
178 | /** |
||
179 | * $_cart - the current cart object |
||
180 | * @var EE_CART |
||
181 | */ |
||
182 | public $cart; |
||
183 | |||
184 | /** |
||
185 | * $_transaction - the current transaction object |
||
186 | * @var EE_Transaction |
||
187 | */ |
||
188 | public $transaction; |
||
189 | |||
190 | /** |
||
191 | * the related attendee object for the primary registrant |
||
192 | * @type EE_Attendee |
||
193 | */ |
||
194 | public $primary_attendee_obj; |
||
195 | |||
196 | /** |
||
197 | * $payment_method - the payment method object for the selected method of payment |
||
198 | * @type EE_Payment_Method |
||
199 | */ |
||
200 | public $payment_method; |
||
201 | |||
202 | /** |
||
203 | * $payment - if a payment was successfully made during the reg process, |
||
204 | * then here it is !!! |
||
205 | * @type EE_Payment |
||
206 | */ |
||
207 | public $payment; |
||
208 | |||
209 | /** |
||
210 | * if a payment method was selected that uses an on-site gateway, then this is the billing form |
||
211 | * @type EE_Billing_Info_Form | EE_Billing_Attendee_Info_Form |
||
212 | */ |
||
213 | public $billing_form; |
||
214 | |||
215 | /** |
||
216 | * the entire registration form composed of ALL of the subsections generated by the various reg steps |
||
217 | * @type EE_Form_Section_Proper |
||
218 | */ |
||
219 | public $registration_form; |
||
220 | |||
221 | /** |
||
222 | * array of EE_SPCO_Reg_Step objects |
||
223 | * @type EE_SPCO_Reg_Step[] |
||
224 | */ |
||
225 | public $reg_steps = array(); |
||
226 | |||
227 | /** |
||
228 | * array of EE_Payment_Method objects |
||
229 | * @type EE_Payment_Method[] |
||
230 | */ |
||
231 | public $available_payment_methods = array(); |
||
232 | |||
233 | |||
234 | |||
235 | /** |
||
236 | * class constructor |
||
237 | * |
||
238 | * @access public |
||
239 | * @return \EE_Checkout |
||
|
|||
240 | */ |
||
241 | public function __construct( ) { |
||
249 | |||
250 | |||
251 | |||
252 | /** |
||
253 | * returns true if ANY reg status was updated during checkout |
||
254 | * @return array |
||
255 | */ |
||
256 | public function any_reg_status_updated() { |
||
257 | foreach ( $this->reg_status_updated as $reg_status ) { |
||
258 | if ( $reg_status ) { |
||
259 | return true; |
||
260 | } |
||
261 | } |
||
262 | return false; |
||
263 | } |
||
264 | |||
265 | |||
266 | |||
267 | /** |
||
268 | * @param $REG_ID |
||
269 | * @return array |
||
270 | */ |
||
271 | public function reg_status_updated( $REG_ID ) { |
||
274 | |||
275 | |||
276 | |||
277 | /** |
||
278 | * @param $REG_ID |
||
279 | * @param $reg_status |
||
280 | */ |
||
281 | public function set_reg_status_updated( $REG_ID, $reg_status ) { |
||
284 | |||
285 | |||
286 | |||
287 | /** |
||
288 | * exit_spco |
||
289 | * |
||
290 | * @return bool |
||
291 | */ |
||
292 | public function exit_spco() { |
||
295 | |||
296 | |||
297 | |||
298 | /** |
||
299 | * set_exit_spco |
||
300 | * can ONLY be set by the Finalize_Registration reg step |
||
301 | */ |
||
302 | public function set_exit_spco() { |
||
307 | |||
308 | |||
309 | |||
310 | |||
311 | |||
312 | /** |
||
313 | * reset_for_current_request |
||
314 | * |
||
315 | * @access public |
||
316 | * @return void |
||
317 | */ |
||
318 | public function reset_for_current_request() { |
||
332 | |||
333 | |||
334 | |||
335 | /** |
||
336 | * add_reg_step |
||
337 | * |
||
338 | * @access public |
||
339 | * @param EE_SPCO_Reg_Step $reg_step_obj |
||
340 | * @return void |
||
341 | */ |
||
342 | public function add_reg_step( EE_SPCO_Reg_Step $reg_step_obj ) { |
||
345 | |||
346 | |||
347 | |||
348 | /** |
||
349 | * skip_reg_step |
||
350 | * if the current reg step does not need to run for some reason, |
||
351 | * then this will advance SPCO to the next reg step, |
||
352 | * and mark the skipped step as completed |
||
353 | * |
||
354 | * @access public |
||
355 | * @param string $reg_step_slug |
||
356 | * @return void |
||
357 | * @throws \EE_Error |
||
358 | */ |
||
359 | public function skip_reg_step( $reg_step_slug = '' ) { |
||
377 | |||
378 | |||
379 | |||
380 | /** |
||
381 | * remove_reg_step |
||
382 | * |
||
383 | * @access public |
||
384 | * @param string $reg_step_slug |
||
385 | * @param bool $reset whether to reset reg steps after removal |
||
386 | * @throws EE_Error |
||
387 | */ |
||
388 | public function remove_reg_step( $reg_step_slug = '', $reset = true ) { |
||
401 | |||
402 | |||
403 | |||
404 | /** |
||
405 | * set_reg_step_order |
||
406 | * |
||
407 | * @access public |
||
408 | * @param string $reg_step_slug |
||
409 | * @param int $order |
||
410 | * @return void |
||
411 | */ |
||
412 | public function set_reg_step_order( $reg_step_slug = '', $order = 100 ) { |
||
417 | |||
418 | |||
419 | |||
420 | /** |
||
421 | * set_current_step |
||
422 | * |
||
423 | * @access public |
||
424 | * @param string $current_step |
||
425 | * @return void |
||
426 | */ |
||
427 | public function set_current_step( $current_step ) { |
||
450 | |||
451 | |||
452 | |||
453 | /** |
||
454 | * set_next_step |
||
455 | * advances the reg_steps array pointer and sets the next step, then reverses pointer back to the current step |
||
456 | * |
||
457 | * @access public |
||
458 | * @return void |
||
459 | */ |
||
460 | public function set_next_step() { |
||
477 | |||
478 | |||
479 | |||
480 | |||
481 | /** |
||
482 | * get_next_reg_step |
||
483 | * this simply returns the next step from reg_steps array |
||
484 | * |
||
485 | * @access public |
||
486 | * @return EE_SPCO_Reg_Step | null |
||
487 | */ |
||
488 | public function get_next_reg_step() { |
||
493 | |||
494 | |||
495 | |||
496 | |||
497 | /** |
||
498 | * get_prev_reg_step |
||
499 | * this simply returns the previous step from reg_steps array |
||
500 | * |
||
501 | * @access public |
||
502 | * @return EE_SPCO_Reg_Step | null |
||
503 | */ |
||
504 | public function get_prev_reg_step() { |
||
509 | |||
510 | |||
511 | |||
512 | /** |
||
513 | * sort_reg_steps |
||
514 | * |
||
515 | * @access public |
||
516 | * @return void |
||
517 | */ |
||
518 | public function sort_reg_steps() { |
||
522 | |||
523 | |||
524 | |||
525 | /** |
||
526 | * find_reg_step |
||
527 | * finds a reg step by the given slug |
||
528 | * |
||
529 | * @access public |
||
530 | * @param string $reg_step_slug |
||
531 | * @return EE_SPCO_Reg_Step|null |
||
532 | */ |
||
533 | public function find_reg_step( $reg_step_slug = '' ) { |
||
550 | |||
551 | |||
552 | |||
553 | /** |
||
554 | * reg_step_sorting_callback |
||
555 | * |
||
556 | * @access public |
||
557 | * @param EE_SPCO_Reg_Step $reg_step_A |
||
558 | * @param EE_SPCO_Reg_Step $reg_step_B |
||
559 | * @return array() |
||
560 | */ |
||
561 | public function reg_step_sorting_callback( EE_SPCO_Reg_Step $reg_step_A, EE_SPCO_Reg_Step $reg_step_B ) { |
||
573 | |||
574 | |||
575 | |||
576 | /** |
||
577 | * set_reg_step_initiated |
||
578 | * |
||
579 | * @access public |
||
580 | * @param EE_SPCO_Reg_Step $reg_step |
||
581 | * @throws \EE_Error |
||
582 | */ |
||
583 | public function set_reg_step_initiated( EE_SPCO_Reg_Step $reg_step ) { |
||
611 | |||
612 | |||
613 | |||
614 | /** |
||
615 | * set_reg_step_JSON_info |
||
616 | * |
||
617 | * @access public |
||
618 | * @return void |
||
619 | */ |
||
620 | public function set_reg_step_JSON_info() { |
||
629 | |||
630 | |||
631 | |||
632 | /** |
||
633 | * reset_reg_steps |
||
634 | * |
||
635 | * @access public |
||
636 | * @return bool |
||
637 | */ |
||
638 | public function reset_reg_steps() { |
||
646 | |||
647 | |||
648 | |||
649 | /** |
||
650 | * get_registration_time_limit |
||
651 | * |
||
652 | * @access public |
||
653 | * @return string |
||
654 | */ |
||
655 | public function get_registration_time_limit() { |
||
665 | |||
666 | |||
667 | |||
668 | /** |
||
669 | * payment_required |
||
670 | * @return boolean |
||
671 | */ |
||
672 | public function payment_required() { |
||
681 | |||
682 | |||
683 | |||
684 | /** |
||
685 | * get_cart_for_transaction |
||
686 | * |
||
687 | * @access public |
||
688 | * @param EE_Transaction $transaction |
||
689 | * @return EE_Cart |
||
690 | */ |
||
691 | public function get_cart_for_transaction( $transaction ) { |
||
701 | |||
702 | |||
703 | |||
704 | /** |
||
705 | * initialize_txn_reg_steps_array |
||
706 | * |
||
707 | * @access public |
||
708 | * @return array |
||
709 | */ |
||
710 | public function initialize_txn_reg_steps_array() { |
||
717 | |||
718 | |||
719 | |||
720 | /** |
||
721 | * update_txn_reg_steps_array |
||
722 | * |
||
723 | * @access public |
||
724 | * @return bool |
||
725 | * @throws \EE_Error |
||
726 | */ |
||
727 | public function update_txn_reg_steps_array() { |
||
743 | |||
744 | |||
745 | |||
746 | /** |
||
747 | * stash_transaction_and_checkout |
||
748 | * |
||
749 | * @access public |
||
750 | * @return void |
||
751 | * @throws \EE_Error |
||
752 | */ |
||
753 | public function stash_transaction_and_checkout() { |
||
763 | |||
764 | |||
765 | |||
766 | /** |
||
767 | * track_transaction_and_registration_status_updates |
||
768 | * stores whether any updates were made to the TXN or it's related registrations |
||
769 | * |
||
770 | * @access public |
||
771 | * @return bool |
||
772 | * @throws \EE_Error |
||
773 | */ |
||
774 | public function track_transaction_and_registration_status_updates() { |
||
795 | |||
796 | |||
797 | |||
798 | /** |
||
799 | * visit_allows_processing_of_this_registration |
||
800 | * determines if the current SPCO visit should allow the passed EE_Registration to be used in processing. |
||
801 | * one of the following conditions must be met: |
||
802 | * EITHER: A) first time thru SPCO -> process ALL registrations ( NOT a revisit ) |
||
803 | * OR : B) primary registrant is editing info -> process ALL registrations ( primary_revisit ) |
||
804 | * OR : C) another registrant is editing info -> ONLY process their registration ( revisit AND their reg_url_link matches ) |
||
805 | * |
||
806 | * @access public |
||
807 | * @param EE_Registration $registration |
||
808 | * @return bool |
||
809 | * @throws \EE_Error |
||
810 | */ |
||
811 | public function visit_allows_processing_of_this_registration( EE_Registration $registration ) { |
||
820 | |||
821 | |||
822 | |||
823 | /** |
||
824 | * _transaction_has_primary_registration |
||
825 | * |
||
826 | * @access private |
||
827 | * @return bool |
||
828 | */ |
||
829 | public function transaction_has_primary_registrant() { |
||
832 | |||
833 | |||
834 | |||
835 | /** |
||
836 | * save_all_data |
||
837 | * simply loops through the current transaction and saves all data for each registration |
||
838 | * |
||
839 | * @access public |
||
840 | * @param bool $show_errors |
||
841 | * @return bool |
||
842 | * @throws \EE_Error |
||
843 | */ |
||
844 | public function save_all_data( $show_errors = TRUE ) { |
||
861 | |||
862 | |||
863 | |||
864 | /** |
||
865 | * _save_registration_attendee |
||
866 | * |
||
867 | * @param EE_Registration $registration |
||
868 | * @param bool $show_errors |
||
869 | * @return void |
||
870 | * @throws \EE_Error |
||
871 | */ |
||
872 | private function _save_registration( $registration, $show_errors = TRUE ) { |
||
903 | |||
904 | |||
905 | |||
906 | /** |
||
907 | * _save_registration_attendee |
||
908 | * |
||
909 | * @param EE_Registration $registration |
||
910 | * @param bool $show_errors |
||
911 | * @return void |
||
912 | * @throws \EE_Error |
||
913 | */ |
||
914 | private function _save_registration_attendee( $registration, $show_errors = TRUE ) { |
||
939 | |||
940 | |||
941 | |||
942 | /** |
||
943 | * _save_question_answers |
||
944 | * |
||
945 | * @param EE_Registration $registration |
||
946 | * @param bool $show_errors |
||
947 | * @return void |
||
948 | * @throws \EE_Error |
||
949 | */ |
||
950 | private function _save_registration_answers( $registration, $show_errors = TRUE ) { |
||
975 | |||
976 | |||
977 | |||
978 | /** |
||
979 | * refresh_all_entities |
||
980 | * will either refresh the entity map with objects form the db or from the checkout cache |
||
981 | * |
||
982 | * @access public |
||
983 | * @param bool $from_db |
||
984 | * @return bool |
||
985 | * @throws \EE_Error |
||
986 | */ |
||
987 | public function refresh_all_entities( $from_db = false ) { |
||
997 | |||
998 | |||
999 | |||
1000 | /** |
||
1001 | * refresh_entity_map |
||
1002 | * simply loops through the current transaction and updates each |
||
1003 | * model's entity map using EEM_Base::refresh_entity_map_from_db() |
||
1004 | * |
||
1005 | * @access public |
||
1006 | * @return bool |
||
1007 | * @throws \EE_Error |
||
1008 | */ |
||
1009 | protected function refresh_from_db() { |
||
1030 | |||
1031 | |||
1032 | |||
1033 | /** |
||
1034 | * _refresh_primary_attendee_obj_from_db |
||
1035 | * |
||
1036 | * @param EE_Transaction $transaction |
||
1037 | * @return EE_Attendee | null |
||
1038 | * @throws \EE_Error |
||
1039 | */ |
||
1040 | protected function _refresh_primary_attendee_obj_from_db( EE_Transaction $transaction ) { |
||
1061 | |||
1062 | |||
1063 | |||
1064 | /** |
||
1065 | * refresh_entity_map |
||
1066 | * simply loops through the current transaction and updates |
||
1067 | * each model's entity map using EEM_Base::refresh_entity_map_with() |
||
1068 | * |
||
1069 | * @access public |
||
1070 | * @return bool |
||
1071 | * @throws \EE_Error |
||
1072 | */ |
||
1073 | protected function refresh_entity_map() { |
||
1119 | |||
1120 | |||
1121 | |||
1122 | /** |
||
1123 | * _refresh_registration |
||
1124 | * |
||
1125 | * @param string | int $reg_cache_ID |
||
1126 | * @param EE_Registration $registration |
||
1127 | * @return void |
||
1128 | * @throws \EE_Error |
||
1129 | */ |
||
1130 | protected function _refresh_registration( $reg_cache_ID, $registration ) { |
||
1147 | |||
1148 | |||
1149 | |||
1150 | /** |
||
1151 | * _save_registration_attendee |
||
1152 | * |
||
1153 | * @param EE_Registration $registration |
||
1154 | * @return void |
||
1155 | * @throws \EE_Error |
||
1156 | */ |
||
1157 | protected function _refresh_registration_attendee( $registration ) { |
||
1170 | |||
1171 | |||
1172 | |||
1173 | /** |
||
1174 | * _refresh_registration_answers |
||
1175 | * |
||
1176 | * @param EE_Registration $registration |
||
1177 | * @return void |
||
1178 | * @throws \EE_Error |
||
1179 | */ |
||
1180 | protected function _refresh_registration_answers( $registration ) { |
||
1198 | |||
1199 | |||
1200 | |||
1201 | /** |
||
1202 | * __wakeup |
||
1203 | * to conserve db space, we are removing the EE_Checkout object from EE_SPCO_Reg_Step objects upon serialization |
||
1204 | * this will reinstate the EE_Checkout object on each EE_SPCO_Reg_Step object |
||
1205 | */ |
||
1206 | public function __wakeup() { |
||
1211 | |||
1212 | |||
1213 | |||
1214 | /** |
||
1215 | * debug |
||
1216 | * |
||
1217 | * @param string $class |
||
1218 | * @param string $func |
||
1219 | * @param string $line |
||
1220 | * @param array $info |
||
1221 | * @param bool $display_request |
||
1222 | * @throws \EE_Error |
||
1223 | */ |
||
1224 | public function log( $class = '', $func = '', $line = '', $info = array(), $display_request = false ) { |
||
1263 | |||
1264 | |||
1265 | /** |
||
1266 | * _strip_objects |
||
1267 | * |
||
1268 | * @param array $info |
||
1269 | * @return array |
||
1270 | */ |
||
1271 | public function _strip_objects( $info = array() ) { |
||
1289 | |||
1290 | |||
1291 | |||
1292 | |||
1293 | } |
||
1294 | // End of file EE_Checkout.class.php |
||
1295 | // Location: /EE_Checkout.class.php |
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.