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:
1 | <?php |
||
47 | class SearchMappingService { |
||
48 | |||
49 | /** @var ConfigService */ |
||
50 | private $configService; |
||
51 | |||
52 | /** @var MiscService */ |
||
53 | private $miscService; |
||
54 | |||
55 | |||
56 | /** |
||
57 | * SearchMappingService constructor. |
||
58 | * |
||
59 | * @param ConfigService $configService |
||
60 | * @param MiscService $miscService |
||
61 | */ |
||
62 | public function __construct(ConfigService $configService, MiscService $miscService) { |
||
66 | |||
67 | |||
68 | /** |
||
69 | * @param ISearchRequest $request |
||
70 | * @param DocumentAccess $access |
||
71 | * @param string $providerId |
||
72 | * |
||
73 | * @return array |
||
74 | * @throws ConfigurationException |
||
75 | * @throws SearchQueryGenerationException |
||
76 | */ |
||
77 | public function generateSearchQuery( |
||
84 | |||
85 | |||
86 | /** |
||
87 | * @param ISearchRequest $request |
||
88 | * @param DocumentAccess $access |
||
89 | * @param string $providerId |
||
90 | * |
||
91 | * @return array |
||
92 | * @throws ConfigurationException |
||
93 | * @throws SearchQueryGenerationException |
||
94 | */ |
||
95 | public function generateSearchQueryParams( |
||
123 | |||
124 | |||
125 | /** |
||
126 | * @param ISearchRequest $request |
||
127 | * @param array $arr |
||
128 | */ |
||
129 | private function improveSearchQuerying(ISearchRequest $request, array &$arr) { |
||
134 | |||
135 | |||
136 | // /** |
||
137 | // * @param SearchRequest $request |
||
138 | // * @param array $arr |
||
139 | // */ |
||
140 | // private function improveSearchWildcardQueries(SearchRequest $request, &$arr) { |
||
141 | // |
||
142 | // $queries = $request->getWildcardQueries(); |
||
143 | // foreach ($queries as $query) { |
||
144 | // $wildcards = []; |
||
145 | // foreach ($query as $entry) { |
||
146 | // $wildcards[] = ['wildcard' => $entry]; |
||
147 | // } |
||
148 | // |
||
149 | // array_push($arr['bool']['must']['bool']['should'], $wildcards); |
||
150 | // } |
||
151 | // |
||
152 | // } |
||
153 | |||
154 | |||
155 | /** |
||
156 | * @param ISearchRequest $request |
||
157 | * @param array $arr |
||
158 | */ |
||
159 | View Code Duplication | private function improveSearchWildcardFilters(ISearchRequest $request, array &$arr) { |
|
172 | |||
173 | |||
174 | /** |
||
175 | * @param ISearchRequest $request |
||
176 | * @param array $arr |
||
177 | */ |
||
178 | View Code Duplication | private function improveSearchRegexFilters(ISearchRequest $request, array &$arr) { |
|
191 | |||
192 | |||
193 | /** |
||
194 | * @param ISearchRequest $request |
||
195 | * |
||
196 | * @return array |
||
197 | * @throws SearchQueryGenerationException |
||
198 | */ |
||
199 | private function generateSearchQueryContent(ISearchRequest $request): array { |
||
218 | |||
219 | |||
220 | /** |
||
221 | * @param string $word |
||
222 | * |
||
223 | * @return QueryContent |
||
224 | * @throws QueryContentGenerationException |
||
225 | */ |
||
226 | private function generateQueryContent(string $word): QueryContent { |
||
235 | |||
236 | |||
237 | /** |
||
238 | * @param ISearchRequest $request |
||
239 | * @param QueryContent[] $queryContents |
||
240 | * |
||
241 | * @return array |
||
242 | */ |
||
243 | private function generateSearchQueryFromQueryContent( |
||
260 | |||
261 | |||
262 | /** |
||
263 | * @param ISearchRequest $request |
||
264 | * @param QueryContent $content |
||
265 | * |
||
266 | * @return array |
||
267 | */ |
||
268 | private function generateQueryContentFields(ISearchRequest $request, QueryContent $content |
||
288 | |||
289 | |||
290 | /** |
||
291 | * @param DocumentAccess $access |
||
292 | * |
||
293 | * @return array |
||
294 | */ |
||
295 | private function generateSearchQueryAccess(DocumentAccess $access): array { |
||
312 | |||
313 | |||
314 | /** |
||
315 | * @param ISearchRequest $request |
||
316 | * @param string $field |
||
317 | * |
||
318 | * @return bool |
||
319 | */ |
||
320 | private function fieldIsOutLimit(ISearchRequest $request, string $field): bool { |
||
332 | |||
333 | |||
334 | /** |
||
335 | * @param string $k |
||
336 | * @param array $tags |
||
337 | * |
||
338 | * @return array |
||
339 | */ |
||
340 | private function generateSearchQueryTags(string $k, array $tags): array { |
||
349 | |||
350 | |||
351 | /** |
||
352 | * @param ISearchRequest $request |
||
353 | * |
||
354 | * @return array |
||
355 | */ |
||
356 | private function generateSearchHighlighting(ISearchRequest $request): array { |
||
370 | |||
371 | |||
372 | /** |
||
373 | * @param string $providerId |
||
374 | * @param string $documentId |
||
375 | * |
||
376 | * @return array |
||
377 | * @throws ConfigurationException |
||
378 | */ |
||
379 | public function getDocumentQuery(string $providerId, string $documentId): array { |
||
386 | |||
387 | |||
388 | /** |
||
389 | * @param ISearchRequest $request |
||
390 | * |
||
391 | * @return array |
||
392 | */ |
||
393 | private function getPartsFields(ISearchRequest $request) { |
||
400 | |||
401 | } |
||
402 | |||
403 |
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.