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 | * If you don’t need to track the total number of hits at all you can improve |
||
| 39 | * query times by setting this option to false. Defaults to true. |
||
| 40 | * |
||
| 41 | * @var bool |
||
| 42 | */ |
||
| 43 | private $trackTotalHits; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * To retrieve hits from a certain offset. Defaults to 0. |
||
| 47 | * |
||
| 48 | * @var int |
||
| 49 | */ |
||
| 50 | private $from; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * The number of hits to return. Defaults to 10. If you do not care about getting some |
||
| 54 | * hits back but only about the number of matches and/or aggregations, setting the value |
||
| 55 | * to 0 will help performance. |
||
| 56 | * |
||
| 57 | * @var int |
||
| 58 | */ |
||
| 59 | private $size; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Allows to control how the _source field is returned with every hit. By default |
||
| 63 | * operations return the contents of the _source field unless you have used the |
||
| 64 | * stored_fields parameter or if the _source field is disabled. |
||
| 65 | * |
||
| 66 | * @var bool |
||
| 67 | */ |
||
| 68 | private $source; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Allows to selectively load specific stored fields for each document represented by a search hit. |
||
| 72 | * |
||
| 73 | * @var array |
||
| 74 | */ |
||
| 75 | private $storedFields; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Allows to return a script evaluation (based on different fields) for each hit. |
||
| 79 | * Script fields can work on fields that are not stored, and allow to return custom |
||
| 80 | * values to be returned (the evaluated value of the script). Script fields can |
||
| 81 | * also access the actual _source document indexed and extract specific elements |
||
| 82 | * to be returned from it (can be an "object" type). |
||
| 83 | * |
||
| 84 | * @var array |
||
| 85 | */ |
||
| 86 | private $scriptFields; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Allows to return the doc value representation of a field for each hit. Doc value |
||
| 90 | * fields can work on fields that are not stored. Note that if the fields parameter |
||
| 91 | * specifies fields without docvalues it will try to load the value from the fielddata |
||
| 92 | * cache causing the terms for that field to be loaded to memory (cached), which will |
||
| 93 | * result in more memory consumption. |
||
| 94 | * |
||
| 95 | * @var array |
||
| 96 | */ |
||
| 97 | private $docValueFields; |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Enables explanation for each hit on how its score was computed. |
||
| 101 | * |
||
| 102 | * @var bool |
||
| 103 | */ |
||
| 104 | private $explain; |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Returns a version for each search hit. |
||
| 108 | * |
||
| 109 | * @var bool |
||
| 110 | */ |
||
| 111 | private $version; |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Allows to configure different boost level per index when searching across more |
||
| 115 | * than one indices. This is very handy when hits coming from one index matter more |
||
| 116 | * than hits coming from another index (think social graph where each user has an index). |
||
| 117 | * |
||
| 118 | * @var array |
||
| 119 | */ |
||
| 120 | private $indicesBoost; |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Exclude documents which have a _score less than the minimum specified in min_score. |
||
| 124 | * |
||
| 125 | * @var int |
||
| 126 | */ |
||
| 127 | private $minScore; |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Pagination of results can be done by using the from and size but the cost becomes |
||
| 131 | * prohibitive when the deep pagination is reached. The index.max_result_window which |
||
| 132 | * defaults to 10,000 is a safeguard, search requests take heap memory and time |
||
| 133 | * proportional to from + size. The Scroll api is recommended for efficient deep |
||
| 134 | * scrolling but scroll contexts are costly and it is not recommended to use it for |
||
| 135 | * real time user requests. The search_after parameter circumvents this problem by |
||
| 136 | * providing a live cursor. The idea is to use the results from the previous page to |
||
| 137 | * help the retrieval of the next page. |
||
| 138 | * |
||
| 139 | * @var array |
||
| 140 | */ |
||
| 141 | private $searchAfter; |
||
| 142 | |||
| 143 | /** |
||
| 144 | * URI parameters alongside Request body search. |
||
| 145 | * |
||
| 146 | * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/search-uri-request.html |
||
| 147 | * |
||
| 148 | * @var array |
||
| 149 | */ |
||
| 150 | private $uriParams = []; |
||
| 151 | |||
| 152 | /** |
||
| 153 | * While a search request returns a single “page” of results, the scroll API can be used to retrieve |
||
| 154 | * large numbers of results (or even all results) from a single search request, in much the same way |
||
| 155 | * as you would use a cursor on a traditional database. Scrolling is not intended for real time user |
||
| 156 | * requests, but rather for processing large amounts of data, e.g. in order to reindex the contents |
||
| 157 | * of one index into a new index with a different configuration. |
||
| 158 | * |
||
| 159 | * @var string |
||
| 160 | */ |
||
| 161 | private $scroll; |
||
| 162 | |||
| 163 | /** |
||
| 164 | * @var OrderedSerializer |
||
| 165 | */ |
||
| 166 | private static $serializer; |
||
| 167 | |||
| 168 | /** |
||
| 169 | * @var SearchEndpointInterface[] |
||
| 170 | */ |
||
| 171 | private $endpoints = []; |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Initializes serializer. |
||
| 175 | */ |
||
| 176 | public function __construct() |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Destroys search endpoint. |
||
| 190 | * |
||
| 191 | * @param string $type Endpoint type. |
||
| 192 | */ |
||
| 193 | public function destroyEndpoint($type) |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Adds query to the search. |
||
| 200 | * |
||
| 201 | * @param BuilderInterface $query |
||
| 202 | * @param string $boolType |
||
| 203 | * @param string $key |
||
| 204 | * |
||
| 205 | * @return $this |
||
| 206 | */ |
||
| 207 | public function addQuery(BuilderInterface $query, $boolType = BoolQuery::MUST, $key = null) |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Returns endpoint instance. |
||
| 217 | * |
||
| 218 | * @param string $type Endpoint type. |
||
| 219 | * |
||
| 220 | * @return SearchEndpointInterface |
||
| 221 | */ |
||
| 222 | private function getEndpoint($type) |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Returns queries inside BoolQuery instance. |
||
| 233 | * |
||
| 234 | * @return BuilderInterface |
||
| 235 | */ |
||
| 236 | public function getQueries() |
||
| 242 | |||
| 243 | /** |
||
| 244 | * Sets query endpoint parameters. |
||
| 245 | * |
||
| 246 | * @param array $parameters |
||
| 247 | * |
||
| 248 | * @return $this |
||
| 249 | */ |
||
| 250 | public function setQueryParameters(array $parameters) |
||
| 256 | |||
| 257 | /** |
||
| 258 | * Sets parameters to the endpoint. |
||
| 259 | * |
||
| 260 | * @param string $endpointName |
||
| 261 | * @param array $parameters |
||
| 262 | */ |
||
| 263 | public function setEndpointParameters($endpointName, array $parameters) |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Adds a post filter to search. |
||
| 272 | * |
||
| 273 | * @param BuilderInterface $filter Filter. |
||
| 274 | * @param string $boolType Example boolType values: |
||
| 275 | * - must |
||
| 276 | * - must_not |
||
| 277 | * - should. |
||
| 278 | * @param string $key |
||
| 279 | * |
||
| 280 | * @return $this. |
||
| 281 | */ |
||
| 282 | public function addPostFilter(BuilderInterface $filter, $boolType = BoolQuery::MUST, $key = null) |
||
| 290 | |||
| 291 | /** |
||
| 292 | * Returns queries inside BoolFilter instance. |
||
| 293 | * |
||
| 294 | * @return BuilderInterface |
||
| 295 | */ |
||
| 296 | public function getPostFilters() |
||
| 302 | |||
| 303 | /** |
||
| 304 | * Sets post filter endpoint parameters. |
||
| 305 | * |
||
| 306 | * @param array $parameters |
||
| 307 | * |
||
| 308 | * @return $this |
||
| 309 | */ |
||
| 310 | public function setPostFilterParameters(array $parameters) |
||
| 316 | |||
| 317 | /** |
||
| 318 | * Adds aggregation into search. |
||
| 319 | * |
||
| 320 | * @param AbstractAggregation $aggregation |
||
| 321 | * |
||
| 322 | * @return $this |
||
| 323 | */ |
||
| 324 | public function addAggregation(AbstractAggregation $aggregation) |
||
| 330 | |||
| 331 | /** |
||
| 332 | * Returns all aggregations. |
||
| 333 | * |
||
| 334 | * @return BuilderInterface[] |
||
| 335 | */ |
||
| 336 | public function getAggregations() |
||
| 340 | |||
| 341 | /** |
||
| 342 | * Adds inner hit into search. |
||
| 343 | * |
||
| 344 | * @param NestedInnerHit $innerHit |
||
| 345 | * |
||
| 346 | * @return $this |
||
| 347 | */ |
||
| 348 | public function addInnerHit(NestedInnerHit $innerHit) |
||
| 354 | |||
| 355 | /** |
||
| 356 | * Returns all inner hits. |
||
| 357 | * |
||
| 358 | * @return BuilderInterface[] |
||
| 359 | */ |
||
| 360 | public function getInnerHits() |
||
| 364 | |||
| 365 | /** |
||
| 366 | * Adds sort to search. |
||
| 367 | * |
||
| 368 | * @param BuilderInterface $sort |
||
| 369 | * |
||
| 370 | * @return $this |
||
| 371 | */ |
||
| 372 | public function addSort(BuilderInterface $sort) |
||
| 378 | |||
| 379 | /** |
||
| 380 | * Returns all set sorts. |
||
| 381 | * |
||
| 382 | * @return BuilderInterface[] |
||
| 383 | */ |
||
| 384 | public function getSorts() |
||
| 388 | |||
| 389 | /** |
||
| 390 | * Allows to highlight search results on one or more fields. |
||
| 391 | * |
||
| 392 | * @param Highlight $highlight |
||
| 393 | * |
||
| 394 | * @return $this. |
||
| 395 | */ |
||
| 396 | public function addHighlight($highlight) |
||
| 402 | |||
| 403 | /** |
||
| 404 | * Returns highlight builder. |
||
| 405 | * |
||
| 406 | * @return BuilderInterface |
||
| 407 | */ |
||
| 408 | public function getHighlights() |
||
| 415 | |||
| 416 | /** |
||
| 417 | * Adds suggest into search. |
||
| 418 | * |
||
| 419 | * @param BuilderInterface $suggest |
||
| 420 | * |
||
| 421 | * @return $this |
||
| 422 | */ |
||
| 423 | public function addSuggest(NamedBuilderInterface $suggest) |
||
| 429 | |||
| 430 | /** |
||
| 431 | * Returns all suggests. |
||
| 432 | * |
||
| 433 | * @return BuilderInterface[] |
||
| 434 | */ |
||
| 435 | public function getSuggests() |
||
| 439 | |||
| 440 | /** |
||
| 441 | * @return int |
||
| 442 | */ |
||
| 443 | public function getFrom() |
||
| 447 | |||
| 448 | /** |
||
| 449 | * @param int $from |
||
| 450 | * @return $this |
||
| 451 | */ |
||
| 452 | public function setFrom($from) |
||
| 457 | |||
| 458 | /** |
||
| 459 | * @return bool |
||
| 460 | */ |
||
| 461 | public function isTrackTotalHits() |
||
| 465 | |||
| 466 | /** |
||
| 467 | * @param bool $trackTotalHits |
||
| 468 | * @return $this |
||
| 469 | */ |
||
| 470 | public function setTrackTotalHits(bool $trackTotalHits) |
||
| 475 | |||
| 476 | /** |
||
| 477 | * @return int |
||
| 478 | */ |
||
| 479 | public function getSize() |
||
| 483 | |||
| 484 | /** |
||
| 485 | * @param int $size |
||
| 486 | * @return $this |
||
| 487 | */ |
||
| 488 | public function setSize($size) |
||
| 493 | |||
| 494 | /** |
||
| 495 | * @return bool |
||
| 496 | */ |
||
| 497 | public function isSource() |
||
| 501 | |||
| 502 | /** |
||
| 503 | * @param bool $source |
||
| 504 | * @return $this |
||
| 505 | */ |
||
| 506 | public function setSource($source) |
||
| 511 | |||
| 512 | /** |
||
| 513 | * @return array |
||
| 514 | */ |
||
| 515 | public function getStoredFields() |
||
| 519 | |||
| 520 | /** |
||
| 521 | * @param array $storedFields |
||
| 522 | * @return $this |
||
| 523 | */ |
||
| 524 | public function setStoredFields($storedFields) |
||
| 529 | |||
| 530 | /** |
||
| 531 | * @return array |
||
| 532 | */ |
||
| 533 | public function getScriptFields() |
||
| 537 | |||
| 538 | /** |
||
| 539 | * @param array $scriptFields |
||
| 540 | * @return $this |
||
| 541 | */ |
||
| 542 | public function setScriptFields($scriptFields) |
||
| 547 | |||
| 548 | /** |
||
| 549 | * @return array |
||
| 550 | */ |
||
| 551 | public function getDocValueFields() |
||
| 555 | |||
| 556 | /** |
||
| 557 | * @param array $docValueFields |
||
| 558 | * @return $this |
||
| 559 | */ |
||
| 560 | public function setDocValueFields($docValueFields) |
||
| 565 | |||
| 566 | /** |
||
| 567 | * @return bool |
||
| 568 | */ |
||
| 569 | public function isExplain() |
||
| 573 | |||
| 574 | /** |
||
| 575 | * @param bool $explain |
||
| 576 | * @return $this |
||
| 577 | */ |
||
| 578 | public function setExplain($explain) |
||
| 583 | |||
| 584 | /** |
||
| 585 | * @return bool |
||
| 586 | */ |
||
| 587 | public function isVersion() |
||
| 591 | |||
| 592 | /** |
||
| 593 | * @param bool $version |
||
| 594 | * @return $this |
||
| 595 | */ |
||
| 596 | public function setVersion($version) |
||
| 601 | |||
| 602 | /** |
||
| 603 | * @return array |
||
| 604 | */ |
||
| 605 | public function getIndicesBoost() |
||
| 609 | |||
| 610 | /** |
||
| 611 | * @param array $indicesBoost |
||
| 612 | * @return $this |
||
| 613 | */ |
||
| 614 | public function setIndicesBoost($indicesBoost) |
||
| 619 | |||
| 620 | /** |
||
| 621 | * @return int |
||
| 622 | */ |
||
| 623 | public function getMinScore() |
||
| 627 | |||
| 628 | /** |
||
| 629 | * @param int $minScore |
||
| 630 | * @return $this |
||
| 631 | */ |
||
| 632 | public function setMinScore($minScore) |
||
| 637 | |||
| 638 | /** |
||
| 639 | * @return array |
||
| 640 | */ |
||
| 641 | public function getSearchAfter() |
||
| 645 | |||
| 646 | /** |
||
| 647 | * @param array $searchAfter |
||
| 648 | * @return $this |
||
| 649 | */ |
||
| 650 | public function setSearchAfter($searchAfter) |
||
| 655 | |||
| 656 | /** |
||
| 657 | * @return string |
||
| 658 | */ |
||
| 659 | public function getScroll() |
||
| 663 | |||
| 664 | /** |
||
| 665 | * @param string $scroll |
||
| 666 | * @return $this |
||
| 667 | */ |
||
| 668 | public function setScroll($scroll = '5m') |
||
| 676 | |||
| 677 | /** |
||
| 678 | * @param string $name |
||
| 679 | * @param string|array|bool $value |
||
| 680 | * |
||
| 681 | * @return $this |
||
| 682 | */ |
||
| 683 | public function addUriParam($name, $value) |
||
| 718 | |||
| 719 | /** |
||
| 720 | * Returns query url parameters. |
||
| 721 | * |
||
| 722 | * @return array |
||
| 723 | */ |
||
| 724 | public function getUriParams() |
||
| 728 | |||
| 729 | /** |
||
| 730 | * {@inheritdoc} |
||
| 731 | */ |
||
| 732 | public function toArray() |
||
| 759 | } |
||
| 760 |
Let’s assume you have a class which uses late-static binding:
The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the
getSomeVariable()on that sub-class, you will receive a runtime error:In the case above, it makes sense to update
SomeClassto useselfinstead: