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_Datetime 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_Datetime, and based on these observations, apply Extract Interface, too.
1 | <?php if ( ! defined('EVENT_ESPRESSO_VERSION')) { |
||
11 | class EEM_Datetime extends EEM_Soft_Delete_Base |
||
12 | { |
||
13 | |||
14 | /** |
||
15 | * @var EEM_Datetime $_instance |
||
16 | */ |
||
17 | protected static $_instance; |
||
18 | |||
19 | |||
20 | |||
21 | /** |
||
22 | * private constructor to prevent direct creation |
||
23 | * |
||
24 | * @Constructor |
||
25 | * @access private |
||
26 | * @param string $timezone string representing the timezone we want to set for returned Date Time Strings (and any |
||
27 | * incoming timezone data that gets saved). Note this just sends the timezone info to the |
||
28 | * date time model field objects. Default is NULL (and will be assumed using the set |
||
29 | * timezone in the 'timezone_string' wp option) |
||
30 | * @throws \EE_Error |
||
31 | */ |
||
32 | protected function __construct($timezone) |
||
93 | |||
94 | |||
95 | |||
96 | /** |
||
97 | * create new blank datetime |
||
98 | * |
||
99 | * @access public |
||
100 | * @return EE_Datetime[] array on success, FALSE on fail |
||
101 | * @throws \EE_Error |
||
102 | */ |
||
103 | public function create_new_blank_datetime() |
||
119 | |||
120 | |||
121 | |||
122 | /** |
||
123 | * get event start date from db |
||
124 | * |
||
125 | * @access public |
||
126 | * @param int $EVT_ID |
||
127 | * @return EE_Datetime[] array on success, FALSE on fail |
||
128 | * @throws \EE_Error |
||
129 | */ |
||
130 | public function get_all_event_dates($EVT_ID = 0) |
||
141 | |||
142 | |||
143 | |||
144 | /** |
||
145 | * get all datetimes attached to an event ordered by the DTT_order field |
||
146 | * |
||
147 | * @public |
||
148 | * @param int $EVT_ID event id |
||
149 | * @param boolean $include_expired |
||
150 | * @param boolean $include_deleted |
||
151 | * @param int $limit If included then limit the count of results by |
||
152 | * the given number |
||
153 | * @return EE_Datetime[] |
||
154 | * @throws \EE_Error |
||
155 | */ |
||
156 | public function get_datetimes_for_event_ordered_by_DTT_order( |
||
190 | |||
191 | |||
192 | |||
193 | /** |
||
194 | * Gets the datetimes for the event (with the given limit), and orders them by "importance". By importance, we mean |
||
195 | * that the primary datetimes are most important (DEPRECATED FOR NOW), and then the earlier datetimes are the most |
||
196 | * important. Maybe we'll want this to take into account datetimes that haven't already passed, but we don't yet. |
||
197 | * |
||
198 | * @param int $EVT_ID |
||
199 | * @param int $limit |
||
200 | * @return EE_Datetime[]|EE_Base_Class[] |
||
201 | * @throws \EE_Error |
||
202 | */ |
||
203 | public function get_datetimes_for_event_ordered_by_importance($EVT_ID = 0, $limit = null) |
||
214 | |||
215 | |||
216 | |||
217 | /** |
||
218 | * @param int $EVT_ID |
||
219 | * @param boolean $include_expired |
||
220 | * @param boolean $include_deleted |
||
221 | * @return EE_Datetime |
||
222 | * @throws \EE_Error |
||
223 | */ |
||
224 | public function get_oldest_datetime_for_event($EVT_ID, $include_expired = false, $include_deleted = false) |
||
233 | |||
234 | |||
235 | |||
236 | /** |
||
237 | * Gets the 'primary' datetime for an event. |
||
238 | * |
||
239 | * @param int $EVT_ID |
||
240 | * @param bool $try_to_exclude_expired |
||
241 | * @param bool $try_to_exclude_deleted |
||
242 | * @return \EE_Datetime |
||
243 | * @throws \EE_Error |
||
244 | */ |
||
245 | public function get_primary_datetime_for_event( |
||
264 | |||
265 | |||
266 | |||
267 | /** |
||
268 | * Gets ALL the datetimes for an event (including trashed ones, for now), ordered |
||
269 | * only by start date |
||
270 | * |
||
271 | * @param int $EVT_ID |
||
272 | * @param boolean $include_expired |
||
273 | * @param boolean $include_deleted |
||
274 | * @param int $limit |
||
275 | * @return EE_Datetime[] |
||
276 | * @throws \EE_Error |
||
277 | */ |
||
278 | View Code Duplication | public function get_datetimes_for_event_ordered_by_start_time( |
|
303 | |||
304 | |||
305 | |||
306 | /** |
||
307 | * Gets ALL the datetimes for an ticket (including trashed ones, for now), ordered |
||
308 | * only by start date |
||
309 | * |
||
310 | * @param int $TKT_ID |
||
311 | * @param boolean $include_expired |
||
312 | * @param boolean $include_deleted |
||
313 | * @param int $limit |
||
314 | * @return EE_Datetime[] |
||
315 | * @throws \EE_Error |
||
316 | */ |
||
317 | View Code Duplication | public function get_datetimes_for_ticket_ordered_by_start_time( |
|
342 | |||
343 | |||
344 | |||
345 | /** |
||
346 | * Gets all the datetimes for a ticket (including trashed ones, for now), ordered by the DTT_order for the |
||
347 | * datetimes. |
||
348 | * |
||
349 | * @param int $TKT_ID ID of ticket to retrieve the datetimes for |
||
350 | * @param boolean $include_expired whether to include expired datetimes or not |
||
351 | * @param boolean $include_deleted whether to include trashed datetimes or not. |
||
352 | * @param int|null $limit if null, no limit, if int then limit results by |
||
353 | * that number |
||
354 | * @return EE_Datetime[] |
||
355 | * @throws \EE_Error |
||
356 | */ |
||
357 | View Code Duplication | public function get_datetimes_for_ticket_ordered_by_DTT_order( |
|
383 | |||
384 | |||
385 | |||
386 | /** |
||
387 | * Gets the most important datetime for a particular event (ie, the primary event usually. But if for some WACK |
||
388 | * reason it doesn't exist, we consider the earliest event the most important) |
||
389 | * |
||
390 | * @param int $EVT_ID |
||
391 | * @return EE_Datetime |
||
392 | * @throws \EE_Error |
||
393 | */ |
||
394 | public function get_most_important_datetime_for_event($EVT_ID) |
||
403 | |||
404 | |||
405 | |||
406 | /** |
||
407 | * This returns a wpdb->results Array of all DTT month and years matching the incoming query params and |
||
408 | * 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 | * @param string $evt_active_status A string representing the evt active status to filter the months by. |
||
412 | * Can be: |
||
413 | * - '' = no filter |
||
414 | * - upcoming = Published events with at least one upcoming datetime. |
||
415 | * - expired = Events with all datetimes expired. |
||
416 | * - active = Events that are published and have at least one datetime that |
||
417 | * starts before now and ends after now. |
||
418 | * - inactive = Events that are either not published. |
||
419 | * @return EE_Base_Class[] |
||
420 | * @throws \EE_Error |
||
421 | */ |
||
422 | public function get_dtt_months_and_years($where_params, $evt_active_status = '') |
||
491 | |||
492 | |||
493 | |||
494 | /** |
||
495 | * Updates the DTT_sold attribute on each datetime (based on the registrations |
||
496 | * for the tickets for each datetime) |
||
497 | * |
||
498 | * @param EE_Datetime[] $datetimes |
||
499 | */ |
||
500 | public function update_sold($datetimes) |
||
506 | |||
507 | |||
508 | |||
509 | /** |
||
510 | * Gets the total number of tickets available at a particular datetime |
||
511 | * (does NOT take into account the datetime's spaces available) |
||
512 | * |
||
513 | * @param int $DTT_ID |
||
514 | * @param array $query_params |
||
515 | * @return int of tickets available. If sold out, return less than 1. If infinite, returns EE_INF, IF there are NO |
||
516 | * tickets attached to datetime then FALSE is returned. |
||
517 | */ |
||
518 | public function sum_tickets_currently_available_at_datetime($DTT_ID, array $query_params = array()) |
||
526 | |||
527 | |||
528 | |||
529 | /** |
||
530 | * This returns an array of counts of datetimes in the database for each Datetime status that can be queried. |
||
531 | * |
||
532 | * @param array $stati_to_include If included you can restrict the statuses we return counts for by including the |
||
533 | * stati you want counts for as values in the array. An empty array returns counts |
||
534 | * for all valid stati. |
||
535 | * @param array $query_params If included can be used to refine the conditions for returning the count (i.e. |
||
536 | * only for Datetimes connected to a specific event, or specific ticket. |
||
537 | * @return array The value returned is an array indexed by Datetime Status and the values are the counts. The |
||
538 | * @throws \EE_Error |
||
539 | * stati used as index keys are: EE_Datetime::active EE_Datetime::upcoming EE_Datetime::expired |
||
540 | */ |
||
541 | public function get_datetime_counts_by_status(array $stati_to_include = array(), array $query_params = array()) |
||
573 | |||
574 | |||
575 | |||
576 | /** |
||
577 | * Returns the specific count for a given Datetime status matching any given query_params. |
||
578 | * |
||
579 | * @param string $status Valid string representation for Datetime status requested. (Defaults to Active). |
||
580 | * @param array $query_params |
||
581 | * @return int |
||
582 | * @throws \EE_Error |
||
583 | */ |
||
584 | public function get_datetime_count_for_status($status = EE_Datetime::active, array $query_params = array()) |
||
589 | |||
590 | |||
591 | |||
592 | } |
||
593 | // End of file EEM_Datetime.model.php |
||
595 |
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..