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 SearchMappingService 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 SearchMappingService, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
48 | class SearchMappingService { |
||
49 | |||
50 | /** @var ConfigService */ |
||
51 | private $configService; |
||
52 | |||
53 | /** @var MiscService */ |
||
54 | private $miscService; |
||
55 | |||
56 | /** @var IUserStoragesService */ |
||
57 | private $userStoragesService; |
||
58 | |||
59 | /** |
||
60 | * SearchMappingService constructor. |
||
61 | * |
||
62 | * @param ConfigService $configService |
||
63 | * @param MiscService $miscService |
||
64 | * @param IUserStoragesService $userStoragesService |
||
|
|||
65 | */ |
||
66 | public function __construct(ConfigService $configService, MiscService $miscService, IUserStoragesService $userStoragesService = null) { |
||
71 | |||
72 | |||
73 | /** |
||
74 | * @param ISearchRequest $request |
||
75 | * @param IDocumentAccess $access |
||
76 | * @param string $providerId |
||
77 | * |
||
78 | * @return array |
||
79 | * @throws ConfigurationException |
||
80 | * @throws SearchQueryGenerationException |
||
81 | */ |
||
82 | public function generateSearchQuery( |
||
89 | |||
90 | |||
91 | /** |
||
92 | * @param ISearchRequest $request |
||
93 | * @param IDocumentAccess $access |
||
94 | * @param string $providerId |
||
95 | * |
||
96 | * @return array |
||
97 | * @throws ConfigurationException |
||
98 | * @throws SearchQueryGenerationException |
||
99 | */ |
||
100 | public function generateSearchQueryParams( |
||
135 | |||
136 | |||
137 | /** |
||
138 | * @param ISearchRequest $request |
||
139 | * @param array $arr |
||
140 | */ |
||
141 | private function improveSearchQuerying(ISearchRequest $request, array &$arr) { |
||
146 | |||
147 | |||
148 | // /** |
||
149 | // * @param SearchRequest $request |
||
150 | // * @param array $arr |
||
151 | // */ |
||
152 | // private function improveSearchWildcardQueries(SearchRequest $request, &$arr) { |
||
153 | // |
||
154 | // $queries = $request->getWildcardQueries(); |
||
155 | // foreach ($queries as $query) { |
||
156 | // $wildcards = []; |
||
157 | // foreach ($query as $entry) { |
||
158 | // $wildcards[] = ['wildcard' => $entry]; |
||
159 | // } |
||
160 | // |
||
161 | // array_push($arr['bool']['must']['bool']['should'], $wildcards); |
||
162 | // } |
||
163 | // |
||
164 | // } |
||
165 | |||
166 | |||
167 | /** |
||
168 | * @param ISearchRequest $request |
||
169 | * @param array $arr |
||
170 | */ |
||
171 | View Code Duplication | private function improveSearchWildcardFilters(ISearchRequest $request, array &$arr) { |
|
184 | |||
185 | |||
186 | /** |
||
187 | * @param ISearchRequest $request |
||
188 | * @param array $arr |
||
189 | */ |
||
190 | View Code Duplication | private function improveSearchRegexFilters(ISearchRequest $request, array &$arr) { |
|
203 | |||
204 | |||
205 | /** |
||
206 | * @param ISearchRequest $request |
||
207 | * |
||
208 | * @return array |
||
209 | * @throws SearchQueryGenerationException |
||
210 | */ |
||
211 | private function generateSearchQueryContent(ISearchRequest $request): array { |
||
230 | |||
231 | |||
232 | /** |
||
233 | * @param string $word |
||
234 | * |
||
235 | * @return QueryContent |
||
236 | * @throws QueryContentGenerationException |
||
237 | */ |
||
238 | private function generateQueryContent(string $word): QueryContent { |
||
247 | |||
248 | |||
249 | /** |
||
250 | * @param ISearchRequest $request |
||
251 | * @param QueryContent[] $queryContents |
||
252 | * |
||
253 | * @return array |
||
254 | */ |
||
255 | private function generateSearchQueryFromQueryContent( |
||
272 | |||
273 | |||
274 | /** |
||
275 | * @param ISearchRequest $request |
||
276 | * @param QueryContent $content |
||
277 | * |
||
278 | * @return array |
||
279 | */ |
||
280 | private function generateQueryContentFields(ISearchRequest $request, QueryContent $content |
||
315 | |||
316 | |||
317 | /** |
||
318 | * @param IDocumentAccess $access |
||
319 | * |
||
320 | * @return array |
||
321 | */ |
||
322 | private function generateSearchQueryAccess(IDocumentAccess $access): array { |
||
355 | |||
356 | /** |
||
357 | * @return array |
||
358 | */ |
||
359 | private function getExternalFileShares() : array { |
||
365 | |||
366 | |||
367 | /** |
||
368 | * @param ISearchRequest $request |
||
369 | * @param string $field |
||
370 | * |
||
371 | * @return bool |
||
372 | */ |
||
373 | private function fieldIsOutLimit(ISearchRequest $request, string $field): bool { |
||
385 | |||
386 | |||
387 | /** |
||
388 | * @param string $k |
||
389 | * @param array $tags |
||
390 | * |
||
391 | * @return array |
||
392 | */ |
||
393 | private function generateSearchQueryTags(string $k, array $tags): array { |
||
402 | |||
403 | |||
404 | /** |
||
405 | * @param ISearchRequestSimpleQuery[] $queries |
||
406 | * |
||
407 | * @return array |
||
408 | */ |
||
409 | private function generateSearchSimpleQuery(array $queries): array { |
||
453 | |||
454 | |||
455 | /** |
||
456 | * @param ISearchRequest $request |
||
457 | * |
||
458 | * @return array |
||
459 | */ |
||
460 | private function generateSearchHighlighting(ISearchRequest $request): array { |
||
474 | |||
475 | |||
476 | /** |
||
477 | * @param string $providerId |
||
478 | * @param string $documentId |
||
479 | * |
||
480 | * @return array |
||
481 | * @throws ConfigurationException |
||
482 | */ |
||
483 | public function getDocumentQuery(string $providerId, string $documentId): array { |
||
490 | |||
491 | |||
492 | /** |
||
493 | * @param ISearchRequest $request |
||
494 | * |
||
495 | * @return array |
||
496 | */ |
||
497 | private function getPartsFields(ISearchRequest $request) { |
||
504 | |||
505 | } |
||
506 | |||
507 |
This check looks for
@param
annotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.