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 |
||
| 49 | class SearchMappingService { |
||
| 50 | |||
| 51 | /** @var ConfigService */ |
||
| 52 | private $configService; |
||
| 53 | |||
| 54 | /** @var MiscService */ |
||
| 55 | private $miscService; |
||
| 56 | |||
| 57 | |||
| 58 | /** |
||
| 59 | * SearchMappingService constructor. |
||
| 60 | * |
||
| 61 | * @param ConfigService $configService |
||
| 62 | * @param MiscService $miscService |
||
| 63 | */ |
||
| 64 | public function __construct(ConfigService $configService, MiscService $miscService) { |
||
| 68 | |||
| 69 | |||
| 70 | /** |
||
| 71 | * @param ISearchRequest $request |
||
| 72 | * @param IDocumentAccess $access |
||
| 73 | * @param string $providerId |
||
| 74 | * |
||
| 75 | * @return array |
||
| 76 | * @throws ConfigurationException |
||
| 77 | * @throws SearchQueryGenerationException |
||
| 78 | */ |
||
| 79 | public function generateSearchQuery( |
||
| 86 | |||
| 87 | |||
| 88 | /** |
||
| 89 | * @param ISearchRequest $request |
||
| 90 | * @param IDocumentAccess $access |
||
| 91 | * @param string $providerId |
||
| 92 | * |
||
| 93 | * @return array |
||
| 94 | * @throws ConfigurationException |
||
| 95 | * @throws SearchQueryGenerationException |
||
| 96 | */ |
||
| 97 | public function generateSearchQueryParams( |
||
| 132 | |||
| 133 | |||
| 134 | /** |
||
| 135 | * @param ISearchRequest $request |
||
| 136 | * @param array $arr |
||
| 137 | */ |
||
| 138 | private function improveSearchQuerying(ISearchRequest $request, array &$arr) { |
||
| 143 | |||
| 144 | |||
| 145 | // /** |
||
| 146 | // * @param SearchRequest $request |
||
| 147 | // * @param array $arr |
||
| 148 | // */ |
||
| 149 | // private function improveSearchWildcardQueries(SearchRequest $request, &$arr) { |
||
| 150 | // |
||
| 151 | // $queries = $request->getWildcardQueries(); |
||
| 152 | // foreach ($queries as $query) { |
||
| 153 | // $wildcards = []; |
||
| 154 | // foreach ($query as $entry) { |
||
| 155 | // $wildcards[] = ['wildcard' => $entry]; |
||
| 156 | // } |
||
| 157 | // |
||
| 158 | // array_push($arr['bool']['must']['bool']['should'], $wildcards); |
||
| 159 | // } |
||
| 160 | // |
||
| 161 | // } |
||
| 162 | |||
| 163 | |||
| 164 | /** |
||
| 165 | * @param ISearchRequest $request |
||
| 166 | * @param array $arr |
||
| 167 | */ |
||
| 168 | View Code Duplication | private function improveSearchWildcardFilters(ISearchRequest $request, array &$arr) { |
|
| 181 | |||
| 182 | |||
| 183 | /** |
||
| 184 | * @param ISearchRequest $request |
||
| 185 | * @param array $arr |
||
| 186 | */ |
||
| 187 | View Code Duplication | private function improveSearchRegexFilters(ISearchRequest $request, array &$arr) { |
|
| 200 | |||
| 201 | |||
| 202 | /** |
||
| 203 | * @param ISearchRequest $request |
||
| 204 | * |
||
| 205 | * @return array |
||
| 206 | * @throws SearchQueryGenerationException |
||
| 207 | */ |
||
| 208 | private function generateSearchQueryContent(ISearchRequest $request): array { |
||
| 227 | |||
| 228 | |||
| 229 | /** |
||
| 230 | * @param string $word |
||
| 231 | * |
||
| 232 | * @return QueryContent |
||
| 233 | * @throws QueryContentGenerationException |
||
| 234 | */ |
||
| 235 | private function generateQueryContent(string $word): QueryContent { |
||
| 244 | |||
| 245 | |||
| 246 | /** |
||
| 247 | * @param ISearchRequest $request |
||
| 248 | * @param QueryContent[] $contents |
||
| 249 | * |
||
| 250 | * @return array |
||
| 251 | */ |
||
| 252 | private function generateSearchQueryFromQueryContent(ISearchRequest $request, array $contents): array { |
||
| 271 | |||
| 272 | |||
| 273 | /** |
||
| 274 | * @param ISearchRequest $request |
||
| 275 | * @param QueryContent $content |
||
| 276 | * |
||
| 277 | * @return array |
||
| 278 | */ |
||
| 279 | private function generateQueryContentFields(ISearchRequest $request, QueryContent $content): array { |
||
| 313 | |||
| 314 | |||
| 315 | /** |
||
| 316 | * @param IDocumentAccess $access |
||
| 317 | * |
||
| 318 | * @return array |
||
| 319 | */ |
||
| 320 | private function generateSearchQueryAccess(IDocumentAccess $access): array { |
||
| 337 | |||
| 338 | |||
| 339 | /** |
||
| 340 | * @param ISearchRequest $request |
||
| 341 | * @param string $field |
||
| 342 | * |
||
| 343 | * @return bool |
||
| 344 | */ |
||
| 345 | private function fieldIsOutLimit(ISearchRequest $request, string $field): bool { |
||
| 357 | |||
| 358 | |||
| 359 | /** |
||
| 360 | * @param string $k |
||
| 361 | * @param array $tags |
||
| 362 | * |
||
| 363 | * @return array |
||
| 364 | */ |
||
| 365 | private function generateSearchQueryTags(string $k, array $tags): array { |
||
| 374 | |||
| 375 | |||
| 376 | /** |
||
| 377 | * @param ISearchRequestSimpleQuery[] $queries |
||
| 378 | * |
||
| 379 | * @return array |
||
| 380 | */ |
||
| 381 | private function generateSearchSimpleQuery(array $queries): array { |
||
| 425 | |||
| 426 | |||
| 427 | /** |
||
| 428 | * @param ISearchRequest $request |
||
| 429 | * |
||
| 430 | * @return array |
||
| 431 | */ |
||
| 432 | private function generateSearchHighlighting(ISearchRequest $request): array { |
||
| 446 | |||
| 447 | |||
| 448 | /** |
||
| 449 | * @param string $providerId |
||
| 450 | * @param string $documentId |
||
| 451 | * |
||
| 452 | * @return array |
||
| 453 | * @throws ConfigurationException |
||
| 454 | */ |
||
| 455 | public function getDocumentQuery(string $providerId, string $documentId): array { |
||
| 462 | |||
| 463 | |||
| 464 | /** |
||
| 465 | * @param ISearchRequest $request |
||
| 466 | * |
||
| 467 | * @return array |
||
| 468 | */ |
||
| 469 | private function getPartsFields(ISearchRequest $request) { |
||
| 476 | |||
| 477 | } |
||
| 478 | |||
| 479 |
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.