Complex classes like EE_SPCO_Reg_Step 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_SPCO_Reg_Step, and based on these observations, apply Extract Interface, too.
1 | <?php if (! defined('EVENT_ESPRESSO_VERSION')) { |
||
16 | abstract class EE_SPCO_Reg_Step |
||
17 | { |
||
18 | |||
19 | /** |
||
20 | * $_completed - TRUE if this step has fully completed it's duties |
||
21 | * |
||
22 | * @access protected |
||
23 | * @type bool $_completed |
||
24 | */ |
||
25 | protected $_completed = false; |
||
26 | |||
27 | /** |
||
28 | * $_is_current_step - TRUE if this is the current step |
||
29 | * |
||
30 | * @access protected |
||
31 | * @type bool $_is_current_step |
||
32 | */ |
||
33 | protected $_is_current_step = false; |
||
34 | |||
35 | /** |
||
36 | * $_order - when the reg step should be run relative to other steps |
||
37 | * |
||
38 | * @access protected |
||
39 | * @type int $_template |
||
40 | */ |
||
41 | protected $_order = 0; |
||
42 | |||
43 | /** |
||
44 | * $_slug - URL param for this step |
||
45 | * |
||
46 | * @access protected |
||
47 | * @type string $_slug |
||
48 | */ |
||
49 | protected $_slug; |
||
50 | |||
51 | /** |
||
52 | * $_name - Step Name - translatable string |
||
53 | * |
||
54 | * @access protected |
||
55 | * @type string $_slug |
||
56 | */ |
||
57 | protected $_name; |
||
58 | |||
59 | /** |
||
60 | * $_submit_button_text - translatable string that appears on this step's submit button |
||
61 | * |
||
62 | * @access protected |
||
63 | * @type string $_slug |
||
64 | */ |
||
65 | protected $_submit_button_text; |
||
66 | |||
67 | /** |
||
68 | * $_template - template name |
||
69 | * |
||
70 | * @access protected |
||
71 | * @type string $_template |
||
72 | */ |
||
73 | protected $_template; |
||
74 | |||
75 | /** |
||
76 | * $_reg_form_name - the form input name and id attribute |
||
77 | * |
||
78 | * @access protected |
||
79 | * @var string $_reg_form_name |
||
80 | */ |
||
81 | protected $_reg_form_name; |
||
82 | |||
83 | /** |
||
84 | * $_success_message - text to display upon successful form submission |
||
85 | * |
||
86 | * @access private |
||
87 | * @var string $_success_message |
||
88 | */ |
||
89 | protected $_success_message; |
||
90 | |||
91 | /** |
||
92 | * $_instructions - a brief description of how to complete the reg step. Usually displayed in conjunction with |
||
93 | * the previous step's success message. |
||
94 | * |
||
95 | * @access private |
||
96 | * @var string $_instructions |
||
97 | */ |
||
98 | protected $_instructions; |
||
99 | |||
100 | /** |
||
101 | * $_valid_data - the normalized and validated data for this step |
||
102 | * |
||
103 | * @access public |
||
104 | * @var array $_valid_data |
||
105 | */ |
||
106 | protected $_valid_data = array(); |
||
107 | |||
108 | /** |
||
109 | * $reg_form - the registration form for this step |
||
110 | * |
||
111 | * @access public |
||
112 | * @var EE_Form_Section_Proper $reg_form |
||
113 | */ |
||
114 | public $reg_form; |
||
115 | |||
116 | /** |
||
117 | * $checkout - EE_Checkout object for handling the properties of the current checkout process |
||
118 | * |
||
119 | * @access public |
||
120 | * @var EE_Checkout $checkout |
||
121 | */ |
||
122 | public $checkout; |
||
123 | |||
124 | |||
125 | |||
126 | /** |
||
127 | * @return void |
||
128 | */ |
||
129 | abstract public function translate_js_strings(); |
||
130 | |||
131 | |||
132 | |||
133 | /** |
||
134 | * @return void |
||
135 | */ |
||
136 | abstract public function enqueue_styles_and_scripts(); |
||
137 | |||
138 | |||
139 | |||
140 | /** |
||
141 | * @return boolean |
||
142 | */ |
||
143 | abstract public function initialize_reg_step(); |
||
144 | |||
145 | |||
146 | |||
147 | /** |
||
148 | * @return string |
||
149 | */ |
||
150 | abstract public function generate_reg_form(); |
||
151 | |||
152 | |||
153 | |||
154 | /** |
||
155 | * @return boolean |
||
156 | */ |
||
157 | abstract public function process_reg_step(); |
||
158 | |||
159 | |||
160 | |||
161 | /** |
||
162 | * @return boolean |
||
163 | */ |
||
164 | abstract public function update_reg_step(); |
||
165 | |||
166 | |||
167 | |||
168 | /** |
||
169 | * @return boolean |
||
170 | */ |
||
171 | public function completed() |
||
172 | { |
||
173 | return $this->_completed; |
||
174 | } |
||
175 | |||
176 | |||
177 | |||
178 | /** |
||
179 | * set_completed - toggles $_completed to TRUE |
||
180 | */ |
||
181 | public function set_completed() |
||
182 | { |
||
183 | // DEBUG LOG |
||
184 | //$this->checkout->log( __CLASS__, __FUNCTION__, __LINE__ ); |
||
185 | $this->_completed = apply_filters('FHEE__EE_SPCO_Reg_Step__set_completed___completed', true, $this); |
||
186 | } |
||
187 | |||
188 | |||
189 | |||
190 | /** |
||
191 | * set_completed - toggles $_completed to FALSE |
||
192 | */ |
||
193 | public function set_not_completed() |
||
194 | { |
||
195 | $this->_completed = false; |
||
196 | } |
||
197 | |||
198 | |||
199 | |||
200 | /** |
||
201 | * @return string |
||
202 | */ |
||
203 | public function name() |
||
204 | { |
||
205 | return $this->_name; |
||
206 | } |
||
207 | |||
208 | |||
209 | |||
210 | /** |
||
211 | * @return string |
||
212 | */ |
||
213 | public function slug() |
||
214 | { |
||
215 | return $this->_slug; |
||
216 | } |
||
217 | |||
218 | |||
219 | |||
220 | /** |
||
221 | * submit_button_text |
||
222 | * the text that appears on the reg step form submit button |
||
223 | * |
||
224 | * @return string |
||
225 | */ |
||
226 | public function submit_button_text() |
||
227 | { |
||
228 | return $this->_submit_button_text; |
||
229 | } |
||
230 | |||
231 | |||
232 | |||
233 | /** |
||
234 | * set_submit_button_text |
||
235 | * sets the text that appears on the reg step form submit button |
||
236 | * |
||
237 | * @param string $submit_button_text |
||
238 | */ |
||
239 | public function set_submit_button_text($submit_button_text = '') |
||
240 | { |
||
241 | if (! empty($submit_button_text)) { |
||
242 | $this->_submit_button_text = $submit_button_text; |
||
243 | } else if ($this->checkout->next_step instanceof EE_SPCO_Reg_Step) { |
||
244 | if ($this->checkout->revisit) { |
||
245 | $this->_submit_button_text = sprintf(__('Update %s', 'event_espresso'), |
||
246 | $this->checkout->current_step->name()); |
||
247 | } else { |
||
248 | $this->_submit_button_text = sprintf(__('Proceed to %s', 'event_espresso'), |
||
249 | $this->checkout->next_step->name()); |
||
250 | } |
||
251 | } |
||
252 | // filters the submit button text |
||
253 | $this->_submit_button_text = apply_filters( |
||
254 | 'FHEE__EE_SPCO_Reg_Step__set_submit_button_text___submit_button_text', |
||
255 | $this->_submit_button_text, |
||
256 | $this->checkout |
||
257 | ); |
||
258 | } |
||
259 | |||
260 | |||
261 | |||
262 | /** |
||
263 | * @param boolean $is_current_step |
||
264 | */ |
||
265 | public function set_is_current_step($is_current_step) |
||
266 | { |
||
267 | $this->_is_current_step = $is_current_step; |
||
268 | } |
||
269 | |||
270 | |||
271 | |||
272 | /** |
||
273 | * @return boolean |
||
274 | */ |
||
275 | public function is_current_step() |
||
276 | { |
||
277 | return $this->_is_current_step; |
||
278 | } |
||
279 | |||
280 | |||
281 | |||
282 | /** |
||
283 | * @return boolean |
||
284 | */ |
||
285 | public function is_final_step() |
||
286 | { |
||
287 | return $this instanceof EE_SPCO_Reg_Step_Finalize_Registration ? true : false; |
||
288 | } |
||
289 | |||
290 | |||
291 | |||
292 | /** |
||
293 | * @param int $order |
||
294 | */ |
||
295 | public function set_order($order) |
||
296 | { |
||
297 | $this->_order = $order; |
||
298 | } |
||
299 | |||
300 | |||
301 | |||
302 | /** |
||
303 | * @return int |
||
304 | */ |
||
305 | public function order() |
||
306 | { |
||
307 | return $this->_order; |
||
308 | } |
||
309 | |||
310 | |||
311 | |||
312 | /** |
||
313 | * @return string |
||
314 | */ |
||
315 | public function template() |
||
316 | { |
||
317 | return $this->_template; |
||
318 | } |
||
319 | |||
320 | |||
321 | |||
322 | /** |
||
323 | * @return string |
||
324 | */ |
||
325 | public function success_message() |
||
326 | { |
||
327 | return $this->_success_message; |
||
328 | } |
||
329 | |||
330 | |||
331 | |||
332 | /** |
||
333 | * _set_success_message |
||
334 | * |
||
335 | * @param string $success_message |
||
336 | */ |
||
337 | protected function _set_success_message($success_message) |
||
338 | { |
||
339 | $this->_success_message = $success_message; |
||
340 | } |
||
341 | |||
342 | |||
343 | |||
344 | /** |
||
345 | * _reset_success_message |
||
346 | * |
||
347 | * @return void |
||
348 | */ |
||
349 | protected function _reset_success_message() |
||
350 | { |
||
351 | $this->_success_message = ''; |
||
352 | } |
||
353 | |||
354 | |||
355 | |||
356 | /** |
||
357 | * @return string |
||
358 | */ |
||
359 | public function _instructions() |
||
360 | { |
||
361 | return $this->_instructions; |
||
362 | } |
||
363 | |||
364 | |||
365 | |||
366 | /** |
||
367 | * @param string $instructions |
||
368 | */ |
||
369 | public function set_instructions($instructions) |
||
370 | { |
||
371 | $this->_instructions = apply_filters( |
||
372 | 'FHEE__EE_SPCO_Reg_Step__set_instructions__instructions', |
||
373 | $instructions, |
||
374 | $this |
||
375 | ); |
||
376 | } |
||
377 | |||
378 | |||
379 | |||
380 | /** |
||
381 | * @param array $valid_data |
||
382 | */ |
||
383 | public function set_valid_data($valid_data) |
||
384 | { |
||
385 | $this->_valid_data = $valid_data; |
||
386 | } |
||
387 | |||
388 | |||
389 | |||
390 | /** |
||
391 | * @return array |
||
392 | */ |
||
393 | public function valid_data() |
||
394 | { |
||
395 | if (empty($this->_valid_data)) { |
||
396 | $this->_valid_data = $this->reg_form->valid_data(); |
||
397 | } |
||
398 | return $this->_valid_data; |
||
399 | } |
||
400 | |||
401 | |||
402 | |||
403 | /** |
||
404 | * @return string |
||
405 | */ |
||
406 | public function reg_form_name() |
||
407 | { |
||
408 | if (empty($this->_reg_form_name)) { |
||
409 | $this->set_reg_form_name('ee-spco-' . $this->slug() . '-reg-step-form'); |
||
410 | } |
||
411 | return $this->_reg_form_name; |
||
412 | } |
||
413 | |||
414 | |||
415 | |||
416 | /** |
||
417 | * @param string $reg_form_name |
||
418 | */ |
||
419 | protected function set_reg_form_name($reg_form_name) |
||
420 | { |
||
421 | $this->_reg_form_name = $reg_form_name; |
||
422 | } |
||
423 | |||
424 | |||
425 | |||
426 | /** |
||
427 | * reg_step_url |
||
428 | * |
||
429 | * @param string $action |
||
430 | * @return string |
||
431 | */ |
||
432 | public function reg_step_url($action = '') |
||
433 | { |
||
434 | $query_args = array('step' => $this->slug()); |
||
435 | if (! empty($action)) { |
||
436 | $query_args['action'] = $action; |
||
437 | } |
||
438 | // final step has no display |
||
439 | if ($this instanceof EE_SPCO_Reg_Step_Finalize_Registration && $action === 'display_spco_reg_step') { |
||
440 | $query_args['action'] = 'process_reg_step'; |
||
441 | } |
||
442 | if ($this->checkout->revisit) { |
||
443 | $query_args['revisit'] = true; |
||
444 | } |
||
445 | if ($this->checkout->reg_url_link) { |
||
446 | $query_args['e_reg_url_link'] = $this->checkout->reg_url_link; |
||
447 | } |
||
448 | return add_query_arg($query_args, $this->checkout->reg_page_base_url); |
||
449 | } |
||
450 | |||
451 | |||
452 | |||
453 | /** |
||
454 | * creates the default hidden inputs section |
||
455 | * |
||
456 | * @return EE_Form_Section_Proper |
||
457 | * @throws \EE_Error |
||
458 | */ |
||
459 | public function reg_step_hidden_inputs() |
||
460 | { |
||
461 | // hidden inputs for admin registrations |
||
462 | if ($this->checkout->admin_request) { |
||
463 | return new EE_Form_Section_Proper( |
||
464 | array( |
||
465 | 'layout_strategy' => new EE_Div_Per_Section_Layout(), |
||
466 | 'html_id' => 'ee-' . $this->slug() . '-hidden-inputs', |
||
467 | 'subsections' => array( |
||
468 | 'next_step' => new EE_Fixed_Hidden_Input( |
||
469 | array( |
||
470 | 'html_name' => 'next_step', |
||
471 | 'html_id' => 'spco-' . $this->slug() . '-next-step', |
||
472 | 'default' => $this->checkout->next_step instanceof EE_SPCO_Reg_Step |
||
473 | ? $this->checkout->next_step->slug() : '', |
||
474 | ) |
||
475 | ), |
||
476 | ), |
||
477 | ) |
||
478 | ); |
||
479 | } else { |
||
480 | $default_form_action = apply_filters( |
||
481 | 'FHEE__EE_SPCO_Reg_Step__reg_step_hidden_inputs__default_form_action', |
||
482 | empty($this->checkout->reg_url_link) |
||
483 | ? 'process_reg_step' |
||
484 | : 'update_reg_step', |
||
485 | $this |
||
486 | ); |
||
487 | // hidden inputs for frontend registrations |
||
488 | return new EE_Form_Section_Proper( |
||
489 | array( |
||
490 | 'layout_strategy' => new EE_Div_Per_Section_Layout(), |
||
491 | 'html_id' => 'ee-' . $this->slug() . '-hidden-inputs', |
||
492 | 'subsections' => array( |
||
493 | 'action' => new EE_Fixed_Hidden_Input( |
||
494 | array( |
||
495 | 'html_name' => 'action', |
||
496 | 'html_id' => 'spco-' . $this->slug() . '-action', |
||
497 | 'default' => $default_form_action, |
||
498 | ) |
||
499 | ), |
||
500 | 'next_step' => new EE_Fixed_Hidden_Input( |
||
501 | array( |
||
502 | 'html_name' => 'next_step', |
||
503 | 'html_id' => 'spco-' . $this->slug() . '-next-step', |
||
504 | 'default' => $this->checkout->next_step instanceof EE_SPCO_Reg_Step |
||
505 | ? $this->checkout->next_step->slug() : '', |
||
506 | ) |
||
507 | ), |
||
508 | 'e_reg_url_link' => new EE_Fixed_Hidden_Input( |
||
509 | array( |
||
510 | 'html_name' => 'e_reg_url_link', |
||
511 | 'html_id' => 'spco-reg_url_link', |
||
512 | 'default' => $this->checkout->reg_url_link, |
||
513 | ) |
||
514 | ), |
||
515 | 'revisit' => new EE_Fixed_Hidden_Input( |
||
516 | array( |
||
517 | 'html_name' => 'revisit', |
||
518 | 'html_id' => 'spco-revisit', |
||
519 | 'default' => $this->checkout->revisit, |
||
520 | ) |
||
521 | ), |
||
522 | ), |
||
523 | ) |
||
524 | ); |
||
525 | } |
||
526 | } |
||
527 | |||
528 | |||
529 | |||
530 | /** |
||
531 | * generate_reg_form_for_actions |
||
532 | * |
||
533 | * @param array $actions |
||
534 | * @return void |
||
535 | */ |
||
536 | public function generate_reg_form_for_actions($actions = array()) |
||
537 | { |
||
538 | $actions = array_merge( |
||
539 | array( |
||
540 | 'generate_reg_form', |
||
541 | 'display_spco_reg_step', |
||
542 | 'process_reg_step', |
||
543 | 'update_reg_step', |
||
544 | ), $actions |
||
545 | ); |
||
546 | $this->checkout->generate_reg_form = in_array($this->checkout->action, $actions, true) ? true : false; |
||
547 | } |
||
548 | |||
549 | |||
550 | |||
551 | /** |
||
552 | * @return string |
||
553 | * @throws \EE_Error |
||
554 | */ |
||
555 | public function display_reg_form() |
||
556 | { |
||
557 | $html = ''; |
||
558 | if ($this->reg_form instanceof EE_Form_Section_Proper) { |
||
559 | $html .= ! $this->checkout->admin_request ? $this->reg_form->form_open($this->reg_step_url()) : ''; |
||
560 | if (EE_Registry::instance()->REQ->ajax) { |
||
561 | $this->reg_form->localize_validation_rules(); |
||
562 | $this->checkout->json_response->add_validation_rules(EE_Form_Section_Proper::js_localization()); |
||
563 | } |
||
564 | $html .= $this->reg_form->get_html(); |
||
565 | $html .= ! $this->checkout->admin_request ? $this->reg_step_submit_button() : ''; |
||
566 | $html .= ! $this->checkout->admin_request ? $this->reg_form->form_close() : ''; |
||
567 | } |
||
568 | return $html; |
||
569 | } |
||
570 | |||
571 | |||
572 | |||
573 | /** |
||
574 | * div_class - returns nothing for current step, but a css class of "hidden" for others |
||
575 | * |
||
576 | * @return string |
||
577 | * @throws \EE_Error |
||
578 | */ |
||
579 | public function reg_step_submit_button() |
||
580 | { |
||
581 | if (! $this->checkout->next_step instanceof EE_SPCO_Reg_Step) { |
||
582 | return ''; |
||
583 | } |
||
584 | ob_start(); |
||
585 | do_action('AHEE__before_spco_whats_next_buttons', $this->slug(), $this->checkout->next_step->slug(), |
||
586 | $this->checkout); |
||
587 | $html = ob_get_clean(); |
||
588 | // generate submit button |
||
589 | $sbmt_btn = new EE_Submit_Input(array( |
||
590 | 'html_name' => 'spco-go-to-step-' . $this->checkout->next_step->slug(), |
||
591 | 'html_id' => 'spco-go-to-step-' . $this->checkout->next_step->slug(), |
||
592 | 'html_class' => 'spco-next-step-btn', |
||
593 | 'other_html_attributes' => ' rel="' . $this->slug() . '"', |
||
594 | 'default' => $this->submit_button_text(), |
||
595 | )); |
||
596 | $sbmt_btn->set_button_css_attributes(true, 'large'); |
||
597 | $sbmt_btn_html = $sbmt_btn->get_html_for_input(); |
||
598 | $html .= EEH_HTML::div( |
||
599 | apply_filters('FHEE__EE_SPCO_Reg_Step__reg_step_submit_button__sbmt_btn_html', $sbmt_btn_html, $this), |
||
600 | 'spco-' . $this->slug() . '-whats-next-buttons-dv', |
||
601 | 'spco-whats-next-buttons' |
||
602 | ); |
||
603 | return $html; |
||
604 | } |
||
605 | |||
606 | |||
607 | |||
608 | /** |
||
609 | * div_class - returns nothing for current step, but a css class of "hidden" for others |
||
610 | * |
||
611 | * @return string |
||
612 | */ |
||
613 | public function div_class() |
||
617 | |||
618 | |||
619 | |||
620 | /** |
||
621 | * div_class - returns a css class of "hidden" for current step, but nothing for others |
||
622 | * |
||
623 | * @return string |
||
624 | */ |
||
625 | public function edit_lnk_url() |
||
629 | |||
630 | |||
631 | |||
632 | /** |
||
633 | * div_class - returns a css class of "hidden" for current step, but nothing for others |
||
634 | * |
||
635 | * @return string |
||
636 | */ |
||
637 | public function edit_link_class() |
||
641 | |||
642 | |||
643 | |||
644 | /** |
||
645 | * update_checkout with changes that have been made to the cart |
||
646 | * |
||
647 | * @return void |
||
648 | * @throws \EE_Error |
||
649 | */ |
||
650 | public function update_checkout() |
||
656 | |||
657 | |||
658 | |||
659 | /** |
||
660 | * __sleep |
||
661 | * to conserve db space, let's remove the reg_form and the EE_Checkout object from EE_SPCO_Reg_Step objects upon |
||
662 | * serialization EE_Checkout will handle the reimplementation of itself upon waking, but we won't bother with the |
||
663 | * reg form, because if needed, it will be regenerated anyways |
||
664 | * |
||
665 | * @return array |
||
666 | */ |
||
667 | public function __sleep() |
||
668 | { |
||
669 | // remove the reg form and the checkout |
||
670 | return array_diff(array_keys(get_object_vars($this)), array('reg_form', 'checkout')); |
||
671 | } |
||
672 | |||
673 | |||
674 | |||
675 | } |
||
676 | // End of file EE_SPCO_Reg_Step.class.php |
||
677 | // Location: /EE_SPCO_Reg_Step.class.php |