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 | * reg_statuses_that_allow_payment |
||
194 | * a filterable list of registration statuses that allow a registrant to make a payment |
||
195 | * |
||
196 | * @access public |
||
197 | * @return array |
||
198 | */ |
||
199 | public static function reg_statuses_that_allow_payment() |
||
207 | |||
208 | |||
209 | |||
210 | /** |
||
211 | * inactive_reg_statuses |
||
212 | * a filterable list of registration statuses that are considered active |
||
213 | * |
||
214 | * @access public |
||
215 | * @return array |
||
216 | */ |
||
217 | public static function active_reg_statuses() |
||
226 | |||
227 | |||
228 | |||
229 | /** |
||
230 | * inactive_reg_statuses |
||
231 | * a filterable list of registration statuses that are not considered active |
||
232 | * |
||
233 | * @access public |
||
234 | * @return array |
||
235 | */ |
||
236 | public static function inactive_reg_statuses() |
||
244 | |||
245 | |||
246 | |||
247 | /** |
||
248 | * closed_reg_statuses |
||
249 | * a filterable list of registration statuses that are considered "closed" |
||
250 | * meaning they should not be considered in any calculations involving monies owing |
||
251 | * |
||
252 | * @access public |
||
253 | * @return array |
||
254 | */ |
||
255 | public static function closed_reg_statuses() |
||
262 | |||
263 | |||
264 | |||
265 | /** |
||
266 | * get list of registration statuses |
||
267 | * |
||
268 | * @access public |
||
269 | * @param array $exclude The status ids to exclude from the returned results |
||
270 | * @param bool $translated If true will return the values as singular localized strings |
||
271 | * @return array |
||
272 | */ |
||
273 | public static function reg_status_array($exclude = array(), $translated = false) |
||
279 | |||
280 | |||
281 | |||
282 | /** |
||
283 | * get list of registration statuses |
||
284 | * |
||
285 | * @access private |
||
286 | * @param array $exclude |
||
287 | * @return array |
||
288 | */ |
||
289 | private function _get_registration_status_array($exclude = array()) |
||
307 | |||
308 | |||
309 | |||
310 | /** |
||
311 | * Gets the injected table analyzer, or throws an exception |
||
312 | * |
||
313 | * @return TableAnalysis |
||
314 | * @throws \EE_Error |
||
315 | */ |
||
316 | View Code Duplication | protected function _get_table_analysis() |
|
325 | |||
326 | |||
327 | |||
328 | /** |
||
329 | * This returns a wpdb->results array of all registration date month and years matching the incoming query params |
||
330 | * and grouped by month and year. |
||
331 | * |
||
332 | * @param array $where_params Array of query_params as described in the comments for EEM_Base::get_all() |
||
333 | * @return array |
||
334 | * @throws \EE_Error |
||
335 | */ |
||
336 | public function get_reg_months_and_years($where_params) |
||
347 | |||
348 | |||
349 | |||
350 | /** |
||
351 | * retrieve ALL registrations for a particular Attendee from db |
||
352 | * |
||
353 | * @access public |
||
354 | * @param int $ATT_ID |
||
355 | * @return EE_Registration[] |
||
356 | */ |
||
357 | public function get_all_registrations_for_attendee($ATT_ID = 0) |
||
364 | |||
365 | |||
366 | |||
367 | /** |
||
368 | * Gets a registration given their REG_url_link. Yes, this should usually |
||
369 | * be passed via a GET parameter. |
||
370 | * |
||
371 | * @param string $REG_url_link |
||
372 | * @return EE_Registration |
||
373 | */ |
||
374 | public function get_registration_for_reg_url_link($REG_url_link) |
||
381 | |||
382 | |||
383 | |||
384 | /** |
||
385 | * retrieve registration for a specific transaction attendee from db |
||
386 | * |
||
387 | * @access public |
||
388 | * @param int $TXN_ID |
||
389 | * @param int $ATT_ID |
||
390 | * @param int $att_nmbr in case the ATT_ID is the same for multiple registrations (same details used) then the |
||
391 | * attendee number is required |
||
392 | * @return mixed array on success, FALSE on fail |
||
393 | */ |
||
394 | public function get_registration_for_transaction_attendee($TXN_ID = 0, $ATT_ID = 0, $att_nmbr = 0) |
||
404 | |||
405 | |||
406 | |||
407 | /** |
||
408 | * get the number of registrations per day for the Registration Admin page Reports Tab. |
||
409 | * (doesn't utilize models because it's a fairly specialized query) |
||
410 | * |
||
411 | * @access public |
||
412 | * @param $period string which can be passed to php's strtotime function (eg "-1 month") |
||
413 | * @return stdClass[] with properties regDate and total |
||
414 | */ |
||
415 | public function get_registrations_per_day_report($period = '-1 month') |
||
437 | |||
438 | |||
439 | |||
440 | /** |
||
441 | * Get the number of registrations per day including the count of registrations for each Registration Status. |
||
442 | * Note: EEM_Registration::status_id_incomplete registrations are excluded from the results. |
||
443 | * |
||
444 | * @param string $period |
||
445 | * @return stdClass[] with properties Registration_REG_date and a column for each registration status as the STS_ID |
||
446 | * (i.e. RAP) |
||
447 | */ |
||
448 | View Code Duplication | public function get_registrations_per_day_and_per_status_report($period = '-1 month') |
|
490 | |||
491 | |||
492 | |||
493 | /** |
||
494 | * get the number of registrations per event for the Registration Admin page Reports Tab |
||
495 | * |
||
496 | * @access public |
||
497 | * @param $period string which can be passed to php's strtotime function (eg "-1 month") |
||
498 | * @return stdClass[] each with properties event_name, reg_limit, and total |
||
499 | */ |
||
500 | public function get_registrations_per_event_report($period = '-1 month') |
||
522 | |||
523 | |||
524 | |||
525 | /** |
||
526 | * Get the number of registrations per event grouped by registration status. |
||
527 | * Note: EEM_Registration::status_id_incomplete registrations are excluded from the results. |
||
528 | * |
||
529 | * @param string $period |
||
530 | * @return stdClass[] with properties `Registration_Event` and a column for each registration status as the STS_ID |
||
531 | * (i.e. RAP) |
||
532 | */ |
||
533 | View Code Duplication | public function get_registrations_per_event_and_per_status_report($period = '-1 month') |
|
573 | |||
574 | |||
575 | |||
576 | /** |
||
577 | * Returns the EE_Registration of the primary attendee on the transaction id provided |
||
578 | * |
||
579 | * @param int $TXN_ID |
||
580 | * @return EE_Registration |
||
581 | */ |
||
582 | public function get_primary_registration_for_transaction_ID($TXN_ID = 0) |
||
594 | |||
595 | |||
596 | |||
597 | /** |
||
598 | * get_event_registration_count |
||
599 | * |
||
600 | * @access public |
||
601 | * @param int $EVT_ID |
||
602 | * @param boolean $for_incomplete_payments |
||
603 | * @return int |
||
604 | */ |
||
605 | public function get_event_registration_count($EVT_ID, $for_incomplete_payments = false) |
||
614 | |||
615 | |||
616 | |||
617 | /** |
||
618 | * Deletes all registrations with no transactions. Note that this needs to be very efficient |
||
619 | * and so it uses wpdb directly |
||
620 | * |
||
621 | * @global WPDB $wpdb |
||
622 | * @return int number deleted |
||
623 | */ |
||
624 | public function delete_registrations_with_no_transaction() |
||
634 | |||
635 | |||
636 | |||
637 | /** |
||
638 | * Count registrations checked into (or out of) a datetime |
||
639 | * |
||
640 | * @param int $DTT_ID datetime ID |
||
641 | * @param boolean $checked_in whether to count registrations checked IN or OUT |
||
642 | * @return int |
||
643 | */ |
||
644 | public function count_registrations_checked_into_datetime($DTT_ID, $checked_in = true) |
||
669 | |||
670 | |||
671 | |||
672 | /** |
||
673 | * Count registrations checked into (or out of) an event. |
||
674 | * |
||
675 | * @param int $EVT_ID event ID |
||
676 | * @param boolean $checked_in whether to count registrations checked IN or OUT |
||
677 | * @return int |
||
678 | */ |
||
679 | public function count_registrations_checked_into_event($EVT_ID, $checked_in = true) |
||
708 | |||
709 | |||
710 | |||
711 | /** |
||
712 | * The purpose of this method is to retrieve an array of |
||
713 | * EE_Registration objects that represent the latest registration |
||
714 | * for each ATT_ID given in the function argument. |
||
715 | * |
||
716 | * @param array $attendee_ids |
||
717 | * @return EE_Registration[] |
||
718 | */ |
||
719 | public function get_latest_registration_for_each_of_given_contacts($attendee_ids = array()) |
||
760 | |||
761 | |||
762 | |||
763 | } |
||
764 | // End of file EEM_Registration.model.php |
||
766 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..