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 |
||
| 23 | class IndexRepository extends AbstractRepository |
||
| 24 | { |
||
| 25 | /** |
||
| 26 | * Default orderings for index records. |
||
| 27 | * |
||
| 28 | * @var array |
||
| 29 | */ |
||
| 30 | protected $defaultOrderings = [ |
||
| 31 | 'start_date' => QueryInterface::ORDER_ASCENDING, |
||
| 32 | 'start_time' => QueryInterface::ORDER_ASCENDING, |
||
| 33 | ]; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Index types for selection. |
||
| 37 | * |
||
| 38 | * @var array |
||
| 39 | */ |
||
| 40 | protected $indexTypes = []; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Override page ids. |
||
| 44 | * |
||
| 45 | * @var array |
||
| 46 | */ |
||
| 47 | protected $overridePageIds = []; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Set the index types. |
||
| 51 | * |
||
| 52 | * @param array $types |
||
| 53 | */ |
||
| 54 | public function setIndexTypes(array $types) |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Override page IDs. |
||
| 61 | * |
||
| 62 | * @param array $overridePageIds |
||
| 63 | */ |
||
| 64 | public function setOverridePageIds($overridePageIds) |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Find List. |
||
| 71 | * |
||
| 72 | * @param int $limit |
||
| 73 | * @param int|string $listStartTime |
||
| 74 | * @param int $startOffsetHours |
||
| 75 | * @param int $overrideStartDate |
||
| 76 | * @param int $overrideEndDate |
||
| 77 | * |
||
| 78 | * @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface |
||
| 79 | */ |
||
| 80 | public function findList( |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Find by custom search. |
||
| 114 | * |
||
| 115 | * @param \DateTime $startDate |
||
| 116 | * @param \DateTime $endDate |
||
| 117 | * @param array $customSearch |
||
| 118 | * @param int $limit |
||
| 119 | * |
||
| 120 | * @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface |
||
| 121 | */ |
||
| 122 | public function findBySearch(\DateTime $startDate = null, \DateTime $endDate = null, array $customSearch = [], int $limit = 0) |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Find Past Events. |
||
| 165 | * |
||
| 166 | * @param int $limit |
||
| 167 | * @param string $sort |
||
| 168 | * |
||
| 169 | * @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface |
||
| 170 | */ |
||
| 171 | public function findByPast( |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Find by traversing information. |
||
| 191 | * |
||
| 192 | * @param Index $index |
||
| 193 | * @param bool|true $future |
||
| 194 | * @param bool|false $past |
||
| 195 | * @param int $limit |
||
| 196 | * @param string $sort |
||
| 197 | * @param bool $useIndexTime |
||
| 198 | * |
||
| 199 | * @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface |
||
| 200 | */ |
||
| 201 | public function findByTraversing( |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Find by traversing information. |
||
| 240 | * |
||
| 241 | * @param DomainObjectInterface $event |
||
| 242 | * @param bool|true $future |
||
| 243 | * @param bool|false $past |
||
| 244 | * @param int $limit |
||
| 245 | * @param string $sort |
||
| 246 | * |
||
| 247 | * @throws Exception |
||
| 248 | * |
||
| 249 | * @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface |
||
| 250 | */ |
||
| 251 | public function findByEventTraversing( |
||
| 286 | |||
| 287 | /** |
||
| 288 | * find Year. |
||
| 289 | * |
||
| 290 | * @param int $year |
||
| 291 | * |
||
| 292 | * @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface |
||
| 293 | */ |
||
| 294 | public function findYear(int $year) |
||
| 298 | |||
| 299 | /** |
||
| 300 | * find Month. |
||
| 301 | * |
||
| 302 | * @param int $year |
||
| 303 | * @param int $month |
||
| 304 | * |
||
| 305 | * @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface |
||
| 306 | */ |
||
| 307 | public function findMonth(int $year, int $month) |
||
| 314 | |||
| 315 | /** |
||
| 316 | * find Week. |
||
| 317 | * |
||
| 318 | * @param int $year |
||
| 319 | * @param int $week |
||
| 320 | * @param int $weekStart See documentation for settings.weekStart |
||
| 321 | * |
||
| 322 | * @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface |
||
| 323 | */ |
||
| 324 | public function findWeek($year, $week, $weekStart = 1) |
||
| 335 | |||
| 336 | /** |
||
| 337 | * Find different types and locations |
||
| 338 | * |
||
| 339 | * @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface |
||
| 340 | */ |
||
| 341 | public function findDifferentTypesAndLocations() |
||
| 350 | |||
| 351 | /** |
||
| 352 | * find day. |
||
| 353 | * |
||
| 354 | * @param int $year |
||
| 355 | * @param int $month |
||
| 356 | * @param int $day |
||
| 357 | * |
||
| 358 | * @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface |
||
| 359 | */ |
||
| 360 | public function findDay(int $year, int $month, int $day) |
||
| 366 | |||
| 367 | /** |
||
| 368 | * Set the default sorting direction. |
||
| 369 | * |
||
| 370 | * @param string $direction |
||
| 371 | * @param string $field |
||
| 372 | */ |
||
| 373 | public function setDefaultSortingDirection($direction, $field = '') |
||
| 377 | |||
| 378 | /** |
||
| 379 | * Find by time slot. |
||
| 380 | * |
||
| 381 | * @param int $startTime |
||
| 382 | * @param int|null $endTime null means open end |
||
| 383 | * |
||
| 384 | * @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface |
||
| 385 | */ |
||
| 386 | public function findByTimeSlot($startTime, $endTime = null) |
||
| 394 | |||
| 395 | /** |
||
| 396 | * Find all indices by the given Event model. |
||
| 397 | * |
||
| 398 | * @param DomainObjectInterface $event |
||
| 399 | * |
||
| 400 | * @throws Exception |
||
| 401 | * |
||
| 402 | * @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface |
||
| 403 | */ |
||
| 404 | public function findByEvent(DomainObjectInterface $event) |
||
| 417 | |||
| 418 | /** |
||
| 419 | * storage page selection. |
||
| 420 | * |
||
| 421 | * @return array |
||
| 422 | */ |
||
| 423 | protected function getStoragePageIds() |
||
| 444 | |||
| 445 | /** |
||
| 446 | * Get the default constraint for the queries. |
||
| 447 | * |
||
| 448 | * @param QueryInterface $query |
||
| 449 | * |
||
| 450 | * @return array |
||
| 451 | */ |
||
| 452 | protected function getDefaultConstraints(QueryInterface $query) |
||
| 477 | |||
| 478 | /** |
||
| 479 | * Add time frame related queries. |
||
| 480 | * |
||
| 481 | * @param array $constraints |
||
| 482 | * @param QueryInterface $query |
||
| 483 | * @param int $startTime |
||
| 484 | * @param int|null $endTime |
||
| 485 | * |
||
| 486 | * @see IndexUtility::isIndexInRange |
||
| 487 | */ |
||
| 488 | protected function addTimeFrameConstraints(&$constraints, QueryInterface $query, $startTime = null, $endTime = null) |
||
| 544 | |||
| 545 | /** |
||
| 546 | * Get the sorting. |
||
| 547 | * |
||
| 548 | * @param string $direction |
||
| 549 | * @param string $field |
||
| 550 | * |
||
| 551 | * @return array |
||
| 552 | */ |
||
| 553 | protected function getSorting($direction, $field = '') |
||
| 564 | } |
||
| 565 |