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 |
||
13 | class EEM_Registration extends EEM_Soft_Delete_Base |
||
14 | { |
||
15 | |||
16 | /** |
||
17 | * @var EEM_Registration $_instance |
||
18 | */ |
||
19 | protected static $_instance; |
||
20 | |||
21 | /** |
||
22 | * Keys are the status IDs for registrations (eg, RAP, RCN, etc), and the values |
||
23 | * are status codes (eg, approved, cancelled, etc) |
||
24 | * |
||
25 | * @var array |
||
26 | */ |
||
27 | private static $_reg_status; |
||
28 | |||
29 | /** |
||
30 | * The value of REG_count for a primary registrant |
||
31 | */ |
||
32 | const PRIMARY_REGISTRANT_COUNT = 1; |
||
33 | |||
34 | /** |
||
35 | * Status ID (STS_ID on esp_status table) to indicate an INCOMPLETE registration. |
||
36 | * Initial status for registrations when they are first created |
||
37 | * Payments are NOT allowed. |
||
38 | * Automatically toggled to whatever the default Event registration status is upon completion of the attendee |
||
39 | * information reg step NO space reserved. Registration is NOT active |
||
40 | */ |
||
41 | const status_id_incomplete = 'RIC'; |
||
42 | |||
43 | /** |
||
44 | * Status ID (STS_ID on esp_status table) to indicate an UNAPPROVED registration. |
||
45 | * Payments are NOT allowed. |
||
46 | * Event Admin must manually toggle STS_ID for it to change |
||
47 | * No space reserved. |
||
48 | * Registration is active |
||
49 | */ |
||
50 | const status_id_not_approved = 'RNA'; |
||
51 | |||
52 | /** |
||
53 | * Status ID (STS_ID on esp_status table) to indicate registration is PENDING_PAYMENT . |
||
54 | * Payments are allowed. |
||
55 | * STS_ID will automatically be toggled to RAP if payment is made in full by the attendee |
||
56 | * No space reserved. |
||
57 | * Registration is active |
||
58 | */ |
||
59 | const status_id_pending_payment = 'RPP'; |
||
60 | |||
61 | /** |
||
62 | * Status ID (STS_ID on esp_status table) to indicate registration is on the WAIT_LIST . |
||
63 | * Payments are allowed. |
||
64 | * STS_ID will automatically be toggled to RAP if payment is made in full by the attendee |
||
65 | * No space reserved. |
||
66 | * Registration is active |
||
67 | */ |
||
68 | const status_id_wait_list = 'RWL'; |
||
69 | |||
70 | /** |
||
71 | * Status ID (STS_ID on esp_status table) to indicate an APPROVED registration. |
||
72 | * the TXN may or may not be completed ( paid in full ) |
||
73 | * Payments are allowed. |
||
74 | * A space IS reserved. |
||
75 | * Registration is active |
||
76 | */ |
||
77 | const status_id_approved = 'RAP'; |
||
78 | |||
79 | /** |
||
80 | * Status ID (STS_ID on esp_status table) to indicate a registration was CANCELLED by the attendee. |
||
81 | * Payments are NOT allowed. |
||
82 | * NO space reserved. |
||
83 | * Registration is NOT active |
||
84 | */ |
||
85 | const status_id_cancelled = 'RCN'; |
||
86 | |||
87 | /** |
||
88 | * Status ID (STS_ID on esp_status table) to indicate a registration was DECLINED by the Event Admin |
||
89 | * Payments are NOT allowed. |
||
90 | * No space reserved. |
||
91 | * Registration is NOT active |
||
92 | */ |
||
93 | const status_id_declined = 'RDC'; |
||
94 | |||
95 | /** |
||
96 | * @var TableAnalysis $table_analysis |
||
97 | */ |
||
98 | protected $_table_analysis; |
||
99 | |||
100 | |||
101 | /** |
||
102 | * private constructor to prevent direct creation |
||
103 | * |
||
104 | * @Constructor |
||
105 | * @access protected |
||
106 | * @param string $timezone string representing the timezone we want to set for returned Date Time Strings (and any |
||
107 | * incoming timezone data that gets saved). Note this just sends the timezone info to the |
||
108 | * date time model field objects. Default is NULL (and will be assumed using the set |
||
109 | * timezone in the 'timezone_string' wp option) |
||
110 | * @throws EE_Error |
||
111 | */ |
||
112 | protected function __construct($timezone = null) |
||
240 | |||
241 | |||
242 | /** |
||
243 | * a list of ALL valid registration statuses currently in use within the system |
||
244 | * generated by combining the filterable active and inactive reg status arrays |
||
245 | * |
||
246 | * @return array |
||
247 | */ |
||
248 | public static function reg_statuses() |
||
257 | |||
258 | |||
259 | /** |
||
260 | * reg_statuses_that_allow_payment |
||
261 | * a filterable list of registration statuses that allow a registrant to make a payment |
||
262 | * |
||
263 | * @access public |
||
264 | * @return array |
||
265 | */ |
||
266 | public static function reg_statuses_that_allow_payment() |
||
276 | |||
277 | |||
278 | /** |
||
279 | * active_reg_statuses |
||
280 | * a filterable list of registration statuses that are considered active |
||
281 | * |
||
282 | * @access public |
||
283 | * @return array |
||
284 | */ |
||
285 | public static function active_reg_statuses() |
||
297 | |||
298 | |||
299 | /** |
||
300 | * inactive_reg_statuses |
||
301 | * a filterable list of registration statuses that are not considered active |
||
302 | * |
||
303 | * @access public |
||
304 | * @return array |
||
305 | */ |
||
306 | public static function inactive_reg_statuses() |
||
317 | |||
318 | |||
319 | /** |
||
320 | * closed_reg_statuses |
||
321 | * a filterable list of registration statuses that are considered "closed" |
||
322 | * meaning they should not be considered in any calculations involving monies owing |
||
323 | * |
||
324 | * @access public |
||
325 | * @return array |
||
326 | */ |
||
327 | public static function closed_reg_statuses() |
||
338 | |||
339 | |||
340 | /** |
||
341 | * get list of registration statuses |
||
342 | * |
||
343 | * @access public |
||
344 | * @param array $exclude The status ids to exclude from the returned results |
||
345 | * @param bool $translated If true will return the values as singular localized strings |
||
346 | * @return array |
||
347 | * @throws EE_Error |
||
348 | */ |
||
349 | public static function reg_status_array($exclude = array(), $translated = false) |
||
356 | |||
357 | |||
358 | /** |
||
359 | * get list of registration statuses |
||
360 | * |
||
361 | * @access private |
||
362 | * @param array $exclude |
||
363 | * @return void |
||
364 | * @throws EE_Error |
||
365 | */ |
||
366 | private function _get_registration_status_array($exclude = array()) |
||
384 | |||
385 | |||
386 | /** |
||
387 | * Gets the injected table analyzer, or throws an exception |
||
388 | * |
||
389 | * @return TableAnalysis |
||
390 | * @throws EE_Error |
||
391 | */ |
||
392 | protected function _get_table_analysis() |
||
404 | |||
405 | |||
406 | /** |
||
407 | * This returns a wpdb->results array of all registration date month and years matching the incoming query params |
||
408 | * and grouped by month and year. |
||
409 | * |
||
410 | * @param array $where_params Array of query_params as described in the comments for EEM_Base::get_all() |
||
411 | * @return array |
||
412 | * @throws EE_Error |
||
413 | */ |
||
414 | public function get_reg_months_and_years($where_params) |
||
425 | |||
426 | |||
427 | /** |
||
428 | * retrieve ALL registrations for a particular Attendee from db |
||
429 | * |
||
430 | * @param int $ATT_ID |
||
431 | * @return EE_Base_Class[]|EE_Registration[]|null |
||
432 | * @throws EE_Error |
||
433 | */ |
||
434 | public function get_all_registrations_for_attendee($ATT_ID = 0) |
||
441 | |||
442 | |||
443 | /** |
||
444 | * Gets a registration given their REG_url_link. Yes, this should usually |
||
445 | * be passed via a GET parameter. |
||
446 | * |
||
447 | * @param string $REG_url_link |
||
448 | * @return EE_Base_Class|EE_Registration|null |
||
449 | * @throws EE_Error |
||
450 | */ |
||
451 | public function get_registration_for_reg_url_link($REG_url_link) |
||
458 | |||
459 | |||
460 | /** |
||
461 | * retrieve registration for a specific transaction attendee from db |
||
462 | * |
||
463 | * @access public |
||
464 | * @param int $TXN_ID |
||
465 | * @param int $ATT_ID |
||
466 | * @param int $att_nmbr in case the ATT_ID is the same for multiple registrations (same details used) then the |
||
467 | * attendee number is required |
||
468 | * @return mixed array on success, FALSE on fail |
||
469 | * @throws EE_Error |
||
470 | */ |
||
471 | public function get_registration_for_transaction_attendee($TXN_ID = 0, $ATT_ID = 0, $att_nmbr = 0) |
||
481 | |||
482 | |||
483 | /** |
||
484 | * get the number of registrations per day for the Registration Admin page Reports Tab. |
||
485 | * (doesn't utilize models because it's a fairly specialized query) |
||
486 | * |
||
487 | * @access public |
||
488 | * @param $period string which can be passed to php's strtotime function (eg "-1 month") |
||
489 | * @return stdClass[] with properties regDate and total |
||
490 | * @throws EE_Error |
||
491 | */ |
||
492 | public function get_registrations_per_day_report($period = '-1 month') |
||
522 | |||
523 | |||
524 | /** |
||
525 | * Get the number of registrations per day including the count of registrations for each Registration Status. |
||
526 | * Note: EEM_Registration::status_id_incomplete registrations are excluded from the results. |
||
527 | * |
||
528 | * @param string $period |
||
529 | * @return stdClass[] with properties Registration_REG_date and a column for each registration status as the STS_ID |
||
530 | * @throws EE_Error |
||
531 | * (i.e. RAP) |
||
532 | */ |
||
533 | View Code Duplication | public function get_registrations_per_day_and_per_status_report($period = '-1 month') |
|
574 | |||
575 | |||
576 | /** |
||
577 | * get the number of registrations per event for the Registration Admin page Reports Tab |
||
578 | * |
||
579 | * @access public |
||
580 | * @param $period string which can be passed to php's strtotime function (eg "-1 month") |
||
581 | * @return stdClass[] each with properties event_name, reg_limit, and total |
||
582 | * @throws EE_Error |
||
583 | */ |
||
584 | public function get_registrations_per_event_report($period = '-1 month') |
||
618 | |||
619 | |||
620 | /** |
||
621 | * Get the number of registrations per event grouped by registration status. |
||
622 | * Note: EEM_Registration::status_id_incomplete registrations are excluded from the results. |
||
623 | * |
||
624 | * @param string $period |
||
625 | * @return stdClass[] with properties `Registration_Event` and a column for each registration status as the STS_ID |
||
626 | * @throws EE_Error |
||
627 | * (i.e. RAP) |
||
628 | */ |
||
629 | View Code Duplication | public function get_registrations_per_event_and_per_status_report($period = '-1 month') |
|
668 | |||
669 | |||
670 | /** |
||
671 | * Returns the EE_Registration of the primary attendee on the transaction id provided |
||
672 | * |
||
673 | * @param int $TXN_ID |
||
674 | * @return EE_Base_Class|EE_Registration|null |
||
675 | * @throws EE_Error |
||
676 | */ |
||
677 | public function get_primary_registration_for_transaction_ID($TXN_ID = 0) |
||
689 | |||
690 | |||
691 | /** |
||
692 | * get_event_registration_count |
||
693 | * |
||
694 | * @access public |
||
695 | * @param int $EVT_ID |
||
696 | * @param boolean $for_incomplete_payments |
||
697 | * @return int |
||
698 | * @throws EE_Error |
||
699 | */ |
||
700 | public function get_event_registration_count($EVT_ID, $for_incomplete_payments = false) |
||
709 | |||
710 | |||
711 | /** |
||
712 | * Deletes all registrations with no transactions. Note that this needs to be very efficient |
||
713 | * and so it uses wpdb directly |
||
714 | * |
||
715 | * @global WPDB $wpdb |
||
716 | * @return int number deleted |
||
717 | * @throws EE_Error |
||
718 | */ |
||
719 | public function delete_registrations_with_no_transaction() |
||
731 | |||
732 | |||
733 | /** |
||
734 | * Count registrations checked into (or out of) a datetime |
||
735 | * |
||
736 | * @param int $DTT_ID datetime ID |
||
737 | * @param boolean $checked_in whether to count registrations checked IN or OUT |
||
738 | * @return int |
||
739 | * @throws EE_Error |
||
740 | */ |
||
741 | public function count_registrations_checked_into_datetime($DTT_ID, $checked_in = true) |
||
765 | |||
766 | |||
767 | /** |
||
768 | * Count registrations checked into (or out of) an event. |
||
769 | * |
||
770 | * @param int $EVT_ID event ID |
||
771 | * @param boolean $checked_in whether to count registrations checked IN or OUT |
||
772 | * @return int |
||
773 | * @throws EE_Error |
||
774 | */ |
||
775 | public function count_registrations_checked_into_event($EVT_ID, $checked_in = true) |
||
801 | |||
802 | |||
803 | /** |
||
804 | * The purpose of this method is to retrieve an array of |
||
805 | * EE_Registration objects that represent the latest registration |
||
806 | * for each ATT_ID given in the function argument. |
||
807 | * |
||
808 | * @param array $attendee_ids |
||
809 | * @return EE_Base_Class[]|EE_Registration[] |
||
810 | * @throws EE_Error |
||
811 | */ |
||
812 | public function get_latest_registration_for_each_of_given_contacts($attendee_ids = array()) |
||
855 | |||
856 | |||
857 | |||
858 | /** |
||
859 | * returns a count of registrations for the supplied event having the status as specified |
||
860 | * |
||
861 | * @param int $EVT_ID |
||
862 | * @param array $statuses |
||
863 | * @return int |
||
864 | * @throws InvalidArgumentException |
||
865 | * @throws InvalidStatusException |
||
866 | * @throws EE_Error |
||
867 | */ |
||
868 | public function event_reg_count_for_statuses($EVT_ID, $statuses = array()) |
||
895 | } |
||
896 |
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.