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 | |||
57 | /** |
||
58 | * SearchMappingService constructor. |
||
59 | * |
||
60 | * @param ConfigService $configService |
||
61 | * @param MiscService $miscService |
||
62 | */ |
||
63 | public function __construct(ConfigService $configService, MiscService $miscService) { |
||
67 | |||
68 | |||
69 | /** |
||
70 | * @param ISearchRequest $request |
||
71 | * @param IDocumentAccess $access |
||
72 | * @param string $providerId |
||
73 | * |
||
74 | * @return array |
||
75 | * @throws ConfigurationException |
||
76 | * @throws SearchQueryGenerationException |
||
77 | */ |
||
78 | public function generateSearchQuery( |
||
85 | |||
86 | |||
87 | /** |
||
88 | * @param ISearchRequest $request |
||
89 | * @param IDocumentAccess $access |
||
90 | * @param string $providerId |
||
91 | * |
||
92 | * @return array |
||
93 | * @throws ConfigurationException |
||
94 | * @throws SearchQueryGenerationException |
||
95 | */ |
||
96 | public function generateSearchQueryParams( |
||
129 | |||
130 | |||
131 | /** |
||
132 | * @param ISearchRequest $request |
||
133 | * @param array $arr |
||
134 | */ |
||
135 | private function improveSearchQuerying(ISearchRequest $request, array &$arr) { |
||
140 | |||
141 | |||
142 | // /** |
||
143 | // * @param SearchRequest $request |
||
144 | // * @param array $arr |
||
145 | // */ |
||
146 | // private function improveSearchWildcardQueries(SearchRequest $request, &$arr) { |
||
147 | // |
||
148 | // $queries = $request->getWildcardQueries(); |
||
149 | // foreach ($queries as $query) { |
||
150 | // $wildcards = []; |
||
151 | // foreach ($query as $entry) { |
||
152 | // $wildcards[] = ['wildcard' => $entry]; |
||
153 | // } |
||
154 | // |
||
155 | // array_push($arr['bool']['must']['bool']['should'], $wildcards); |
||
156 | // } |
||
157 | // |
||
158 | // } |
||
159 | |||
160 | |||
161 | /** |
||
162 | * @param ISearchRequest $request |
||
163 | * @param array $arr |
||
164 | */ |
||
165 | View Code Duplication | private function improveSearchWildcardFilters(ISearchRequest $request, array &$arr) { |
|
178 | |||
179 | |||
180 | /** |
||
181 | * @param ISearchRequest $request |
||
182 | * @param array $arr |
||
183 | */ |
||
184 | View Code Duplication | private function improveSearchRegexFilters(ISearchRequest $request, array &$arr) { |
|
197 | |||
198 | |||
199 | /** |
||
200 | * @param ISearchRequest $request |
||
201 | * |
||
202 | * @return array |
||
203 | * @throws SearchQueryGenerationException |
||
204 | */ |
||
205 | private function generateSearchQueryContent(ISearchRequest $request): array { |
||
224 | |||
225 | |||
226 | /** |
||
227 | * @param string $word |
||
228 | * |
||
229 | * @return QueryContent |
||
230 | * @throws QueryContentGenerationException |
||
231 | */ |
||
232 | private function generateQueryContent(string $word): QueryContent { |
||
241 | |||
242 | |||
243 | /** |
||
244 | * @param ISearchRequest $request |
||
245 | * @param QueryContent[] $queryContents |
||
246 | * |
||
247 | * @return array |
||
248 | */ |
||
249 | private function generateSearchQueryFromQueryContent( |
||
266 | |||
267 | |||
268 | /** |
||
269 | * @param ISearchRequest $request |
||
270 | * @param QueryContent $content |
||
271 | * |
||
272 | * @return array |
||
273 | */ |
||
274 | private function generateQueryContentFields(ISearchRequest $request, QueryContent $content |
||
309 | |||
310 | |||
311 | /** |
||
312 | * @param IDocumentAccess $access |
||
313 | * |
||
314 | * @return array |
||
315 | */ |
||
316 | private function generateSearchQueryAccess(IDocumentAccess $access): array { |
||
333 | |||
334 | |||
335 | /** |
||
336 | * @param ISearchRequest $request |
||
337 | * @param string $field |
||
338 | * |
||
339 | * @return bool |
||
340 | */ |
||
341 | private function fieldIsOutLimit(ISearchRequest $request, string $field): bool { |
||
353 | |||
354 | |||
355 | /** |
||
356 | * @param string $k |
||
357 | * @param array $tags |
||
358 | * |
||
359 | * @return array |
||
360 | */ |
||
361 | private function generateSearchQueryTags(string $k, array $tags): array { |
||
370 | |||
371 | |||
372 | /** |
||
373 | * @param ISearchRequestSimpleQuery[] $queries |
||
374 | * |
||
375 | * @return array |
||
376 | */ |
||
377 | private function generateSearchSimpleQuery(array $queries): array { |
||
410 | |||
411 | |||
412 | /** |
||
413 | * @param ISearchRequest $request |
||
414 | * |
||
415 | * @return array |
||
416 | */ |
||
417 | private function generateSearchHighlighting(ISearchRequest $request): array { |
||
431 | |||
432 | |||
433 | /** |
||
434 | * @param string $providerId |
||
435 | * @param string $documentId |
||
436 | * |
||
437 | * @return array |
||
438 | * @throws ConfigurationException |
||
439 | */ |
||
440 | public function getDocumentQuery(string $providerId, string $documentId): array { |
||
447 | |||
448 | |||
449 | /** |
||
450 | * @param ISearchRequest $request |
||
451 | * |
||
452 | * @return array |
||
453 | */ |
||
454 | private function getPartsFields(ISearchRequest $request) { |
||
461 | |||
462 | } |
||
463 | |||
464 |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.