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 |
||
17 | class EEM_Registration extends EEM_Soft_Delete_Base |
||
18 | { |
||
19 | |||
20 | // private instance of the Registration object |
||
21 | protected static $_instance = null; |
||
22 | |||
23 | /** |
||
24 | * Keys are the status IDs for registrations (eg, RAP, RCN, etc), and the values |
||
25 | * are status codes (eg, approved, cancelled, etc) |
||
26 | * |
||
27 | * @var array |
||
28 | */ |
||
29 | private static $_reg_status; |
||
30 | |||
31 | /** |
||
32 | * The value of REG_count for a primary registrant |
||
33 | */ |
||
34 | const PRIMARY_REGISTRANT_COUNT = 1; |
||
35 | |||
36 | /** |
||
37 | * Status ID (STS_ID on esp_status table) to indicate an INCOMPLETE registration. |
||
38 | * Initial status for registrations when they are first created |
||
39 | * Payments are NOT allowed. |
||
40 | * Automatically toggled to whatever the default Event registration status is upon completion of the attendee |
||
41 | * information reg step NO space reserved. Registration is NOT active |
||
42 | */ |
||
43 | const status_id_incomplete = 'RIC'; |
||
44 | |||
45 | /** |
||
46 | * Status ID (STS_ID on esp_status table) to indicate an UNAPPROVED registration. |
||
47 | * Payments are NOT allowed. |
||
48 | * Event Admin must manually toggle STS_ID for it to change |
||
49 | * No space reserved. |
||
50 | * Registration is active |
||
51 | */ |
||
52 | const status_id_not_approved = 'RNA'; |
||
53 | |||
54 | /** |
||
55 | * Status ID (STS_ID on esp_status table) to indicate registration is PENDING_PAYMENT . |
||
56 | * Payments are allowed. |
||
57 | * STS_ID will automatically be toggled to RAP if payment is made in full by the attendee |
||
58 | * No space reserved. |
||
59 | * Registration is active |
||
60 | */ |
||
61 | const status_id_pending_payment = 'RPP'; |
||
62 | |||
63 | /** |
||
64 | * Status ID (STS_ID on esp_status table) to indicate registration is on the WAIT_LIST . |
||
65 | * Payments are allowed. |
||
66 | * STS_ID will automatically be toggled to RAP if payment is made in full by the attendee |
||
67 | * No space reserved. |
||
68 | * Registration is active |
||
69 | */ |
||
70 | const status_id_wait_list = 'RWL'; |
||
71 | |||
72 | /** |
||
73 | * Status ID (STS_ID on esp_status table) to indicate an APPROVED registration. |
||
74 | * the TXN may or may not be completed ( paid in full ) |
||
75 | * Payments are allowed. |
||
76 | * A space IS reserved. |
||
77 | * Registration is active |
||
78 | */ |
||
79 | const status_id_approved = 'RAP'; |
||
80 | |||
81 | /** |
||
82 | * Status ID (STS_ID on esp_status table) to indicate a registration was CANCELLED by the attendee. |
||
83 | * Payments are NOT allowed. |
||
84 | * NO space reserved. |
||
85 | * Registration is NOT active |
||
86 | */ |
||
87 | const status_id_cancelled = 'RCN'; |
||
88 | |||
89 | /** |
||
90 | * Status ID (STS_ID on esp_status table) to indicate a registration was DECLINED by the Event Admin |
||
91 | * Payments are NOT allowed. |
||
92 | * No space reserved. |
||
93 | * Registration is NOT active |
||
94 | */ |
||
95 | const status_id_declined = 'RDC'; |
||
96 | |||
97 | /** |
||
98 | * @var TableAnalysis $table_analysis |
||
99 | */ |
||
100 | protected $_table_analysis; |
||
101 | |||
102 | |||
103 | |||
104 | /** |
||
105 | * private constructor to prevent direct creation |
||
106 | * |
||
107 | * @Constructor |
||
108 | * @access protected |
||
109 | * @param string $timezone string representing the timezone we want to set for returned Date Time Strings (and any |
||
110 | * incoming timezone data that gets saved). Note this just sends the timezone info to the |
||
111 | * date time model field objects. Default is NULL (and will be assumed using the set |
||
112 | * timezone in the 'timezone_string' wp option) |
||
113 | */ |
||
114 | protected function __construct($timezone = null) |
||
189 | |||
190 | |||
191 | |||
192 | /** |
||
193 | * a filterable list of registration statuses |
||
194 | * |
||
195 | * @return array |
||
196 | */ |
||
197 | public static function reg_statuses() |
||
212 | |||
213 | |||
214 | |||
215 | /** |
||
216 | * reg_statuses_that_allow_payment |
||
217 | * a filterable list of registration statuses that allow a registrant to make a payment |
||
218 | * |
||
219 | * @access public |
||
220 | * @return array |
||
221 | */ |
||
222 | public static function reg_statuses_that_allow_payment() |
||
232 | |||
233 | |||
234 | |||
235 | /** |
||
236 | * active_reg_statuses |
||
237 | * a filterable list of registration statuses that are considered active |
||
238 | * |
||
239 | * @access public |
||
240 | * @return array |
||
241 | */ |
||
242 | public static function active_reg_statuses() |
||
254 | |||
255 | |||
256 | |||
257 | /** |
||
258 | * inactive_reg_statuses |
||
259 | * a filterable list of registration statuses that are not considered active |
||
260 | * |
||
261 | * @access public |
||
262 | * @return array |
||
263 | */ |
||
264 | public static function inactive_reg_statuses() |
||
275 | |||
276 | |||
277 | |||
278 | /** |
||
279 | * closed_reg_statuses |
||
280 | * a filterable list of registration statuses that are considered "closed" |
||
281 | * meaning they should not be considered in any calculations involving monies owing |
||
282 | * |
||
283 | * @access public |
||
284 | * @return array |
||
285 | */ |
||
286 | public static function closed_reg_statuses() |
||
297 | |||
298 | |||
299 | |||
300 | /** |
||
301 | * get list of registration statuses |
||
302 | * |
||
303 | * @access public |
||
304 | * @param array $exclude The status ids to exclude from the returned results |
||
305 | * @param bool $translated If true will return the values as singular localized strings |
||
306 | * @return array |
||
307 | */ |
||
308 | public static function reg_status_array($exclude = array(), $translated = false) |
||
314 | |||
315 | |||
316 | |||
317 | /** |
||
318 | * get list of registration statuses |
||
319 | * |
||
320 | * @access private |
||
321 | * @param array $exclude |
||
322 | * @return array |
||
323 | */ |
||
324 | private function _get_registration_status_array($exclude = array()) |
||
342 | |||
343 | |||
344 | |||
345 | /** |
||
346 | * Gets the injected table analyzer, or throws an exception |
||
347 | * |
||
348 | * @return TableAnalysis |
||
349 | * @throws \EE_Error |
||
350 | */ |
||
351 | View Code Duplication | protected function _get_table_analysis() |
|
364 | |||
365 | |||
366 | |||
367 | /** |
||
368 | * This returns a wpdb->results array of all registration date month and years matching the incoming query params |
||
369 | * and grouped by month and year. |
||
370 | * |
||
371 | * @param array $where_params Array of query_params as described in the comments for EEM_Base::get_all() |
||
372 | * @return array |
||
373 | * @throws \EE_Error |
||
374 | */ |
||
375 | public function get_reg_months_and_years($where_params) |
||
386 | |||
387 | |||
388 | |||
389 | /** |
||
390 | * retrieve ALL registrations for a particular Attendee from db |
||
391 | * |
||
392 | * @access public |
||
393 | * @param int $ATT_ID |
||
394 | * @return EE_Registration[] |
||
395 | */ |
||
396 | public function get_all_registrations_for_attendee($ATT_ID = 0) |
||
403 | |||
404 | |||
405 | |||
406 | /** |
||
407 | * Gets a registration given their REG_url_link. Yes, this should usually |
||
408 | * be passed via a GET parameter. |
||
409 | * |
||
410 | * @param string $REG_url_link |
||
411 | * @return EE_Registration |
||
412 | */ |
||
413 | public function get_registration_for_reg_url_link($REG_url_link) |
||
420 | |||
421 | |||
422 | |||
423 | /** |
||
424 | * retrieve registration for a specific transaction attendee from db |
||
425 | * |
||
426 | * @access public |
||
427 | * @param int $TXN_ID |
||
428 | * @param int $ATT_ID |
||
429 | * @param int $att_nmbr in case the ATT_ID is the same for multiple registrations (same details used) then the |
||
430 | * attendee number is required |
||
431 | * @return mixed array on success, FALSE on fail |
||
432 | */ |
||
433 | public function get_registration_for_transaction_attendee($TXN_ID = 0, $ATT_ID = 0, $att_nmbr = 0) |
||
443 | |||
444 | |||
445 | |||
446 | /** |
||
447 | * get the number of registrations per day for the Registration Admin page Reports Tab. |
||
448 | * (doesn't utilize models because it's a fairly specialized query) |
||
449 | * |
||
450 | * @access public |
||
451 | * @param $period string which can be passed to php's strtotime function (eg "-1 month") |
||
452 | * @return stdClass[] with properties regDate and total |
||
453 | */ |
||
454 | public function get_registrations_per_day_report($period = '-1 month') |
||
479 | |||
480 | |||
481 | |||
482 | /** |
||
483 | * Get the number of registrations per day including the count of registrations for each Registration Status. |
||
484 | * Note: EEM_Registration::status_id_incomplete registrations are excluded from the results. |
||
485 | * |
||
486 | * @param string $period |
||
487 | * @return stdClass[] with properties Registration_REG_date and a column for each registration status as the STS_ID |
||
488 | * (i.e. RAP) |
||
489 | */ |
||
490 | View Code Duplication | public function get_registrations_per_day_and_per_status_report($period = '-1 month') |
|
532 | |||
533 | |||
534 | |||
535 | /** |
||
536 | * get the number of registrations per event for the Registration Admin page Reports Tab |
||
537 | * |
||
538 | * @access public |
||
539 | * @param $period string which can be passed to php's strtotime function (eg "-1 month") |
||
540 | * @return stdClass[] each with properties event_name, reg_limit, and total |
||
541 | */ |
||
542 | public function get_registrations_per_event_report($period = '-1 month') |
||
567 | |||
568 | |||
569 | |||
570 | /** |
||
571 | * Get the number of registrations per event grouped by registration status. |
||
572 | * Note: EEM_Registration::status_id_incomplete registrations are excluded from the results. |
||
573 | * |
||
574 | * @param string $period |
||
575 | * @return stdClass[] with properties `Registration_Event` and a column for each registration status as the STS_ID |
||
576 | * (i.e. RAP) |
||
577 | */ |
||
578 | View Code Duplication | public function get_registrations_per_event_and_per_status_report($period = '-1 month') |
|
618 | |||
619 | |||
620 | |||
621 | /** |
||
622 | * Returns the EE_Registration of the primary attendee on the transaction id provided |
||
623 | * |
||
624 | * @param int $TXN_ID |
||
625 | * @return EE_Registration |
||
626 | */ |
||
627 | public function get_primary_registration_for_transaction_ID($TXN_ID = 0) |
||
639 | |||
640 | |||
641 | |||
642 | /** |
||
643 | * get_event_registration_count |
||
644 | * |
||
645 | * @access public |
||
646 | * @param int $EVT_ID |
||
647 | * @param boolean $for_incomplete_payments |
||
648 | * @return int |
||
649 | */ |
||
650 | public function get_event_registration_count($EVT_ID, $for_incomplete_payments = false) |
||
659 | |||
660 | |||
661 | |||
662 | /** |
||
663 | * Deletes all registrations with no transactions. Note that this needs to be very efficient |
||
664 | * and so it uses wpdb directly |
||
665 | * |
||
666 | * @global WPDB $wpdb |
||
667 | * @return int number deleted |
||
668 | */ |
||
669 | public function delete_registrations_with_no_transaction() |
||
680 | |||
681 | |||
682 | |||
683 | /** |
||
684 | * Count registrations checked into (or out of) a datetime |
||
685 | * |
||
686 | * @param int $DTT_ID datetime ID |
||
687 | * @param boolean $checked_in whether to count registrations checked IN or OUT |
||
688 | * @return int |
||
689 | */ |
||
690 | public function count_registrations_checked_into_datetime($DTT_ID, $checked_in = true) |
||
714 | |||
715 | |||
716 | |||
717 | /** |
||
718 | * Count registrations checked into (or out of) an event. |
||
719 | * |
||
720 | * @param int $EVT_ID event ID |
||
721 | * @param boolean $checked_in whether to count registrations checked IN or OUT |
||
722 | * @return int |
||
723 | */ |
||
724 | public function count_registrations_checked_into_event($EVT_ID, $checked_in = true) |
||
750 | |||
751 | |||
752 | |||
753 | /** |
||
754 | * The purpose of this method is to retrieve an array of |
||
755 | * EE_Registration objects that represent the latest registration |
||
756 | * for each ATT_ID given in the function argument. |
||
757 | * |
||
758 | * @param array $attendee_ids |
||
759 | * @return EE_Registration[] |
||
760 | */ |
||
761 | public function get_latest_registration_for_each_of_given_contacts($attendee_ids = array()) |
||
804 | |||
805 | |||
806 | |||
807 | /** |
||
808 | * returns a count of registrations for the supplied event having the status as specified |
||
809 | * |
||
810 | * @param EE_Event $event |
||
811 | * @param string $status |
||
812 | * @return int |
||
813 | * @throws EE_Error |
||
814 | */ |
||
815 | public function event_reg_count_for_status(EE_Event $event, $status = EEM_Registration::status_id_approved) |
||
835 | |||
836 | } |
||
837 | // End of file EEM_Registration.model.php |
||
839 |
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.