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 |
||
51 | class SearchResultSetService |
||
52 | { |
||
53 | /** |
||
54 | * Additional filters, which will be added to the query, as well as to |
||
55 | * suggest queries. |
||
56 | * |
||
57 | * @var array |
||
58 | */ |
||
59 | protected $additionalFilters = []; |
||
60 | |||
61 | /** |
||
62 | * Track, if the number of results per page has been changed by the current request |
||
63 | * |
||
64 | * @var bool |
||
65 | */ |
||
66 | protected $resultsPerPageChanged = false; |
||
67 | |||
68 | /** |
||
69 | * @var Search |
||
70 | */ |
||
71 | protected $search; |
||
72 | |||
73 | /** |
||
74 | * @var SearchResultSet |
||
75 | */ |
||
76 | protected $lastResultSet = null; |
||
77 | |||
78 | /** |
||
79 | * @var boolean |
||
80 | */ |
||
81 | protected $isSolrAvailable = false; |
||
82 | |||
83 | /** |
||
84 | * @var TypoScriptConfiguration |
||
85 | */ |
||
86 | protected $typoScriptConfiguration; |
||
87 | |||
88 | /** |
||
89 | * @var SolrLogManager; |
||
90 | */ |
||
91 | protected $logger = null; |
||
92 | |||
93 | /** |
||
94 | * @var FrontendUserSession |
||
95 | */ |
||
96 | protected $session = null; |
||
97 | |||
98 | /** |
||
99 | * @var SearchResultBuilder |
||
100 | */ |
||
101 | protected $searchResultBuilder; |
||
102 | |||
103 | 46 | /** |
|
104 | * @param TypoScriptConfiguration $configuration |
||
105 | 46 | * @param Search $search |
|
106 | 46 | * @param SolrLogManager $solrLogManager |
|
107 | 46 | * @param FrontendUserSession $frontendUserSession |
|
108 | 46 | * @param SearchResultBuilder $resultBuilder |
|
109 | */ |
||
110 | public function __construct(TypoScriptConfiguration $configuration, Search $search, SolrLogManager $solrLogManager = null, FrontendUserSession $frontendUserSession = null, SearchResultBuilder $resultBuilder = null) |
||
111 | { |
||
112 | $this->search = $search; |
||
113 | $this->typoScriptConfiguration = $configuration; |
||
114 | 30 | $this->logger = is_null($solrLogManager) ? GeneralUtility::makeInstance(SolrLogManager::class, __CLASS__) : $solrLogManager; |
|
115 | $this->session = is_null($frontendUserSession) ? GeneralUtility::makeInstance(FrontendUserSession::class) : $frontendUserSession; |
||
116 | 30 | $this->searchResultBuilder = is_null($resultBuilder) ? GeneralUtility::makeInstance(SearchResultBuilder::class) : $resultBuilder; |
|
117 | 30 | } |
|
118 | |||
119 | /** |
||
120 | * @param bool $useCache |
||
121 | * @return bool |
||
122 | */ |
||
123 | 30 | public function getIsSolrAvailable($useCache = true) |
|
124 | { |
||
125 | 30 | $this->isSolrAvailable = $this->search->ping($useCache); |
|
126 | return $this->isSolrAvailable; |
||
127 | } |
||
128 | |||
129 | /** |
||
130 | * @return bool |
||
131 | */ |
||
132 | public function getHasSearched() |
||
136 | |||
137 | /** |
||
138 | * Retrieves the used search instance. |
||
139 | * |
||
140 | * @return Search |
||
141 | */ |
||
142 | public function getSearch() |
||
146 | |||
147 | /** |
||
148 | * Initializes the Query object and SearchComponents and returns |
||
149 | * the initialized query object, when a search should be executed. |
||
150 | * |
||
151 | * @param string|null $rawQuery |
||
152 | * @param int $resultsPerPage |
||
153 | * @return Query |
||
154 | */ |
||
155 | protected function getPreparedQuery($rawQuery, $resultsPerPage) |
||
190 | |||
191 | 38 | /** |
|
192 | 2 | * @param Query $query |
|
193 | * @param SearchRequest $searchRequest |
||
194 | */ |
||
195 | 38 | protected function initializeRegisteredSearchComponents(Query $query, SearchRequest $searchRequest) |
|
214 | 32 | ||
215 | 31 | /** |
|
216 | * Returns the number of results per Page. |
||
217 | * |
||
218 | 32 | * Also influences how many result documents are returned by the Solr |
|
219 | * server as the return value is used in the Solr "rows" GET parameter. |
||
220 | 38 | * |
|
221 | * @param SearchRequest $searchRequest |
||
222 | * @return int number of results to show per page |
||
223 | */ |
||
224 | protected function getNumberOfResultsPerPage(SearchRequest $searchRequest) |
||
251 | |||
252 | 2 | /** |
|
253 | * Does post processing of the response. |
||
254 | * |
||
255 | 38 | * @param \Apache_Solr_Response $response The search's response. |
|
256 | */ |
||
257 | protected function processResponse(\Apache_Solr_Response $response) |
||
261 | |||
262 | /** |
||
263 | * Wrap all results document it a custom EXT:solr SearchResult object. |
||
264 | 40 | * |
|
265 | * Can be overwritten: |
||
266 | 40 | * |
|
267 | 40 | * $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['solr']['searchResultClassName '] = '' |
|
268 | * |
||
269 | 40 | * to use a custom result object. |
|
270 | 40 | * |
|
271 | * @param \Apache_Solr_Response $response |
||
272 | * @throws \Apache_Solr_ParserException |
||
273 | */ |
||
274 | protected function wrapResultDocumentInResultObject(\Apache_Solr_Response $response) |
||
307 | |||
308 | /** |
||
309 | 3 | * @return string |
|
310 | 3 | */ |
|
311 | protected function getResultSetClassName() |
||
316 | 2 | ||
317 | /** |
||
318 | * Checks it the results should be hidden in the response. |
||
319 | * |
||
320 | 2 | * @param SearchRequest $searchRequest |
|
321 | 2 | * @return bool |
|
322 | */ |
||
323 | protected function shouldHideResultsFromInitialSearch(SearchRequest $searchRequest) |
||
327 | 2 | ||
328 | 2 | /** |
|
329 | * Initializes additional filters configured through TypoScript and |
||
330 | 2 | * Flexforms for use in regular queries and suggest queries. |
|
331 | 2 | * |
|
332 | 2 | * @param Query $query |
|
333 | * @return void |
||
334 | 2 | */ |
|
335 | protected function applyPageSectionsRootLineFilter(Query $query) |
||
348 | |||
349 | /** |
||
350 | * Retrieves the configuration filters from the TypoScript configuration, except the __pageSections filter. |
||
351 | 40 | * |
|
352 | * @return array |
||
353 | */ |
||
354 | 40 | public function getAdditionalFilters() |
|
390 | |||
391 | /** |
||
392 | * Performs a search and returns a SearchResultSet. |
||
393 | 29 | * |
|
394 | * @param SearchRequest $searchRequest |
||
395 | 29 | * @return SearchResultSet |
|
396 | 29 | */ |
|
397 | 29 | public function search(SearchRequest $searchRequest) |
|
462 | 43 | ||
463 | 2 | /** |
|
464 | * @param SearchResultSet $searchResultSet |
||
465 | * @return SearchResultSet |
||
466 | 43 | */ |
|
467 | 43 | protected function getAutoCorrection(SearchResultSet $searchResultSet) |
|
488 | |||
489 | /** |
||
490 | 2 | * @param SearchResultSet $searchResultSet |
|
491 | * @return SearchResultSet |
||
492 | */ |
||
493 | 2 | protected function peformAutoCorrection(SearchResultSet $searchResultSet) |
|
522 | 38 | ||
523 | 38 | /** |
|
524 | 38 | * Allows to modify a query before eventually handing it over to Solr. |
|
525 | 38 | * |
|
526 | 38 | * @param Query $query The current query before it's being handed over to Solr. |
|
527 | * @param SearchRequest $searchRequest The searchRequest, relevant in the current context |
||
528 | 38 | * @param Search $search The search, relevant in the current context |
|
529 | * @throws \UnexpectedValueException |
||
530 | 38 | * @return Query The modified query that is actually going to be given to Solr. |
|
531 | 4 | */ |
|
532 | protected function modifyQuery(Query $query, SearchRequest $searchRequest, Search $search) |
||
560 | |||
561 | 38 | /** |
|
562 | * Retrieves a single document from solr by document id. |
||
563 | 38 | * |
|
564 | * @param string $documentId |
||
565 | * @return SearchResult |
||
566 | */ |
||
567 | public function getDocumentById($documentId) |
||
579 | 1 | ||
580 | /** |
||
581 | * This method is used to call the registered hooks during the search execution. |
||
582 | * |
||
583 | 1 | * @param string $eventName |
|
584 | * @param SearchResultSet $resultSet |
||
585 | * @return SearchResultSet |
||
586 | */ |
||
587 | 1 | private function handleSearchHook($eventName, SearchResultSet $resultSet) |
|
602 | 1 | ||
603 | /** |
||
604 | 1 | * @return SearchResultSet |
|
605 | 1 | */ |
|
606 | public function getLastResultSet() |
||
610 | 1 | ||
611 | 1 | /** |
|
612 | 1 | * This method returns true when the last search was executed with an empty query |
|
613 | 1 | * string or whitespaces only. When no search was triggered it will return false. |
|
614 | 1 | * |
|
615 | 1 | * @return bool |
|
616 | 1 | */ |
|
617 | public function getLastSearchWasExecutedWithEmptyQueryString() |
||
626 | |||
627 | /** |
||
628 | * @return bool |
||
629 | */ |
||
630 | protected function getInitialSearchIsConfigured() |
||
634 | |||
635 | 38 | /** |
|
636 | * @return mixed |
||
637 | */ |
||
638 | 38 | protected function getRegisteredSearchComponents() |
|
642 | 31 | ||
643 | 31 | /** |
|
644 | * This method is used to reference the SearchResult object from the response in the SearchResultSet object. |
||
645 | * |
||
646 | * @param \Apache_Solr_Response $response |
||
647 | 31 | * @param SearchResultSet $resultSet |
|
648 | 31 | */ |
|
649 | protected function addSearchResultsToResultSet($response, $resultSet) |
||
659 | |||
660 | /** |
||
661 | 38 | * @param string $rawQuery |
|
662 | * @return Query|object |
||
663 | */ |
||
664 | protected function getQueryInstance($rawQuery) |
||
669 | |||
670 | } |
||
671 |
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: