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 | * To order by core fields, use one of |
||
| 21 | * 'system:creation-date', 'system:modification-date', 'system:id'. |
||
| 22 | * @var integer|string |
||
| 23 | */ |
||
| 24 | protected static $_fetchSortField = null; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * The direction that entries should be sorted in, available options are |
||
| 28 | * RAND, ASC or DESC. Defaults to null, which implies ASC |
||
| 29 | * @var string |
||
| 30 | */ |
||
| 31 | protected static $_fetchSortDirection = null; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Setter function for the default sorting direction of the Fetch |
||
| 35 | * function. Available options are RAND, ASC or DESC. |
||
| 36 | * |
||
| 37 | * @param string $direction |
||
| 38 | * The direction that entries should be sorted in, available options |
||
| 39 | * are RAND, ASC or DESC. |
||
| 40 | */ |
||
| 41 | public static function setFetchSortingDirection($direction) |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Sets the field to applying the sorting direction on when fetching |
||
| 54 | * entries |
||
| 55 | * |
||
| 56 | * @param integer $field_id |
||
| 57 | * The ID of the Field that should be sorted on |
||
| 58 | */ |
||
| 59 | public static function setFetchSortingField($field_id) |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Convenience function that will set sorting field and direction |
||
| 66 | * by calling `setFetchSortingField()` & `setFetchSortingDirection()` |
||
| 67 | * |
||
| 68 | * @see toolkit.EntryManager#setFetchSortingField() |
||
| 69 | * @see toolkit.EntryManager#setFetchSortingDirection() |
||
| 70 | * @param integer $field_id |
||
| 71 | * The ID of the Field that should be sorted on |
||
| 72 | * @param string $direction |
||
| 73 | * The direction that entries should be sorted in, available options |
||
| 74 | * are RAND, ASC or DESC. Defaults to ASC |
||
| 75 | */ |
||
| 76 | public static function setFetchSorting($field_id, $direction = 'ASC') |
||
| 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 | * Executes the SQL queries need to save a field's data for the specified |
||
| 98 | * entry id. |
||
| 99 | * |
||
| 100 | * It first locks the table for writes, it then deletes existing data and then |
||
| 101 | * it inserts a new row for the data. Errors are discarded and the lock is |
||
| 102 | * released, if it was acquired. |
||
| 103 | * |
||
| 104 | * @param int $entry_id |
||
| 105 | * The entry id to save the data for |
||
| 106 | * @param int $field_id |
||
| 107 | * The field id to save the data for |
||
| 108 | * @param array $field |
||
| 109 | * The field data to save |
||
| 110 | */ |
||
| 111 | protected static function saveFieldData($entry_id, $field_id, $field) |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Given an Entry object, iterate over all of the fields in that object |
||
| 173 | * an insert them into their relevant entry tables. |
||
| 174 | * |
||
| 175 | * @see EntryManager::saveFieldData() |
||
| 176 | * @param Entry $entry |
||
| 177 | * An Entry object to insert into the database |
||
| 178 | * @throws DatabaseException |
||
| 179 | * @return boolean |
||
| 180 | */ |
||
| 181 | public static function add(Entry $entry) |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Update an existing Entry object given an Entry object |
||
| 203 | * |
||
| 204 | * @see EntryManager::saveFieldData() |
||
| 205 | * @param Entry $entry |
||
| 206 | * An Entry object |
||
| 207 | * @throws DatabaseException |
||
| 208 | * @return boolean |
||
| 209 | */ |
||
| 210 | public static function edit(Entry $entry) |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Given an Entry ID, or an array of Entry ID's, delete all |
||
| 234 | * data associated with this Entry using a Field's `entryDataCleanup()` |
||
| 235 | * function, and then remove this Entry from `tbl_entries`. If the `$entries` |
||
| 236 | * all belong to the same section, passing `$section_id` will improve |
||
| 237 | * performance |
||
| 238 | * |
||
| 239 | * @param array|integer $entries |
||
| 240 | * An entry_id, or an array of entry id's to delete |
||
| 241 | * @param integer $section_id (optional) |
||
| 242 | * If possible, the `$section_id` of the the `$entries`. This parameter |
||
| 243 | * should be left as null if the `$entries` array contains entry_id's for |
||
| 244 | * multiple sections. |
||
| 245 | * @throws DatabaseException |
||
| 246 | * @throws Exception |
||
| 247 | * @return boolean |
||
| 248 | */ |
||
| 249 | public static function delete($entries, $section_id = null) |
||
| 340 | |||
| 341 | /** |
||
| 342 | * This function will return an array of Entry objects given an ID or an array of ID's. |
||
| 343 | * Do not provide `$entry_id` as an array if not specifying the `$section_id`. This function |
||
| 344 | * is commonly passed custom SQL statements through the `$where` and `$join` parameters |
||
| 345 | * that is generated by the fields of this section. |
||
| 346 | * |
||
| 347 | * @since Symphony 2.7.0 it will also call a new method on fields, |
||
| 348 | * `buildSortingSelectSQL()`, to make sure fields can add ordering columns in |
||
| 349 | * the SELECT clause. This is required on MySQL 5.7+ strict mode. |
||
| 350 | * |
||
| 351 | * @param integer|array $entry_id |
||
| 352 | * An array of Entry ID's or an Entry ID to return |
||
| 353 | * @param integer $section_id |
||
| 354 | * The ID of the Section that these entries are contained in |
||
| 355 | * @param integer $limit |
||
| 356 | * The limit of entries to return |
||
| 357 | * @param integer $start |
||
| 358 | * The starting offset of the entries to return |
||
| 359 | * @param string $where |
||
| 360 | * Any custom WHERE clauses. The tbl_entries alias is `e` |
||
| 361 | * @param string $joins |
||
| 362 | * Any custom JOIN's |
||
| 363 | * @param boolean $group |
||
| 364 | * Whether the entries need to be grouped by Entry ID or not |
||
| 365 | * @param boolean $buildentries |
||
| 366 | * Whether to return an array of entry ID's or Entry objects. Defaults to |
||
| 367 | * true, which will return Entry objects |
||
| 368 | * @param array $element_names |
||
| 369 | * Choose whether to get data from a subset of fields or all fields in a section, |
||
| 370 | * by providing an array of field names. Defaults to null, which will load data |
||
| 371 | * from all fields in a section. |
||
| 372 | * @param boolean $enable_sort |
||
| 373 | * Defaults to true, if false this function will not apply any sorting |
||
| 374 | * @throws Exception |
||
| 375 | * @return array |
||
| 376 | * If `$buildentries` is true, this function will return an array of Entry objects, |
||
| 377 | * otherwise it will return an associative array of Entry data from `tbl_entries` |
||
| 378 | */ |
||
| 379 | public static function fetch($entry_id = null, $section_id = null, $limit = null, $start = null, $where = null, $joins = null, $group = false, $buildentries = true, $element_names = null, $enable_sort = true) |
||
| 481 | |||
| 482 | /** |
||
| 483 | * Given an array of Entry data from `tbl_entries` and a section ID, return an |
||
| 484 | * array of Entry objects. For performance reasons, it's possible to pass an array |
||
| 485 | * of field handles via `$element_names`, so that only a subset of the section schema |
||
| 486 | * will be queried. This function currently only supports Entry from one section at a |
||
| 487 | * time. |
||
| 488 | * |
||
| 489 | * @param array $rows |
||
| 490 | * An array of Entry data from `tbl_entries` including the Entry ID, Entry section, |
||
| 491 | * the ID of the Author who created the Entry, and a Unix timestamp of creation |
||
| 492 | * @param integer $section_id |
||
| 493 | * The section ID of the entries in the `$rows` |
||
| 494 | * @param array $element_names |
||
| 495 | * Choose whether to get data from a subset of fields or all fields in a section, |
||
| 496 | * by providing an array of field names. Defaults to null, which will load data |
||
| 497 | * from all fields in a section. |
||
| 498 | * @throws DatabaseException |
||
| 499 | * @return array |
||
| 500 | * An array of Entry objects |
||
| 501 | */ |
||
| 502 | public static function __buildEntries(array $rows, $section_id, $element_names = null) |
||
| 611 | |||
| 612 | |||
| 613 | /** |
||
| 614 | * Given an Entry ID, return the Section ID that it belongs to |
||
| 615 | * |
||
| 616 | * @param integer $entry_id |
||
| 617 | * The ID of the Entry to return it's section |
||
| 618 | * @return integer |
||
| 619 | * The Section ID for this Entry's section |
||
| 620 | */ |
||
| 621 | public static function fetchEntrySectionID($entry_id) |
||
| 628 | |||
| 629 | /** |
||
| 630 | * Return the count of the number of entries in a particular section. |
||
| 631 | * |
||
| 632 | * @param integer $section_id |
||
| 633 | * The ID of the Section where the Entries are to be counted |
||
| 634 | * @param string $where |
||
| 635 | * Any custom WHERE clauses |
||
| 636 | * @param string $joins |
||
| 637 | * Any custom JOIN's |
||
| 638 | * @param boolean $group |
||
| 639 | * Whether the entries need to be grouped by Entry ID or not |
||
| 640 | * @return integer |
||
| 641 | */ |
||
| 642 | public static function fetchCount($section_id = null, $where = null, $joins = null, $group = false) |
||
| 667 | |||
| 668 | /** |
||
| 669 | * Returns an array of Entry objects, with some basic pagination given |
||
| 670 | * the number of Entry's to return and the current starting offset. This |
||
| 671 | * function in turn calls the fetch function that does alot of the heavy |
||
| 672 | * lifting. For instance, if there are 60 entries in a section and the pagination |
||
| 673 | * dictates that per page, 15 entries are to be returned, by passing 2 to |
||
| 674 | * the $page parameter you could return entries 15-30 |
||
| 675 | * |
||
| 676 | * @param integer $page |
||
| 677 | * The page to return, defaults to 1 |
||
| 678 | * @param integer $section_id |
||
| 679 | * The ID of the Section that these entries are contained in |
||
| 680 | * @param integer $entriesPerPage |
||
| 681 | * The number of entries to return per page. |
||
| 682 | * @param string $where |
||
| 683 | * Any custom WHERE clauses |
||
| 684 | * @param string $joins |
||
| 685 | * Any custom JOIN's |
||
| 686 | * @param boolean $group |
||
| 687 | * Whether the entries need to be grouped by Entry ID or not |
||
| 688 | * @param boolean $records_only |
||
| 689 | * If this is set to true, an array of Entry objects will be returned |
||
| 690 | * without any basic pagination information. Defaults to false |
||
| 691 | * @param boolean $buildentries |
||
| 692 | * Whether to return an array of entry ID's or Entry objects. Defaults to |
||
| 693 | * true, which will return Entry objects |
||
| 694 | * @param array $element_names |
||
| 695 | * Choose whether to get data from a subset of fields or all fields in a section, |
||
| 696 | * by providing an array of field names. Defaults to null, which will load data |
||
| 697 | * from all fields in a section. |
||
| 698 | * @throws Exception |
||
| 699 | * @return array |
||
| 700 | * Either an array of Entry objects, or an associative array containing |
||
| 701 | * the total entries, the start position, the entries per page and the |
||
| 702 | * Entry objects |
||
| 703 | */ |
||
| 704 | public static function fetchByPage($page = 1, $section_id, $entriesPerPage, $where = null, $joins = null, $group = false, $records_only = false, $buildentries = true, array $element_names = null) |
||
| 747 | |||
| 748 | /** |
||
| 749 | * Creates a new Entry object using this class as the parent. |
||
| 750 | * |
||
| 751 | * @return Entry |
||
| 752 | */ |
||
| 753 | public static function create() |
||
| 757 | } |
||
| 758 |
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.