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 |
||
45 | class IndexService { |
||
46 | |||
47 | /** @var IndexesRequest */ |
||
48 | private $indexesRequest; |
||
49 | |||
50 | /** @var ConfigService */ |
||
51 | private $configService; |
||
52 | |||
53 | /** @var ProviderService */ |
||
54 | private $providerService; |
||
55 | |||
56 | /** @var PlatformService */ |
||
57 | private $platformService; |
||
58 | |||
59 | /** @var MiscService */ |
||
60 | private $miscService; |
||
61 | |||
62 | |||
63 | /** @var Runner */ |
||
64 | private $runner = null; |
||
65 | |||
66 | /** |
||
67 | * IndexService constructor. |
||
68 | * |
||
69 | * @param IndexesRequest $indexesRequest |
||
70 | * @param ConfigService $configService |
||
71 | * @param ProviderService $providerService |
||
72 | * @param PlatformService $platformService |
||
73 | * @param MiscService $miscService |
||
74 | */ |
||
75 | public function __construct( |
||
76 | IndexesRequest $indexesRequest, ConfigService $configService, ProviderService $providerService, |
||
77 | PlatformService $platformService, MiscService $miscService |
||
78 | ) { |
||
79 | $this->indexesRequest = $indexesRequest; |
||
80 | $this->configService = $configService; |
||
81 | $this->providerService = $providerService; |
||
82 | $this->platformService = $platformService; |
||
83 | $this->miscService = $miscService; |
||
84 | } |
||
85 | |||
86 | |||
87 | /** |
||
88 | * @param Runner $runner |
||
89 | */ |
||
90 | public function setRunner(Runner $runner) { |
||
93 | |||
94 | |||
95 | /** |
||
96 | * @param $action |
||
97 | * |
||
98 | * @throws InterruptException |
||
99 | * @throws TickDoesNotExistException |
||
100 | */ |
||
101 | private function updateRunner($action) { |
||
108 | |||
109 | |||
110 | /** |
||
111 | * @param IFullTextSearchPlatform $platform |
||
112 | * @param IFullTextSearchProvider $provider |
||
113 | * @param string $userId |
||
114 | * |
||
115 | * @throws Exception |
||
116 | */ |
||
117 | public function indexProviderContentFromUser( |
||
128 | |||
129 | |||
130 | /** |
||
131 | * @param IFullTextSearchProvider $provider |
||
132 | * @param IndexDocument[] $documents |
||
133 | * |
||
134 | * @return IndexDocument[] |
||
135 | * @throws InterruptException |
||
136 | * @throws TickDoesNotExistException |
||
137 | */ |
||
138 | private function updateDocumentsWithCurrIndex(IFullTextSearchProvider $provider, array $documents) { |
||
161 | |||
162 | |||
163 | /** |
||
164 | * @param IFullTextSearchProvider $provider |
||
165 | * @param IndexDocument $document |
||
166 | * |
||
167 | * @return bool |
||
168 | */ |
||
169 | private function isDocumentUpToDate(IFullTextSearchProvider $provider, IndexDocument $document) { |
||
182 | |||
183 | |||
184 | /** |
||
185 | * @param IFullTextSearchProvider $provider |
||
186 | * |
||
187 | * @return ProviderIndexes |
||
188 | */ |
||
189 | private function getProviderIndexFromProvider(IFullTextSearchProvider $provider) { |
||
194 | |||
195 | |||
196 | /** |
||
197 | * @param IFullTextSearchPlatform $platform |
||
198 | * @param IFullTextSearchProvider $provider |
||
199 | * @param IndexDocument[] $documents |
||
200 | * |
||
201 | * @throws Exception |
||
202 | */ |
||
203 | private function indexChunks( |
||
227 | |||
228 | |||
229 | /** |
||
230 | * @param IFullTextSearchPlatform $platform |
||
231 | * @param IFullTextSearchProvider $provider |
||
232 | * @param IndexDocument[] $chunk |
||
233 | * |
||
234 | * @throws NoResultException |
||
235 | * @throws DatabaseException |
||
236 | */ |
||
237 | private function indexChunk( |
||
250 | |||
251 | |||
252 | /** |
||
253 | * @param IndexDocument[] $documents |
||
254 | * |
||
255 | * @return array |
||
256 | */ |
||
257 | private function filterDocumentsToIndex($documents) { |
||
272 | |||
273 | |||
274 | /** |
||
275 | * @param IFullTextSearchPlatform $platform |
||
276 | * @param IFullTextSearchProvider $provider |
||
277 | * @param Index $index |
||
278 | * |
||
279 | * @internal param int|string $documentId |
||
280 | * @throws Exception |
||
281 | */ |
||
282 | public function updateDocument( |
||
305 | |||
306 | |||
307 | /** |
||
308 | * @param Index[] $indexes |
||
309 | * |
||
310 | * @throws DatabaseException |
||
311 | */ |
||
312 | public function updateIndexes($indexes) { |
||
322 | |||
323 | |||
324 | /** |
||
325 | * @param Index $index |
||
326 | * |
||
327 | * @throws Exception |
||
328 | */ |
||
329 | private function updateIndex(Index $index) { |
||
352 | |||
353 | |||
354 | /** |
||
355 | * @param string $providerId |
||
356 | * @param string|int $documentId |
||
357 | * @param int $status |
||
358 | * @param bool $reset |
||
359 | * |
||
360 | * @throws DatabaseException |
||
361 | */ |
||
362 | public function updateIndexStatus($providerId, $documentId, $status, $reset = false) { |
||
373 | |||
374 | |||
375 | /** |
||
376 | * @param string $providerId |
||
377 | * @param string|int $documentId |
||
378 | * |
||
379 | * @return ExtendedIndex |
||
380 | * @throws IndexDoesNotExistException |
||
381 | */ |
||
382 | public function getIndex($providerId, $documentId) { |
||
385 | |||
386 | |||
387 | /** |
||
388 | * @return Index[] |
||
389 | */ |
||
390 | public function getQueuedIndexes() { |
||
393 | |||
394 | |||
395 | /** |
||
396 | * @param string $providerId |
||
397 | * |
||
398 | * @throws Exception |
||
399 | */ |
||
400 | public function resetIndex($providerId = '') { |
||
419 | |||
420 | |||
421 | } |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.