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() |
||
169 | { |
||
170 | $this->serializer = new OrderedSerializer( |
||
171 | [ |
||
172 | new CustomReferencedNormalizer(), |
||
173 | new CustomNormalizer(), |
||
174 | ] |
||
175 | ); |
||
176 | } |
||
177 | |||
178 | /** |
||
179 | * Destroys search endpoint. |
||
180 | * |
||
181 | * @param string $type Endpoint type. |
||
182 | */ |
||
183 | public function destroyEndpoint($type) |
||
184 | { |
||
185 | unset($this->endpoints[$type]); |
||
186 | } |
||
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) |
||
198 | { |
||
199 | $endpoint = $this->getEndpoint(QueryEndpoint::NAME); |
||
200 | $endpoint->addToBool($query, $boolType, $key); |
||
201 | |||
202 | return $this; |
||
203 | } |
||
204 | |||
205 | /** |
||
206 | * Returns endpoint instance. |
||
207 | * |
||
208 | * @param string $type Endpoint type. |
||
209 | * |
||
210 | * @return SearchEndpointInterface |
||
211 | */ |
||
212 | private function getEndpoint($type) |
||
213 | { |
||
214 | if (!array_key_exists($type, $this->endpoints)) { |
||
215 | $this->endpoints[$type] = SearchEndpointFactory::get($type); |
||
216 | } |
||
217 | |||
218 | return $this->endpoints[$type]; |
||
219 | } |
||
220 | |||
221 | /** |
||
222 | * Returns queries inside BoolQuery instance. |
||
223 | * |
||
224 | * @return BuilderInterface |
||
225 | */ |
||
226 | public function getQueries() |
||
227 | { |
||
228 | $endpoint = $this->getEndpoint(QueryEndpoint::NAME); |
||
229 | |||
230 | return $endpoint->getBool(); |
||
231 | } |
||
232 | |||
233 | /** |
||
234 | * Sets query endpoint parameters. |
||
235 | * |
||
236 | * @param array $parameters |
||
237 | * |
||
238 | * @return $this |
||
239 | */ |
||
240 | public function setQueryParameters(array $parameters) |
||
241 | { |
||
242 | $this->setEndpointParameters(QueryEndpoint::NAME, $parameters); |
||
243 | |||
244 | return $this; |
||
245 | } |
||
246 | |||
247 | /** |
||
248 | * Sets parameters to the endpoint. |
||
249 | * |
||
250 | * @param string $endpointName |
||
251 | * @param array $parameters |
||
252 | */ |
||
253 | public function setEndpointParameters($endpointName, array $parameters) |
||
254 | { |
||
255 | /** @var AbstractSearchEndpoint $endpoint */ |
||
256 | $endpoint = $this->getEndpoint($endpointName); |
||
257 | $endpoint->setParameters($parameters); |
||
258 | } |
||
259 | |||
260 | /** |
||
261 | * Adds a post filter to search. |
||
262 | * |
||
263 | * @param BuilderInterface $filter Filter. |
||
264 | * @param string $boolType Example boolType values: |
||
265 | * - must |
||
266 | * - must_not |
||
267 | * - should. |
||
268 | * @param string $key |
||
269 | * |
||
270 | * @return $this. |
||
271 | */ |
||
272 | public function addPostFilter(BuilderInterface $filter, $boolType = BoolQuery::MUST, $key = null) |
||
280 | |||
281 | /** |
||
282 | * Returns queries inside BoolFilter instance. |
||
283 | * |
||
284 | * @return BuilderInterface |
||
285 | */ |
||
286 | public function getPostFilters() |
||
287 | { |
||
292 | |||
293 | /** |
||
294 | * Sets post filter endpoint parameters. |
||
295 | * |
||
296 | * @param array $parameters |
||
297 | * |
||
298 | * @return $this |
||
299 | */ |
||
300 | public function setPostFilterParameters(array $parameters) |
||
306 | |||
307 | /** |
||
308 | * Adds aggregation into search. |
||
309 | * |
||
310 | * @param AbstractAggregation $aggregation |
||
311 | * |
||
312 | * @return $this |
||
313 | */ |
||
314 | public function addAggregation(AbstractAggregation $aggregation) |
||
320 | |||
321 | /** |
||
322 | * Returns all aggregations. |
||
323 | * |
||
324 | * @return BuilderInterface[] |
||
325 | */ |
||
326 | public function getAggregations() |
||
330 | |||
331 | /** |
||
332 | * Adds inner hit into search. |
||
333 | * |
||
334 | * @param NestedInnerHit $innerHit |
||
335 | * |
||
336 | * @return $this |
||
337 | */ |
||
338 | public function addInnerHit(NestedInnerHit $innerHit) |
||
344 | |||
345 | /** |
||
346 | * Returns all inner hits. |
||
347 | * |
||
348 | * @return BuilderInterface[] |
||
349 | */ |
||
350 | public function getInnerHits() |
||
354 | |||
355 | /** |
||
356 | * Adds sort to search. |
||
357 | * |
||
358 | * @param BuilderInterface $sort |
||
359 | * |
||
360 | * @return $this |
||
361 | */ |
||
362 | public function addSort(BuilderInterface $sort) |
||
368 | |||
369 | /** |
||
370 | * Returns all set sorts. |
||
371 | * |
||
372 | * @return BuilderInterface[] |
||
373 | */ |
||
374 | public function getSorts() |
||
378 | |||
379 | /** |
||
380 | * Allows to highlight search results on one or more fields. |
||
381 | * |
||
382 | * @param Highlight $highlight |
||
383 | * |
||
384 | * @return $this. |
||
385 | */ |
||
386 | public function addHighlight($highlight) |
||
392 | |||
393 | /** |
||
394 | * Returns highlight builder. |
||
395 | * |
||
396 | * @return BuilderInterface |
||
397 | */ |
||
398 | public function getHighlights() |
||
405 | |||
406 | /** |
||
407 | * Adds suggest into search. |
||
408 | * |
||
409 | * @param BuilderInterface $suggest |
||
410 | * |
||
411 | * @return $this |
||
412 | */ |
||
413 | public function addSuggest(BuilderInterface $suggest) |
||
419 | |||
420 | /** |
||
421 | * Returns all suggests. |
||
422 | * |
||
423 | * @return BuilderInterface[] |
||
424 | */ |
||
425 | public function getSuggests() |
||
429 | |||
430 | /** |
||
431 | * @return int |
||
432 | */ |
||
433 | public function getFrom() |
||
437 | |||
438 | /** |
||
439 | * @param int $from |
||
440 | * @return $this |
||
441 | */ |
||
442 | public function setFrom($from) |
||
447 | |||
448 | /** |
||
449 | * @return int |
||
450 | */ |
||
451 | public function getSize() |
||
455 | |||
456 | /** |
||
457 | * @param int $size |
||
458 | * @return $this |
||
459 | */ |
||
460 | public function setSize($size) |
||
465 | |||
466 | /** |
||
467 | * @return bool |
||
468 | */ |
||
469 | public function isSource() |
||
473 | |||
474 | /** |
||
475 | * @param bool $source |
||
476 | * @return $this |
||
477 | */ |
||
478 | public function setSource($source) |
||
483 | |||
484 | /** |
||
485 | * @return array |
||
486 | */ |
||
487 | public function getStoredFields() |
||
491 | |||
492 | /** |
||
493 | * @param array $storedFields |
||
494 | * @return $this |
||
495 | */ |
||
496 | public function setStoredFields($storedFields) |
||
501 | |||
502 | /** |
||
503 | * @return array |
||
504 | */ |
||
505 | public function getScriptFields() |
||
509 | |||
510 | /** |
||
511 | * @param array $scriptFields |
||
512 | * @return $this |
||
513 | */ |
||
514 | public function setScriptFields($scriptFields) |
||
519 | |||
520 | /** |
||
521 | * @return array |
||
522 | */ |
||
523 | public function getDocValueFields() |
||
527 | |||
528 | /** |
||
529 | * @param array $docValueFields |
||
530 | * @return $this |
||
531 | */ |
||
532 | public function setDocValueFields($docValueFields) |
||
537 | |||
538 | /** |
||
539 | * @return bool |
||
540 | */ |
||
541 | public function isExplain() |
||
545 | |||
546 | /** |
||
547 | * @param bool $explain |
||
548 | * @return $this |
||
549 | */ |
||
550 | public function setExplain($explain) |
||
555 | |||
556 | /** |
||
557 | * @return bool |
||
558 | */ |
||
559 | public function isVersion() |
||
563 | |||
564 | /** |
||
565 | * @param bool $version |
||
566 | * @return $this |
||
567 | */ |
||
568 | public function setVersion($version) |
||
573 | |||
574 | /** |
||
575 | * @return array |
||
576 | */ |
||
577 | public function getIndicesBoost() |
||
581 | |||
582 | /** |
||
583 | * @param array $indicesBoost |
||
584 | * @return $this |
||
585 | */ |
||
586 | public function setIndicesBoost($indicesBoost) |
||
591 | |||
592 | /** |
||
593 | * @return int |
||
594 | */ |
||
595 | public function getMinScore() |
||
599 | |||
600 | /** |
||
601 | * @param int $minScore |
||
602 | * @return $this |
||
603 | */ |
||
604 | public function setMinScore($minScore) |
||
609 | |||
610 | /** |
||
611 | * @return array |
||
612 | */ |
||
613 | public function getSearchAfter() |
||
617 | |||
618 | /** |
||
619 | * @param array $searchAfter |
||
620 | * @return $this |
||
621 | */ |
||
622 | public function setSearchAfter($searchAfter) |
||
627 | |||
628 | /** |
||
629 | * @return string |
||
630 | */ |
||
631 | public function getScroll() |
||
635 | |||
636 | /** |
||
637 | * @param string $scroll |
||
638 | * @return $this |
||
639 | */ |
||
640 | public function setScroll($scroll = '5m') |
||
645 | |||
646 | /** |
||
647 | * @param string $name |
||
648 | * @param string|array|bool $value |
||
649 | * |
||
650 | * @return $this |
||
651 | */ |
||
652 | public function addUriParam($name, $value) |
||
679 | |||
680 | /** |
||
681 | * Returns query url parameters. |
||
682 | * |
||
683 | * @return array |
||
684 | */ |
||
685 | public function getUriParams() |
||
689 | |||
690 | /** |
||
691 | * {@inheritdoc} |
||
692 | */ |
||
693 | public function toArray() |
||
719 | } |
||
720 |
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: