Complex classes like IndexService 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 IndexService, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
57 | class IndexService implements IIndexService { |
||
58 | |||
59 | |||
60 | /** @var IndexesRequest */ |
||
61 | private $indexesRequest; |
||
62 | |||
63 | /** @var ConfigService */ |
||
64 | private $configService; |
||
65 | |||
66 | /** @var ProviderService */ |
||
67 | private $providerService; |
||
68 | |||
69 | /** @var PlatformService */ |
||
70 | private $platformService; |
||
71 | |||
72 | /** @var MiscService */ |
||
73 | private $miscService; |
||
74 | |||
75 | |||
76 | /** @var Runner */ |
||
77 | private $runner = null; |
||
78 | |||
79 | /** @var array */ |
||
80 | private $queuedDeleteIndex = []; |
||
81 | |||
82 | /** @var int */ |
||
83 | private $currentTotalDocuments = 0; |
||
84 | |||
85 | |||
86 | /** |
||
87 | * IndexService constructor. |
||
88 | * |
||
89 | * @param IndexesRequest $indexesRequest |
||
90 | * @param ConfigService $configService |
||
91 | * @param ProviderService $providerService |
||
92 | * @param PlatformService $platformService |
||
93 | * @param MiscService $miscService |
||
94 | */ |
||
95 | public function __construct( |
||
105 | |||
106 | |||
107 | /** |
||
108 | * @param Runner $runner |
||
109 | */ |
||
110 | public function setRunner(Runner $runner) { |
||
113 | |||
114 | |||
115 | /** |
||
116 | * @param string $action |
||
117 | * @param bool $force |
||
118 | * |
||
119 | * @throws Exception |
||
120 | */ |
||
121 | private function updateRunnerAction(string $action, bool $force = false) { |
||
128 | |||
129 | /** |
||
130 | * @param string $info |
||
131 | * @param string $value |
||
132 | * @param int $color |
||
133 | */ |
||
134 | private function updateRunnerInfo( |
||
143 | |||
144 | /** |
||
145 | * @param array $data |
||
146 | */ |
||
147 | private function updateRunnerInfoArray(array $data) { |
||
154 | |||
155 | |||
156 | /** |
||
157 | * @param IFullTextSearchPlatform $platform |
||
158 | * @param IFullTextSearchProvider $provider |
||
159 | * @param string $userId |
||
160 | * @param IndexOptions $options |
||
161 | * |
||
162 | * @throws Exception |
||
163 | */ |
||
164 | public function indexProviderContentFromUser( |
||
208 | |||
209 | |||
210 | /** |
||
211 | * @param IFullTextSearchProvider $provider |
||
212 | * @param IIndexDocument[] $documents |
||
213 | * @param IIndexOptions $options |
||
214 | * |
||
215 | * @return IIndexDocument[] |
||
216 | * @throws Exception |
||
217 | */ |
||
218 | private function updateDocumentsWithCurrIndex( |
||
258 | |||
259 | |||
260 | /** |
||
261 | * @param IFullTextSearchProvider $provider |
||
262 | * @param IIndexDocument $document |
||
263 | * |
||
264 | * @return bool |
||
265 | */ |
||
266 | private function isDocumentUpToDate(IFullTextSearchProvider $provider, IIndexDocument $document |
||
279 | |||
280 | |||
281 | /** |
||
282 | * @param string $providerId |
||
283 | * |
||
284 | * @return ProviderIndexes |
||
285 | */ |
||
286 | private function getProviderIndexFromProvider(string $providerId): ProviderIndexes { |
||
291 | |||
292 | |||
293 | /** |
||
294 | * @param IFullTextSearchPlatform $platform |
||
295 | * @param IFullTextSearchProvider $provider |
||
296 | * @param IIndexDocument[] $documents |
||
297 | * @param IndexOptions $options |
||
298 | * |
||
299 | * @throws Exception |
||
300 | */ |
||
301 | private function indexDocuments( |
||
344 | |||
345 | |||
346 | /** |
||
347 | * @param IIndexDocument $document |
||
348 | * |
||
349 | * @throws NotIndexableDocumentException |
||
350 | */ |
||
351 | private function filterDocumentBeforeIndex(IIndexDocument $document) { |
||
363 | |||
364 | |||
365 | /** |
||
366 | * @param IFullTextSearchPlatform $platform |
||
367 | * @param IIndexDocument $document |
||
368 | * |
||
369 | * @return IIndex |
||
370 | * @throws Exception |
||
371 | */ |
||
372 | public function indexDocument(IFullTextSearchPlatform $platform, IIndexDocument $document |
||
392 | |||
393 | |||
394 | /** |
||
395 | * @param IFullTextSearchPlatform $platform |
||
396 | * @param IFullTextSearchProvider $provider |
||
397 | * @param Index $index |
||
398 | * |
||
399 | * @throws Exception |
||
400 | */ |
||
401 | public function updateDocument( |
||
444 | |||
445 | |||
446 | /** |
||
447 | * @param Index[] $indexes |
||
448 | * |
||
449 | * @throws DatabaseException |
||
450 | */ |
||
451 | public function updateIndexes(array $indexes) { |
||
461 | |||
462 | |||
463 | /** |
||
464 | * @param IIndex $index |
||
465 | * |
||
466 | * @throws Exception |
||
467 | */ |
||
468 | private function updateIndex(IIndex $index) { |
||
493 | |||
494 | |||
495 | /** |
||
496 | * @param IIndex $index |
||
497 | */ |
||
498 | private function updateIndexError(IIndex $index) { |
||
501 | |||
502 | |||
503 | /** |
||
504 | * @param string $providerId |
||
505 | * @param string $documentId |
||
506 | * @param int $status |
||
507 | * @param bool $reset |
||
508 | * |
||
509 | * @throws Exception |
||
510 | */ |
||
511 | public function updateIndexStatus( |
||
529 | |||
530 | |||
531 | /** |
||
532 | * @param string $providerId |
||
533 | * @param array $documentIds |
||
534 | * @param int $status |
||
535 | * @param bool $reset |
||
536 | * |
||
537 | * @throws DatabaseException |
||
538 | */ |
||
539 | public function updateIndexesStatus( |
||
559 | |||
560 | |||
561 | /** |
||
562 | * @param Index $index |
||
563 | */ |
||
564 | public function resetErrorFromIndex(Index $index) { |
||
569 | |||
570 | |||
571 | /** |
||
572 | * |
||
573 | */ |
||
574 | private function resetErrorFromQueue() { |
||
579 | |||
580 | /** |
||
581 | * |
||
582 | */ |
||
583 | public function resetErrorsAll() { |
||
586 | |||
587 | |||
588 | /** |
||
589 | * @return Index[] |
||
590 | */ |
||
591 | public function getErrorIndexes(): array { |
||
594 | |||
595 | |||
596 | /** |
||
597 | * @param string $providerId |
||
598 | * @param array $documentId |
||
599 | * |
||
600 | * @return Index[] |
||
601 | * @throws IndexDoesNotExistException |
||
602 | */ |
||
603 | public function getIndexes($providerId, $documentId) { |
||
606 | |||
607 | |||
608 | /** |
||
609 | * @param bool $all |
||
610 | * |
||
611 | * @return Index[] |
||
612 | */ |
||
613 | public function getQueuedIndexes(bool $all = false): array { |
||
616 | |||
617 | |||
618 | /** |
||
619 | * @param string $providerId |
||
620 | * |
||
621 | * @throws Exception |
||
622 | */ |
||
623 | public function resetIndex(string $providerId = '') { |
||
648 | |||
649 | |||
650 | /** |
||
651 | * @param string $providerId |
||
652 | * @param string $documentId |
||
653 | * |
||
654 | * @return IIndex |
||
655 | * @throws IndexDoesNotExistException |
||
656 | */ |
||
657 | public function getIndex(string $providerId, string $documentId): IIndex { |
||
660 | |||
661 | |||
662 | /** |
||
663 | * @param string $providerId |
||
664 | * @param string $documentId |
||
665 | * @param string $userId |
||
666 | * @param int $status |
||
667 | * |
||
668 | * @return IIndex |
||
669 | */ |
||
670 | public function createIndex(string $providerId, string $documentId, string $userId, int $status |
||
689 | |||
690 | } |
||
691 |