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')) { |
||
| 14 | class EEM_Datetime extends EEM_Soft_Delete_Base |
||
| 15 | { |
||
| 16 | |||
| 17 | /** |
||
| 18 | * @var EEM_Datetime $_instance |
||
| 19 | */ |
||
| 20 | protected static $_instance; |
||
| 21 | |||
| 22 | |||
| 23 | /** |
||
| 24 | * private constructor to prevent direct creation |
||
| 25 | * |
||
| 26 | * @param string $timezone A string representing the timezone we want to set for returned Date Time Strings |
||
| 27 | * (and any incoming timezone data that gets saved). |
||
| 28 | * Note this just sends the timezone info to the date time model field objects. |
||
| 29 | * Default is NULL |
||
| 30 | * (and will be assumed using the set timezone in the 'timezone_string' wp option) |
||
| 31 | * @throws EE_Error |
||
| 32 | * @throws InvalidArgumentException |
||
| 33 | * @throws InvalidArgumentException |
||
| 34 | */ |
||
| 35 | protected function __construct($timezone) |
||
| 147 | |||
| 148 | |||
| 149 | /** |
||
| 150 | * create new blank datetime |
||
| 151 | * |
||
| 152 | * @access public |
||
| 153 | * @return EE_Datetime[] array on success, FALSE on fail |
||
| 154 | * @throws EE_Error |
||
| 155 | */ |
||
| 156 | public function create_new_blank_datetime() |
||
| 187 | |||
| 188 | |||
| 189 | /** |
||
| 190 | * get event start date from db |
||
| 191 | * |
||
| 192 | * @access public |
||
| 193 | * @param int $EVT_ID |
||
| 194 | * @return EE_Datetime[] array on success, FALSE on fail |
||
| 195 | * @throws EE_Error |
||
| 196 | */ |
||
| 197 | public function get_all_event_dates($EVT_ID = 0) |
||
| 208 | |||
| 209 | |||
| 210 | /** |
||
| 211 | * get all datetimes attached to an event ordered by the DTT_order field |
||
| 212 | * |
||
| 213 | * @public |
||
| 214 | * @param int $EVT_ID event id |
||
| 215 | * @param boolean $include_expired |
||
| 216 | * @param boolean $include_deleted |
||
| 217 | * @param int $limit If included then limit the count of results by |
||
| 218 | * the given number |
||
| 219 | * @return EE_Datetime[] |
||
| 220 | * @throws EE_Error |
||
| 221 | */ |
||
| 222 | public function get_datetimes_for_event_ordered_by_DTT_order( |
||
| 256 | |||
| 257 | |||
| 258 | /** |
||
| 259 | * Gets the datetimes for the event (with the given limit), and orders them by "importance". |
||
| 260 | * By importance, we mean that the primary datetimes are most important (DEPRECATED FOR NOW), |
||
| 261 | * and then the earlier datetimes are the most important. |
||
| 262 | * Maybe we'll want this to take into account datetimes that haven't already passed, but we don't yet. |
||
| 263 | * |
||
| 264 | * @param int $EVT_ID |
||
| 265 | * @param int $limit |
||
| 266 | * @return EE_Datetime[]|EE_Base_Class[] |
||
| 267 | * @throws EE_Error |
||
| 268 | */ |
||
| 269 | public function get_datetimes_for_event_ordered_by_importance($EVT_ID = 0, $limit = null) |
||
| 280 | |||
| 281 | |||
| 282 | /** |
||
| 283 | * @param int $EVT_ID |
||
| 284 | * @param boolean $include_expired |
||
| 285 | * @param boolean $include_deleted |
||
| 286 | * @return EE_Datetime |
||
| 287 | * @throws EE_Error |
||
| 288 | */ |
||
| 289 | public function get_oldest_datetime_for_event($EVT_ID, $include_expired = false, $include_deleted = false) |
||
| 302 | |||
| 303 | |||
| 304 | /** |
||
| 305 | * Gets the 'primary' datetime for an event. |
||
| 306 | * |
||
| 307 | * @param int $EVT_ID |
||
| 308 | * @param bool $try_to_exclude_expired |
||
| 309 | * @param bool $try_to_exclude_deleted |
||
| 310 | * @return \EE_Datetime |
||
| 311 | * @throws EE_Error |
||
| 312 | */ |
||
| 313 | public function get_primary_datetime_for_event( |
||
| 332 | |||
| 333 | |||
| 334 | /** |
||
| 335 | * Gets ALL the datetimes for an event (including trashed ones, for now), ordered |
||
| 336 | * only by start date |
||
| 337 | * |
||
| 338 | * @param int $EVT_ID |
||
| 339 | * @param boolean $include_expired |
||
| 340 | * @param boolean $include_deleted |
||
| 341 | * @param int $limit |
||
| 342 | * @return EE_Datetime[] |
||
| 343 | * @throws EE_Error |
||
| 344 | */ |
||
| 345 | View Code Duplication | public function get_datetimes_for_event_ordered_by_start_time( |
|
| 370 | |||
| 371 | |||
| 372 | /** |
||
| 373 | * Gets ALL the datetimes for an ticket (including trashed ones, for now), ordered |
||
| 374 | * only by start date |
||
| 375 | * |
||
| 376 | * @param int $TKT_ID |
||
| 377 | * @param boolean $include_expired |
||
| 378 | * @param boolean $include_deleted |
||
| 379 | * @param int $limit |
||
| 380 | * @return EE_Datetime[] |
||
| 381 | * @throws EE_Error |
||
| 382 | */ |
||
| 383 | View Code Duplication | public function get_datetimes_for_ticket_ordered_by_start_time( |
|
| 408 | |||
| 409 | |||
| 410 | /** |
||
| 411 | * Gets all the datetimes for a ticket (including trashed ones, for now), ordered by the DTT_order for the |
||
| 412 | * datetimes. |
||
| 413 | * |
||
| 414 | * @param int $TKT_ID ID of ticket to retrieve the datetimes for |
||
| 415 | * @param boolean $include_expired whether to include expired datetimes or not |
||
| 416 | * @param boolean $include_deleted whether to include trashed datetimes or not. |
||
| 417 | * @param int|null $limit if null, no limit, if int then limit results by |
||
| 418 | * that number |
||
| 419 | * @return EE_Datetime[] |
||
| 420 | * @throws EE_Error |
||
| 421 | */ |
||
| 422 | View Code Duplication | public function get_datetimes_for_ticket_ordered_by_DTT_order( |
|
| 448 | |||
| 449 | |||
| 450 | /** |
||
| 451 | * Gets the most important datetime for a particular event (ie, the primary event usually. But if for some WACK |
||
| 452 | * reason it doesn't exist, we consider the earliest event the most important) |
||
| 453 | * |
||
| 454 | * @param int $EVT_ID |
||
| 455 | * @return EE_Datetime |
||
| 456 | * @throws EE_Error |
||
| 457 | */ |
||
| 458 | public function get_most_important_datetime_for_event($EVT_ID) |
||
| 466 | |||
| 467 | |||
| 468 | /** |
||
| 469 | * This returns a wpdb->results Array of all DTT month and years matching the incoming query params and |
||
| 470 | * grouped by month and year. |
||
| 471 | * |
||
| 472 | * @param array $where_params Array of query_params as described in the comments for EEM_Base::get_all() |
||
| 473 | * @param string $evt_active_status A string representing the evt active status to filter the months by. |
||
| 474 | * Can be: |
||
| 475 | * - '' = no filter |
||
| 476 | * - upcoming = Published events with at least one upcoming datetime. |
||
| 477 | * - expired = Events with all datetimes expired. |
||
| 478 | * - active = Events that are published and have at least one datetime that |
||
| 479 | * starts before now and ends after now. |
||
| 480 | * - inactive = Events that are either not published. |
||
| 481 | * @return EE_Base_Class[] |
||
| 482 | * @throws EE_Error |
||
| 483 | * @throws InvalidArgumentException |
||
| 484 | * @throws InvalidArgumentException |
||
| 485 | */ |
||
| 486 | public function get_dtt_months_and_years($where_params, $evt_active_status = '') |
||
| 560 | |||
| 561 | |||
| 562 | /** |
||
| 563 | * Updates the DTT_sold attribute on each datetime (based on the registrations |
||
| 564 | * for the tickets for each datetime) |
||
| 565 | * |
||
| 566 | * @param EE_Base_Class[]|EE_Datetime[] $datetimes |
||
| 567 | * @throws EE_Error |
||
| 568 | */ |
||
| 569 | public function update_sold($datetimes) |
||
| 583 | |||
| 584 | |||
| 585 | /** |
||
| 586 | * Gets the total number of tickets available at a particular datetime |
||
| 587 | * (does NOT take into account the datetime's spaces available) |
||
| 588 | * |
||
| 589 | * @param int $DTT_ID |
||
| 590 | * @param array $query_params |
||
| 591 | * @return int of tickets available. If sold out, return less than 1. If infinite, returns EE_INF, IF there are NO |
||
| 592 | * tickets attached to datetime then FALSE is returned. |
||
| 593 | */ |
||
| 594 | public function sum_tickets_currently_available_at_datetime($DTT_ID, array $query_params = array()) |
||
| 602 | |||
| 603 | |||
| 604 | /** |
||
| 605 | * This returns an array of counts of datetimes in the database for each Datetime status that can be queried. |
||
| 606 | * |
||
| 607 | * @param array $stati_to_include If included you can restrict the statuses we return counts for by including the |
||
| 608 | * stati you want counts for as values in the array. An empty array returns counts |
||
| 609 | * for all valid stati. |
||
| 610 | * @param array $query_params If included can be used to refine the conditions for returning the count (i.e. |
||
| 611 | * only for Datetimes connected to a specific event, or specific ticket. |
||
| 612 | * @return array The value returned is an array indexed by Datetime Status and the values are the counts. The |
||
| 613 | * @throws EE_Error |
||
| 614 | * stati used as index keys are: EE_Datetime::active EE_Datetime::upcoming |
||
| 615 | * EE_Datetime::expired |
||
| 616 | */ |
||
| 617 | public function get_datetime_counts_by_status(array $stati_to_include = array(), array $query_params = array()) |
||
| 653 | |||
| 654 | |||
| 655 | /** |
||
| 656 | * Returns the specific count for a given Datetime status matching any given query_params. |
||
| 657 | * |
||
| 658 | * @param string $status Valid string representation for Datetime status requested. (Defaults to Active). |
||
| 659 | * @param array $query_params |
||
| 660 | * @return int |
||
| 661 | * @throws EE_Error |
||
| 662 | */ |
||
| 663 | public function get_datetime_count_for_status($status = EE_Datetime::active, array $query_params = array()) |
||
| 668 | } |
||
| 669 | // End of file EEM_Datetime.model.php |
||
| 671 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.