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 source fields for each document represented by a search hit. |
||
| 72 | * |
||
| 73 | * @var array |
||
| 74 | */ |
||
| 75 | private $sourceFields; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Allows to selectively load specific stored fields for each document represented by a search hit. |
||
| 79 | * |
||
| 80 | * @var array |
||
| 81 | */ |
||
| 82 | private $storedFields; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Allows to return a script evaluation (based on different fields) for each hit. |
||
| 86 | * Script fields can work on fields that are not stored, and allow to return custom |
||
| 87 | * values to be returned (the evaluated value of the script). Script fields can |
||
| 88 | * also access the actual _source document indexed and extract specific elements |
||
| 89 | * to be returned from it (can be an "object" type). |
||
| 90 | * |
||
| 91 | * @var array |
||
| 92 | */ |
||
| 93 | private $scriptFields; |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Allows to return the doc value representation of a field for each hit. Doc value |
||
| 97 | * fields can work on fields that are not stored. Note that if the fields parameter |
||
| 98 | * specifies fields without docvalues it will try to load the value from the fielddata |
||
| 99 | * cache causing the terms for that field to be loaded to memory (cached), which will |
||
| 100 | * result in more memory consumption. |
||
| 101 | * |
||
| 102 | * @var array |
||
| 103 | */ |
||
| 104 | private $docValueFields; |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Enables explanation for each hit on how its score was computed. |
||
| 108 | * |
||
| 109 | * @var bool |
||
| 110 | */ |
||
| 111 | private $explain; |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Returns a version for each search hit. |
||
| 115 | * |
||
| 116 | * @var bool |
||
| 117 | */ |
||
| 118 | private $version; |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Allows to configure different boost level per index when searching across more |
||
| 122 | * than one indices. This is very handy when hits coming from one index matter more |
||
| 123 | * than hits coming from another index (think social graph where each user has an index). |
||
| 124 | * |
||
| 125 | * @var array |
||
| 126 | */ |
||
| 127 | private $indicesBoost; |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Exclude documents which have a _score less than the minimum specified in min_score. |
||
| 131 | * |
||
| 132 | * @var int |
||
| 133 | */ |
||
| 134 | private $minScore; |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Pagination of results can be done by using the from and size but the cost becomes |
||
| 138 | * prohibitive when the deep pagination is reached. The index.max_result_window which |
||
| 139 | * defaults to 10,000 is a safeguard, search requests take heap memory and time |
||
| 140 | * proportional to from + size. The Scroll api is recommended for efficient deep |
||
| 141 | * scrolling but scroll contexts are costly and it is not recommended to use it for |
||
| 142 | * real time user requests. The search_after parameter circumvents this problem by |
||
| 143 | * providing a live cursor. The idea is to use the results from the previous page to |
||
| 144 | * help the retrieval of the next page. |
||
| 145 | * |
||
| 146 | * @var array |
||
| 147 | */ |
||
| 148 | private $searchAfter; |
||
| 149 | |||
| 150 | /** |
||
| 151 | * URI parameters alongside Request body search. |
||
| 152 | * |
||
| 153 | * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/search-uri-request.html |
||
| 154 | * |
||
| 155 | * @var array |
||
| 156 | */ |
||
| 157 | private $uriParams = []; |
||
| 158 | |||
| 159 | /** |
||
| 160 | * While a search request returns a single “page” of results, the scroll API can be used to retrieve |
||
| 161 | * large numbers of results (or even all results) from a single search request, in much the same way |
||
| 162 | * as you would use a cursor on a traditional database. Scrolling is not intended for real time user |
||
| 163 | * requests, but rather for processing large amounts of data, e.g. in order to reindex the contents |
||
| 164 | * of one index into a new index with a different configuration. |
||
| 165 | * |
||
| 166 | * @var string |
||
| 167 | */ |
||
| 168 | private $scroll; |
||
| 169 | |||
| 170 | /** |
||
| 171 | * @var OrderedSerializer |
||
| 172 | */ |
||
| 173 | private static $serializer; |
||
| 174 | |||
| 175 | /** |
||
| 176 | * @var SearchEndpointInterface[] |
||
| 177 | */ |
||
| 178 | private $endpoints = []; |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Constructor to initialize static properties |
||
| 182 | */ |
||
| 183 | public function __construct() |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Wakeup method to initialize static properties |
||
| 190 | */ |
||
| 191 | public function __wakeup() |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Initializes the serializer |
||
| 198 | */ |
||
| 199 | private function initializeSerializer() |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Destroys search endpoint. |
||
| 213 | * |
||
| 214 | * @param string $type Endpoint type. |
||
| 215 | */ |
||
| 216 | public function destroyEndpoint($type) |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Adds query to the search. |
||
| 223 | * |
||
| 224 | * @param BuilderInterface $query |
||
| 225 | * @param string $boolType |
||
| 226 | * @param string $key |
||
| 227 | * |
||
| 228 | * @return $this |
||
| 229 | */ |
||
| 230 | public function addQuery(BuilderInterface $query, $boolType = BoolQuery::MUST, $key = null) |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Returns endpoint instance. |
||
| 240 | * |
||
| 241 | * @param string $type Endpoint type. |
||
| 242 | * |
||
| 243 | * @return SearchEndpointInterface |
||
| 244 | */ |
||
| 245 | private function getEndpoint($type) |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Returns queries inside BoolQuery instance. |
||
| 256 | * |
||
| 257 | * @return BoolQuery |
||
| 258 | */ |
||
| 259 | public function getQueries() |
||
| 265 | |||
| 266 | /** |
||
| 267 | * Sets query endpoint parameters. |
||
| 268 | * |
||
| 269 | * @param array $parameters |
||
| 270 | * |
||
| 271 | * @return $this |
||
| 272 | */ |
||
| 273 | public function setQueryParameters(array $parameters) |
||
| 279 | |||
| 280 | /** |
||
| 281 | * Sets parameters to the endpoint. |
||
| 282 | * |
||
| 283 | * @param string $endpointName |
||
| 284 | * @param array $parameters |
||
| 285 | * |
||
| 286 | * @return $this |
||
| 287 | */ |
||
| 288 | public function setEndpointParameters($endpointName, array $parameters) |
||
| 296 | |||
| 297 | /** |
||
| 298 | * Adds a post filter to search. |
||
| 299 | * |
||
| 300 | * @param BuilderInterface $filter Filter. |
||
| 301 | * @param string $boolType Example boolType values: |
||
| 302 | * - must |
||
| 303 | * - must_not |
||
| 304 | * - should. |
||
| 305 | * @param string $key |
||
| 306 | * |
||
| 307 | * @return $this. |
||
| 308 | */ |
||
| 309 | public function addPostFilter(BuilderInterface $filter, $boolType = BoolQuery::MUST, $key = null) |
||
| 317 | |||
| 318 | /** |
||
| 319 | * Returns queries inside BoolFilter instance. |
||
| 320 | * |
||
| 321 | * @return BoolQuery |
||
| 322 | */ |
||
| 323 | public function getPostFilters() |
||
| 329 | |||
| 330 | /** |
||
| 331 | * Sets post filter endpoint parameters. |
||
| 332 | * |
||
| 333 | * @param array $parameters |
||
| 334 | * |
||
| 335 | * @return $this |
||
| 336 | */ |
||
| 337 | public function setPostFilterParameters(array $parameters) |
||
| 343 | |||
| 344 | /** |
||
| 345 | * Adds aggregation into search. |
||
| 346 | * |
||
| 347 | * @param AbstractAggregation $aggregation |
||
| 348 | * |
||
| 349 | * @return $this |
||
| 350 | */ |
||
| 351 | public function addAggregation(AbstractAggregation $aggregation) |
||
| 357 | |||
| 358 | /** |
||
| 359 | * Returns all aggregations. |
||
| 360 | * |
||
| 361 | * @return BuilderInterface[] |
||
| 362 | */ |
||
| 363 | public function getAggregations() |
||
| 367 | |||
| 368 | /** |
||
| 369 | * Adds inner hit into search. |
||
| 370 | * |
||
| 371 | * @param NestedInnerHit $innerHit |
||
| 372 | * |
||
| 373 | * @return $this |
||
| 374 | */ |
||
| 375 | public function addInnerHit(NestedInnerHit $innerHit) |
||
| 381 | |||
| 382 | /** |
||
| 383 | * Returns all inner hits. |
||
| 384 | * |
||
| 385 | * @return BuilderInterface[] |
||
| 386 | */ |
||
| 387 | public function getInnerHits() |
||
| 391 | |||
| 392 | /** |
||
| 393 | * Adds sort to search. |
||
| 394 | * |
||
| 395 | * @param BuilderInterface $sort |
||
| 396 | * |
||
| 397 | * @return $this |
||
| 398 | */ |
||
| 399 | public function addSort(BuilderInterface $sort) |
||
| 405 | |||
| 406 | /** |
||
| 407 | * Returns all set sorts. |
||
| 408 | * |
||
| 409 | * @return BuilderInterface[] |
||
| 410 | */ |
||
| 411 | public function getSorts() |
||
| 415 | |||
| 416 | /** |
||
| 417 | * Allows to highlight search results on one or more fields. |
||
| 418 | * |
||
| 419 | * @param Highlight $highlight |
||
| 420 | * |
||
| 421 | * @return $this. |
||
| 422 | */ |
||
| 423 | public function addHighlight($highlight) |
||
| 429 | |||
| 430 | /** |
||
| 431 | * Returns highlight builder. |
||
| 432 | * |
||
| 433 | * @return BuilderInterface |
||
| 434 | */ |
||
| 435 | public function getHighlights() |
||
| 442 | |||
| 443 | /** |
||
| 444 | * Adds suggest into search. |
||
| 445 | * |
||
| 446 | * @param BuilderInterface $suggest |
||
| 447 | * |
||
| 448 | * @return $this |
||
| 449 | */ |
||
| 450 | public function addSuggest(NamedBuilderInterface $suggest) |
||
| 456 | |||
| 457 | /** |
||
| 458 | * Returns all suggests. |
||
| 459 | * |
||
| 460 | * @return BuilderInterface[] |
||
| 461 | */ |
||
| 462 | public function getSuggests() |
||
| 466 | |||
| 467 | /** |
||
| 468 | * @return null|int |
||
| 469 | */ |
||
| 470 | public function getFrom() |
||
| 474 | |||
| 475 | /** |
||
| 476 | * @param null|int $from |
||
| 477 | * |
||
| 478 | * @return $this |
||
| 479 | */ |
||
| 480 | public function setFrom($from) |
||
| 486 | |||
| 487 | /** |
||
| 488 | * @return bool |
||
| 489 | */ |
||
| 490 | public function isTrackTotalHits() |
||
| 494 | |||
| 495 | /** |
||
| 496 | * @param bool $trackTotalHits |
||
| 497 | * |
||
| 498 | * @return $this |
||
| 499 | */ |
||
| 500 | public function setTrackTotalHits(bool $trackTotalHits) |
||
| 506 | |||
| 507 | /** |
||
| 508 | * @return null|int |
||
| 509 | */ |
||
| 510 | public function getSize() |
||
| 514 | |||
| 515 | /** |
||
| 516 | * @param null|int $size |
||
| 517 | * |
||
| 518 | * @return $this |
||
| 519 | */ |
||
| 520 | public function setSize($size) |
||
| 526 | |||
| 527 | /** |
||
| 528 | * @return bool |
||
| 529 | */ |
||
| 530 | public function isSource() |
||
| 534 | |||
| 535 | /** |
||
| 536 | * @param bool $source |
||
| 537 | * |
||
| 538 | * @return $this |
||
| 539 | */ |
||
| 540 | public function setSource($source) |
||
| 546 | |||
| 547 | /** |
||
| 548 | * @return array |
||
| 549 | */ |
||
| 550 | public function getSourceFields() |
||
| 554 | |||
| 555 | /** |
||
| 556 | * @param array $sourceFields |
||
| 557 | * |
||
| 558 | * @return $this |
||
| 559 | */ |
||
| 560 | public function setSourceFields($sourceFields) |
||
| 566 | |||
| 567 | /** |
||
| 568 | * @return array |
||
| 569 | */ |
||
| 570 | public function getStoredFields() |
||
| 574 | |||
| 575 | /** |
||
| 576 | * @param array $storedFields |
||
| 577 | * |
||
| 578 | * @return $this |
||
| 579 | */ |
||
| 580 | public function setStoredFields($storedFields) |
||
| 586 | |||
| 587 | /** |
||
| 588 | * @return array |
||
| 589 | */ |
||
| 590 | public function getScriptFields() |
||
| 594 | |||
| 595 | /** |
||
| 596 | * @param array $scriptFields |
||
| 597 | * |
||
| 598 | * @return $this |
||
| 599 | */ |
||
| 600 | public function setScriptFields($scriptFields) |
||
| 606 | |||
| 607 | /** |
||
| 608 | * @return array |
||
| 609 | */ |
||
| 610 | public function getDocValueFields() |
||
| 614 | |||
| 615 | /** |
||
| 616 | * @param array $docValueFields |
||
| 617 | * |
||
| 618 | * @return $this |
||
| 619 | */ |
||
| 620 | public function setDocValueFields($docValueFields) |
||
| 626 | |||
| 627 | /** |
||
| 628 | * @return bool |
||
| 629 | */ |
||
| 630 | public function isExplain() |
||
| 634 | |||
| 635 | /** |
||
| 636 | * @param bool $explain |
||
| 637 | * |
||
| 638 | * @return $this |
||
| 639 | */ |
||
| 640 | public function setExplain($explain) |
||
| 646 | |||
| 647 | /** |
||
| 648 | * @return bool |
||
| 649 | */ |
||
| 650 | public function isVersion() |
||
| 654 | |||
| 655 | /** |
||
| 656 | * @param bool $version |
||
| 657 | * |
||
| 658 | * @return $this |
||
| 659 | */ |
||
| 660 | public function setVersion($version) |
||
| 666 | |||
| 667 | /** |
||
| 668 | * @return array |
||
| 669 | */ |
||
| 670 | public function getIndicesBoost() |
||
| 674 | |||
| 675 | /** |
||
| 676 | * @param array $indicesBoost |
||
| 677 | * |
||
| 678 | * @return $this |
||
| 679 | */ |
||
| 680 | public function setIndicesBoost($indicesBoost) |
||
| 686 | |||
| 687 | /** |
||
| 688 | * @return int |
||
| 689 | */ |
||
| 690 | public function getMinScore() |
||
| 694 | |||
| 695 | /** |
||
| 696 | * @param int $minScore |
||
| 697 | * |
||
| 698 | * @return $this |
||
| 699 | */ |
||
| 700 | public function setMinScore($minScore) |
||
| 706 | |||
| 707 | /** |
||
| 708 | * @return array |
||
| 709 | */ |
||
| 710 | public function getSearchAfter() |
||
| 714 | |||
| 715 | /** |
||
| 716 | * @param array $searchAfter |
||
| 717 | * |
||
| 718 | * @return $this |
||
| 719 | */ |
||
| 720 | public function setSearchAfter($searchAfter) |
||
| 726 | |||
| 727 | /** |
||
| 728 | * @return string |
||
| 729 | */ |
||
| 730 | public function getScroll() |
||
| 734 | |||
| 735 | /** |
||
| 736 | * @param string $scroll |
||
| 737 | * |
||
| 738 | * @return $this |
||
| 739 | */ |
||
| 740 | public function setScroll($scroll = '5m') |
||
| 748 | |||
| 749 | /** |
||
| 750 | * @param string $name |
||
| 751 | * @param string|array|bool $value |
||
| 752 | * |
||
| 753 | * @return $this |
||
| 754 | */ |
||
| 755 | public function addUriParam($name, $value) |
||
| 790 | |||
| 791 | /** |
||
| 792 | * Returns query url parameters. |
||
| 793 | * |
||
| 794 | * @return array |
||
| 795 | */ |
||
| 796 | public function getUriParams() |
||
| 800 | |||
| 801 | /** |
||
| 802 | * {@inheritdoc} |
||
| 803 | */ |
||
| 804 | public function toArray() |
||
| 837 | } |
||
| 838 |
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: