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) |
||
| 55 | { |
||
| 56 | $this->indexTypes = $types; |
||
| 57 | } |
||
| 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 | * |
||
| 119 | * @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface |
||
| 120 | */ |
||
| 121 | public function findBySearch(\DateTime $startDate = null, \DateTime $endDate = null, array $customSearch = []) |
||
| 154 | /** |
||
| 155 | * Find Past Events |
||
| 156 | * |
||
| 157 | * @param int $limit |
||
| 158 | * @param string $sort |
||
| 159 | * |
||
| 160 | * @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface |
||
| 161 | */ |
||
| 162 | |||
| 163 | public function findByPast( |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Find by traversing information. |
||
| 185 | * |
||
| 186 | * @param Index $index |
||
| 187 | * @param bool|true $future |
||
| 188 | * @param bool|false $past |
||
| 189 | * @param int $limit |
||
| 190 | * @param string $sort |
||
| 191 | * @param bool $useIndexTime |
||
| 192 | * |
||
| 193 | * @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface |
||
| 194 | */ |
||
| 195 | public function findByTraversing( |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Find by traversing information. |
||
| 234 | * |
||
| 235 | * @param DomainObjectInterface $event |
||
| 236 | * @param bool|true $future |
||
| 237 | * @param bool|false $past |
||
| 238 | * @param int $limit |
||
| 239 | * @param string $sort |
||
| 240 | * |
||
| 241 | * @throws Exception |
||
| 242 | * |
||
| 243 | * @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface |
||
| 244 | */ |
||
| 245 | public function findByEventTraversing( |
||
| 280 | |||
| 281 | /** |
||
| 282 | * find Year. |
||
| 283 | * |
||
| 284 | * @param int $year |
||
| 285 | * |
||
| 286 | * @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface |
||
| 287 | */ |
||
| 288 | public function findYear($year) |
||
| 292 | |||
| 293 | /** |
||
| 294 | * find Month. |
||
| 295 | * |
||
| 296 | * @param int $year |
||
| 297 | * @param int $month |
||
| 298 | * |
||
| 299 | * @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface |
||
| 300 | */ |
||
| 301 | public function findMonth(int $year, int $month) |
||
| 308 | |||
| 309 | /** |
||
| 310 | * find Week. |
||
| 311 | * |
||
| 312 | * @param int $year |
||
| 313 | * @param int $week |
||
| 314 | * @param int $weekStart See documentation for settings.weekStart |
||
| 315 | * |
||
| 316 | * @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface |
||
| 317 | */ |
||
| 318 | public function findWeek($year, $week, $weekStart = 1) |
||
| 329 | |||
| 330 | /** |
||
| 331 | * find day. |
||
| 332 | * |
||
| 333 | * @param int $year |
||
| 334 | * @param int $month |
||
| 335 | * @param int $day |
||
| 336 | * |
||
| 337 | * @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface |
||
| 338 | */ |
||
| 339 | public function findDay($year, $month, $day) |
||
| 345 | |||
| 346 | /** |
||
| 347 | * Set the default sorting direction. |
||
| 348 | * |
||
| 349 | * @param string $direction |
||
| 350 | * @param string $field |
||
| 351 | */ |
||
| 352 | public function setDefaultSortingDirection($direction, $field = '') |
||
| 356 | |||
| 357 | /** |
||
| 358 | * Find by time slot. |
||
| 359 | * |
||
| 360 | * @param int $startTime |
||
| 361 | * @param int|null $endTime null means open end |
||
| 362 | * |
||
| 363 | * @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface |
||
| 364 | */ |
||
| 365 | public function findByTimeSlot($startTime, $endTime = null) |
||
| 373 | |||
| 374 | /** |
||
| 375 | * Find all indices by the given Event model. |
||
| 376 | * |
||
| 377 | * @param DomainObjectInterface $event |
||
| 378 | * |
||
| 379 | * @throws Exception |
||
| 380 | * |
||
| 381 | * @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface |
||
| 382 | */ |
||
| 383 | public function findByEvent(DomainObjectInterface $event) |
||
| 396 | |||
| 397 | /** |
||
| 398 | * storage page selection. |
||
| 399 | * |
||
| 400 | * @return array |
||
| 401 | */ |
||
| 402 | protected function getStoragePageIds() |
||
| 423 | |||
| 424 | /** |
||
| 425 | * Get the default constraint for the queries. |
||
| 426 | * |
||
| 427 | * @param QueryInterface $query |
||
| 428 | * |
||
| 429 | * @return array |
||
| 430 | */ |
||
| 431 | protected function getDefaultConstraints(QueryInterface $query) |
||
| 456 | |||
| 457 | /** |
||
| 458 | * Add time frame related queries. |
||
| 459 | * |
||
| 460 | * @param array $constraints |
||
| 461 | * @param QueryInterface $query |
||
| 462 | * @param int $startTime |
||
| 463 | * @param int|null $endTime |
||
| 464 | * |
||
| 465 | * @see IndexUtility::isIndexInRange |
||
| 466 | */ |
||
| 467 | protected function addTimeFrameConstraints(&$constraints, QueryInterface $query, $startTime = null, $endTime = null) |
||
| 523 | |||
| 524 | /** |
||
| 525 | * Get the sorting. |
||
| 526 | * |
||
| 527 | * @param string $direction |
||
| 528 | * @param string $field |
||
| 529 | * |
||
| 530 | * @return array |
||
| 531 | */ |
||
| 532 | protected function getSorting($direction, $field = '') |
||
| 543 | } |
||
| 544 |