Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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 |
||
44 | class IndexService { |
||
45 | |||
46 | /** @var IndexesRequest */ |
||
47 | private $indexesRequest; |
||
48 | |||
49 | /** @var ConfigService */ |
||
50 | private $configService; |
||
51 | |||
52 | /** @var ProviderService */ |
||
53 | private $providerService; |
||
54 | |||
55 | /** @var PlatformService */ |
||
56 | private $platformService; |
||
57 | |||
58 | /** @var MiscService */ |
||
59 | private $miscService; |
||
60 | |||
61 | |||
62 | /** @var Runner */ |
||
63 | private $runner = null; |
||
64 | |||
65 | /** |
||
66 | * IndexService constructor. |
||
67 | * |
||
68 | * @param IndexesRequest $indexesRequest |
||
69 | * @param ConfigService $configService |
||
70 | * @param ProviderService $providerService |
||
71 | * @param PlatformService $platformService |
||
72 | * @param MiscService $miscService |
||
73 | */ |
||
74 | View Code Duplication | public function __construct( |
|
84 | |||
85 | |||
86 | /** |
||
87 | * @param Runner $runner |
||
88 | */ |
||
89 | public function setRunner(Runner $runner) { |
||
92 | |||
93 | |||
94 | /** |
||
95 | * @param $action |
||
96 | * |
||
97 | * @throws InterruptException |
||
98 | * @throws TickDoesNotExistException |
||
99 | */ |
||
100 | private function updateRunner($action) { |
||
107 | |||
108 | |||
109 | /** |
||
110 | * @param IFullTextSearchPlatform $platform |
||
111 | * @param IFullTextSearchProvider $provider |
||
112 | * @param string $userId |
||
113 | * |
||
114 | * @throws Exception |
||
115 | */ |
||
116 | public function indexProviderContentFromUser( |
||
127 | |||
128 | |||
129 | /** |
||
130 | * @param IFullTextSearchProvider $provider |
||
131 | * @param IndexDocument[] $documents |
||
132 | * |
||
133 | * @return IndexDocument[] |
||
134 | * @throws InterruptException |
||
135 | * @throws TickDoesNotExistException |
||
136 | */ |
||
137 | private function updateDocumentsWithCurrIndex(IFullTextSearchProvider $provider, array $documents) { |
||
160 | |||
161 | |||
162 | /** |
||
163 | * @param IFullTextSearchProvider $provider |
||
164 | * @param IndexDocument $document |
||
165 | * |
||
166 | * @return bool |
||
167 | */ |
||
168 | private function isDocumentUpToDate(IFullTextSearchProvider $provider, IndexDocument $document) { |
||
181 | |||
182 | |||
183 | /** |
||
184 | * @param IFullTextSearchProvider $provider |
||
185 | * |
||
186 | * @return ProviderIndexes |
||
187 | */ |
||
188 | private function getProviderIndexFromProvider(IFullTextSearchProvider $provider) { |
||
193 | |||
194 | |||
195 | /** |
||
196 | * @param IFullTextSearchPlatform $platform |
||
197 | * @param IFullTextSearchProvider $provider |
||
198 | * @param IndexDocument[] $documents |
||
199 | * |
||
200 | * @throws Exception |
||
201 | */ |
||
202 | private function indexChunks( |
||
226 | |||
227 | |||
228 | /** |
||
229 | * @param IFullTextSearchPlatform $platform |
||
230 | * @param IFullTextSearchProvider $provider |
||
231 | * @param IndexDocument[] $chunk |
||
232 | * |
||
233 | * @throws NoResultException |
||
234 | * @throws DatabaseException |
||
235 | */ |
||
236 | private function indexChunk( |
||
248 | |||
249 | |||
250 | /** |
||
251 | * @param IndexDocument[] $documents |
||
252 | * |
||
253 | * @return array |
||
254 | */ |
||
255 | private function filterDocumentsToIndex($documents) { |
||
268 | |||
269 | |||
270 | /** |
||
271 | * @param IFullTextSearchPlatform $platform |
||
272 | * @param IFullTextSearchProvider $provider |
||
273 | * @param Index $index |
||
274 | * |
||
275 | * @internal param int|string $documentId |
||
276 | * @throws Exception |
||
277 | */ |
||
278 | public function updateDocument( |
||
291 | |||
292 | |||
293 | /** |
||
294 | * @param Index[] $indexes |
||
295 | * |
||
296 | * @throws DatabaseException |
||
297 | */ |
||
298 | public function updateIndexes($indexes) { |
||
308 | |||
309 | |||
310 | /** |
||
311 | * @param Index $index |
||
312 | * |
||
313 | * @throws Exception |
||
314 | */ |
||
315 | private function updateIndex(Index $index) { |
||
334 | |||
335 | |||
336 | /** |
||
337 | * @param string $providerId |
||
338 | * @param string|int $documentId |
||
339 | * @param int $status |
||
340 | * @param bool $reset |
||
341 | * |
||
342 | * @throws DatabaseException |
||
343 | */ |
||
344 | public function updateIndexStatus($providerId, $documentId, $status, $reset = false) { |
||
355 | |||
356 | |||
357 | /** |
||
358 | * @param string $providerId |
||
359 | * @param string|int $documentId |
||
360 | * |
||
361 | * @return ExtendedIndex |
||
362 | * @throws IndexDoesNotExistException |
||
363 | */ |
||
364 | public function getIndex($providerId, $documentId) { |
||
367 | |||
368 | |||
369 | /** |
||
370 | * @return Index[] |
||
371 | */ |
||
372 | public function getQueuedIndexes() { |
||
375 | |||
376 | |||
377 | /** |
||
378 | * @param string $providerId |
||
379 | * |
||
380 | * @throws Exception |
||
381 | */ |
||
382 | public function resetIndex($providerId = '') { |
||
401 | |||
402 | |||
403 | } |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.