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 |
||
15 | class EEM_Registration extends EEM_Soft_Delete_Base { |
||
16 | |||
17 | // private instance of the Registration object |
||
18 | protected static $_instance = NULL; |
||
19 | |||
20 | /** |
||
21 | * Keys are the status IDs for registrations (eg, RAP, RCN, etc), and the values |
||
22 | * are status codes (eg, approved, cancelled, etc) |
||
23 | * @var array |
||
24 | */ |
||
25 | private static $_reg_status; |
||
26 | |||
27 | /** |
||
28 | * The value of REG_count for a primary registrant |
||
29 | */ |
||
30 | const PRIMARY_REGISTRANT_COUNT = 1; |
||
31 | |||
32 | /** |
||
33 | * Status ID (STS_ID on esp_status table) to indicate an INCOMPLETE registration. |
||
34 | * Initial status for registrations when they are first created |
||
35 | * Payments are NOT allowed. |
||
36 | * Automatically toggled to whatever the default Event registration status is upon completion of the attendee information reg step |
||
37 | * NO space reserved. |
||
38 | * Registration is NOT active |
||
39 | */ |
||
40 | const status_id_incomplete = 'RIC'; |
||
41 | |||
42 | /** |
||
43 | * Status ID (STS_ID on esp_status table) to indicate an UNAPPROVED registration. |
||
44 | * Payments are NOT allowed. |
||
45 | * Event Admin must manually toggle STS_ID for it to change |
||
46 | * No space reserved. |
||
47 | * Registration is active |
||
48 | */ |
||
49 | const status_id_not_approved = 'RNA'; |
||
50 | |||
51 | /** |
||
52 | * Status ID (STS_ID on esp_status table) to indicate registration is PENDING_PAYMENT . |
||
53 | * Payments are allowed. |
||
54 | * STS_ID will automatically be toggled to RAP if payment is made in full by the attendee |
||
55 | * No space reserved. |
||
56 | * Registration is active |
||
57 | */ |
||
58 | const status_id_pending_payment = 'RPP'; |
||
59 | |||
60 | /** |
||
61 | * Status ID (STS_ID on esp_status table) to indicate registration is on the WAIT_LIST . |
||
62 | * Payments are allowed. |
||
63 | * STS_ID will automatically be toggled to RAP if payment is made in full by the attendee |
||
64 | * No space reserved. |
||
65 | * Registration is active |
||
66 | */ |
||
67 | const status_id_wait_list = 'RWL'; |
||
68 | |||
69 | /** |
||
70 | * Status ID (STS_ID on esp_status table) to indicate an APPROVED registration. |
||
71 | * the TXN may or may not be completed ( paid in full ) |
||
72 | * Payments are allowed. |
||
73 | * A space IS reserved. |
||
74 | * Registration is active |
||
75 | */ |
||
76 | const status_id_approved = 'RAP'; |
||
77 | |||
78 | /** |
||
79 | * Status ID (STS_ID on esp_status table) to indicate a registration was CANCELLED by the attendee. |
||
80 | * Payments are NOT allowed. |
||
81 | * NO space reserved. |
||
82 | * Registration is NOT active |
||
83 | */ |
||
84 | const status_id_cancelled = 'RCN'; |
||
85 | |||
86 | /** |
||
87 | * Status ID (STS_ID on esp_status table) to indicate a registration was DECLINED by the Event Admin |
||
88 | * Payments are NOT allowed. |
||
89 | * No space reserved. |
||
90 | * Registration is NOT active |
||
91 | */ |
||
92 | const status_id_declined = 'RDC'; |
||
93 | |||
94 | /** |
||
95 | * @var TableAnalysis $table_analysis |
||
96 | */ |
||
97 | protected $_table_analysis; |
||
98 | |||
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 incoming timezone data that gets saved). |
||
107 | * Note this just sends the timezone info to the date time model field objects. Default is NULL (and will be assumed using the set timezone in the 'timezone_string' wp option) |
||
108 | */ |
||
109 | protected function __construct( $timezone = null ) { |
||
153 | |||
154 | |||
155 | |||
156 | /** |
||
157 | * reg_statuses_that_allow_payment |
||
158 | * a filterable list of registration statuses that allow a registrant to make a payment |
||
159 | * |
||
160 | * @access public |
||
161 | * @return array |
||
162 | */ |
||
163 | public static function reg_statuses_that_allow_payment() { |
||
173 | |||
174 | |||
175 | |||
176 | /** |
||
177 | * inactive_reg_statuses |
||
178 | * a filterable list of registration statuses that are considered active |
||
179 | * |
||
180 | * @access public |
||
181 | * @return array |
||
182 | */ |
||
183 | public static function active_reg_statuses() { |
||
194 | |||
195 | |||
196 | |||
197 | /** |
||
198 | * inactive_reg_statuses |
||
199 | * a filterable list of registration statuses that are not considered active |
||
200 | * |
||
201 | * @access public |
||
202 | * @return array |
||
203 | */ |
||
204 | public static function inactive_reg_statuses() { |
||
214 | |||
215 | |||
216 | |||
217 | /** |
||
218 | * closed_reg_statuses |
||
219 | * a filterable list of registration statuses that are considered "closed" |
||
220 | * meaning they should not be considered in any calculations involving monies owing |
||
221 | * |
||
222 | * @access public |
||
223 | * @return array |
||
224 | */ |
||
225 | public static function closed_reg_statuses() { |
||
234 | |||
235 | |||
236 | |||
237 | /** |
||
238 | * get list of registration statuses |
||
239 | * |
||
240 | * |
||
241 | * @access public |
||
242 | * @param array $exclude The status ids to exclude from the returned results |
||
243 | * @param bool $translated If true will return the values as singular localized strings |
||
244 | * @return array |
||
245 | */ |
||
246 | public static function reg_status_array( $exclude = array(), $translated = FALSE ) { |
||
250 | |||
251 | |||
252 | |||
253 | /** |
||
254 | * get list of registration statuses |
||
255 | * @access private |
||
256 | * @param array $exclude |
||
257 | * @return array |
||
258 | */ |
||
259 | private function _get_registration_status_array( $exclude = array() ) { |
||
276 | |||
277 | /** |
||
278 | * Gets the injected table analyzer, or throws an exception |
||
279 | * @return TableAnalysis |
||
280 | * @throws \EE_Error |
||
281 | */ |
||
282 | View Code Duplication | protected function _get_table_analysis() { |
|
294 | |||
295 | |||
296 | |||
297 | /** |
||
298 | * This returns a wpdb->results array of all registration date month and years matching the incoming query params and grouped by month and year. |
||
299 | * |
||
300 | * @param array $where_params Array of query_params as described in the comments for EEM_Base::get_all() |
||
301 | * @return array |
||
302 | * @throws \EE_Error |
||
303 | */ |
||
304 | public function get_reg_months_and_years( $where_params ) { |
||
314 | |||
315 | |||
316 | |||
317 | |||
318 | /** |
||
319 | * retrieve ALL registrations for a particular Attendee from db |
||
320 | * @access public |
||
321 | * @param int $ATT_ID |
||
322 | * @return EE_Registration[] |
||
323 | */ |
||
324 | public function get_all_registrations_for_attendee( $ATT_ID = 0 ) { |
||
330 | |||
331 | |||
332 | |||
333 | /** |
||
334 | * Gets a registration given their REG_url_link. Yes, this should usually |
||
335 | * be passed via a GET parameter. |
||
336 | * @param string $REG_url_link |
||
337 | * @return EE_Registration |
||
338 | */ |
||
339 | public function get_registration_for_reg_url_link($REG_url_link){ |
||
345 | |||
346 | |||
347 | |||
348 | |||
349 | /** |
||
350 | * retrieve registration for a specific transaction attendee from db |
||
351 | * |
||
352 | * @access public |
||
353 | * @param int $TXN_ID |
||
354 | * @param int $ATT_ID |
||
355 | * @param int $att_nmbr in case the ATT_ID is the same for multiple registrations (same details used) then the attendee number is required |
||
356 | * @return mixed array on success, FALSE on fail |
||
357 | */ |
||
358 | public function get_registration_for_transaction_attendee( $TXN_ID = 0, $ATT_ID = 0, $att_nmbr = 0 ) { |
||
367 | |||
368 | |||
369 | /** |
||
370 | * get the number of registrations per day for the Registration Admin page Reports Tab. |
||
371 | * (doesn't utilize models because it's a fairly specialized query) |
||
372 | * @access public |
||
373 | * @param $period string which can be passed to php's strtotime function (eg "-1 month") |
||
374 | * @return stdClass[] with properties regDate and total |
||
375 | */ |
||
376 | public function get_registrations_per_day_report( $period = '-1 month' ) { |
||
400 | |||
401 | |||
402 | /** |
||
403 | * Get the number of registrations per day including the count of registrations for each Registration Status. |
||
404 | * Note: EEM_Registration::status_id_incomplete registrations are excluded from the results. |
||
405 | * |
||
406 | * @param string $period |
||
407 | * |
||
408 | * @return stdClass[] with properties Registration_REG_date and a column for each registration status as the STS_ID |
||
409 | * (i.e. RAP) |
||
410 | */ |
||
411 | View Code Duplication | public function get_registrations_per_day_and_per_status_report( $period = '-1 month' ) { |
|
463 | |||
464 | |||
465 | |||
466 | |||
467 | |||
468 | |||
469 | /** |
||
470 | * get the number of registrations per event for the Registration Admin page Reports Tab |
||
471 | * @access public |
||
472 | * @param $period string which can be passed to php's strtotime function (eg "-1 month") |
||
473 | * @return stdClass[] each with properties event_name, reg_limit, and total |
||
474 | */ |
||
475 | public function get_registrations_per_event_report( $period = '-1 month' ) { |
||
498 | |||
499 | |||
500 | /** |
||
501 | * Get the number of registrations per event grouped by registration status. |
||
502 | * Note: EEM_Registration::status_id_incomplete registrations are excluded from the results. |
||
503 | * |
||
504 | * @param string $period |
||
505 | * |
||
506 | * @return stdClass[] with properties `Registration_Event` and a column for each registration status as the STS_ID |
||
507 | * (i.e. RAP) |
||
508 | */ |
||
509 | View Code Duplication | public function get_registrations_per_event_and_per_status_report( $period = '-1 month' ) { |
|
558 | |||
559 | |||
560 | /** |
||
561 | * Returns the EE_Registration of the primary attendee on the transaction id provided |
||
562 | * @param int $TXN_ID |
||
563 | * @return EE_Registration |
||
564 | */ |
||
565 | public function get_primary_registration_for_transaction_ID( $TXN_ID = 0){ |
||
571 | |||
572 | |||
573 | /** |
||
574 | * get_event_registration_count |
||
575 | * |
||
576 | * @access public |
||
577 | * @param int $EVT_ID |
||
578 | * @param boolean $for_incomplete_payments |
||
579 | * @return int |
||
580 | */ |
||
581 | public function get_event_registration_count ( $EVT_ID, $for_incomplete_payments = FALSE ) { |
||
590 | |||
591 | /** |
||
592 | * Deletes all registrations with no transactions. Note that this needs to be very efficient |
||
593 | * and so it uses wpdb directly |
||
594 | * @global WPDB $wpdb |
||
595 | * @return int number deleted |
||
596 | */ |
||
597 | public function delete_registrations_with_no_transaction() { |
||
603 | |||
604 | /** |
||
605 | * Count registrations checked into (or out of) a datetime |
||
606 | * |
||
607 | * @param int $DTT_ID datetime ID |
||
608 | * @param boolean $checked_in whether to count registrations checked IN or OUT |
||
609 | * @return int |
||
610 | */ |
||
611 | public function count_registrations_checked_into_datetime( $DTT_ID, $checked_in = true) { |
||
634 | |||
635 | /** |
||
636 | * Count registrations checked into (or out of) an event. |
||
637 | * |
||
638 | * @param int $EVT_ID event ID |
||
639 | * @param boolean $checked_in whether to count registrations checked IN or OUT |
||
640 | * @return int |
||
641 | */ |
||
642 | public function count_registrations_checked_into_event( $EVT_ID, $checked_in = true ) { |
||
667 | |||
668 | |||
669 | |||
670 | |||
671 | |||
672 | /** |
||
673 | * The purpose of this method is to retrieve an array of |
||
674 | * EE_Registration objects that represent the latest registration |
||
675 | * for each ATT_ID given in the function argument. |
||
676 | * |
||
677 | * @param array $attendee_ids |
||
678 | * @return EE_Registration[] |
||
679 | */ |
||
680 | public function get_latest_registration_for_each_of_given_contacts( $attendee_ids = array() ) { |
||
732 | |||
733 | |||
734 | |||
735 | } |
||
736 | // End of file EEM_Registration.model.php |
||
738 |
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..