Complex classes like Query 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 Query, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
52 | class Query |
||
53 | { |
||
54 | /** |
||
55 | * @var QueryStringContainer |
||
56 | */ |
||
57 | protected $queryStringContainer = null; |
||
58 | |||
59 | /** |
||
60 | * @var Pagination |
||
61 | */ |
||
62 | protected $pagination = null; |
||
63 | |||
64 | /** |
||
65 | * @var Operator |
||
66 | */ |
||
67 | protected $operator = null; |
||
68 | |||
69 | /** |
||
70 | * ParameterBuilder for filters. |
||
71 | * |
||
72 | * @var Filters |
||
73 | */ |
||
74 | protected $filters = null; |
||
75 | |||
76 | /** |
||
77 | * Holds the query fields with their associated boosts. The key represents |
||
78 | * the field name, value represents the field's boost. These are the fields |
||
79 | * that will actually be searched. |
||
80 | * |
||
81 | * Used in Solr's qf parameter |
||
82 | * |
||
83 | * @var QueryFields |
||
84 | * @see http://wiki.apache.org/solr/DisMaxQParserPlugin#qf_.28Query_Fields.29 |
||
85 | */ |
||
86 | protected $queryFields = null; |
||
87 | |||
88 | /** |
||
89 | * Holds the phrase fields with their associated boosts. The key represents |
||
90 | * the field name, value represents the field's boost. These are the fields |
||
91 | * for those Apache Solr should build phrase quieries and by phrase occurrences should be boosted. |
||
92 | * |
||
93 | * @var PhraseFields |
||
94 | * @see https://lucene.apache.org/solr/guide/7_0/the-dismax-query-parser.html#pf-phrase-fields-parameter |
||
95 | */ |
||
96 | protected $phraseFields; |
||
97 | |||
98 | /** |
||
99 | * Holds the bigram phrase fields with their associated boosts. The key represents |
||
100 | * the field name, value represents the field's boost. These are the fields |
||
101 | * for those Apache Solr should build the phrases from triplets and sentences. |
||
102 | * |
||
103 | * @var BigramPhraseFields |
||
104 | * @see "pf2" https://lucene.apache.org/solr/guide/7_0/the-extended-dismax-query-parser.html#extended-dismax-parameters |
||
105 | */ |
||
106 | protected $bigramPhraseFields; |
||
107 | |||
108 | /** |
||
109 | * Holds the trigram phrase fields with their associated boosts. The key represents |
||
110 | * the field name, value represents the field's boost. These are the fields |
||
111 | * for those Apache Solr should build the phrases from triplets and sentences. |
||
112 | * |
||
113 | * @var TrigramPhraseFields |
||
114 | * @see "pf3" https://lucene.apache.org/solr/guide/7_0/the-extended-dismax-query-parser.html#extended-dismax-parameters |
||
115 | */ |
||
116 | protected $trigramPhraseFields; |
||
117 | |||
118 | /** |
||
119 | * List of fields that will be returned in the result documents. |
||
120 | * |
||
121 | * used in Solr's fl parameter |
||
122 | * |
||
123 | * @var ReturnFields |
||
124 | * @see http://wiki.apache.org/solr/CommonQueryParameters#fl |
||
125 | */ |
||
126 | protected $returnFields = null; |
||
127 | |||
128 | /** |
||
129 | * ParameterBuilder for the highlighting. |
||
130 | * |
||
131 | * @var Highlighting |
||
132 | */ |
||
133 | protected $highlighting = null; |
||
134 | |||
135 | /** |
||
136 | * ParameterBuilder for the faceting. |
||
137 | * |
||
138 | * @var Faceting |
||
139 | */ |
||
140 | protected $faceting = null; |
||
141 | |||
142 | /** |
||
143 | * ParameterBuilder for the spellchecking |
||
144 | * |
||
145 | * @var Spellchecking |
||
146 | */ |
||
147 | protected $spellchecking = null; |
||
148 | |||
149 | /** |
||
150 | * ParameterBuilder for the grouping. |
||
151 | * |
||
152 | * @var Grouping |
||
153 | */ |
||
154 | protected $grouping = null; |
||
155 | |||
156 | /** |
||
157 | * ParameterBuilder for the field collapsing (variants) |
||
158 | * |
||
159 | * @var FieldCollapsing |
||
160 | */ |
||
161 | protected $fieldCollapsing = null; |
||
162 | |||
163 | /** |
||
164 | * ParameterBuilder for the debugging. |
||
165 | * |
||
166 | * @var Debug |
||
167 | */ |
||
168 | protected $debug = null; |
||
169 | |||
170 | /** |
||
171 | * ParameterBuilder for the sorting |
||
172 | * |
||
173 | * @var Sorting |
||
174 | */ |
||
175 | protected $sorting = null; |
||
176 | |||
177 | /** |
||
178 | * ParameterBuilder for the slops (qs,ps,ps2,ps3) |
||
179 | * |
||
180 | * @var Slops |
||
181 | */ |
||
182 | protected $slops = null; |
||
183 | |||
184 | /** |
||
185 | * ParameterBuilder for the elevation |
||
186 | * |
||
187 | * @var Elevation |
||
188 | */ |
||
189 | protected $elevation = null; |
||
190 | |||
191 | /** |
||
192 | * @var QueryParametersContainer |
||
193 | */ |
||
194 | protected $queryParametersContainer = null; |
||
195 | |||
196 | /** |
||
197 | * Query constructor. |
||
198 | * @param string $keywords |
||
199 | */ |
||
200 | public function __construct($keywords) |
||
224 | |||
225 | /** |
||
226 | * @param QueryFields $queryFields |
||
227 | */ |
||
228 | public function setQueryFields(QueryFields $queryFields) |
||
232 | |||
233 | /** |
||
234 | * @return QueryFields |
||
235 | */ |
||
236 | public function getQueryFields() |
||
240 | |||
241 | /** |
||
242 | * @param PhraseFields $phraseFields |
||
243 | * @return void |
||
244 | */ |
||
245 | public function setPhraseFields(PhraseFields $phraseFields) |
||
249 | |||
250 | /** |
||
251 | * @return PhraseFields |
||
252 | */ |
||
253 | public function getPhraseFields() |
||
257 | |||
258 | /** |
||
259 | * @return BigramPhraseFields |
||
260 | */ |
||
261 | public function getBigramPhraseFields() |
||
265 | |||
266 | /** |
||
267 | * @param BigramPhraseFields $bigramPhraseFields |
||
268 | * @return void |
||
269 | */ |
||
270 | public function setBigramPhraseFields(BigramPhraseFields $bigramPhraseFields) |
||
274 | |||
275 | /** |
||
276 | * @return TrigramPhraseFields |
||
277 | */ |
||
278 | public function getTrigramPhraseFields() |
||
282 | |||
283 | /** |
||
284 | * @param TrigramPhraseFields $trigramPhraseFields |
||
285 | * @return void |
||
286 | */ |
||
287 | public function setTrigramPhraseFields(TrigramPhraseFields $trigramPhraseFields) |
||
291 | |||
292 | /** |
||
293 | * returns a string representation of the query |
||
294 | * |
||
295 | * @return string the string representation of the query |
||
296 | */ |
||
297 | public function __toString() |
||
301 | |||
302 | /** |
||
303 | * Builds the query string which is then used for Solr's q parameters |
||
304 | * |
||
305 | * @return QueryStringContainer |
||
306 | */ |
||
307 | public function getQueryStringContainer() |
||
311 | |||
312 | /** |
||
313 | * @param QueryStringContainer $queryStringContainer |
||
314 | */ |
||
315 | public function setQueryStringContainer(QueryStringContainer $queryStringContainer) |
||
319 | |||
320 | /** |
||
321 | * @return Pagination |
||
322 | */ |
||
323 | public function getPagination(): Pagination |
||
327 | |||
328 | /** |
||
329 | * @param Pagination $pagination |
||
330 | */ |
||
331 | public function setPagination(Pagination $pagination) |
||
335 | |||
336 | // query elevation |
||
337 | |||
338 | /** |
||
339 | * @param Elevation $elevation |
||
340 | */ |
||
341 | public function setElevation(Elevation $elevation) |
||
345 | |||
346 | /** |
||
347 | * @return Elevation |
||
348 | */ |
||
349 | public function getElevation(): Elevation |
||
353 | |||
354 | // collapsing |
||
355 | |||
356 | /** |
||
357 | * @param FieldCollapsing $fieldCollapsing |
||
358 | */ |
||
359 | public function setFieldCollapsing(FieldCollapsing $fieldCollapsing) |
||
363 | |||
364 | /** |
||
365 | * @return FieldCollapsing |
||
366 | */ |
||
367 | public function getFieldCollapsing(): FieldCollapsing |
||
371 | |||
372 | // grouping |
||
373 | |||
374 | /** |
||
375 | * Activates and deactivates grouping for the current query. |
||
376 | * |
||
377 | * @param Grouping $grouping TRUE to enable grouping, FALSE to disable grouping |
||
378 | * @return void |
||
379 | */ |
||
380 | public function setGrouping(Grouping $grouping) |
||
384 | |||
385 | /** |
||
386 | * @return Grouping |
||
387 | */ |
||
388 | public function getGrouping(): Grouping |
||
392 | |||
393 | /** |
||
394 | * Returns the number of results that should be shown per page or the number of groups, when grouping is active |
||
395 | * |
||
396 | * @return int number of results to show per page |
||
397 | */ |
||
398 | public function getRows() |
||
406 | |||
407 | // faceting |
||
408 | |||
409 | /** |
||
410 | * Activates and deactivates faceting for the current query. |
||
411 | * |
||
412 | * @param Faceting $faceting TRUE to enable faceting, FALSE to disable faceting |
||
413 | * @return void |
||
414 | */ |
||
415 | public function setFaceting(Faceting $faceting) |
||
419 | |||
420 | /** |
||
421 | * @return Faceting |
||
422 | */ |
||
423 | public function getFaceting(): Faceting |
||
427 | |||
428 | /** |
||
429 | * Sets the filters to use. |
||
430 | * |
||
431 | * @param Filters $filters |
||
432 | */ |
||
433 | public function setFilters(Filters $filters) |
||
437 | |||
438 | /** |
||
439 | * Gets all currently applied filters. |
||
440 | * |
||
441 | * @return Filters Array of filters |
||
442 | */ |
||
443 | public function getFilters(): Filters |
||
447 | |||
448 | /** |
||
449 | * @param ReturnFields $returnFields |
||
450 | */ |
||
451 | public function setReturnFields(ReturnFields $returnFields) |
||
455 | |||
456 | /** |
||
457 | * @return ReturnFields |
||
458 | */ |
||
459 | public function getReturnFields(): ReturnFields |
||
463 | |||
464 | /** |
||
465 | * Gets the query type, Solr's qt parameter. |
||
466 | * |
||
467 | * @return string Query type, qt parameter. |
||
468 | */ |
||
469 | public function getQueryType() |
||
473 | |||
474 | /** |
||
475 | * Sets the query type, Solr's qt parameter. |
||
476 | * |
||
477 | * @param string|bool $queryType String query type or boolean FALSE to disable / reset the qt parameter. |
||
478 | * @see http://wiki.apache.org/solr/CoreQueryParameters#qt |
||
479 | */ |
||
480 | public function setQueryType($queryType) |
||
484 | |||
485 | /** |
||
486 | * Set the operator that should be used for the query. Operators an be created e.g. by using |
||
487 | * Operator::and() |
||
488 | * |
||
489 | * @param Operator $operator |
||
490 | */ |
||
491 | public function setOperator(Operator $operator) |
||
495 | |||
496 | /** |
||
497 | * Returns the operator of the query. |
||
498 | * |
||
499 | * @return Operator |
||
500 | */ |
||
501 | public function getOperator(): Operator |
||
505 | |||
506 | /** |
||
507 | * @return Slops |
||
508 | */ |
||
509 | public function getSlops(): Slops |
||
513 | |||
514 | /** |
||
515 | * @param Slops $slops |
||
516 | */ |
||
517 | public function setSlops(Slops $slops) |
||
521 | |||
522 | /** |
||
523 | * Gets the alternative query, Solr's q.alt parameter. |
||
524 | * |
||
525 | * @return string Alternative query, q.alt parameter. |
||
526 | */ |
||
527 | public function getAlternativeQuery() |
||
531 | |||
532 | /** |
||
533 | * Sets an alternative query, Solr's q.alt parameter. |
||
534 | * |
||
535 | * This query supports the complete Lucene Query Language. |
||
536 | * |
||
537 | * @param string $alternativeQuery String alternative query or boolean FALSE to disable / reset the q.alt parameter. |
||
538 | * @see http://wiki.apache.org/solr/DisMaxQParserPlugin#q.alt |
||
539 | */ |
||
540 | public function setAlternativeQuery($alternativeQuery) |
||
544 | |||
545 | // keywords |
||
546 | |||
547 | /** |
||
548 | * Set the query to omit the response header |
||
549 | * |
||
550 | * @param bool $omitHeader TRUE (default) to omit response headers, FALSE to re-enable |
||
551 | */ |
||
552 | public function setOmitHeader($omitHeader = true) |
||
557 | |||
558 | /** |
||
559 | * Sets the minimum match (mm) parameter |
||
560 | * |
||
561 | * @param mixed $minimumMatch Minimum match parameter as string or boolean FALSE to disable / reset the mm parameter |
||
562 | * @see http://wiki.apache.org/solr/DisMaxRequestHandler#mm_.28Minimum_.27Should.27_Match.29 |
||
563 | */ |
||
564 | public function setMinimumMatch($minimumMatch) |
||
568 | |||
569 | /** |
||
570 | * Sets the boost function (bf) parameter |
||
571 | * |
||
572 | * @param mixed $boostFunction boost function parameter as string or boolean FALSE to disable / reset the bf parameter |
||
573 | * @see http://wiki.apache.org/solr/DisMaxRequestHandler#bf_.28Boost_Functions.29 |
||
574 | */ |
||
575 | public function setBoostFunction($boostFunction) |
||
579 | |||
580 | /** |
||
581 | * Sets the boost query (bq) parameter |
||
582 | * |
||
583 | * @param mixed $boostQuery boost query parameter as string or array to set a boost query or boolean FALSE to disable / reset the bq parameter |
||
584 | * @see http://wiki.apache.org/solr/DisMaxQParserPlugin#bq_.28Boost_Query.29 |
||
585 | */ |
||
586 | public function setBoostQuery($boostQuery) |
||
594 | |||
595 | /** |
||
596 | * Set the tie breaker (tie) parameter |
||
597 | * |
||
598 | * @param mixed $tieParameter tie breaker parameter as string or boolean FALSE to disable / reset the tie parameter |
||
599 | * @return void |
||
600 | */ |
||
601 | public function setTieParameter($tieParameter) |
||
605 | |||
606 | /** |
||
607 | * Gets a specific query parameter by its name. |
||
608 | * |
||
609 | * @param string $parameterName The parameter to return |
||
610 | * @param mixed $defaultIfEmpty |
||
611 | * @return mixed The parameter's value or $defaultIfEmpty if not set |
||
612 | */ |
||
613 | public function getQueryParameter($parameterName, $defaultIfEmpty = null) |
||
618 | |||
619 | /** |
||
620 | * The build method calls build on all ParameterBuilder that fill the QueryParameterContainer |
||
621 | * |
||
622 | * @return void |
||
623 | */ |
||
624 | protected function build() |
||
646 | |||
647 | /** |
||
648 | * Builds an array of query parameters to use for the search query. |
||
649 | * |
||
650 | * @return array An array ready to use with query parameters |
||
651 | */ |
||
652 | public function getQueryParameters() |
||
657 | |||
658 | /** |
||
659 | * @return QueryParametersContainer |
||
660 | */ |
||
661 | public function getQueryParametersContainer(): QueryParametersContainer |
||
665 | |||
666 | /** |
||
667 | * Adds a parameter to the query. |
||
668 | * |
||
669 | * @param string $parameterName |
||
670 | * @param mixed $value |
||
671 | */ |
||
672 | public function addQueryParameter($parameterName, $value) |
||
676 | |||
677 | // general query parameters |
||
678 | |||
679 | /** |
||
680 | * Enables or disables highlighting of search terms in result teasers. |
||
681 | * |
||
682 | * @param Highlighting $highlighting |
||
683 | * @see http://wiki.apache.org/solr/HighlightingParameters |
||
684 | * @return void |
||
685 | */ |
||
686 | public function setHighlighting(Highlighting $highlighting) |
||
690 | |||
691 | /** |
||
692 | * @return Highlighting |
||
693 | */ |
||
694 | public function getHighlighting(): Highlighting |
||
698 | |||
699 | // misc |
||
700 | /** |
||
701 | * @param Spellchecking $spellchecking |
||
702 | */ |
||
703 | public function setSpellchecking(Spellchecking $spellchecking) |
||
707 | |||
708 | /** |
||
709 | * @return Spellchecking |
||
710 | */ |
||
711 | public function getSpellchecking(): Spellchecking |
||
715 | |||
716 | /** |
||
717 | * Sets the sort parameter. |
||
718 | * |
||
719 | * $sorting must include a field name (or the pseudo-field score), |
||
720 | * followed by a space, |
||
721 | * followed by a sort direction (asc or desc). |
||
722 | * |
||
723 | * Multiple fallback sortings can be separated by comma, |
||
724 | * ie: <field name> <direction>[,<field name> <direction>]... |
||
725 | * |
||
726 | * @param string|bool $sorting Either a comma-separated list of sort fields and directions or FALSE to reset sorting to the default behavior (sort by score / relevance) |
||
727 | * @see http://wiki.apache.org/solr/CommonQueryParameters#sort |
||
728 | */ |
||
729 | public function setSorting($sorting) |
||
736 | |||
737 | /** |
||
738 | * Enables or disables the debug parameter for the query. |
||
739 | * |
||
740 | * @param bool $debugMode Enables debugging when set to TRUE, deactivates debugging when set to FALSE, defaults to TRUE. |
||
741 | */ |
||
742 | public function setDebugMode($debugMode = true) |
||
746 | } |
||
747 |