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( |
||
| 124 | |||
| 125 | |||
| 126 | /** |
||
| 127 | * @param ISearchRequest $request |
||
| 128 | * @param array $arr |
||
| 129 | */ |
||
| 130 | private function improveSearchQuerying(ISearchRequest $request, array &$arr) { |
||
| 135 | |||
| 136 | |||
| 137 | // /** |
||
| 138 | // * @param SearchRequest $request |
||
| 139 | // * @param array $arr |
||
| 140 | // */ |
||
| 141 | // private function improveSearchWildcardQueries(SearchRequest $request, &$arr) { |
||
| 142 | // |
||
| 143 | // $queries = $request->getWildcardQueries(); |
||
| 144 | // foreach ($queries as $query) { |
||
| 145 | // $wildcards = []; |
||
| 146 | // foreach ($query as $entry) { |
||
| 147 | // $wildcards[] = ['wildcard' => $entry]; |
||
| 148 | // } |
||
| 149 | // |
||
| 150 | // array_push($arr['bool']['must']['bool']['should'], $wildcards); |
||
| 151 | // } |
||
| 152 | // |
||
| 153 | // } |
||
| 154 | |||
| 155 | |||
| 156 | /** |
||
| 157 | * @param ISearchRequest $request |
||
| 158 | * @param array $arr |
||
| 159 | */ |
||
| 160 | View Code Duplication | private function improveSearchWildcardFilters(ISearchRequest $request, array &$arr) { |
|
| 173 | |||
| 174 | |||
| 175 | /** |
||
| 176 | * @param ISearchRequest $request |
||
| 177 | * @param array $arr |
||
| 178 | */ |
||
| 179 | View Code Duplication | private function improveSearchRegexFilters(ISearchRequest $request, array &$arr) { |
|
| 192 | |||
| 193 | |||
| 194 | /** |
||
| 195 | * @param ISearchRequest $request |
||
| 196 | * |
||
| 197 | * @return array |
||
| 198 | * @throws SearchQueryGenerationException |
||
| 199 | */ |
||
| 200 | private function generateSearchQueryContent(ISearchRequest $request): array { |
||
| 219 | |||
| 220 | |||
| 221 | /** |
||
| 222 | * @param string $word |
||
| 223 | * |
||
| 224 | * @return QueryContent |
||
| 225 | * @throws QueryContentGenerationException |
||
| 226 | */ |
||
| 227 | private function generateQueryContent(string $word): QueryContent { |
||
| 236 | |||
| 237 | |||
| 238 | /** |
||
| 239 | * @param ISearchRequest $request |
||
| 240 | * @param QueryContent[] $queryContents |
||
| 241 | * |
||
| 242 | * @return array |
||
| 243 | */ |
||
| 244 | private function generateSearchQueryFromQueryContent( |
||
| 261 | |||
| 262 | |||
| 263 | /** |
||
| 264 | * @param ISearchRequest $request |
||
| 265 | * @param QueryContent $content |
||
| 266 | * |
||
| 267 | * @return array |
||
| 268 | */ |
||
| 269 | private function generateQueryContentFields(ISearchRequest $request, QueryContent $content |
||
| 289 | |||
| 290 | |||
| 291 | /** |
||
| 292 | * @param DocumentAccess $access |
||
| 293 | * |
||
| 294 | * @return array |
||
| 295 | */ |
||
| 296 | private function generateSearchQueryAccess(DocumentAccess $access): array { |
||
| 313 | |||
| 314 | |||
| 315 | /** |
||
| 316 | * @param ISearchRequest $request |
||
| 317 | * @param string $field |
||
| 318 | * |
||
| 319 | * @return bool |
||
| 320 | */ |
||
| 321 | private function fieldIsOutLimit(ISearchRequest $request, string $field): bool { |
||
| 333 | |||
| 334 | |||
| 335 | /** |
||
| 336 | * @param string $k |
||
| 337 | * @param array $tags |
||
| 338 | * |
||
| 339 | * @return array |
||
| 340 | */ |
||
| 341 | private function generateSearchQueryTags(string $k, array $tags): array { |
||
| 350 | |||
| 351 | |||
| 352 | /** |
||
| 353 | * @param ISearchRequest $request |
||
| 354 | * |
||
| 355 | * @return array |
||
| 356 | */ |
||
| 357 | private function generateSearchHighlighting(ISearchRequest $request): array { |
||
| 371 | |||
| 372 | |||
| 373 | /** |
||
| 374 | * @param string $providerId |
||
| 375 | * @param string $documentId |
||
| 376 | * |
||
| 377 | * @return array |
||
| 378 | * @throws ConfigurationException |
||
| 379 | */ |
||
| 380 | public function getDocumentQuery(string $providerId, string $documentId): array { |
||
| 387 | |||
| 388 | |||
| 389 | /** |
||
| 390 | * @param ISearchRequest $request |
||
| 391 | * |
||
| 392 | * @return array |
||
| 393 | */ |
||
| 394 | private function getPartsFields(ISearchRequest $request) { |
||
| 401 | |||
| 402 | } |
||
| 403 | |||
| 404 |
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
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey 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.