Complex classes like IndexRepository 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 IndexRepository, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 27 | class IndexRepository extends AbstractRepository |
||
| 28 | { |
||
| 29 | /** |
||
| 30 | * Default orderings for index records. |
||
| 31 | * |
||
| 32 | * @var array |
||
| 33 | */ |
||
| 34 | protected $defaultOrderings = [ |
||
| 35 | 'start_date' => QueryInterface::ORDER_ASCENDING, |
||
| 36 | 'start_time' => QueryInterface::ORDER_ASCENDING, |
||
| 37 | ]; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Index types for selection. |
||
| 41 | * |
||
| 42 | * @var array |
||
| 43 | */ |
||
| 44 | protected $indexTypes = []; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Override page ids. |
||
| 48 | * |
||
| 49 | * @var array |
||
| 50 | */ |
||
| 51 | protected $overridePageIds; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Create query. |
||
| 55 | * |
||
| 56 | * @return QueryInterface |
||
| 57 | */ |
||
| 58 | public function createQuery() |
||
| 59 | { |
||
| 60 | $query = parent::createQuery(); |
||
| 61 | |||
| 62 | return $query; |
||
| 63 | } |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Set the index types. |
||
| 67 | * |
||
| 68 | * @param array $types |
||
| 69 | */ |
||
| 70 | public function setIndexTypes(array $types) |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Override page IDs. |
||
| 77 | * |
||
| 78 | * @param array $overridePageIds |
||
| 79 | */ |
||
| 80 | public function setOverridePageIds($overridePageIds) |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Select indecies for Backend. |
||
| 87 | * |
||
| 88 | * @param OptionRequest $options |
||
| 89 | * |
||
| 90 | * @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface |
||
| 91 | */ |
||
| 92 | public function findAllForBackend(OptionRequest $options) |
||
| 93 | { |
||
| 94 | $query = $this->createQuery(); |
||
| 95 | $query->getQuerySettings()->setIgnoreEnableFields(true); |
||
| 96 | $query->getQuerySettings()->setRespectSysLanguage(false); |
||
| 97 | $query->getQuerySettings()->setLanguageOverlayMode(false); |
||
| 98 | |||
| 99 | // Notice Selection without any language handling |
||
| 100 | unset($GLOBALS['TCA']['tx_calendarize_domain_model_index']['ctrl']['languageField'], $GLOBALS['TCA']['tx_calendarize_domain_model_index']['ctrl']['transOrigPointerField']); |
||
| 101 | |||
| 102 | if ('asc' === $options->getDirection()) { |
||
| 103 | $query->setOrderings([ |
||
| 104 | 'start_date' => QueryInterface::ORDER_ASCENDING, |
||
| 105 | 'start_time' => QueryInterface::ORDER_ASCENDING, |
||
| 106 | ]); |
||
| 107 | } else { |
||
| 108 | $query->setOrderings([ |
||
| 109 | 'start_date' => QueryInterface::ORDER_DESCENDING, |
||
| 110 | 'start_time' => QueryInterface::ORDER_DESCENDING, |
||
| 111 | ]); |
||
| 112 | } |
||
| 113 | |||
| 114 | if ((int)$options->getPid() > 0) { |
||
| 115 | $query->matching($query->equals('pid', (int)$options->getPid())); |
||
| 116 | } |
||
| 117 | |||
| 118 | return $query->execute(); |
||
| 119 | } |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Find List. |
||
| 123 | * |
||
| 124 | * @param int $limit |
||
| 125 | * @param int|string $listStartTime |
||
| 126 | * @param int $startOffsetHours |
||
| 127 | * @param int $overrideStartDate |
||
| 128 | * @param int $overrideEndDate |
||
| 129 | * |
||
| 130 | * @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface |
||
| 131 | */ |
||
| 132 | public function findList( |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Find by custom search. |
||
| 166 | * |
||
| 167 | * @param \DateTime $startDate |
||
| 168 | * @param \DateTime $endDate |
||
| 169 | * @param array $customSearch |
||
| 170 | * @param int $limit |
||
| 171 | * |
||
| 172 | * @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface |
||
| 173 | */ |
||
| 174 | public function findBySearch(\DateTime $startDate = null, \DateTime $endDate = null, array $customSearch = [], int $limit = 0) |
||
| 268 | |||
| 269 | /** |
||
| 270 | * Find Past Events. |
||
| 271 | * |
||
| 272 | * @param int $limit |
||
| 273 | * @param string $sort |
||
| 274 | * |
||
| 275 | * @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface |
||
| 276 | */ |
||
| 277 | public function findByPast( |
||
| 294 | |||
| 295 | /** |
||
| 296 | * Find by traversing information. |
||
| 297 | * |
||
| 298 | * @param Index $index |
||
| 299 | * @param bool|true $future |
||
| 300 | * @param bool|false $past |
||
| 301 | * @param int $limit |
||
| 302 | * @param string $sort |
||
| 303 | * @param bool $useIndexTime |
||
| 304 | * |
||
| 305 | * @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface |
||
| 306 | */ |
||
| 307 | public function findByTraversing( |
||
| 342 | |||
| 343 | /** |
||
| 344 | * Find by traversing information. |
||
| 345 | * |
||
| 346 | * @param DomainObjectInterface $event |
||
| 347 | * @param bool|true $future |
||
| 348 | * @param bool|false $past |
||
| 349 | * @param int $limit |
||
| 350 | * @param string $sort |
||
| 351 | * |
||
| 352 | * @throws Exception |
||
| 353 | * |
||
| 354 | * @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface |
||
| 355 | */ |
||
| 356 | public function findByEventTraversing( |
||
| 395 | |||
| 396 | /** |
||
| 397 | * find Year. |
||
| 398 | * |
||
| 399 | * @param int $year |
||
| 400 | * |
||
| 401 | * @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface |
||
| 402 | */ |
||
| 403 | public function findYear(int $year) |
||
| 407 | |||
| 408 | /** |
||
| 409 | * find quarter. |
||
| 410 | * |
||
| 411 | * @param int $year |
||
| 412 | * @param int $quarter |
||
| 413 | * |
||
| 414 | * @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface |
||
| 415 | */ |
||
| 416 | public function findQuarter(int $year, int $quarter) |
||
| 422 | |||
| 423 | /** |
||
| 424 | * find Month. |
||
| 425 | * |
||
| 426 | * @param int $year |
||
| 427 | * @param int $month |
||
| 428 | * |
||
| 429 | * @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface |
||
| 430 | */ |
||
| 431 | public function findMonth(int $year, int $month) |
||
| 438 | |||
| 439 | /** |
||
| 440 | * find Week. |
||
| 441 | * |
||
| 442 | * @param int $year |
||
| 443 | * @param int $week |
||
| 444 | * @param int $weekStart See documentation for settings.weekStart |
||
| 445 | * |
||
| 446 | * @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface |
||
| 447 | */ |
||
| 448 | public function findWeek($year, $week, $weekStart = 1) |
||
| 459 | |||
| 460 | /** |
||
| 461 | * Find different types and locations. |
||
| 462 | * |
||
| 463 | * @return array |
||
| 464 | */ |
||
| 465 | public function findDifferentTypesAndLocations(): array |
||
| 471 | |||
| 472 | /** |
||
| 473 | * find day. |
||
| 474 | * |
||
| 475 | * @param int $year |
||
| 476 | * @param int $month |
||
| 477 | * @param int $day |
||
| 478 | * |
||
| 479 | * @throws Exception |
||
| 480 | * |
||
| 481 | * @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface |
||
| 482 | */ |
||
| 483 | public function findDay(int $year, int $month, int $day) |
||
| 493 | |||
| 494 | /** |
||
| 495 | * Set the default sorting direction. |
||
| 496 | * |
||
| 497 | * @param string $direction |
||
| 498 | * @param string $field |
||
| 499 | */ |
||
| 500 | public function setDefaultSortingDirection($direction, $field = '') |
||
| 504 | |||
| 505 | /** |
||
| 506 | * Find by time slot. |
||
| 507 | * |
||
| 508 | * @param int $startTime |
||
| 509 | * @param int|null $endTime null means open end |
||
| 510 | * |
||
| 511 | * @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface |
||
| 512 | */ |
||
| 513 | public function findByTimeSlot($startTime, $endTime = null) |
||
| 528 | |||
| 529 | /** |
||
| 530 | * Find all indices by the given Event model. |
||
| 531 | * |
||
| 532 | * @param DomainObjectInterface $event |
||
| 533 | * |
||
| 534 | * @throws Exception |
||
| 535 | * |
||
| 536 | * @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface |
||
| 537 | */ |
||
| 538 | public function findByEvent(DomainObjectInterface $event) |
||
| 551 | |||
| 552 | /** |
||
| 553 | * storage page selection. |
||
| 554 | * |
||
| 555 | * @return array |
||
| 556 | */ |
||
| 557 | protected function getStoragePageIds() |
||
| 578 | |||
| 579 | /** |
||
| 580 | * Get the default constraint for the queries. |
||
| 581 | * |
||
| 582 | * @param QueryInterface $query |
||
| 583 | * |
||
| 584 | * @return array |
||
| 585 | */ |
||
| 586 | protected function getDefaultConstraints(QueryInterface $query) |
||
| 611 | |||
| 612 | /** |
||
| 613 | * Add time frame related queries. |
||
| 614 | * |
||
| 615 | * @param array $constraints |
||
| 616 | * @param QueryInterface $query |
||
| 617 | * @param int $startTime |
||
| 618 | * @param int|null $endTime |
||
| 619 | * |
||
| 620 | * @see IndexUtility::isIndexInRange |
||
| 621 | */ |
||
| 622 | protected function addTimeFrameConstraints(&$constraints, QueryInterface $query, $startTime = null, $endTime = null) |
||
| 649 | |||
| 650 | /** |
||
| 651 | * Adds time frame constraints which respect the actual index times. |
||
| 652 | * Do not call this method directly. Call IndexRepository::addTimeFrameConstraints instead. |
||
| 653 | * |
||
| 654 | * @param array $constraints |
||
| 655 | * @param QueryInterface $query |
||
| 656 | * @param array $arguments |
||
| 657 | * |
||
| 658 | * @see IndexRepository::addTimeFrameConstraints |
||
| 659 | */ |
||
| 660 | protected function addDateTimeFrameConstraints(&$constraints, QueryInterface $query, array $arguments) |
||
| 701 | |||
| 702 | /** |
||
| 703 | * Adds time frame constraints which respect only the index dates, not the actual index times. |
||
| 704 | * Do not call this method directly. Call IndexRepository::addTimeFrameConstraints instead. |
||
| 705 | * |
||
| 706 | * @param array $constraints |
||
| 707 | * @param QueryInterface $query |
||
| 708 | * @param array $arguments |
||
| 709 | * |
||
| 710 | * @see IndexRepository::addTimeFrameConstraints |
||
| 711 | */ |
||
| 712 | protected function addDateFrameConstraints(&$constraints, QueryInterface $query, array $arguments) |
||
| 753 | |||
| 754 | /** |
||
| 755 | * Get the sorting. |
||
| 756 | * |
||
| 757 | * @param string $direction |
||
| 758 | * @param string $field |
||
| 759 | * |
||
| 760 | * @return array |
||
| 761 | */ |
||
| 762 | protected function getSorting($direction, $field = '') |
||
| 780 | } |
||
| 781 |