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 |
||
| 303 | |||
| 304 | |||
| 305 | /** |
||
| 306 | * @param DocumentAccess $access |
||
| 307 | * |
||
| 308 | * @return array |
||
| 309 | */ |
||
| 310 | private function generateSearchQueryAccess(DocumentAccess $access): array { |
||
| 327 | |||
| 328 | |||
| 329 | /** |
||
| 330 | * @param ISearchRequest $request |
||
| 331 | * @param string $field |
||
| 332 | * |
||
| 333 | * @return bool |
||
| 334 | */ |
||
| 335 | private function fieldIsOutLimit(ISearchRequest $request, string $field): bool { |
||
| 347 | |||
| 348 | |||
| 349 | /** |
||
| 350 | * @param string $k |
||
| 351 | * @param array $tags |
||
| 352 | * |
||
| 353 | * @return array |
||
| 354 | */ |
||
| 355 | private function generateSearchQueryTags(string $k, array $tags): array { |
||
| 364 | |||
| 365 | |||
| 366 | /** |
||
| 367 | * @param ISearchRequest $request |
||
| 368 | * |
||
| 369 | * @return array |
||
| 370 | */ |
||
| 371 | private function generateSearchHighlighting(ISearchRequest $request): array { |
||
| 385 | |||
| 386 | |||
| 387 | /** |
||
| 388 | * @param string $providerId |
||
| 389 | * @param string $documentId |
||
| 390 | * |
||
| 391 | * @return array |
||
| 392 | * @throws ConfigurationException |
||
| 393 | */ |
||
| 394 | public function getDocumentQuery(string $providerId, string $documentId): array { |
||
| 401 | |||
| 402 | |||
| 403 | /** |
||
| 404 | * @param ISearchRequest $request |
||
| 405 | * |
||
| 406 | * @return array |
||
| 407 | */ |
||
| 408 | private function getPartsFields(ISearchRequest $request) { |
||
| 415 | |||
| 416 | } |
||
| 417 | |||
| 418 |
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.