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 | * The maximum number of documents to collect for each shard, upon reaching which the query execution will terminate early. |
||
63 | * |
||
64 | * @var integer |
||
65 | */ |
||
66 | private $terminateAfter; |
||
67 | |||
68 | /** |
||
69 | * Explicit timeout for each search request. Defaults to no timeout. |
||
70 | * |
||
71 | * @var time units |
||
72 | */ |
||
73 | private $timeout; |
||
74 | |||
75 | /** |
||
76 | * Allows to control how the _source field is returned with every hit. By default |
||
77 | * operations return the contents of the _source field unless you have used the |
||
78 | * stored_fields parameter or if the _source field is disabled. |
||
79 | * |
||
80 | * @var bool |
||
81 | */ |
||
82 | private $source; |
||
83 | |||
84 | /** |
||
85 | * Allows to selectively load specific stored fields for each document represented by a search hit. |
||
86 | * |
||
87 | * @var array |
||
88 | */ |
||
89 | private $storedFields; |
||
90 | |||
91 | /** |
||
92 | * Allows to return a script evaluation (based on different fields) for each hit. |
||
93 | * Script fields can work on fields that are not stored, and allow to return custom |
||
94 | * values to be returned (the evaluated value of the script). Script fields can |
||
95 | * also access the actual _source document indexed and extract specific elements |
||
96 | * to be returned from it (can be an "object" type). |
||
97 | * |
||
98 | * @var array |
||
99 | */ |
||
100 | private $scriptFields; |
||
101 | |||
102 | /** |
||
103 | * Allows to return the doc value representation of a field for each hit. Doc value |
||
104 | * fields can work on fields that are not stored. Note that if the fields parameter |
||
105 | * specifies fields without docvalues it will try to load the value from the fielddata |
||
106 | * cache causing the terms for that field to be loaded to memory (cached), which will |
||
107 | * result in more memory consumption. |
||
108 | * |
||
109 | * @var array |
||
110 | */ |
||
111 | private $docValueFields; |
||
112 | |||
113 | /** |
||
114 | * Enables explanation for each hit on how its score was computed. |
||
115 | * |
||
116 | * @var bool |
||
117 | */ |
||
118 | private $explain; |
||
119 | |||
120 | /** |
||
121 | * Returns a version for each search hit. |
||
122 | * |
||
123 | * @var bool |
||
124 | */ |
||
125 | private $version; |
||
126 | |||
127 | /** |
||
128 | * Allows to configure different boost level per index when searching across more |
||
129 | * than one indices. This is very handy when hits coming from one index matter more |
||
130 | * than hits coming from another index (think social graph where each user has an index). |
||
131 | * |
||
132 | * @var array |
||
133 | */ |
||
134 | private $indicesBoost; |
||
135 | |||
136 | /** |
||
137 | * Exclude documents which have a _score less than the minimum specified in min_score. |
||
138 | * |
||
139 | * @var int |
||
140 | */ |
||
141 | private $minScore; |
||
142 | |||
143 | /** |
||
144 | * Pagination of results can be done by using the from and size but the cost becomes |
||
145 | * prohibitive when the deep pagination is reached. The index.max_result_window which |
||
146 | * defaults to 10,000 is a safeguard, search requests take heap memory and time |
||
147 | * proportional to from + size. The Scroll api is recommended for efficient deep |
||
148 | * scrolling but scroll contexts are costly and it is not recommended to use it for |
||
149 | * real time user requests. The search_after parameter circumvents this problem by |
||
150 | * providing a live cursor. The idea is to use the results from the previous page to |
||
151 | * help the retrieval of the next page. |
||
152 | * |
||
153 | * @var array |
||
154 | */ |
||
155 | private $searchAfter; |
||
156 | |||
157 | /** |
||
158 | * URI parameters alongside Request body search. |
||
159 | * |
||
160 | * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/search-uri-request.html |
||
161 | * |
||
162 | * @var array |
||
163 | */ |
||
164 | private $uriParams = []; |
||
165 | |||
166 | /** |
||
167 | * While a search request returns a single “page” of results, the scroll API can be used to retrieve |
||
168 | * large numbers of results (or even all results) from a single search request, in much the same way |
||
169 | * as you would use a cursor on a traditional database. Scrolling is not intended for real time user |
||
170 | * requests, but rather for processing large amounts of data, e.g. in order to reindex the contents |
||
171 | * of one index into a new index with a different configuration. |
||
172 | * |
||
173 | * @var string |
||
174 | */ |
||
175 | private $scroll; |
||
176 | |||
177 | /** |
||
178 | * @var OrderedSerializer |
||
179 | */ |
||
180 | private static $serializer; |
||
181 | |||
182 | /** |
||
183 | * @var SearchEndpointInterface[] |
||
184 | */ |
||
185 | private $endpoints = []; |
||
186 | |||
187 | /** |
||
188 | * Constructor to initialize static properties |
||
189 | */ |
||
190 | public function __construct() |
||
194 | |||
195 | /** |
||
196 | * Wakeup method to initialize static properties |
||
197 | */ |
||
198 | public function __wakeup() |
||
202 | |||
203 | /** |
||
204 | * Initializes the serializer |
||
205 | */ |
||
206 | private function initializeSerializer() |
||
217 | |||
218 | /** |
||
219 | * Destroys search endpoint. |
||
220 | * |
||
221 | * @param string $type Endpoint type. |
||
222 | */ |
||
223 | public function destroyEndpoint($type) |
||
227 | |||
228 | /** |
||
229 | * Adds query to the search. |
||
230 | * |
||
231 | * @param BuilderInterface $query |
||
232 | * @param string $boolType |
||
233 | * @param string $key |
||
234 | * |
||
235 | * @return $this |
||
236 | */ |
||
237 | public function addQuery(BuilderInterface $query, $boolType = BoolQuery::MUST, $key = null) |
||
244 | |||
245 | /** |
||
246 | * Returns endpoint instance. |
||
247 | * |
||
248 | * @param string $type Endpoint type. |
||
249 | * |
||
250 | * @return SearchEndpointInterface |
||
251 | */ |
||
252 | private function getEndpoint($type) |
||
260 | |||
261 | /** |
||
262 | * Returns queries inside BoolQuery instance. |
||
263 | * |
||
264 | * @return BoolQuery |
||
265 | */ |
||
266 | public function getQueries() |
||
272 | |||
273 | /** |
||
274 | * Sets query endpoint parameters. |
||
275 | * |
||
276 | * @param array $parameters |
||
277 | * |
||
278 | * @return $this |
||
279 | */ |
||
280 | public function setQueryParameters(array $parameters) |
||
286 | |||
287 | /** |
||
288 | * Sets parameters to the endpoint. |
||
289 | * |
||
290 | * @param string $endpointName |
||
291 | * @param array $parameters |
||
292 | * |
||
293 | * @return $this |
||
294 | */ |
||
295 | public function setEndpointParameters($endpointName, array $parameters) |
||
303 | |||
304 | /** |
||
305 | * Adds a post filter to search. |
||
306 | * |
||
307 | * @param BuilderInterface $filter Filter. |
||
308 | * @param string $boolType Example boolType values: |
||
309 | * - must |
||
310 | * - must_not |
||
311 | * - should. |
||
312 | * @param string $key |
||
313 | * |
||
314 | * @return $this. |
||
315 | */ |
||
316 | public function addPostFilter(BuilderInterface $filter, $boolType = BoolQuery::MUST, $key = null) |
||
324 | |||
325 | /** |
||
326 | * Returns queries inside BoolFilter instance. |
||
327 | * |
||
328 | * @return BoolQuery |
||
329 | */ |
||
330 | public function getPostFilters() |
||
336 | |||
337 | /** |
||
338 | * Sets post filter endpoint parameters. |
||
339 | * |
||
340 | * @param array $parameters |
||
341 | * |
||
342 | * @return $this |
||
343 | */ |
||
344 | public function setPostFilterParameters(array $parameters) |
||
350 | |||
351 | /** |
||
352 | * Adds aggregation into search. |
||
353 | * |
||
354 | * @param AbstractAggregation $aggregation |
||
355 | * |
||
356 | * @return $this |
||
357 | */ |
||
358 | public function addAggregation(AbstractAggregation $aggregation) |
||
364 | |||
365 | /** |
||
366 | * Returns all aggregations. |
||
367 | * |
||
368 | * @return BuilderInterface[] |
||
369 | */ |
||
370 | public function getAggregations() |
||
374 | |||
375 | /** |
||
376 | * Adds inner hit into search. |
||
377 | * |
||
378 | * @param NestedInnerHit $innerHit |
||
379 | * |
||
380 | * @return $this |
||
381 | */ |
||
382 | public function addInnerHit(NestedInnerHit $innerHit) |
||
388 | |||
389 | /** |
||
390 | * Returns all inner hits. |
||
391 | * |
||
392 | * @return BuilderInterface[] |
||
393 | */ |
||
394 | public function getInnerHits() |
||
398 | |||
399 | /** |
||
400 | * Adds sort to search. |
||
401 | * |
||
402 | * @param BuilderInterface $sort |
||
403 | * |
||
404 | * @return $this |
||
405 | */ |
||
406 | public function addSort(BuilderInterface $sort) |
||
412 | |||
413 | /** |
||
414 | * Returns all set sorts. |
||
415 | * |
||
416 | * @return BuilderInterface[] |
||
417 | */ |
||
418 | public function getSorts() |
||
422 | |||
423 | /** |
||
424 | * Allows to highlight search results on one or more fields. |
||
425 | * |
||
426 | * @param Highlight $highlight |
||
427 | * |
||
428 | * @return $this. |
||
429 | */ |
||
430 | public function addHighlight($highlight) |
||
436 | |||
437 | /** |
||
438 | * Returns highlight builder. |
||
439 | * |
||
440 | * @return BuilderInterface |
||
441 | */ |
||
442 | public function getHighlights() |
||
449 | |||
450 | /** |
||
451 | * Adds suggest into search. |
||
452 | * |
||
453 | * @param BuilderInterface $suggest |
||
454 | * |
||
455 | * @return $this |
||
456 | */ |
||
457 | public function addSuggest(NamedBuilderInterface $suggest) |
||
463 | |||
464 | /** |
||
465 | * Returns all suggests. |
||
466 | * |
||
467 | * @return BuilderInterface[] |
||
468 | */ |
||
469 | public function getSuggests() |
||
473 | |||
474 | /** |
||
475 | * @return int |
||
476 | */ |
||
477 | public function getFrom() |
||
481 | |||
482 | /** |
||
483 | * @param int $from |
||
484 | * |
||
485 | * @return $this |
||
486 | */ |
||
487 | public function setFrom($from) |
||
493 | |||
494 | /** |
||
495 | * @param int $time |
||
496 | * |
||
497 | * @return $this |
||
498 | */ |
||
499 | public function setTerminateAfter(int $time) |
||
505 | |||
506 | /** |
||
507 | * @param string $time |
||
508 | * |
||
509 | * @return $this |
||
510 | */ |
||
511 | public function setTimeout($time) |
||
517 | |||
518 | /** |
||
519 | * @return bool |
||
520 | */ |
||
521 | public function isTrackTotalHits() |
||
525 | |||
526 | /** |
||
527 | * @param bool $trackTotalHits |
||
528 | * |
||
529 | * @return $this |
||
530 | */ |
||
531 | public function setTrackTotalHits(bool $trackTotalHits) |
||
537 | |||
538 | /** |
||
539 | * @return int |
||
540 | */ |
||
541 | public function getSize() |
||
545 | |||
546 | /** |
||
547 | * @param int $size |
||
548 | * |
||
549 | * @return $this |
||
550 | */ |
||
551 | public function setSize($size) |
||
557 | |||
558 | /** |
||
559 | * @return bool |
||
560 | */ |
||
561 | public function isSource() |
||
565 | |||
566 | /** |
||
567 | * @param bool $source |
||
568 | * |
||
569 | * @return $this |
||
570 | */ |
||
571 | public function setSource($source) |
||
577 | |||
578 | /** |
||
579 | * @return array |
||
580 | */ |
||
581 | public function getStoredFields() |
||
585 | |||
586 | /** |
||
587 | * @param array $storedFields |
||
588 | * |
||
589 | * @return $this |
||
590 | */ |
||
591 | public function setStoredFields($storedFields) |
||
597 | |||
598 | /** |
||
599 | * @return array |
||
600 | */ |
||
601 | public function getScriptFields() |
||
605 | |||
606 | /** |
||
607 | * @param array $scriptFields |
||
608 | * |
||
609 | * @return $this |
||
610 | */ |
||
611 | public function setScriptFields($scriptFields) |
||
617 | |||
618 | /** |
||
619 | * @return array |
||
620 | */ |
||
621 | public function getDocValueFields() |
||
625 | |||
626 | /** |
||
627 | * @param array $docValueFields |
||
628 | * |
||
629 | * @return $this |
||
630 | */ |
||
631 | public function setDocValueFields($docValueFields) |
||
637 | |||
638 | /** |
||
639 | * @return bool |
||
640 | */ |
||
641 | public function isExplain() |
||
645 | |||
646 | /** |
||
647 | * @param bool $explain |
||
648 | * |
||
649 | * @return $this |
||
650 | */ |
||
651 | public function setExplain($explain) |
||
657 | |||
658 | /** |
||
659 | * @return bool |
||
660 | */ |
||
661 | public function isVersion() |
||
665 | |||
666 | /** |
||
667 | * @param bool $version |
||
668 | * |
||
669 | * @return $this |
||
670 | */ |
||
671 | public function setVersion($version) |
||
677 | |||
678 | /** |
||
679 | * @return array |
||
680 | */ |
||
681 | public function getIndicesBoost() |
||
685 | |||
686 | /** |
||
687 | * @param array $indicesBoost |
||
688 | * |
||
689 | * @return $this |
||
690 | */ |
||
691 | public function setIndicesBoost($indicesBoost) |
||
697 | |||
698 | /** |
||
699 | * @return int |
||
700 | */ |
||
701 | public function getMinScore() |
||
705 | |||
706 | /** |
||
707 | * @param int $minScore |
||
708 | * |
||
709 | * @return $this |
||
710 | */ |
||
711 | public function setMinScore($minScore) |
||
717 | |||
718 | /** |
||
719 | * @return array |
||
720 | */ |
||
721 | public function getSearchAfter() |
||
725 | |||
726 | /** |
||
727 | * @param array $searchAfter |
||
728 | * |
||
729 | * @return $this |
||
730 | */ |
||
731 | public function setSearchAfter($searchAfter) |
||
737 | |||
738 | /** |
||
739 | * @return string |
||
740 | */ |
||
741 | public function getScroll() |
||
745 | |||
746 | /** |
||
747 | * @param string $scroll |
||
748 | * |
||
749 | * @return $this |
||
750 | */ |
||
751 | public function setScroll($scroll = '5m') |
||
759 | |||
760 | /** |
||
761 | * @param string $name |
||
762 | * @param string|array|bool|int $value |
||
763 | * |
||
764 | * @return $this |
||
765 | */ |
||
766 | public function addUriParam($name, $value) |
||
801 | |||
802 | /** |
||
803 | * Returns query url parameters. |
||
804 | * |
||
805 | * @return array |
||
806 | */ |
||
807 | public function getUriParams() |
||
811 | |||
812 | /** |
||
813 | * {@inheritdoc} |
||
814 | */ |
||
815 | public function toArray() |
||
844 | } |
||
845 |
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
SomeClass
to useself
instead: