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 EEM_Registration 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 EEM_Registration, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
18 | class EEM_Registration extends EEM_Soft_Delete_Base |
||
19 | { |
||
20 | |||
21 | /** |
||
22 | * @var EEM_Registration $_instance |
||
23 | */ |
||
24 | protected static $_instance; |
||
25 | |||
26 | /** |
||
27 | * Keys are the status IDs for registrations (eg, RAP, RCN, etc), and the values |
||
28 | * are status codes (eg, approved, cancelled, etc) |
||
29 | * |
||
30 | * @var array |
||
31 | */ |
||
32 | private static $_reg_status; |
||
33 | |||
34 | /** |
||
35 | * The value of REG_count for a primary registrant |
||
36 | */ |
||
37 | const PRIMARY_REGISTRANT_COUNT = 1; |
||
38 | |||
39 | /** |
||
40 | * Status ID (STS_ID on esp_status table) to indicate an INCOMPLETE registration. |
||
41 | * Initial status for registrations when they are first created |
||
42 | * Payments are NOT allowed. |
||
43 | * Automatically toggled to whatever the default Event registration status is upon completion of the attendee |
||
44 | * information reg step NO space reserved. Registration is NOT active |
||
45 | */ |
||
46 | const status_id_incomplete = 'RIC'; |
||
47 | |||
48 | /** |
||
49 | * Status ID (STS_ID on esp_status table) to indicate an UNAPPROVED registration. |
||
50 | * Payments are NOT allowed. |
||
51 | * Event Admin must manually toggle STS_ID for it to change |
||
52 | * No space reserved. |
||
53 | * Registration is active |
||
54 | */ |
||
55 | const status_id_not_approved = 'RNA'; |
||
56 | |||
57 | /** |
||
58 | * Status ID (STS_ID on esp_status table) to indicate registration is PENDING_PAYMENT . |
||
59 | * Payments are allowed. |
||
60 | * STS_ID will automatically be toggled to RAP if payment is made in full by the attendee |
||
61 | * No space reserved. |
||
62 | * Registration is active |
||
63 | */ |
||
64 | const status_id_pending_payment = 'RPP'; |
||
65 | |||
66 | /** |
||
67 | * Status ID (STS_ID on esp_status table) to indicate registration is on the WAIT_LIST . |
||
68 | * Payments are allowed. |
||
69 | * STS_ID will automatically be toggled to RAP if payment is made in full by the attendee |
||
70 | * No space reserved. |
||
71 | * Registration is active |
||
72 | */ |
||
73 | const status_id_wait_list = 'RWL'; |
||
74 | |||
75 | /** |
||
76 | * Status ID (STS_ID on esp_status table) to indicate an APPROVED registration. |
||
77 | * the TXN may or may not be completed ( paid in full ) |
||
78 | * Payments are allowed. |
||
79 | * A space IS reserved. |
||
80 | * Registration is active |
||
81 | */ |
||
82 | const status_id_approved = 'RAP'; |
||
83 | |||
84 | /** |
||
85 | * Status ID (STS_ID on esp_status table) to indicate a registration was CANCELLED by the attendee. |
||
86 | * Payments are NOT allowed. |
||
87 | * NO space reserved. |
||
88 | * Registration is NOT active |
||
89 | */ |
||
90 | const status_id_cancelled = 'RCN'; |
||
91 | |||
92 | /** |
||
93 | * Status ID (STS_ID on esp_status table) to indicate a registration was DECLINED by the Event Admin |
||
94 | * Payments are NOT allowed. |
||
95 | * No space reserved. |
||
96 | * Registration is NOT active |
||
97 | */ |
||
98 | const status_id_declined = 'RDC'; |
||
99 | |||
100 | /** |
||
101 | * @var TableAnalysis $table_analysis |
||
102 | */ |
||
103 | protected $_table_analysis; |
||
104 | |||
105 | |||
106 | /** |
||
107 | * private constructor to prevent direct creation |
||
108 | * |
||
109 | * @Constructor |
||
110 | * @access protected |
||
111 | * @param string $timezone string representing the timezone we want to set for returned Date Time Strings (and any |
||
112 | * incoming timezone data that gets saved). Note this just sends the timezone info to the |
||
113 | * date time model field objects. Default is NULL (and will be assumed using the set |
||
114 | * timezone in the 'timezone_string' wp option) |
||
115 | * @throws EE_Error |
||
116 | */ |
||
117 | protected function __construct($timezone = null) |
||
242 | |||
243 | |||
244 | /** |
||
245 | * a list of ALL valid registration statuses currently in use within the system |
||
246 | * generated by combining the filterable active and inactive reg status arrays |
||
247 | * |
||
248 | * @return array |
||
249 | */ |
||
250 | public static function reg_statuses() |
||
259 | |||
260 | |||
261 | /** |
||
262 | * reg_statuses_that_allow_payment |
||
263 | * a filterable list of registration statuses that allow a registrant to make a payment |
||
264 | * |
||
265 | * @access public |
||
266 | * @return array |
||
267 | */ |
||
268 | public static function reg_statuses_that_allow_payment() |
||
278 | |||
279 | |||
280 | /** |
||
281 | * active_reg_statuses |
||
282 | * a filterable list of registration statuses that are considered active |
||
283 | * |
||
284 | * @access public |
||
285 | * @return array |
||
286 | */ |
||
287 | public static function active_reg_statuses() |
||
299 | |||
300 | |||
301 | /** |
||
302 | * inactive_reg_statuses |
||
303 | * a filterable list of registration statuses that are not considered active |
||
304 | * |
||
305 | * @access public |
||
306 | * @return array |
||
307 | */ |
||
308 | public static function inactive_reg_statuses() |
||
319 | |||
320 | |||
321 | /** |
||
322 | * closed_reg_statuses |
||
323 | * a filterable list of registration statuses that are considered "closed" |
||
324 | * meaning they should not be considered in any calculations involving monies owing |
||
325 | * |
||
326 | * @access public |
||
327 | * @return array |
||
328 | */ |
||
329 | public static function closed_reg_statuses() |
||
340 | |||
341 | |||
342 | /** |
||
343 | * get list of registration statuses |
||
344 | * |
||
345 | * @access public |
||
346 | * @param array $exclude The status ids to exclude from the returned results |
||
347 | * @param bool $translated If true will return the values as singular localized strings |
||
348 | * @return array |
||
349 | * @throws EE_Error |
||
350 | */ |
||
351 | public static function reg_status_array($exclude = array(), $translated = false) |
||
358 | |||
359 | |||
360 | /** |
||
361 | * get list of registration statuses |
||
362 | * |
||
363 | * @access private |
||
364 | * @param array $exclude |
||
365 | * @return void |
||
366 | * @throws EE_Error |
||
367 | */ |
||
368 | private function _get_registration_status_array($exclude = array()) |
||
386 | |||
387 | |||
388 | /** |
||
389 | * Gets the injected table analyzer, or throws an exception |
||
390 | * |
||
391 | * @return TableAnalysis |
||
392 | * @throws EE_Error |
||
393 | */ |
||
394 | View Code Duplication | protected function _get_table_analysis() |
|
406 | |||
407 | |||
408 | /** |
||
409 | * This returns a wpdb->results array of all registration date month and years matching the incoming query params |
||
410 | * and grouped by month and year. |
||
411 | * |
||
412 | * @param array $where_params Array of query_params as described in the comments for EEM_Base::get_all() |
||
413 | * @return array |
||
414 | * @throws EE_Error |
||
415 | */ |
||
416 | public function get_reg_months_and_years($where_params) |
||
427 | |||
428 | |||
429 | /** |
||
430 | * retrieve ALL registrations for a particular Attendee from db |
||
431 | * |
||
432 | * @param int $ATT_ID |
||
433 | * @return EE_Base_Class[]|EE_Registration[]|null |
||
434 | * @throws EE_Error |
||
435 | */ |
||
436 | public function get_all_registrations_for_attendee($ATT_ID = 0) |
||
443 | |||
444 | |||
445 | /** |
||
446 | * Gets a registration given their REG_url_link. Yes, this should usually |
||
447 | * be passed via a GET parameter. |
||
448 | * |
||
449 | * @param string $REG_url_link |
||
450 | * @return EE_Base_Class|EE_Registration|null |
||
451 | * @throws EE_Error |
||
452 | */ |
||
453 | public function get_registration_for_reg_url_link($REG_url_link) |
||
460 | |||
461 | |||
462 | /** |
||
463 | * retrieve registration for a specific transaction attendee from db |
||
464 | * |
||
465 | * @access public |
||
466 | * @param int $TXN_ID |
||
467 | * @param int $ATT_ID |
||
468 | * @param int $att_nmbr in case the ATT_ID is the same for multiple registrations (same details used) then the |
||
469 | * attendee number is required |
||
470 | * @return mixed array on success, FALSE on fail |
||
471 | * @throws EE_Error |
||
472 | */ |
||
473 | public function get_registration_for_transaction_attendee($TXN_ID = 0, $ATT_ID = 0, $att_nmbr = 0) |
||
483 | |||
484 | |||
485 | /** |
||
486 | * get the number of registrations per day for the Registration Admin page Reports Tab. |
||
487 | * (doesn't utilize models because it's a fairly specialized query) |
||
488 | * |
||
489 | * @access public |
||
490 | * @param $period string which can be passed to php's strtotime function (eg "-1 month") |
||
491 | * @return stdClass[] with properties regDate and total |
||
492 | * @throws EE_Error |
||
493 | */ |
||
494 | public function get_registrations_per_day_report($period = '-1 month') |
||
519 | |||
520 | |||
521 | /** |
||
522 | * Get the number of registrations per day including the count of registrations for each Registration Status. |
||
523 | * Note: EEM_Registration::status_id_incomplete registrations are excluded from the results. |
||
524 | * |
||
525 | * @param string $period |
||
526 | * @return stdClass[] with properties Registration_REG_date and a column for each registration status as the STS_ID |
||
527 | * @throws EE_Error |
||
528 | * (i.e. RAP) |
||
529 | */ |
||
530 | View Code Duplication | public function get_registrations_per_day_and_per_status_report($period = '-1 month') |
|
571 | |||
572 | |||
573 | /** |
||
574 | * get the number of registrations per event for the Registration Admin page Reports Tab |
||
575 | * |
||
576 | * @access public |
||
577 | * @param $period string which can be passed to php's strtotime function (eg "-1 month") |
||
578 | * @return stdClass[] each with properties event_name, reg_limit, and total |
||
579 | * @throws EE_Error |
||
580 | */ |
||
581 | public function get_registrations_per_event_report($period = '-1 month') |
||
611 | |||
612 | |||
613 | /** |
||
614 | * Get the number of registrations per event grouped by registration status. |
||
615 | * Note: EEM_Registration::status_id_incomplete registrations are excluded from the results. |
||
616 | * |
||
617 | * @param string $period |
||
618 | * @return stdClass[] with properties `Registration_Event` and a column for each registration status as the STS_ID |
||
619 | * @throws EE_Error |
||
620 | * (i.e. RAP) |
||
621 | */ |
||
622 | View Code Duplication | public function get_registrations_per_event_and_per_status_report($period = '-1 month') |
|
661 | |||
662 | |||
663 | /** |
||
664 | * Returns the EE_Registration of the primary attendee on the transaction id provided |
||
665 | * |
||
666 | * @param int $TXN_ID |
||
667 | * @return EE_Base_Class|EE_Registration|null |
||
668 | * @throws EE_Error |
||
669 | */ |
||
670 | public function get_primary_registration_for_transaction_ID($TXN_ID = 0) |
||
682 | |||
683 | |||
684 | /** |
||
685 | * get_event_registration_count |
||
686 | * |
||
687 | * @access public |
||
688 | * @param int $EVT_ID |
||
689 | * @param boolean $for_incomplete_payments |
||
690 | * @return int |
||
691 | * @throws EE_Error |
||
692 | */ |
||
693 | public function get_event_registration_count($EVT_ID, $for_incomplete_payments = false) |
||
702 | |||
703 | |||
704 | /** |
||
705 | * Deletes all registrations with no transactions. Note that this needs to be very efficient |
||
706 | * and so it uses wpdb directly |
||
707 | * |
||
708 | * @global WPDB $wpdb |
||
709 | * @return int number deleted |
||
710 | * @throws EE_Error |
||
711 | */ |
||
712 | public function delete_registrations_with_no_transaction() |
||
723 | |||
724 | |||
725 | /** |
||
726 | * Count registrations checked into (or out of) a datetime |
||
727 | * |
||
728 | * @param int $DTT_ID datetime ID |
||
729 | * @param boolean $checked_in whether to count registrations checked IN or OUT |
||
730 | * @return int |
||
731 | * @throws EE_Error |
||
732 | */ |
||
733 | public function count_registrations_checked_into_datetime($DTT_ID, $checked_in = true) |
||
757 | |||
758 | |||
759 | /** |
||
760 | * Count registrations checked into (or out of) an event. |
||
761 | * |
||
762 | * @param int $EVT_ID event ID |
||
763 | * @param boolean $checked_in whether to count registrations checked IN or OUT |
||
764 | * @return int |
||
765 | * @throws EE_Error |
||
766 | */ |
||
767 | public function count_registrations_checked_into_event($EVT_ID, $checked_in = true) |
||
793 | |||
794 | |||
795 | /** |
||
796 | * The purpose of this method is to retrieve an array of |
||
797 | * EE_Registration objects that represent the latest registration |
||
798 | * for each ATT_ID given in the function argument. |
||
799 | * |
||
800 | * @param array $attendee_ids |
||
801 | * @return EE_Base_Class[]|EE_Registration[] |
||
802 | * @throws EE_Error |
||
803 | */ |
||
804 | public function get_latest_registration_for_each_of_given_contacts($attendee_ids = array()) |
||
847 | |||
848 | |||
849 | |||
850 | /** |
||
851 | * returns a count of registrations for the supplied event having the status as specified |
||
852 | * |
||
853 | * @param int $EVT_ID |
||
854 | * @param array $statuses |
||
855 | * @return int |
||
856 | * @throws InvalidArgumentException |
||
857 | * @throws InvalidStatusException |
||
858 | * @throws EE_Error |
||
859 | */ |
||
860 | public function event_reg_count_for_statuses($EVT_ID, $statuses = array() ) |
||
887 | |||
888 | } |
||
889 | // End of file EEM_Registration.model.php |
||
891 |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.