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')) exit('No direct script access allowed'); |
||
27 | class EEM_Datetime extends EEM_Soft_Delete_Base { |
||
28 | |||
29 | // private instance of the EEM_Datetime object |
||
30 | protected static $_instance = NULL; |
||
31 | |||
32 | /** |
||
33 | * private constructor to prevent direct creation |
||
34 | * @Constructor |
||
35 | * @access private |
||
36 | * @param string $timezone string representing the timezone we want to set for returned Date Time Strings (and any incoming timezone data that gets saved). 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) |
||
37 | */ |
||
38 | protected function __construct( $timezone ) { |
||
73 | |||
74 | |||
75 | |||
76 | |||
77 | /** |
||
78 | * create new blank datetime |
||
79 | * |
||
80 | * @access public |
||
81 | * @return EE_Datetime[] array on success, FALSE on fail |
||
82 | */ |
||
83 | public function create_new_blank_datetime() { |
||
97 | |||
98 | |||
99 | |||
100 | |||
101 | |||
102 | /** |
||
103 | * get event start date from db |
||
104 | * |
||
105 | * @access public |
||
106 | * @param int $EVT_ID |
||
107 | * @return EE_Datetime[] array on success, FALSE on fail |
||
108 | */ |
||
109 | public function get_all_event_dates( $EVT_ID = 0 ) { |
||
121 | |||
122 | |||
123 | |||
124 | |||
125 | |||
126 | /** |
||
127 | * get all datetimes attached to an event ordered by the DTT_order field |
||
128 | * @public |
||
129 | * @param int $EVT_ID event id |
||
130 | * @param boolean $include_expired |
||
131 | * @param boolean $include_deleted |
||
132 | * @param int $limit If included then limit the count of results by |
||
133 | * the given number |
||
134 | * @return EE_Datetime[] |
||
135 | */ |
||
136 | public function get_datetimes_for_event_ordered_by_DTT_order( $EVT_ID, $include_expired = TRUE, $include_deleted= TRUE, $limit = NULL ) { |
||
158 | |||
159 | |||
160 | |||
161 | |||
162 | /** |
||
163 | * Gets the datetimes for the event (with the given limit), and orders them by "importance". By importance, we mean |
||
164 | * that the primary datetimes are most important (DEPRECATED FOR NOW), and then the earlier datetimes are the most important. Maybe we'll want |
||
165 | * this to take into account datetimes that haven't already passed, but we don't yet. |
||
166 | * @param int $EVT_ID |
||
167 | * @param int $limit |
||
168 | * @return EE_Datetime[] |
||
169 | */ |
||
170 | public function get_datetimes_for_event_ordered_by_importance( $EVT_ID = 0, $limit = NULL){ |
||
176 | |||
177 | |||
178 | |||
179 | /** |
||
180 | * |
||
181 | * @param int $EVT_ID |
||
182 | * @param boolean $include_expired |
||
183 | * @param boolean $include_deleted |
||
184 | * @return EE_Datetime |
||
185 | */ |
||
186 | public function get_oldest_datetime_for_event($EVT_ID, $include_expired = false,$include_deleted = false){ |
||
194 | |||
195 | |||
196 | |||
197 | /** |
||
198 | * Gets the 'primary' datetime for an event. |
||
199 | * @param int $EVT_ID |
||
200 | * @param bool $try_to_exclude_expired |
||
201 | * @param bool $try_to_exclude_deleted |
||
202 | * @return \EE_Datetime |
||
203 | */ |
||
204 | public function get_primary_datetime_for_event($EVT_ID,$try_to_exclude_expired = true, $try_to_exclude_deleted = true){ |
||
220 | |||
221 | |||
222 | |||
223 | /** |
||
224 | * Gets ALL the datetimes for an event (including trashed ones, for now), ordered |
||
225 | * only by start date |
||
226 | * @param int $EVT_ID |
||
227 | * @param boolean $include_expired |
||
228 | * @param boolean $include_deleted |
||
229 | * @param int $limit |
||
230 | * @return EE_Datetime[] |
||
231 | */ |
||
232 | View Code Duplication | public function get_datetimes_for_event_ordered_by_start_time($EVT_ID, $include_expired = true, $include_deleted= true, $limit = NULL ){ |
|
251 | |||
252 | /** |
||
253 | * Gets ALL the datetimes for an ticket (including trashed ones, for now), ordered |
||
254 | * only by start date |
||
255 | * @param int $TKT_ID |
||
256 | * @param boolean $include_expired |
||
257 | * @param boolean $include_deleted |
||
258 | * @param int $limit |
||
259 | * @return EE_Datetime[] |
||
260 | */ |
||
261 | View Code Duplication | public function get_datetimes_for_ticket_ordered_by_start_time($TKT_ID, $include_expired = true, $include_deleted= true, $limit = NULL){ |
|
280 | |||
281 | |||
282 | |||
283 | |||
284 | /** |
||
285 | * Gets all the datetimes for a ticket (including trashed ones, for now), ordered by the DTT_order for the datetimes. |
||
286 | * @param int $TKT_ID ID of ticket to retrieve the datetimes for |
||
287 | * @param boolean $include_expired whether to include expired datetimes or not |
||
288 | * @param boolean $include_deleted whether to include trashed datetimes or not. |
||
289 | * @param int|null $limit if null, no limit, if int then limit results by |
||
290 | * that number |
||
291 | * @return EE_Datetime[] |
||
292 | */ |
||
293 | View Code Duplication | public function get_datetimes_for_ticket_ordered_by_DTT_order( $TKT_ID, $include_expired = true, $include_deleted = true, $limit = NULL ) { |
|
313 | |||
314 | |||
315 | /** |
||
316 | * Gets the most important datetime for a particular event (ie, the primary event usually. But if for some WACK |
||
317 | * reason it doesn't exist, we consider the earliest event the most important) |
||
318 | * @param int $EVT_ID |
||
319 | * @return EE_Datetime |
||
320 | */ |
||
321 | public function get_most_important_datetime_for_event($EVT_ID){ |
||
329 | |||
330 | |||
331 | |||
332 | /** |
||
333 | * This returns a wpdb->results Array of all DTT month and years matching the incoming query params and grouped by month and year. |
||
334 | * @param array $where_params Array of query_params as described in the comments for EEM_Base::get_all() |
||
335 | * @param string $evt_active_status A string representing the evt active status to filter the months by. |
||
336 | * Can be: |
||
337 | * - '' = no filter |
||
338 | * - upcoming = Published events with at least one upcoming datetime. |
||
339 | * - expired = Events with all datetimes expired. |
||
340 | * - active = Events that are published and have at least one datetime that starts before now and ends after now. |
||
341 | * - inactive = Events that are either not published. |
||
342 | * @return wpdb results array |
||
343 | */ |
||
344 | public function get_dtt_months_and_years( $where_params, $evt_active_status = '' ) { |
||
410 | |||
411 | /** |
||
412 | * Updates the DTT_sold attribute on each datetime (based on the registrations |
||
413 | * for the tickets for each datetime) |
||
414 | * @param EE_Datetime[] $datetimes |
||
415 | */ |
||
416 | public function update_sold($datetimes){ |
||
421 | |||
422 | |||
423 | |||
424 | /** |
||
425 | * Gets the total number of tickets available at a particular datetime |
||
426 | * (does NOT take into account the datetime's spaces available) |
||
427 | * |
||
428 | * @param int $DTT_ID |
||
429 | * @param array $query_params |
||
430 | * @return int of tickets available. If sold out, return less than 1. If infinite, returns EE_INF, IF there are NO tickets attached to datetime then FALSE is returned. |
||
431 | */ |
||
432 | public function sum_tickets_currently_available_at_datetime( $DTT_ID, $query_params = array() ) { |
||
439 | |||
440 | |||
441 | |||
442 | |||
443 | /** |
||
444 | * This returns an array of counts of datetimes in the database for each Datetime status that can be queried. |
||
445 | * |
||
446 | * @param array $stati_to_include If included you can restrict the statuses we return counts for by including the stati |
||
447 | * you want counts for as values in the array. An empty array returns counts for all valid |
||
448 | * stati. |
||
449 | * @param array $query_params If included can be used to refine the conditions for returning the count (i.e. only for |
||
450 | * Datetimes connected to a specific event, or specific ticket. |
||
451 | * |
||
452 | * @return array The value returned is an array indexed by Datetime Status and the values are the counts. The stati used as index keys are: |
||
453 | * EE_Datetime::active |
||
454 | * EE_Datetime::upcoming |
||
455 | * EE_Datetime::expired |
||
456 | */ |
||
457 | public function get_datetime_counts_by_status( $stati_to_include = array(), $query_params = array() ) { |
||
491 | |||
492 | |||
493 | /** |
||
494 | * Returns the specific count for a given Datetime status matching any given query_params. |
||
495 | * |
||
496 | * @param string $status Valid string representation for Datetime status requested. (Defaults to Active). |
||
497 | * @param array $query_params |
||
498 | * @return int |
||
499 | */ |
||
500 | public function get_datetime_count_for_status( $status = EE_Datetime::active, $query_params = array() ) { |
||
504 | |||
505 | |||
506 | |||
507 | } |
||
508 | // End of file EEM_Datetime.model.php |
||
510 |
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..