Complex classes like Search 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 Search, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 35 | class Search |
||
| 36 | { |
||
| 37 | /** |
||
| 38 | * To retrieve hits from a certain offset. Defaults to 0. |
||
| 39 | * |
||
| 40 | * @var int |
||
| 41 | */ |
||
| 42 | private $from; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * The number of hits to return. Defaults to 10. If you do not care about getting some |
||
| 46 | * hits back but only about the number of matches and/or aggregations, setting the value |
||
| 47 | * to 0 will help performance. |
||
| 48 | * |
||
| 49 | * @var int |
||
| 50 | */ |
||
| 51 | private $size; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Allows to control how the _source field is returned with every hit. By default |
||
| 55 | * operations return the contents of the _source field unless you have used the |
||
| 56 | * stored_fields parameter or if the _source field is disabled. |
||
| 57 | * |
||
| 58 | * @var bool |
||
| 59 | */ |
||
| 60 | private $source; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Allows to selectively load specific stored fields for each document represented by a search hit. |
||
| 64 | * |
||
| 65 | * @var array |
||
| 66 | */ |
||
| 67 | private $storedFields; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Allows to return a script evaluation (based on different fields) for each hit. |
||
| 71 | * Script fields can work on fields that are not stored, and allow to return custom |
||
| 72 | * values to be returned (the evaluated value of the script). Script fields can |
||
| 73 | * also access the actual _source document indexed and extract specific elements |
||
| 74 | * to be returned from it (can be an "object" type). |
||
| 75 | * |
||
| 76 | * @var array |
||
| 77 | */ |
||
| 78 | private $scriptFields; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Allows to return the doc value representation of a field for each hit. Doc value |
||
| 82 | * fields can work on fields that are not stored. Note that if the fields parameter |
||
| 83 | * specifies fields without docvalues it will try to load the value from the fielddata |
||
| 84 | * cache causing the terms for that field to be loaded to memory (cached), which will |
||
| 85 | * result in more memory consumption. |
||
| 86 | * |
||
| 87 | * @var array |
||
| 88 | */ |
||
| 89 | private $docValueFields; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Enables explanation for each hit on how its score was computed. |
||
| 93 | * |
||
| 94 | * @var bool |
||
| 95 | */ |
||
| 96 | private $explain; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Returns a version for each search hit. |
||
| 100 | * |
||
| 101 | * @var bool |
||
| 102 | */ |
||
| 103 | private $version; |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Allows to configure different boost level per index when searching across more |
||
| 107 | * than one indices. This is very handy when hits coming from one index matter more |
||
| 108 | * than hits coming from another index (think social graph where each user has an index). |
||
| 109 | * |
||
| 110 | * @var array |
||
| 111 | */ |
||
| 112 | private $indicesBoost; |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Exclude documents which have a _score less than the minimum specified in min_score. |
||
| 116 | * |
||
| 117 | * @var int |
||
| 118 | */ |
||
| 119 | private $minScore; |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Pagination of results can be done by using the from and size but the cost becomes |
||
| 123 | * prohibitive when the deep pagination is reached. The index.max_result_window which |
||
| 124 | * defaults to 10,000 is a safeguard, search requests take heap memory and time |
||
| 125 | * proportional to from + size. The Scroll api is recommended for efficient deep |
||
| 126 | * scrolling but scroll contexts are costly and it is not recommended to use it for |
||
| 127 | * real time user requests. The search_after parameter circumvents this problem by |
||
| 128 | * providing a live cursor. The idea is to use the results from the previous page to |
||
| 129 | * help the retrieval of the next page. |
||
| 130 | * |
||
| 131 | * @var array |
||
| 132 | */ |
||
| 133 | private $searchAfter; |
||
| 134 | |||
| 135 | /** |
||
| 136 | * URI parameters alongside Request body search. |
||
| 137 | * |
||
| 138 | * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/search-uri-request.html |
||
| 139 | * |
||
| 140 | * @var array |
||
| 141 | */ |
||
| 142 | private $uriParams = []; |
||
| 143 | |||
| 144 | /** |
||
| 145 | * While a search request returns a single “page” of results, the scroll API can be used to retrieve |
||
| 146 | * large numbers of results (or even all results) from a single search request, in much the same way |
||
| 147 | * as you would use a cursor on a traditional database. Scrolling is not intended for real time user |
||
| 148 | * requests, but rather for processing large amounts of data, e.g. in order to reindex the contents |
||
| 149 | * of one index into a new index with a different configuration. |
||
| 150 | * |
||
| 151 | * @var string |
||
| 152 | */ |
||
| 153 | private $scroll; |
||
| 154 | |||
| 155 | /** |
||
| 156 | * @var OrderedSerializer |
||
| 157 | */ |
||
| 158 | private $serializer; |
||
| 159 | |||
| 160 | /** |
||
| 161 | * @var SearchEndpointInterface[] |
||
| 162 | */ |
||
| 163 | private $endpoints = []; |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Initializes serializer. |
||
| 167 | */ |
||
| 168 | public function __construct() |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Destroys search endpoint. |
||
| 180 | * |
||
| 181 | * @param string $type Endpoint type. |
||
| 182 | */ |
||
| 183 | public function destroyEndpoint($type) |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Adds query to the search. |
||
| 190 | * |
||
| 191 | * @param BuilderInterface $query |
||
| 192 | * @param string $boolType |
||
| 193 | * @param string $key |
||
| 194 | * |
||
| 195 | * @return $this |
||
| 196 | */ |
||
| 197 | public function addQuery(BuilderInterface $query, $boolType = BoolQuery::MUST, $key = null) |
||
| 204 | |||
| 205 | /** |
||
| 206 | * Adds multiple queries to the search. |
||
| 207 | * |
||
| 208 | * @param Array $query |
||
| 209 | * @param string $boolType |
||
| 210 | * @param string $key |
||
| 211 | * |
||
| 212 | * @return $this |
||
| 213 | */ |
||
| 214 | public function addQueries(Array $queries) |
||
| 226 | |||
| 227 | /** |
||
| 228 | * Returns endpoint instance. |
||
| 229 | * |
||
| 230 | * @param string $type Endpoint type. |
||
| 231 | * |
||
| 232 | * @return SearchEndpointInterface |
||
| 233 | */ |
||
| 234 | private function getEndpoint($type) |
||
| 242 | |||
| 243 | /** |
||
| 244 | * Returns queries inside BoolQuery instance. |
||
| 245 | * |
||
| 246 | * @return BuilderInterface |
||
| 247 | */ |
||
| 248 | public function getQueries() |
||
| 254 | |||
| 255 | /** |
||
| 256 | * Sets query endpoint parameters. |
||
| 257 | * |
||
| 258 | * @param array $parameters |
||
| 259 | * |
||
| 260 | * @return $this |
||
| 261 | */ |
||
| 262 | public function setQueryParameters(array $parameters) |
||
| 268 | |||
| 269 | /** |
||
| 270 | * Sets parameters to the endpoint. |
||
| 271 | * |
||
| 272 | * @param string $endpointName |
||
| 273 | * @param array $parameters |
||
| 274 | */ |
||
| 275 | public function setEndpointParameters($endpointName, array $parameters) |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Adds a post filter to search. |
||
| 284 | * |
||
| 285 | * @param BuilderInterface $filter Filter. |
||
| 286 | * @param string $boolType Example boolType values: |
||
| 287 | * - must |
||
| 288 | * - must_not |
||
| 289 | * - should. |
||
| 290 | * @param string $key |
||
| 291 | * |
||
| 292 | * @return $this. |
||
| 293 | */ |
||
| 294 | public function addPostFilter(BuilderInterface $filter, $boolType = BoolQuery::MUST, $key = null) |
||
| 302 | |||
| 303 | /** |
||
| 304 | * Returns queries inside BoolFilter instance. |
||
| 305 | * |
||
| 306 | * @return BuilderInterface |
||
| 307 | */ |
||
| 308 | public function getPostFilters() |
||
| 314 | |||
| 315 | /** |
||
| 316 | * Sets post filter endpoint parameters. |
||
| 317 | * |
||
| 318 | * @param array $parameters |
||
| 319 | * |
||
| 320 | * @return $this |
||
| 321 | */ |
||
| 322 | public function setPostFilterParameters(array $parameters) |
||
| 328 | |||
| 329 | /** |
||
| 330 | * Adds aggregation into search. |
||
| 331 | * |
||
| 332 | * @param AbstractAggregation $aggregation |
||
| 333 | * |
||
| 334 | * @return $this |
||
| 335 | */ |
||
| 336 | public function addAggregation(AbstractAggregation $aggregation) |
||
| 342 | |||
| 343 | /** |
||
| 344 | * Returns all aggregations. |
||
| 345 | * |
||
| 346 | * @return BuilderInterface[] |
||
| 347 | */ |
||
| 348 | public function getAggregations() |
||
| 352 | |||
| 353 | /** |
||
| 354 | * Adds inner hit into search. |
||
| 355 | * |
||
| 356 | * @param NestedInnerHit $innerHit |
||
| 357 | * |
||
| 358 | * @return $this |
||
| 359 | */ |
||
| 360 | public function addInnerHit(NestedInnerHit $innerHit) |
||
| 366 | |||
| 367 | /** |
||
| 368 | * Returns all inner hits. |
||
| 369 | * |
||
| 370 | * @return BuilderInterface[] |
||
| 371 | */ |
||
| 372 | public function getInnerHits() |
||
| 376 | |||
| 377 | /** |
||
| 378 | * Adds sort to search. |
||
| 379 | * |
||
| 380 | * @param BuilderInterface $sort |
||
| 381 | * |
||
| 382 | * @return $this |
||
| 383 | */ |
||
| 384 | public function addSort(BuilderInterface $sort) |
||
| 390 | |||
| 391 | /** |
||
| 392 | * Returns all set sorts. |
||
| 393 | * |
||
| 394 | * @return BuilderInterface[] |
||
| 395 | */ |
||
| 396 | public function getSorts() |
||
| 400 | |||
| 401 | /** |
||
| 402 | * Allows to highlight search results on one or more fields. |
||
| 403 | * |
||
| 404 | * @param Highlight $highlight |
||
| 405 | * |
||
| 406 | * @return $this. |
||
| 407 | */ |
||
| 408 | public function addHighlight($highlight) |
||
| 414 | |||
| 415 | /** |
||
| 416 | * Returns highlight builder. |
||
| 417 | * |
||
| 418 | * @return BuilderInterface |
||
| 419 | */ |
||
| 420 | public function getHighlights() |
||
| 427 | |||
| 428 | /** |
||
| 429 | * Adds suggest into search. |
||
| 430 | * |
||
| 431 | * @param BuilderInterface $suggest |
||
| 432 | * |
||
| 433 | * @return $this |
||
| 434 | */ |
||
| 435 | public function addSuggest(BuilderInterface $suggest) |
||
| 441 | |||
| 442 | /** |
||
| 443 | * Returns all suggests. |
||
| 444 | * |
||
| 445 | * @return BuilderInterface[] |
||
| 446 | */ |
||
| 447 | public function getSuggests() |
||
| 451 | |||
| 452 | /** |
||
| 453 | * @return int |
||
| 454 | */ |
||
| 455 | public function getFrom() |
||
| 459 | |||
| 460 | /** |
||
| 461 | * @param int $from |
||
| 462 | * @return $this |
||
| 463 | */ |
||
| 464 | public function setFrom($from) |
||
| 469 | |||
| 470 | /** |
||
| 471 | * @return int |
||
| 472 | */ |
||
| 473 | public function getSize() |
||
| 477 | |||
| 478 | /** |
||
| 479 | * @param int $size |
||
| 480 | * @return $this |
||
| 481 | */ |
||
| 482 | public function setSize($size) |
||
| 487 | |||
| 488 | /** |
||
| 489 | * @return bool |
||
| 490 | */ |
||
| 491 | public function isSource() |
||
| 495 | |||
| 496 | /** |
||
| 497 | * @param bool $source |
||
| 498 | * @return $this |
||
| 499 | */ |
||
| 500 | public function setSource($source) |
||
| 505 | |||
| 506 | /** |
||
| 507 | * @return array |
||
| 508 | */ |
||
| 509 | public function getStoredFields() |
||
| 513 | |||
| 514 | /** |
||
| 515 | * @param array $storedFields |
||
| 516 | * @return $this |
||
| 517 | */ |
||
| 518 | public function setStoredFields($storedFields) |
||
| 523 | |||
| 524 | /** |
||
| 525 | * @return array |
||
| 526 | */ |
||
| 527 | public function getScriptFields() |
||
| 531 | |||
| 532 | /** |
||
| 533 | * @param array $scriptFields |
||
| 534 | * @return $this |
||
| 535 | */ |
||
| 536 | public function setScriptFields($scriptFields) |
||
| 541 | |||
| 542 | /** |
||
| 543 | * @return array |
||
| 544 | */ |
||
| 545 | public function getDocValueFields() |
||
| 549 | |||
| 550 | /** |
||
| 551 | * @param array $docValueFields |
||
| 552 | * @return $this |
||
| 553 | */ |
||
| 554 | public function setDocValueFields($docValueFields) |
||
| 559 | |||
| 560 | /** |
||
| 561 | * @return bool |
||
| 562 | */ |
||
| 563 | public function isExplain() |
||
| 567 | |||
| 568 | /** |
||
| 569 | * @param bool $explain |
||
| 570 | * @return $this |
||
| 571 | */ |
||
| 572 | public function setExplain($explain) |
||
| 577 | |||
| 578 | /** |
||
| 579 | * @return bool |
||
| 580 | */ |
||
| 581 | public function isVersion() |
||
| 585 | |||
| 586 | /** |
||
| 587 | * @param bool $version |
||
| 588 | * @return $this |
||
| 589 | */ |
||
| 590 | public function setVersion($version) |
||
| 595 | |||
| 596 | /** |
||
| 597 | * @return array |
||
| 598 | */ |
||
| 599 | public function getIndicesBoost() |
||
| 603 | |||
| 604 | /** |
||
| 605 | * @param array $indicesBoost |
||
| 606 | * @return $this |
||
| 607 | */ |
||
| 608 | public function setIndicesBoost($indicesBoost) |
||
| 613 | |||
| 614 | /** |
||
| 615 | * @return int |
||
| 616 | */ |
||
| 617 | public function getMinScore() |
||
| 621 | |||
| 622 | /** |
||
| 623 | * @param int $minScore |
||
| 624 | * @return $this |
||
| 625 | */ |
||
| 626 | public function setMinScore($minScore) |
||
| 631 | |||
| 632 | /** |
||
| 633 | * @return array |
||
| 634 | */ |
||
| 635 | public function getSearchAfter() |
||
| 639 | |||
| 640 | /** |
||
| 641 | * @param array $searchAfter |
||
| 642 | * @return $this |
||
| 643 | */ |
||
| 644 | public function setSearchAfter($searchAfter) |
||
| 649 | |||
| 650 | /** |
||
| 651 | * @return string |
||
| 652 | */ |
||
| 653 | public function getScroll() |
||
| 657 | |||
| 658 | /** |
||
| 659 | * @param string $scroll |
||
| 660 | * @return $this |
||
| 661 | */ |
||
| 662 | public function setScroll($scroll = '5m') |
||
| 670 | |||
| 671 | /** |
||
| 672 | * @param string $name |
||
| 673 | * @param string|array|bool $value |
||
| 674 | * |
||
| 675 | * @return $this |
||
| 676 | */ |
||
| 677 | public function addUriParam($name, $value) |
||
| 705 | |||
| 706 | /** |
||
| 707 | * Returns query url parameters. |
||
| 708 | * |
||
| 709 | * @return array |
||
| 710 | */ |
||
| 711 | public function getUriParams() |
||
| 715 | |||
| 716 | /** |
||
| 717 | * {@inheritdoc} |
||
| 718 | */ |
||
| 719 | public function toArray() |
||
| 745 | } |
||
| 746 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: