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 |
||
38 | class SearchMappingService { |
||
39 | |||
40 | /** @var ConfigService */ |
||
41 | private $configService; |
||
42 | |||
43 | /** @var MiscService */ |
||
44 | private $miscService; |
||
45 | |||
46 | |||
47 | /** |
||
48 | * MappingService constructor. |
||
49 | * |
||
50 | * @param ConfigService $configService |
||
51 | * @param MiscService $miscService |
||
52 | */ |
||
53 | public function __construct(ConfigService $configService, MiscService $miscService) { |
||
57 | |||
58 | |||
59 | /** |
||
60 | * @param IFullTextSearchProvider $provider |
||
61 | * @param DocumentAccess $access |
||
62 | * @param SearchRequest $request |
||
63 | * |
||
64 | * @return array |
||
|
|||
65 | * @throws ConfigurationException |
||
66 | * @throws SearchQueryGenerationException |
||
67 | */ |
||
68 | public function generateSearchQuery( |
||
75 | |||
76 | |||
77 | /** |
||
78 | * @param IFullTextSearchProvider $provider |
||
79 | * @param DocumentAccess $access |
||
80 | * @param SearchRequest $request |
||
81 | * |
||
82 | * @return array |
||
83 | * @throws ConfigurationException |
||
84 | * @throws SearchQueryGenerationException |
||
85 | */ |
||
86 | public function generateSearchQueryParams( |
||
110 | |||
111 | |||
112 | /** |
||
113 | * @param SearchRequest $request |
||
114 | * @param array $arr |
||
115 | */ |
||
116 | private function improveSearchQuerying(SearchRequest $request, &$arr) { |
||
121 | |||
122 | |||
123 | /** |
||
124 | * @param SearchRequest $request |
||
125 | * @param array $arr |
||
126 | */ |
||
127 | View Code Duplication | private function improveSearchWildcardQueries(SearchRequest $request, &$arr) { |
|
140 | |||
141 | |||
142 | /** |
||
143 | * @param SearchRequest $request |
||
144 | * @param array $arr |
||
145 | */ |
||
146 | View Code Duplication | private function improveSearchWildcardFilters(SearchRequest $request, &$arr) { |
|
159 | |||
160 | |||
161 | /** |
||
162 | * @param SearchRequest $request |
||
163 | * @param array $arr |
||
164 | */ |
||
165 | View Code Duplication | private function improveSearchRegexFilters(SearchRequest $request, &$arr) { |
|
166 | |||
167 | $filters = $request->getRegexFilters(); |
||
168 | foreach ($filters as $filter) { |
||
169 | $regex = []; |
||
170 | foreach ($filter as $entry) { |
||
171 | $regex[] = ['regexp' => $entry]; |
||
172 | } |
||
173 | |||
174 | $arr['bool']['filter'][]['bool']['should'] = $regex; |
||
175 | } |
||
176 | |||
177 | } |
||
178 | |||
179 | |||
180 | /** |
||
181 | * @param SearchRequest $request |
||
182 | * |
||
183 | * @return array<string,array<string,array>> |
||
184 | * @throws SearchQueryGenerationException |
||
185 | */ |
||
186 | private function generateSearchQueryContent(SearchRequest $request) { |
||
205 | |||
206 | |||
207 | /** |
||
208 | * @param string $word |
||
209 | * |
||
210 | * @return QueryContent |
||
211 | * @throws QueryContentGenerationException |
||
212 | */ |
||
213 | private function generateQueryContent($word) { |
||
222 | |||
223 | |||
224 | /** |
||
225 | * @param SearchRequest $request |
||
226 | * @param QueryContent[] $queryContents |
||
227 | * |
||
228 | * @return array |
||
229 | */ |
||
230 | private function generateSearchQueryFromQueryContent(SearchRequest $request, $queryContents) { |
||
245 | |||
246 | |||
247 | /** |
||
248 | * @param SearchRequest $request |
||
249 | * @param QueryContent $content |
||
250 | * |
||
251 | * @return array |
||
252 | */ |
||
253 | private function generateQueryContentFields(SearchRequest $request, QueryContent $content) { |
||
276 | |||
277 | |||
278 | /** |
||
279 | * @param DocumentAccess $access |
||
280 | * |
||
281 | * @return array<string,array> |
||
282 | */ |
||
283 | private function generateSearchQueryAccess(DocumentAccess $access) { |
||
300 | |||
301 | |||
302 | /** |
||
303 | * @param SearchRequest $request |
||
304 | * @param string $field |
||
305 | * |
||
306 | * @return bool |
||
307 | */ |
||
308 | private function fieldIsOutLimit(SearchRequest $request, $field) { |
||
320 | |||
321 | |||
322 | /** |
||
323 | * @param array $tags |
||
324 | * |
||
325 | * @return array<string,array> |
||
326 | */ |
||
327 | private function generateSearchQueryTags($tags) { |
||
336 | |||
337 | private function generateSearchHighlighting() { |
||
344 | |||
345 | |||
346 | } |
||
347 |
This check looks for the generic type
array
as a return type and suggests a more specific type. This type is inferred from the actual code.