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 |
||
| 24 | class IndexRepository extends AbstractRepository |
||
| 25 | { |
||
| 26 | /** |
||
| 27 | * Default orderings for index records. |
||
| 28 | * |
||
| 29 | * @var array |
||
| 30 | */ |
||
| 31 | protected $defaultOrderings = [ |
||
| 32 | 'start_date' => QueryInterface::ORDER_ASCENDING, |
||
| 33 | 'start_time' => QueryInterface::ORDER_ASCENDING, |
||
| 34 | ]; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Index types for selection. |
||
| 38 | * |
||
| 39 | * @var array |
||
| 40 | */ |
||
| 41 | protected $indexTypes = []; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Override page ids. |
||
| 45 | * |
||
| 46 | * @var array |
||
| 47 | */ |
||
| 48 | protected $overridePageIds = []; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @return QueryInterface |
||
| 52 | */ |
||
| 53 | public function createQuery() |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Set the index types. |
||
| 63 | * |
||
| 64 | * @param array $types |
||
| 65 | */ |
||
| 66 | public function setIndexTypes(array $types) |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Override page IDs. |
||
| 73 | * |
||
| 74 | * @param array $overridePageIds |
||
| 75 | */ |
||
| 76 | public function setOverridePageIds($overridePageIds) |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Select indecies for Backend. |
||
| 83 | * |
||
| 84 | * @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface |
||
| 85 | */ |
||
| 86 | public function findAllForBackend() |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Find List. |
||
| 96 | * |
||
| 97 | * @param int $limit |
||
| 98 | * @param int|string $listStartTime |
||
| 99 | * @param int $startOffsetHours |
||
| 100 | * @param int $overrideStartDate |
||
| 101 | * @param int $overrideEndDate |
||
| 102 | * |
||
| 103 | * @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface |
||
| 104 | */ |
||
| 105 | public function findList( |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Find by custom search. |
||
| 139 | * |
||
| 140 | * @param \DateTime $startDate |
||
| 141 | * @param \DateTime $endDate |
||
| 142 | * @param array $customSearch |
||
| 143 | * @param int $limit |
||
| 144 | * |
||
| 145 | * @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface |
||
| 146 | */ |
||
| 147 | public function findBySearch(\DateTime $startDate = null, \DateTime $endDate = null, array $customSearch = [], int $limit = 0) |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Find Past Events. |
||
| 190 | * |
||
| 191 | * @param int $limit |
||
| 192 | * @param string $sort |
||
| 193 | * |
||
| 194 | * @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface |
||
| 195 | */ |
||
| 196 | public function findByPast( |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Find by traversing information. |
||
| 216 | * |
||
| 217 | * @param Index $index |
||
| 218 | * @param bool|true $future |
||
| 219 | * @param bool|false $past |
||
| 220 | * @param int $limit |
||
| 221 | * @param string $sort |
||
| 222 | * @param bool $useIndexTime |
||
| 223 | * |
||
| 224 | * @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface |
||
| 225 | */ |
||
| 226 | public function findByTraversing( |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Find by traversing information. |
||
| 265 | * |
||
| 266 | * @param DomainObjectInterface $event |
||
| 267 | * @param bool|true $future |
||
| 268 | * @param bool|false $past |
||
| 269 | * @param int $limit |
||
| 270 | * @param string $sort |
||
| 271 | * |
||
| 272 | * @throws Exception |
||
| 273 | * |
||
| 274 | * @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface |
||
| 275 | */ |
||
| 276 | public function findByEventTraversing( |
||
| 311 | |||
| 312 | /** |
||
| 313 | * find Year. |
||
| 314 | * |
||
| 315 | * @param int $year |
||
| 316 | * |
||
| 317 | * @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface |
||
| 318 | */ |
||
| 319 | public function findYear(int $year) |
||
| 323 | |||
| 324 | /** |
||
| 325 | * find Month. |
||
| 326 | * |
||
| 327 | * @param int $year |
||
| 328 | * @param int $month |
||
| 329 | * |
||
| 330 | * @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface |
||
| 331 | */ |
||
| 332 | public function findMonth(int $year, int $month) |
||
| 339 | |||
| 340 | /** |
||
| 341 | * find Week. |
||
| 342 | * |
||
| 343 | * @param int $year |
||
| 344 | * @param int $week |
||
| 345 | * @param int $weekStart See documentation for settings.weekStart |
||
| 346 | * |
||
| 347 | * @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface |
||
| 348 | */ |
||
| 349 | public function findWeek($year, $week, $weekStart = 1) |
||
| 360 | |||
| 361 | /** |
||
| 362 | * Find different types and locations. |
||
| 363 | * |
||
| 364 | * @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface |
||
| 365 | */ |
||
| 366 | public function findDifferentTypesAndLocations() |
||
| 376 | |||
| 377 | /** |
||
| 378 | * find day. |
||
| 379 | * |
||
| 380 | * @param int $year |
||
| 381 | * @param int $month |
||
| 382 | * @param int $day |
||
| 383 | * |
||
| 384 | * @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface |
||
| 385 | */ |
||
| 386 | public function findDay(int $year, int $month, int $day) |
||
| 392 | |||
| 393 | /** |
||
| 394 | * Set the default sorting direction. |
||
| 395 | * |
||
| 396 | * @param string $direction |
||
| 397 | * @param string $field |
||
| 398 | */ |
||
| 399 | public function setDefaultSortingDirection($direction, $field = '') |
||
| 403 | |||
| 404 | /** |
||
| 405 | * Find by time slot. |
||
| 406 | * |
||
| 407 | * @param int $startTime |
||
| 408 | * @param int|null $endTime null means open end |
||
| 409 | * |
||
| 410 | * @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface |
||
| 411 | */ |
||
| 412 | public function findByTimeSlot($startTime, $endTime = null) |
||
| 420 | |||
| 421 | /** |
||
| 422 | * Find all indices by the given Event model. |
||
| 423 | * |
||
| 424 | * @param DomainObjectInterface $event |
||
| 425 | * |
||
| 426 | * @throws Exception |
||
| 427 | * |
||
| 428 | * @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface |
||
| 429 | */ |
||
| 430 | public function findByEvent(DomainObjectInterface $event) |
||
| 443 | |||
| 444 | /** |
||
| 445 | * Get index language mode. |
||
| 446 | * |
||
| 447 | * @return string |
||
| 448 | */ |
||
| 449 | protected function getIndexLanguageMode() |
||
| 465 | |||
| 466 | /** |
||
| 467 | * storage page selection. |
||
| 468 | * |
||
| 469 | * @return array |
||
| 470 | */ |
||
| 471 | protected function getStoragePageIds() |
||
| 492 | |||
| 493 | /** |
||
| 494 | * Get the default constraint for the queries. |
||
| 495 | * |
||
| 496 | * @param QueryInterface $query |
||
| 497 | * |
||
| 498 | * @return array |
||
| 499 | */ |
||
| 500 | protected function getDefaultConstraints(QueryInterface $query) |
||
| 525 | |||
| 526 | /** |
||
| 527 | * Add time frame related queries. |
||
| 528 | * |
||
| 529 | * @param array $constraints |
||
| 530 | * @param QueryInterface $query |
||
| 531 | * @param int $startTime |
||
| 532 | * @param int|null $endTime |
||
| 533 | * |
||
| 534 | * @see IndexUtility::isIndexInRange |
||
| 535 | */ |
||
| 536 | protected function addTimeFrameConstraints(&$constraints, QueryInterface $query, $startTime = null, $endTime = null) |
||
| 592 | |||
| 593 | /** |
||
| 594 | * Get the sorting. |
||
| 595 | * |
||
| 596 | * @param string $direction |
||
| 597 | * @param string $field |
||
| 598 | * |
||
| 599 | * @return array |
||
| 600 | */ |
||
| 601 | protected function getSorting($direction, $field = '') |
||
| 612 | } |
||
| 613 |