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_Attendee 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_Attendee, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
25 | class EE_Attendee extends EE_CPT_Base implements EEI_Contact, EEI_Address, EEI_Admin_Links, EEI_Attendee |
||
26 | { |
||
27 | |||
28 | /** |
||
29 | * Sets some dynamic defaults |
||
30 | * |
||
31 | * @param array $fieldValues |
||
32 | * @param bool $bydb |
||
33 | * @param string $timezone |
||
34 | * @param array $date_formats |
||
35 | * @throws EE_Error |
||
36 | */ |
||
37 | protected function __construct($fieldValues = null, $bydb = false, $timezone = null, $date_formats = array()) |
||
53 | |||
54 | |||
55 | /** |
||
56 | * @param array $props_n_values incoming values |
||
57 | * @param string $timezone incoming timezone (if not set the timezone set for the website will be |
||
58 | * used.) |
||
59 | * @param array $date_formats incoming date_formats in an array where the first value is the |
||
60 | * date_format and the second value is the time format |
||
61 | * @return EE_Attendee |
||
62 | * @throws EE_Error |
||
63 | */ |
||
64 | public static function new_instance($props_n_values = array(), $timezone = null, $date_formats = array()) |
||
69 | |||
70 | |||
71 | /** |
||
72 | * @param array $props_n_values incoming values from the database |
||
73 | * @param string $timezone incoming timezone as set by the model. If not set the timezone for |
||
74 | * the website will be used. |
||
75 | * @return EE_Attendee |
||
76 | */ |
||
77 | public static function new_instance_from_db($props_n_values = array(), $timezone = null) |
||
81 | |||
82 | |||
83 | /** |
||
84 | * Set Attendee First Name |
||
85 | * |
||
86 | * @access public |
||
87 | * @param string $fname |
||
88 | * @throws EE_Error |
||
89 | */ |
||
90 | public function set_fname($fname = '') |
||
94 | |||
95 | |||
96 | /** |
||
97 | * Set Attendee Last Name |
||
98 | * |
||
99 | * @access public |
||
100 | * @param string $lname |
||
101 | * @throws EE_Error |
||
102 | */ |
||
103 | public function set_lname($lname = '') |
||
107 | |||
108 | |||
109 | /** |
||
110 | * Set Attendee Address |
||
111 | * |
||
112 | * @access public |
||
113 | * @param string $address |
||
114 | * @throws EE_Error |
||
115 | */ |
||
116 | public function set_address($address = '') |
||
120 | |||
121 | |||
122 | /** |
||
123 | * Set Attendee Address2 |
||
124 | * |
||
125 | * @access public |
||
126 | * @param string $address2 |
||
127 | * @throws EE_Error |
||
128 | */ |
||
129 | public function set_address2($address2 = '') |
||
133 | |||
134 | |||
135 | /** |
||
136 | * Set Attendee City |
||
137 | * |
||
138 | * @access public |
||
139 | * @param string $city |
||
140 | * @throws EE_Error |
||
141 | */ |
||
142 | public function set_city($city = '') |
||
146 | |||
147 | |||
148 | /** |
||
149 | * Set Attendee State ID |
||
150 | * |
||
151 | * @access public |
||
152 | * @param int $STA_ID |
||
153 | * @throws EE_Error |
||
154 | */ |
||
155 | public function set_state($STA_ID = 0) |
||
159 | |||
160 | |||
161 | /** |
||
162 | * Set Attendee Country ISO Code |
||
163 | * |
||
164 | * @access public |
||
165 | * @param string $CNT_ISO |
||
166 | * @throws EE_Error |
||
167 | */ |
||
168 | public function set_country($CNT_ISO = '') |
||
172 | |||
173 | |||
174 | /** |
||
175 | * Set Attendee Zip/Postal Code |
||
176 | * |
||
177 | * @access public |
||
178 | * @param string $zip |
||
179 | * @throws EE_Error |
||
180 | */ |
||
181 | public function set_zip($zip = '') |
||
185 | |||
186 | |||
187 | /** |
||
188 | * Set Attendee Email Address |
||
189 | * |
||
190 | * @access public |
||
191 | * @param string $email |
||
192 | * @throws EE_Error |
||
193 | */ |
||
194 | public function set_email($email = '') |
||
198 | |||
199 | |||
200 | /** |
||
201 | * Set Attendee Phone |
||
202 | * |
||
203 | * @access public |
||
204 | * @param string $phone |
||
205 | * @throws EE_Error |
||
206 | */ |
||
207 | public function set_phone($phone = '') |
||
211 | |||
212 | |||
213 | /** |
||
214 | * set deleted |
||
215 | * |
||
216 | * @access public |
||
217 | * @param bool $ATT_deleted |
||
218 | * @throws EE_Error |
||
219 | */ |
||
220 | public function set_deleted($ATT_deleted = false) |
||
224 | |||
225 | |||
226 | /** |
||
227 | * Returns the value for the post_author id saved with the cpt |
||
228 | * |
||
229 | * @since 4.5.0 |
||
230 | * @return int |
||
231 | * @throws EE_Error |
||
232 | */ |
||
233 | public function wp_user() |
||
237 | |||
238 | |||
239 | /** |
||
240 | * get Attendee First Name |
||
241 | * |
||
242 | * @access public |
||
243 | * @return string |
||
244 | * @throws EE_Error |
||
245 | */ |
||
246 | public function fname() |
||
250 | |||
251 | |||
252 | /** |
||
253 | * echoes out the attendee's first name |
||
254 | * |
||
255 | * @return void |
||
256 | * @throws EE_Error |
||
257 | */ |
||
258 | public function e_full_name() |
||
262 | |||
263 | |||
264 | /** |
||
265 | * Returns the first and last name concatenated together with a space. |
||
266 | * |
||
267 | * @param bool $apply_html_entities |
||
268 | * @return string |
||
269 | * @throws EE_Error |
||
270 | */ |
||
271 | public function full_name($apply_html_entities = false) |
||
281 | |||
282 | |||
283 | /** |
||
284 | * This returns the value of the `ATT_full_name` field which is usually equivalent to calling `full_name()` unless |
||
285 | * the post_title field has been directly modified in the db for the post (espresso_attendees post type) for this |
||
286 | * attendee. |
||
287 | * |
||
288 | * @param bool $apply_html_entities |
||
289 | * @return string |
||
290 | * @throws EE_Error |
||
291 | */ |
||
292 | public function ATT_full_name($apply_html_entities = false) |
||
298 | |||
299 | |||
300 | /** |
||
301 | * get Attendee Last Name |
||
302 | * |
||
303 | * @access public |
||
304 | * @return string |
||
305 | * @throws EE_Error |
||
306 | */ |
||
307 | public function lname() |
||
311 | |||
312 | |||
313 | /** |
||
314 | * get Attendee bio |
||
315 | * |
||
316 | * @access public |
||
317 | * @return string |
||
318 | * @throws EE_Error |
||
319 | */ |
||
320 | public function bio() |
||
324 | |||
325 | |||
326 | /** |
||
327 | * get Attendee short bio |
||
328 | * |
||
329 | * @access public |
||
330 | * @return string |
||
331 | * @throws EE_Error |
||
332 | */ |
||
333 | public function short_bio() |
||
337 | |||
338 | |||
339 | /** |
||
340 | * Gets the attendee's full address as an array so client code can decide hwo to display it |
||
341 | * |
||
342 | * @return array numerically indexed, with each part of the address that is known. |
||
343 | * Eg, if the user only responded to state and country, |
||
344 | * it would be array(0=>'Alabama',1=>'USA') |
||
345 | * @return array |
||
346 | * @throws EE_Error |
||
347 | */ |
||
348 | public function full_address_as_array() |
||
374 | |||
375 | |||
376 | /** |
||
377 | * get Attendee Address |
||
378 | * |
||
379 | * @return string |
||
380 | * @throws EE_Error |
||
381 | */ |
||
382 | public function address() |
||
386 | |||
387 | |||
388 | /** |
||
389 | * get Attendee Address2 |
||
390 | * |
||
391 | * @return string |
||
392 | * @throws EE_Error |
||
393 | */ |
||
394 | public function address2() |
||
398 | |||
399 | |||
400 | /** |
||
401 | * get Attendee City |
||
402 | * |
||
403 | * @return string |
||
404 | * @throws EE_Error |
||
405 | */ |
||
406 | public function city() |
||
410 | |||
411 | |||
412 | /** |
||
413 | * get Attendee State ID |
||
414 | * |
||
415 | * @return string |
||
416 | * @throws EE_Error |
||
417 | */ |
||
418 | public function state_ID() |
||
422 | |||
423 | |||
424 | /** |
||
425 | * @return string |
||
426 | * @throws EE_Error |
||
427 | */ |
||
428 | public function state_abbrev() |
||
432 | |||
433 | |||
434 | /** |
||
435 | * Gets the state set to this attendee |
||
436 | * |
||
437 | * @return EE_State |
||
438 | * @throws EE_Error |
||
439 | */ |
||
440 | public function state_obj() |
||
444 | |||
445 | |||
446 | /** |
||
447 | * Returns the state's name, otherwise 'Unknown' |
||
448 | * |
||
449 | * @return string |
||
450 | * @throws EE_Error |
||
451 | */ |
||
452 | public function state_name() |
||
460 | |||
461 | |||
462 | /** |
||
463 | * either displays the state abbreviation or the state name, as determined |
||
464 | * by the "FHEE__EEI_Address__state__use_abbreviation" filter. |
||
465 | * defaults to abbreviation |
||
466 | * |
||
467 | * @return string |
||
468 | * @throws EE_Error |
||
469 | */ |
||
470 | public function state() |
||
477 | |||
478 | |||
479 | /** |
||
480 | * get Attendee Country ISO Code |
||
481 | * |
||
482 | * @return string |
||
483 | * @throws EE_Error |
||
484 | */ |
||
485 | public function country_ID() |
||
489 | |||
490 | |||
491 | /** |
||
492 | * Gets country set for this attendee |
||
493 | * |
||
494 | * @return EE_Country |
||
495 | * @throws EE_Error |
||
496 | */ |
||
497 | public function country_obj() |
||
501 | |||
502 | |||
503 | /** |
||
504 | * Returns the country's name if known, otherwise 'Unknown' |
||
505 | * |
||
506 | * @return string |
||
507 | * @throws EE_Error |
||
508 | */ |
||
509 | public function country_name() |
||
516 | |||
517 | |||
518 | /** |
||
519 | * either displays the country ISO2 code or the country name, as determined |
||
520 | * by the "FHEE__EEI_Address__country__use_abbreviation" filter. |
||
521 | * defaults to abbreviation |
||
522 | * |
||
523 | * @return string |
||
524 | * @throws EE_Error |
||
525 | */ |
||
526 | public function country() |
||
533 | |||
534 | |||
535 | /** |
||
536 | * get Attendee Zip/Postal Code |
||
537 | * |
||
538 | * @return string |
||
539 | * @throws EE_Error |
||
540 | */ |
||
541 | public function zip() |
||
545 | |||
546 | |||
547 | /** |
||
548 | * get Attendee Email Address |
||
549 | * |
||
550 | * @return string |
||
551 | * @throws EE_Error |
||
552 | */ |
||
553 | public function email() |
||
557 | |||
558 | |||
559 | /** |
||
560 | * get Attendee Phone # |
||
561 | * |
||
562 | * @return string |
||
563 | * @throws EE_Error |
||
564 | */ |
||
565 | public function phone() |
||
569 | |||
570 | |||
571 | /** |
||
572 | * get deleted |
||
573 | * |
||
574 | * @return bool |
||
575 | * @throws EE_Error |
||
576 | */ |
||
577 | public function deleted() |
||
581 | |||
582 | |||
583 | /** |
||
584 | * Gets registrations of this attendee |
||
585 | * |
||
586 | * @param array $query_params |
||
587 | * @return EE_Registration[] |
||
588 | * @throws EE_Error |
||
589 | */ |
||
590 | public function get_registrations($query_params = array()) |
||
594 | |||
595 | |||
596 | /** |
||
597 | * Gets the most recent registration of this attendee |
||
598 | * |
||
599 | * @return EE_Registration |
||
600 | * @throws EE_Error |
||
601 | */ |
||
602 | public function get_most_recent_registration() |
||
609 | |||
610 | |||
611 | /** |
||
612 | * Gets the most recent registration for this attend at this event |
||
613 | * |
||
614 | * @param int $event_id |
||
615 | * @return EE_Registration |
||
616 | * @throws EE_Error |
||
617 | */ |
||
618 | public function get_most_recent_registration_for_event($event_id) |
||
625 | |||
626 | |||
627 | /** |
||
628 | * returns any events attached to this attendee ($_Event property); |
||
629 | * |
||
630 | * @return array |
||
631 | * @throws EE_Error |
||
632 | */ |
||
633 | public function events() |
||
637 | |||
638 | |||
639 | /** |
||
640 | * Gets the billing info array where keys match espresso_reg_page_billing_inputs(), |
||
641 | * and keys are their cleaned values. @see EE_Attendee::save_and_clean_billing_info_for_payment_method() which was |
||
642 | * used to save the billing info |
||
643 | * |
||
644 | * @param EE_Payment_Method $payment_method the _gateway_name property on the gateway class |
||
645 | * @return EE_Form_Section_Proper|null |
||
646 | * @throws EE_Error |
||
647 | */ |
||
648 | public function billing_info_for_payment_method($payment_method) |
||
676 | |||
677 | |||
678 | /** |
||
679 | * Gets the postmeta key that holds this attendee's billing info for the |
||
680 | * specified payment method |
||
681 | * |
||
682 | * @param EE_Payment_Method $payment_method |
||
683 | * @return string |
||
684 | * @throws EE_Error |
||
685 | */ |
||
686 | public function get_billing_info_postmeta_name($payment_method) |
||
693 | |||
694 | |||
695 | /** |
||
696 | * Saves the billing info to the attendee. @see EE_Attendee::billing_info_for_payment_method() which is used to |
||
697 | * retrieve it |
||
698 | * |
||
699 | * @param EE_Billing_Attendee_Info_Form $billing_form |
||
700 | * @param EE_Payment_Method $payment_method |
||
701 | * @return boolean |
||
702 | * @throws EE_Error |
||
703 | */ |
||
704 | public function save_and_clean_billing_info_for_payment_method($billing_form, $payment_method) |
||
717 | |||
718 | |||
719 | /** |
||
720 | * Return the link to the admin details for the object. |
||
721 | * |
||
722 | * @return string |
||
723 | * @throws EE_Error |
||
724 | * @throws InvalidArgumentException |
||
725 | * @throws InvalidDataTypeException |
||
726 | * @throws InvalidInterfaceException |
||
727 | * @throws ReflectionException |
||
728 | */ |
||
729 | public function get_admin_details_link() |
||
733 | |||
734 | |||
735 | /** |
||
736 | * Returns the link to the editor for the object. Sometimes this is the same as the details. |
||
737 | * |
||
738 | * @return string |
||
739 | * @throws EE_Error |
||
740 | * @throws InvalidArgumentException |
||
741 | * @throws ReflectionException |
||
742 | * @throws InvalidDataTypeException |
||
743 | * @throws InvalidInterfaceException |
||
744 | */ |
||
745 | View Code Duplication | public function get_admin_edit_link() |
|
757 | |||
758 | |||
759 | /** |
||
760 | * Returns the link to a settings page for the object. |
||
761 | * |
||
762 | * @return string |
||
763 | * @throws EE_Error |
||
764 | * @throws InvalidArgumentException |
||
765 | * @throws InvalidDataTypeException |
||
766 | * @throws InvalidInterfaceException |
||
767 | * @throws ReflectionException |
||
768 | */ |
||
769 | public function get_admin_settings_link() |
||
773 | |||
774 | |||
775 | /** |
||
776 | * Returns the link to the "overview" for the object (typically the "list table" view). |
||
777 | * |
||
778 | * @return string |
||
779 | * @throws EE_Error |
||
780 | * @throws InvalidArgumentException |
||
781 | * @throws ReflectionException |
||
782 | * @throws InvalidDataTypeException |
||
783 | * @throws InvalidInterfaceException |
||
784 | */ |
||
785 | View Code Duplication | public function get_admin_overview_link() |
|
796 | } |
||
797 |