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 EntryManager 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 EntryManager, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 15 | class EntryManager |
||
| 16 | { |
||
| 17 | /** |
||
| 18 | * The Field ID that will be used to sort when fetching Entries, defaults |
||
| 19 | * to null, which implies the Entry ID (id column in `tbl_entries`) |
||
| 20 | * |
||
| 21 | * @var integer |
||
| 22 | */ |
||
| 23 | protected static $_fetchSortField = null; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * The direction that entries should be sorted in, available options are |
||
| 27 | * RAND, ASC or DESC. Defaults to null, which implies ASC |
||
| 28 | * |
||
| 29 | * @var string |
||
| 30 | */ |
||
| 31 | protected static $_fetchSortDirection = null; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Convenience function that will set sorting field and direction |
||
| 35 | * by calling `setFetchSortingField()` & `setFetchSortingDirection()` |
||
| 36 | * |
||
| 37 | * @see toolkit.EntryManager#setFetchSortingField() |
||
| 38 | * @see toolkit.EntryManager#setFetchSortingDirection() |
||
| 39 | * @param integer $field_id |
||
| 40 | * The ID of the Field that should be sorted on |
||
| 41 | * @param string $direction |
||
| 42 | * The direction that entries should be sorted in, available options |
||
| 43 | * are RAND, ASC or DESC. Defaults to ASC |
||
| 44 | */ |
||
| 45 | public static function setFetchSorting($field_id, $direction = 'ASC') |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Sets the field to applying the sorting direction on when fetching |
||
| 53 | * entries |
||
| 54 | * |
||
| 55 | * @param integer $field_id |
||
| 56 | * The ID of the Field that should be sorted on |
||
| 57 | */ |
||
| 58 | public static function setFetchSortingField($field_id) |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Setter function for the default sorting direction of the Fetch |
||
| 65 | * function. Available options are RAND, ASC or DESC. |
||
| 66 | * |
||
| 67 | * @param string $direction |
||
| 68 | * The direction that entries should be sorted in, available options |
||
| 69 | * are RAND, ASC or DESC. |
||
| 70 | */ |
||
| 71 | public static function setFetchSortingDirection($direction) |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Returns an object representation of the sorting for the |
||
| 84 | * EntryManager, with the field and direction provided |
||
| 85 | * |
||
| 86 | * @return StdClass |
||
| 87 | */ |
||
| 88 | public static function getFetchSorting() |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Given an Entry object, iterate over all of the fields in that object |
||
| 98 | * an insert them into their relevant entry tables. |
||
| 99 | * |
||
| 100 | * @param Entry $entry |
||
| 101 | * An Entry object to insert into the database |
||
| 102 | * @throws DatabaseException |
||
| 103 | * @return boolean |
||
| 104 | */ |
||
| 105 | public static function add(Entry $entry) |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Update an existing Entry object given an Entry object |
||
| 152 | * |
||
| 153 | * @param Entry $entry |
||
| 154 | * An Entry object |
||
| 155 | * @throws DatabaseException |
||
| 156 | * @return boolean |
||
| 157 | */ |
||
| 158 | public static function edit(Entry $entry) |
||
| 216 | |||
| 217 | /** |
||
| 218 | * Given an Entry ID, or an array of Entry ID's, delete all |
||
| 219 | * data associated with this Entry using a Field's `entryDataCleanup()` |
||
| 220 | * function, and then remove this Entry from `tbl_entries`. If the `$entries` |
||
| 221 | * all belong to the same section, passing `$section_id` will improve |
||
| 222 | * performance |
||
| 223 | * |
||
| 224 | * @param array|integer $entries |
||
| 225 | * An entry_id, or an array of entry id's to delete |
||
| 226 | * @param integer $section_id (optional) |
||
| 227 | * If possible, the `$section_id` of the the `$entries`. This parameter |
||
| 228 | * should be left as null if the `$entries` array contains entry_id's for |
||
| 229 | * multiple sections. |
||
| 230 | * @throws DatabaseException |
||
| 231 | * @throws Exception |
||
| 232 | * @return boolean |
||
| 233 | */ |
||
| 234 | public static function delete($entries, $section_id = null) |
||
| 324 | |||
| 325 | /** |
||
| 326 | * This function will return an array of Entry objects given an ID or an array of ID's. |
||
| 327 | * Do not provide `$entry_id` as an array if not specifying the `$section_id`. This function |
||
| 328 | * is commonly passed custom SQL statements through the `$where` and `$join` parameters |
||
| 329 | * that is generated by the fields of this section |
||
| 330 | * |
||
| 331 | * @param integer|array $entry_id |
||
| 332 | * An array of Entry ID's or an Entry ID to return |
||
| 333 | * @param integer $section_id |
||
| 334 | * The ID of the Section that these entries are contained in |
||
| 335 | * @param integer $limit |
||
| 336 | * The limit of entries to return |
||
| 337 | * @param integer $start |
||
| 338 | * The starting offset of the entries to return |
||
| 339 | * @param string $where |
||
| 340 | * Any custom WHERE clauses. The tbl_entries alias is `e` |
||
| 341 | * @param string $joins |
||
| 342 | * Any custom JOIN's |
||
| 343 | * @param boolean $group |
||
| 344 | * Whether the entries need to be grouped by Entry ID or not |
||
| 345 | * @param boolean $buildentries |
||
| 346 | * Whether to return an array of entry ID's or Entry objects. Defaults to |
||
| 347 | * true, which will return Entry objects |
||
| 348 | * @param array $element_names |
||
| 349 | * Choose whether to get data from a subset of fields or all fields in a section, |
||
| 350 | * by providing an array of field names. Defaults to null, which will load data |
||
| 351 | * from all fields in a section. |
||
| 352 | * @param boolean $enable_sort |
||
| 353 | * Defaults to true, if false this function will not apply any sorting |
||
| 354 | * @throws Exception |
||
| 355 | * @return array |
||
| 356 | * If `$buildentries` is true, this function will return an array of Entry objects, |
||
| 357 | * otherwise it will return an associative array of Entry data from `tbl_entries` |
||
| 358 | */ |
||
| 359 | public static function fetch( |
||
| 360 | $entry_id = null, |
||
| 361 | $section_id = null, |
||
| 362 | $limit = null, |
||
| 363 | $start = null, |
||
| 364 | $where = null, |
||
| 365 | $joins = null, |
||
| 366 | $group = false, |
||
| 367 | $buildentries = true, |
||
| 368 | $element_names = null, |
||
| 369 | $enable_sort = true |
||
| 370 | ) { |
||
| 371 | $sort = null; |
||
| 372 | |||
| 373 | if (!$entry_id && !$section_id) { |
||
| 374 | return false; |
||
| 375 | } |
||
| 376 | |||
| 377 | if (!$section_id) { |
||
| 378 | $section_id = self::fetchEntrySectionID($entry_id); |
||
| 379 | } |
||
| 380 | |||
| 381 | $section = SectionManager::fetch($section_id); |
||
| 382 | if (!is_object($section)) { |
||
| 383 | return false; |
||
| 384 | } |
||
| 385 | |||
| 386 | // SORTING |
||
| 387 | // A single $entry_id doesn't need to be sorted on, or if it's explicitly disabled |
||
| 388 | if ((!is_array($entry_id) && !is_null($entry_id) && is_int($entry_id)) || !$enable_sort) { |
||
| 389 | $sort = null; |
||
| 390 | |||
| 391 | // Check for RAND first, since this works independently of any specific field |
||
| 392 | } elseif (self::$_fetchSortDirection === 'RAND') { |
||
| 393 | $sort = 'ORDER BY RAND() '; |
||
| 394 | |||
| 395 | // Handle Creation Date or the old Date sorting |
||
| 396 | } elseif (self::$_fetchSortField === 'system:creation-date' || self::$_fetchSortField === 'date') { |
||
| 397 | $sort = sprintf('ORDER BY `e`.`creation_date_gmt` %s', self::$_fetchSortDirection); |
||
| 398 | |||
| 399 | // Handle Modification Date sorting |
||
| 400 | } elseif (self::$_fetchSortField === 'system:modification-date') { |
||
| 401 | $sort = sprintf('ORDER BY `e`.`modification_date_gmt` %s', self::$_fetchSortDirection); |
||
| 402 | |||
| 403 | // Handle sorting for System ID |
||
| 404 | View Code Duplication | } elseif (self::$_fetchSortField === 'system:id' || self::$_fetchSortField === 'id') { |
|
| 405 | $sort = sprintf('ORDER BY `e`.`id` %s', self::$_fetchSortDirection); |
||
| 406 | |||
| 407 | // Handle when the sort field is an actual Field |
||
| 408 | } elseif (self::$_fetchSortField && $field = FieldManager::fetch(self::$_fetchSortField)) { |
||
| 409 | if ($field->isSortable()) { |
||
| 410 | $field->buildSortingSQL($joins, $where, $sort, self::$_fetchSortDirection); |
||
| 411 | } |
||
| 412 | |||
| 413 | // Handle if the section has a default sorting field |
||
| 414 | } elseif ($section->getSortingField() && $field = FieldManager::fetch($section->getSortingField())) { |
||
| 415 | if ($field->isSortable()) { |
||
| 416 | $field->buildSortingSQL($joins, $where, $sort, $section->getSortingOrder()); |
||
| 417 | } |
||
| 418 | |||
| 419 | if (!$group) { |
||
| 420 | $group = $field->requiresSQLGrouping(); |
||
| 421 | } |
||
| 422 | } else { |
||
| 423 | View Code Duplication | if (self::$_fetchSortField === 'system:id' || self::$_fetchSortField === 'id') { |
|
| 424 | $sort = 'ORDER BY `e`.`id` ' . self::$_fetchSortDirection; |
||
| 425 | } else { |
||
| 426 | $sort = sprintf('ORDER BY `e`.`id` %s', self::$_fetchSortDirection); |
||
| 427 | } |
||
| 428 | } |
||
| 429 | |||
| 430 | if (!empty($field) && !$group) { |
||
| 431 | $group = $field->requiresSQLGrouping(); |
||
| 432 | } |
||
| 433 | |||
| 434 | if ($entry_id && !is_array($entry_id)) { |
||
| 435 | $entry_id = array($entry_id); |
||
| 436 | } |
||
| 437 | |||
| 438 | $sql = sprintf(" |
||
| 439 | SELECT %s`e`.`id`, `e`.section_id, `e`.`author_id`, |
||
| 440 | `e`.`creation_date` AS `creation_date`, |
||
| 441 | `e`.`modification_date` AS `modification_date` |
||
| 442 | FROM `tbl_entries` AS `e` |
||
| 443 | %s |
||
| 444 | WHERE 1 |
||
| 445 | %s |
||
| 446 | %s |
||
| 447 | %s |
||
| 448 | %s |
||
| 449 | %s |
||
| 450 | ", |
||
| 451 | $group ? 'DISTINCT ' : '', |
||
| 452 | $joins, |
||
| 453 | $entry_id ? "AND `e`.`id` IN ('" . implode("', '", $entry_id) . "') " : '', |
||
| 454 | $section_id ? sprintf("AND `e`.`section_id` = %d", $section_id) : '', |
||
| 455 | $where, |
||
| 456 | $sort, |
||
| 457 | $limit ? sprintf('LIMIT %d, %d', $start, $limit) : '' |
||
| 458 | ); |
||
| 459 | |||
| 460 | $rows = Symphony::Database()->fetch($sql); |
||
| 461 | |||
| 462 | // Create UNIX timestamps, as it has always been (Re: #2501) |
||
| 463 | foreach ($rows as &$entry) { |
||
| 464 | $entry['creation_date'] = DateTimeObj::get('U', $entry['creation_date']); |
||
| 465 | $entry['modification_date'] = DateTimeObj::get('U', $entry['modification_date']); |
||
| 466 | } |
||
| 467 | unset($entry); |
||
| 468 | |||
| 469 | return ($buildentries && (is_array($rows) && !empty($rows)) ? self::__buildEntries($rows, $section_id, |
||
| 470 | $element_names) : $rows); |
||
| 471 | } |
||
| 472 | |||
| 473 | /** |
||
| 474 | * Given an Entry ID, return the Section ID that it belongs to |
||
| 475 | * |
||
| 476 | * @param integer $entry_id |
||
| 477 | * The ID of the Entry to return it's section |
||
| 478 | * @return integer |
||
| 479 | * The Section ID for this Entry's section |
||
| 480 | */ |
||
| 481 | public static function fetchEntrySectionID($entry_id) |
||
| 488 | |||
| 489 | /** |
||
| 490 | * Given an array of Entry data from `tbl_entries` and a section ID, return an |
||
| 491 | * array of Entry objects. For performance reasons, it's possible to pass an array |
||
| 492 | * of field handles via `$element_names`, so that only a subset of the section schema |
||
| 493 | * will be queried. This function currently only supports Entry from one section at a |
||
| 494 | * time. |
||
| 495 | * |
||
| 496 | * @param array $rows |
||
| 497 | * An array of Entry data from `tbl_entries` including the Entry ID, Entry section, |
||
| 498 | * the ID of the Author who created the Entry, and a Unix timestamp of creation |
||
| 499 | * @param integer $section_id |
||
| 500 | * The section ID of the entries in the `$rows` |
||
| 501 | * @param array $element_names |
||
| 502 | * Choose whether to get data from a subset of fields or all fields in a section, |
||
| 503 | * by providing an array of field names. Defaults to null, which will load data |
||
| 504 | * from all fields in a section. |
||
| 505 | * @throws DatabaseException |
||
| 506 | * @return array |
||
| 507 | * An array of Entry objects |
||
| 508 | */ |
||
| 509 | public static function __buildEntries(array $rows, $section_id, $element_names = null) |
||
| 615 | |||
| 616 | /** |
||
| 617 | * Creates a new Entry object using this class as the parent. |
||
| 618 | * |
||
| 619 | * @return Entry |
||
| 620 | */ |
||
| 621 | public static function create() |
||
| 625 | |||
| 626 | /** |
||
| 627 | * Returns an array of Entry objects, with some basic pagination given |
||
| 628 | * the number of Entry's to return and the current starting offset. This |
||
| 629 | * function in turn calls the fetch function that does alot of the heavy |
||
| 630 | * lifting. For instance, if there are 60 entries in a section and the pagination |
||
| 631 | * dictates that per page, 15 entries are to be returned, by passing 2 to |
||
| 632 | * the $page parameter you could return entries 15-30 |
||
| 633 | * |
||
| 634 | * @param integer $page |
||
| 635 | * The page to return, defaults to 1 |
||
| 636 | * @param integer $section_id |
||
| 637 | * The ID of the Section that these entries are contained in |
||
| 638 | * @param integer $entriesPerPage |
||
| 639 | * The number of entries to return per page. |
||
| 640 | * @param string $where |
||
| 641 | * Any custom WHERE clauses |
||
| 642 | * @param string $joins |
||
| 643 | * Any custom JOIN's |
||
| 644 | * @param boolean $group |
||
| 645 | * Whether the entries need to be grouped by Entry ID or not |
||
| 646 | * @param boolean $records_only |
||
| 647 | * If this is set to true, an array of Entry objects will be returned |
||
| 648 | * without any basic pagination information. Defaults to false |
||
| 649 | * @param boolean $buildentries |
||
| 650 | * Whether to return an array of entry ID's or Entry objects. Defaults to |
||
| 651 | * true, which will return Entry objects |
||
| 652 | * @param array $element_names |
||
| 653 | * Choose whether to get data from a subset of fields or all fields in a section, |
||
| 654 | * by providing an array of field names. Defaults to null, which will load data |
||
| 655 | * from all fields in a section. |
||
| 656 | * @throws Exception |
||
| 657 | * @return array |
||
| 658 | * Either an array of Entry objects, or an associative array containing |
||
| 659 | * the total entries, the start position, the entries per page and the |
||
| 660 | * Entry objects |
||
| 661 | */ |
||
| 662 | public static function fetchByPage( |
||
| 716 | |||
| 717 | /** |
||
| 718 | * Return the count of the number of entries in a particular section. |
||
| 719 | * |
||
| 720 | * @param integer $section_id |
||
| 721 | * The ID of the Section where the Entries are to be counted |
||
| 722 | * @param string $where |
||
| 723 | * Any custom WHERE clauses |
||
| 724 | * @param string $joins |
||
| 725 | * Any custom JOIN's |
||
| 726 | * @param boolean $group |
||
| 727 | * Whether the entries need to be grouped by Entry ID or not |
||
| 728 | * @return integer |
||
| 729 | */ |
||
| 730 | public static function fetchCount($section_id = null, $where = null, $joins = null, $group = false) |
||
| 755 | } |
||
| 756 |
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.