Complex classes like SearchResultSetService 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 SearchResultSetService, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
49 | class SearchResultSetService |
||
50 | { |
||
51 | /** |
||
52 | * Additional filters, which will be added to the query, as well as to |
||
53 | * suggest queries. |
||
54 | * |
||
55 | * @var array |
||
56 | */ |
||
57 | protected $additionalFilters = []; |
||
58 | |||
59 | /** |
||
60 | * Track, if the number of results per page has been changed by the current request |
||
61 | * |
||
62 | * @var bool |
||
63 | */ |
||
64 | protected $resultsPerPageChanged = false; |
||
65 | |||
66 | /** |
||
67 | * @var \ApacheSolrForTypo3\Solr\Search |
||
68 | */ |
||
69 | protected $search; |
||
70 | |||
71 | /** |
||
72 | * @var SearchResultSet |
||
73 | */ |
||
74 | protected $lastResultSet = null; |
||
75 | |||
76 | /** |
||
77 | * @var bool |
||
78 | */ |
||
79 | protected $useQueryAwareComponents = true; |
||
80 | |||
81 | /** |
||
82 | * @var |
||
83 | */ |
||
84 | protected $isSolrAvailable = false; |
||
85 | |||
86 | /** |
||
87 | * @var TypoScriptConfiguration |
||
88 | */ |
||
89 | protected $typoScriptConfiguration; |
||
90 | |||
91 | /** |
||
92 | * @var \ApacheSolrForTypo3\Solr\System\Logging\SolrLogManager; |
||
93 | */ |
||
94 | protected $logger = null; |
||
95 | |||
96 | /** |
||
97 | * @param TypoScriptConfiguration $configuration |
||
98 | * @param Search $search |
||
99 | * @param SolrLogManager $solrLogManager |
||
100 | */ |
||
101 | public function __construct(TypoScriptConfiguration $configuration, Search $search, SolrLogManager $solrLogManager = null) |
||
102 | { |
||
103 | 45 | $this->search = $search; |
|
104 | $this->typoScriptConfiguration = $configuration; |
||
105 | 45 | $this->logger = is_null($solrLogManager) ? GeneralUtility::makeInstance(SolrLogManager::class, __CLASS__) : $solrLogManager; |
|
106 | 45 | } |
|
107 | 45 | ||
108 | 45 | /** |
|
109 | * @param bool $useCache |
||
110 | * @return bool |
||
111 | */ |
||
112 | public function getIsSolrAvailable($useCache = true) |
||
113 | { |
||
114 | 29 | $this->isSolrAvailable = $this->search->ping($useCache); |
|
115 | return $this->isSolrAvailable; |
||
116 | 29 | } |
|
117 | 29 | ||
118 | /** |
||
119 | * @return bool |
||
120 | */ |
||
121 | public function getHasSearched() |
||
122 | { |
||
123 | 29 | return $this->search->hasSearched(); |
|
124 | } |
||
125 | 29 | ||
126 | /** |
||
127 | * Retrieves the used search instance. |
||
128 | * |
||
129 | * @return Search |
||
130 | */ |
||
131 | public function getSearch() |
||
132 | { |
||
133 | 2 | return $this->search; |
|
134 | } |
||
135 | 2 | ||
136 | /** |
||
137 | * @param bool $useQueryAwareComponents |
||
138 | */ |
||
139 | public function setUseQueryAwareComponents($useQueryAwareComponents) |
||
140 | { |
||
141 | $this->useQueryAwareComponents = $useQueryAwareComponents; |
||
142 | } |
||
143 | |||
144 | /** |
||
145 | * @return bool |
||
146 | */ |
||
147 | public function getUseQueryAwareComponents() |
||
148 | { |
||
149 | return $this->useQueryAwareComponents; |
||
150 | } |
||
151 | |||
152 | /** |
||
153 | * Initializes the Query object and SearchComponents and returns |
||
154 | * the initialized query object, when a search should be executed. |
||
155 | * |
||
156 | * @param string|null $rawQuery |
||
157 | * @param int $resultsPerPage |
||
158 | * @return Query |
||
159 | */ |
||
160 | protected function getPreparedQuery($rawQuery, $resultsPerPage) |
||
195 | 37 | ||
196 | /** |
||
197 | * @param Query $query |
||
198 | * @param SearchRequest $searchRequest |
||
199 | */ |
||
200 | protected function initializeRegisteredSearchComponents(Query $query, SearchRequest $searchRequest) |
||
201 | { |
||
219 | |||
220 | 37 | /** |
|
221 | * Returns the number of results per Page. |
||
222 | * |
||
223 | * Also influences how many result documents are returned by the Solr |
||
224 | * server as the return value is used in the Solr "rows" GET parameter. |
||
225 | * |
||
226 | * @param SearchRequest $searchRequest |
||
227 | * @return int number of results to show per page |
||
228 | */ |
||
229 | protected function getNumberOfResultsPerPage(SearchRequest $searchRequest) |
||
255 | 37 | ||
256 | /** |
||
257 | * Does post processing of the response. |
||
258 | * |
||
259 | * @param \Apache_Solr_Response $response The search's response. |
||
260 | */ |
||
261 | protected function processResponse(\Apache_Solr_Response $response) |
||
266 | 39 | ||
267 | 39 | /** |
|
268 | * This method is used to add documents to the expanded documents of the SearchResult |
||
269 | 39 | * when collapsing is configured. |
|
270 | 39 | * |
|
271 | * @param \Apache_Solr_Response $response |
||
272 | */ |
||
273 | protected function addExpandedDocumentsFromVariants(\Apache_Solr_Response $response) |
||
312 | 2 | ||
313 | 2 | /** |
|
314 | * Wrap all results document it a custom EXT:solr SearchResult object. |
||
315 | * |
||
316 | 2 | * Can be overwritten: |
|
317 | * |
||
318 | * $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['solr']['searchResultClassName '] = '' |
||
319 | * |
||
320 | 2 | * to use a custom result object. |
|
321 | 2 | * |
|
322 | * @param \Apache_Solr_Response $response |
||
323 | * @throws \Apache_Solr_ParserException |
||
324 | */ |
||
325 | 2 | protected function wrapResultDocumentInResultObject(\Apache_Solr_Response $response) |
|
358 | |||
359 | /** |
||
360 | 1 | * This method is used to wrap the \Apache_Solr_Document instance in an instance of the configured SearchResult |
|
361 | * class. |
||
362 | * |
||
363 | * @param \Apache_Solr_Document $originalDocument |
||
364 | 1 | * @throws \InvalidArgumentException |
|
365 | 1 | * @return SearchResult |
|
366 | 1 | */ |
|
367 | 1 | protected function wrapApacheSolrDocumentInResultObject(\Apache_Solr_Document $originalDocument) |
|
377 | 34 | ||
378 | 28 | /** |
|
379 | 28 | * @return string |
|
380 | */ |
||
381 | protected function getResultClassName() |
||
386 | |||
387 | /** |
||
388 | * @return string |
||
389 | */ |
||
390 | protected function getResultSetClassName() |
||
395 | 28 | ||
396 | 28 | /** |
|
397 | 28 | * Checks it the results should be hidden in the response. |
|
398 | * |
||
399 | * @param SearchRequest $searchRequest |
||
400 | * @return bool |
||
401 | 28 | */ |
|
402 | protected function shouldHideResultsFromInitialSearch(SearchRequest $searchRequest) |
||
406 | |||
407 | 28 | /** |
|
408 | * Initializes additional filters configured through TypoScript and |
||
409 | 28 | * Flexforms for use in regular queries and suggest queries. |
|
410 | 28 | * |
|
411 | * @param Query $query |
||
412 | * @return void |
||
413 | */ |
||
414 | protected function applyPageSectionsRootLineFilter(Query $query) |
||
427 | |||
428 | 37 | /** |
|
429 | * Retrieves the configuration filters from the TypoScript configuration, except the __pageSections filter. |
||
430 | 37 | * |
|
431 | * @return array |
||
432 | */ |
||
433 | public function getAdditionalFilters() |
||
469 | |||
470 | /** |
||
471 | 2 | * Performs a search and returns a SearchResultSet. |
|
472 | * |
||
473 | * @param SearchRequest $searchRequest |
||
474 | 2 | * @return SearchResultSet |
|
475 | */ |
||
476 | 2 | public function search(SearchRequest $searchRequest) |
|
538 | 37 | ||
539 | 37 | /** |
|
540 | * @param SearchResultSet $searchResultSet |
||
541 | 37 | * @return SearchResultSet |
|
542 | */ |
||
543 | protected function getAutoCorrection(SearchResultSet $searchResultSet) |
||
564 | |||
565 | /** |
||
566 | * @param SearchResultSet $searchResultSet |
||
567 | * @return SearchResultSet |
||
568 | */ |
||
569 | protected function peformAutoCorrection(SearchResultSet $searchResultSet) |
||
598 | |||
599 | 37 | /** |
|
600 | * Allows to modify a query before eventually handing it over to Solr. |
||
601 | * |
||
602 | * @param Query $query The current query before it's being handed over to Solr. |
||
603 | * @param SearchRequest $searchRequest The searchRequest, relevant in the current context |
||
604 | * @param Search $search The search, relevant in the current context |
||
605 | * @throws \UnexpectedValueException |
||
606 | * @return Query The modified query that is actually going to be given to Solr. |
||
607 | */ |
||
608 | protected function modifyQuery(Query $query, SearchRequest $searchRequest, Search $search) |
||
636 | |||
637 | /** |
||
638 | * Retrieves a single document from solr by document id. |
||
639 | * |
||
640 | * @param string $documentId |
||
641 | * @return SearchResult |
||
642 | 37 | */ |
|
643 | public function getDocumentById($documentId) |
||
655 | |||
656 | 2 | /** |
|
657 | 2 | * This method is used to call the registered hooks during the search execution. |
|
658 | * |
||
659 | 2 | * @param string $eventName |
|
660 | 2 | * @param SearchResultSet $resultSet |
|
661 | * @return SearchResultSet |
||
662 | */ |
||
663 | private function handleSearchHook($eventName, SearchResultSet $resultSet) |
||
678 | 26 | ||
679 | 26 | /** |
|
680 | * @return SearchResultSet |
||
681 | */ |
||
682 | public function getLastResultSet() |
||
686 | |||
687 | /** |
||
688 | * This method returns true when the last search was executed with an empty query |
||
689 | 28 | * string or whitespaces only. When no search was triggered it will return false. |
|
690 | * |
||
691 | 28 | * @return bool |
|
692 | */ |
||
693 | public function getLastSearchWasExecutedWithEmptyQueryString() |
||
702 | |||
703 | /** |
||
704 | * @param int $requestedPerPage |
||
705 | */ |
||
706 | protected function setPerPageInSession($requestedPerPage) |
||
710 | |||
711 | /** |
||
712 | * @return mixed |
||
713 | 3 | */ |
|
714 | protected function getPerPageFromSession() |
||
718 | |||
719 | /** |
||
720 | * @return bool |
||
721 | 30 | */ |
|
722 | protected function getInitialSearchIsConfigured() |
||
726 | |||
727 | /** |
||
728 | * @return mixed |
||
729 | 6 | */ |
|
730 | protected function getRegisteredSearchComponents() |
||
734 | |||
735 | /** |
||
736 | * This method is used to reference the SearchResult object from the response in the SearchResultSet object. |
||
737 | 30 | * |
|
738 | * @param \Apache_Solr_Response $response |
||
739 | 30 | * @param SearchResultSet $resultSet |
|
740 | */ |
||
741 | protected function addSearchResultsToResultSet($response, $resultSet) |
||
751 | 5 | ||
752 | /** |
||
753 | * @param string $rawQuery |
||
754 | 32 | * @return Query|object |
|
755 | 26 | */ |
|
756 | protected function getQueryInstance($rawQuery) |
||
761 | } |
||
762 |
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: