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 if (! defined('EVENT_ESPRESSO_VERSION')) { |
||
23 | class EE_Attendee extends EE_CPT_Base implements EEI_Contact, EEI_Address, EEI_Admin_Links, EEI_Attendee |
||
24 | { |
||
25 | |||
26 | /** |
||
27 | * Sets some dynamic defaults |
||
28 | * |
||
29 | * @param array $fieldValues |
||
30 | * @param bool $bydb |
||
31 | * @param string $timezone |
||
32 | * @param array $date_formats |
||
33 | */ |
||
34 | protected function __construct($fieldValues = null, $bydb = false, $timezone = null, $date_formats = array()) |
||
50 | |||
51 | |||
52 | /** |
||
53 | * @param array $props_n_values incoming values |
||
54 | * @param string $timezone incoming timezone (if not set the timezone set for the website will be |
||
55 | * used.) |
||
56 | * @param array $date_formats incoming date_formats in an array where the first value is the |
||
57 | * date_format and the second value is the time format |
||
58 | * @return EE_Attendee |
||
59 | */ |
||
60 | public static function new_instance($props_n_values = array(), $timezone = null, $date_formats = array()) |
||
61 | { |
||
62 | $has_object = parent::_check_for_object($props_n_values, __CLASS__, $timezone, $date_formats); |
||
63 | return $has_object ? $has_object : new self($props_n_values, false, $timezone, $date_formats); |
||
64 | } |
||
65 | |||
66 | |||
67 | /** |
||
68 | * @param array $props_n_values incoming values from the database |
||
69 | * @param string $timezone incoming timezone as set by the model. If not set the timezone for |
||
70 | * the website will be used. |
||
71 | * @return EE_Attendee |
||
72 | */ |
||
73 | public static function new_instance_from_db($props_n_values = array(), $timezone = null) |
||
74 | { |
||
75 | return new self($props_n_values, true, $timezone); |
||
76 | } |
||
77 | |||
78 | |||
79 | /** |
||
80 | * Set Attendee First Name |
||
81 | * |
||
82 | * @access public |
||
83 | * @param string $fname |
||
84 | */ |
||
85 | public function set_fname($fname = '') |
||
86 | { |
||
87 | $this->set('ATT_fname', $fname); |
||
88 | } |
||
89 | |||
90 | |||
91 | /** |
||
92 | * Set Attendee Last Name |
||
93 | * |
||
94 | * @access public |
||
95 | * @param string $lname |
||
96 | */ |
||
97 | public function set_lname($lname = '') |
||
98 | { |
||
99 | $this->set('ATT_lname', $lname); |
||
100 | } |
||
101 | |||
102 | |||
103 | /** |
||
104 | * Set Attendee Address |
||
105 | * |
||
106 | * @access public |
||
107 | * @param string $address |
||
108 | */ |
||
109 | public function set_address($address = '') |
||
110 | { |
||
111 | $this->set('ATT_address', $address); |
||
112 | } |
||
113 | |||
114 | |||
115 | /** |
||
116 | * Set Attendee Address2 |
||
117 | * |
||
118 | * @access public |
||
119 | * @param string $address2 |
||
120 | */ |
||
121 | public function set_address2($address2 = '') |
||
122 | { |
||
123 | $this->set('ATT_address2', $address2); |
||
124 | } |
||
125 | |||
126 | |||
127 | /** |
||
128 | * Set Attendee City |
||
129 | * |
||
130 | * @access public |
||
131 | * @param string $city |
||
132 | */ |
||
133 | public function set_city($city = '') |
||
134 | { |
||
135 | $this->set('ATT_city', $city); |
||
136 | } |
||
137 | |||
138 | |||
139 | /** |
||
140 | * Set Attendee State ID |
||
141 | * |
||
142 | * @access public |
||
143 | * @param int $STA_ID |
||
144 | */ |
||
145 | public function set_state($STA_ID = 0) |
||
146 | { |
||
147 | $this->set('STA_ID', $STA_ID); |
||
148 | } |
||
149 | |||
150 | |||
151 | /** |
||
152 | * Set Attendee Country ISO Code |
||
153 | * |
||
154 | * @access public |
||
155 | * @param string $CNT_ISO |
||
156 | */ |
||
157 | public function set_country($CNT_ISO = '') |
||
158 | { |
||
159 | $this->set('CNT_ISO', $CNT_ISO); |
||
160 | } |
||
161 | |||
162 | |||
163 | /** |
||
164 | * Set Attendee Zip/Postal Code |
||
165 | * |
||
166 | * @access public |
||
167 | * @param string $zip |
||
168 | */ |
||
169 | public function set_zip($zip = '') |
||
170 | { |
||
171 | $this->set('ATT_zip', $zip); |
||
172 | } |
||
173 | |||
174 | |||
175 | /** |
||
176 | * Set Attendee Email Address |
||
177 | * |
||
178 | * @access public |
||
179 | * @param string $email |
||
180 | */ |
||
181 | public function set_email($email = '') |
||
182 | { |
||
183 | $this->set('ATT_email', $email); |
||
184 | } |
||
185 | |||
186 | |||
187 | /** |
||
188 | * Set Attendee Phone |
||
189 | * |
||
190 | * @access public |
||
191 | * @param string $phone |
||
192 | */ |
||
193 | public function set_phone($phone = '') |
||
194 | { |
||
195 | $this->set('ATT_phone', $phone); |
||
196 | } |
||
197 | |||
198 | |||
199 | /** |
||
200 | * set deleted |
||
201 | * |
||
202 | * @access public |
||
203 | * @param bool $ATT_deleted |
||
204 | */ |
||
205 | public function set_deleted($ATT_deleted = false) |
||
206 | { |
||
207 | $this->set('ATT_deleted', $ATT_deleted); |
||
208 | } |
||
209 | |||
210 | |||
211 | /** |
||
212 | * Returns the value for the post_author id saved with the cpt |
||
213 | * |
||
214 | * @since 4.5.0 |
||
215 | * @return int |
||
216 | */ |
||
217 | public function wp_user() |
||
218 | { |
||
219 | return $this->get('ATT_author'); |
||
220 | } |
||
221 | |||
222 | |||
223 | /** |
||
224 | * get Attendee First Name |
||
225 | * |
||
226 | * @access public |
||
227 | * @return string |
||
228 | */ |
||
229 | public function fname() |
||
230 | { |
||
231 | return $this->get('ATT_fname'); |
||
232 | } |
||
233 | |||
234 | |||
235 | /** |
||
236 | * echoes out the attendee's first name |
||
237 | * |
||
238 | * @return void |
||
239 | */ |
||
240 | public function e_full_name() |
||
241 | { |
||
242 | echo $this->full_name(); |
||
243 | } |
||
244 | |||
245 | |||
246 | /** |
||
247 | * Returns the first and last name concatenated together with a space. |
||
248 | * |
||
249 | * @param bool $apply_html_entities |
||
250 | * @return string |
||
251 | */ |
||
252 | public function full_name($apply_html_entities = false) |
||
253 | { |
||
254 | $full_name = $this->fname() . ' ' . $this->lname(); |
||
255 | return $apply_html_entities ? htmlentities($full_name, ENT_QUOTES, 'UTF-8') : $full_name; |
||
256 | } |
||
257 | |||
258 | |||
259 | /** |
||
260 | * This returns the value of the `ATT_full_name` field which is usually equivalent to calling `full_name()` unless |
||
261 | * the post_title field has been directly modified in the db for the post (espresso_attendees post type) for this |
||
262 | * attendee. |
||
263 | * |
||
264 | * @param bool $apply_html_entities |
||
265 | * @return string |
||
266 | */ |
||
267 | public function ATT_full_name($apply_html_entities = false) |
||
268 | { |
||
269 | return $apply_html_entities |
||
270 | ? htmlentities($this->get('ATT_full_name'), ENT_QUOTES, 'UTF-8') |
||
271 | : $this->get('ATT_full_name'); |
||
272 | } |
||
273 | |||
274 | |||
275 | /** |
||
276 | * get Attendee Last Name |
||
277 | * |
||
278 | * @access public |
||
279 | * @return string |
||
280 | */ |
||
281 | public function lname() |
||
285 | |||
286 | |||
287 | /** |
||
288 | * Gets the attendee's full address as an array so client code can decide hwo to display it |
||
289 | * |
||
290 | * @return array numerically indexed, with each part of the address that is known. |
||
291 | * Eg, if the user only responded to state and country, |
||
292 | * it would be array(0=>'Alabama',1=>'USA') |
||
293 | * @return array |
||
294 | */ |
||
295 | public function full_address_as_array() |
||
296 | { |
||
297 | $full_address_array = array(); |
||
298 | $initial_address_fields = array('ATT_address', 'ATT_address2', 'ATT_city',); |
||
299 | foreach ($initial_address_fields as $address_field_name) { |
||
300 | $address_fields_value = $this->get($address_field_name); |
||
301 | if (! empty($address_fields_value)) { |
||
302 | $full_address_array[] = $address_fields_value; |
||
303 | } |
||
321 | |||
322 | |||
323 | /** |
||
324 | * get Attendee Address |
||
325 | * |
||
326 | * @return string |
||
327 | */ |
||
328 | public function address() |
||
332 | |||
333 | |||
334 | /** |
||
335 | * get Attendee Address2 |
||
336 | * |
||
337 | * @return string |
||
338 | */ |
||
339 | public function address2() |
||
343 | |||
344 | |||
345 | /** |
||
346 | * get Attendee City |
||
347 | * |
||
348 | * @return string |
||
349 | */ |
||
350 | public function city() |
||
354 | |||
355 | |||
356 | /** |
||
357 | * get Attendee State ID |
||
358 | * |
||
359 | * @return string |
||
360 | */ |
||
361 | public function state_ID() |
||
365 | |||
366 | |||
367 | /** |
||
368 | * @return string |
||
369 | */ |
||
370 | public function state_abbrev() |
||
374 | |||
375 | |||
376 | /** |
||
377 | * Gets the state set to this attendee |
||
378 | * |
||
379 | * @return EE_State |
||
380 | */ |
||
381 | public function state_obj() |
||
385 | |||
386 | |||
387 | /** |
||
388 | * Returns the state's name, otherwise 'Unknown' |
||
389 | * |
||
390 | * @return string |
||
391 | */ |
||
392 | public function state_name() |
||
400 | |||
401 | |||
402 | /** |
||
403 | * either displays the state abbreviation or the state name, as determined |
||
404 | * by the "FHEE__EEI_Address__state__use_abbreviation" filter. |
||
405 | * defaults to abbreviation |
||
406 | * |
||
407 | * @return string |
||
408 | */ |
||
409 | public function state() |
||
417 | |||
418 | |||
419 | /** |
||
420 | * get Attendee Country ISO Code |
||
421 | * |
||
422 | * @return string |
||
423 | */ |
||
424 | public function country_ID() |
||
428 | |||
429 | |||
430 | /** |
||
431 | * Gets country set for this attendee |
||
432 | * |
||
433 | * @return EE_Country |
||
434 | */ |
||
435 | public function country_obj() |
||
439 | |||
440 | |||
441 | /** |
||
442 | * Returns the country's name if known, otherwise 'Unknown' |
||
443 | * |
||
444 | * @return string |
||
445 | */ |
||
446 | public function country_name() |
||
454 | |||
455 | |||
456 | /** |
||
457 | * either displays the country ISO2 code or the country name, as determined |
||
458 | * by the "FHEE__EEI_Address__country__use_abbreviation" filter. |
||
459 | * defaults to abbreviation |
||
460 | * |
||
461 | * @return string |
||
462 | */ |
||
463 | public function country() |
||
471 | |||
472 | |||
473 | /** |
||
474 | * get Attendee Zip/Postal Code |
||
475 | * |
||
476 | * @return string |
||
477 | */ |
||
478 | public function zip() |
||
482 | |||
483 | |||
484 | /** |
||
485 | * get Attendee Email Address |
||
486 | * |
||
487 | * @return string |
||
488 | */ |
||
489 | public function email() |
||
493 | |||
494 | |||
495 | /** |
||
496 | * get Attendee Phone # |
||
497 | * |
||
498 | * @return string |
||
499 | */ |
||
500 | public function phone() |
||
504 | |||
505 | |||
506 | /** |
||
507 | * get deleted |
||
508 | * |
||
509 | * @return bool |
||
510 | */ |
||
511 | public function deleted() |
||
515 | |||
516 | |||
517 | /** |
||
518 | * Gets registrations of this attendee |
||
519 | * |
||
520 | * @param array $query_params |
||
521 | * @return EE_Registration[] |
||
522 | */ |
||
523 | public function get_registrations($query_params = array()) |
||
527 | |||
528 | |||
529 | /** |
||
530 | * Gets the most recent registration of this attendee |
||
531 | * |
||
532 | * @return EE_Registration |
||
533 | */ |
||
534 | public function get_most_recent_registration() |
||
539 | |||
540 | |||
541 | /** |
||
542 | * Gets the most recent registration for this attend at this event |
||
543 | * |
||
544 | * @param int $event_id |
||
545 | * @return EE_Registration |
||
546 | */ |
||
547 | public function get_most_recent_registration_for_event($event_id) |
||
552 | |||
553 | |||
554 | /** |
||
555 | * returns any events attached to this attendee ($_Event property); |
||
556 | * |
||
557 | * @return array |
||
558 | */ |
||
559 | public function events() |
||
563 | |||
564 | |||
565 | /** |
||
566 | * Gets the billing info array where keys match espresso_reg_page_billing_inputs(), |
||
567 | * and keys are their cleaned values. @see EE_Attendee::save_and_clean_billing_info_for_payment_method() which was |
||
568 | * used to save the billing info |
||
569 | * |
||
570 | * @param EE_Payment_Method $payment_method the _gateway_name property on the gateway class |
||
571 | * @return EE_Form_Section_Proper|null |
||
572 | */ |
||
573 | public function billing_info_for_payment_method($payment_method) |
||
589 | |||
590 | |||
591 | /** |
||
592 | * Gets the postmeta key that holds this attendee's billing info for the |
||
593 | * specified payment method |
||
594 | * |
||
595 | * @param EE_Payment_Method $payment_method |
||
596 | * @return string |
||
597 | */ |
||
598 | public function get_billing_info_postmeta_name($payment_method) |
||
606 | |||
607 | |||
608 | /** |
||
609 | * Saves the billing info to the attendee. @see EE_Attendee::billing_info_for_payment_method() which is used to |
||
610 | * retrieve it |
||
611 | * |
||
612 | * @param EE_Billing_Attendee_Info_Form $billing_form |
||
613 | * @param EE_Payment_Method $payment_method |
||
614 | * @return boolean |
||
615 | */ |
||
616 | public function save_and_clean_billing_info_for_payment_method($billing_form, $payment_method) |
||
626 | |||
627 | |||
628 | /** |
||
629 | * Return the link to the admin details for the object. |
||
630 | * |
||
631 | * @return string |
||
632 | */ |
||
633 | public function get_admin_details_link() |
||
637 | |||
638 | |||
639 | /** |
||
640 | * Returns the link to the editor for the object. Sometimes this is the same as the details. |
||
641 | * |
||
642 | * @return string |
||
643 | */ |
||
644 | View Code Duplication | public function get_admin_edit_link() |
|
656 | |||
657 | |||
658 | /** |
||
659 | * Returns the link to a settings page for the object. |
||
660 | * |
||
661 | * @return string |
||
662 | */ |
||
663 | public function get_admin_settings_link() |
||
667 | |||
668 | |||
669 | /** |
||
670 | * Returns the link to the "overview" for the object (typically the "list table" view). |
||
671 | * |
||
672 | * @return string |
||
673 | */ |
||
674 | View Code Duplication | public function get_admin_overview_link() |
|
685 | |||
686 | |||
687 | } |
||
688 | |||
691 |
This check looks at variables that have been passed in as parameters and are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.