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 |
||
52 | class SearchResultSetService |
||
53 | { |
||
54 | |||
55 | /** |
||
56 | * Track, if the number of results per page has been changed by the current request |
||
57 | * |
||
58 | * @var bool |
||
59 | */ |
||
60 | protected $resultsPerPageChanged = false; |
||
61 | |||
62 | /** |
||
63 | * @var Search |
||
64 | */ |
||
65 | protected $search; |
||
66 | |||
67 | /** |
||
68 | * @var SearchResultSet |
||
69 | */ |
||
70 | protected $lastResultSet = null; |
||
71 | |||
72 | /** |
||
73 | * @var boolean |
||
74 | */ |
||
75 | protected $isSolrAvailable = false; |
||
76 | |||
77 | /** |
||
78 | * @var TypoScriptConfiguration |
||
79 | */ |
||
80 | protected $typoScriptConfiguration; |
||
81 | |||
82 | /** |
||
83 | * @var SolrLogManager; |
||
84 | */ |
||
85 | protected $logger = null; |
||
86 | |||
87 | /** |
||
88 | * @var SearchResultBuilder |
||
89 | */ |
||
90 | protected $searchResultBuilder; |
||
91 | |||
92 | /** |
||
93 | * @var QueryBuilder |
||
94 | */ |
||
95 | protected $queryBuilder; |
||
96 | |||
97 | /** |
||
98 | * @param TypoScriptConfiguration $configuration |
||
99 | * @param Search $search |
||
100 | * @param SolrLogManager $solrLogManager |
||
101 | * @param SearchResultBuilder $resultBuilder |
||
102 | * @param QueryBuilder $queryBuilder |
||
103 | */ |
||
104 | 48 | public function __construct(TypoScriptConfiguration $configuration, Search $search, SolrLogManager $solrLogManager = null, SearchResultBuilder $resultBuilder = null, QueryBuilder $queryBuilder = null) |
|
105 | { |
||
106 | 48 | $this->search = $search; |
|
107 | 48 | $this->typoScriptConfiguration = $configuration; |
|
108 | 48 | $this->logger = is_null($solrLogManager) ? GeneralUtility::makeInstance(SolrLogManager::class, __CLASS__) : $solrLogManager; |
|
109 | 48 | $this->searchResultBuilder = is_null($resultBuilder) ? GeneralUtility::makeInstance(SearchResultBuilder::class) : $resultBuilder; |
|
110 | 48 | $this->queryBuilder = is_null($queryBuilder) ? GeneralUtility::makeInstance(QueryBuilder::class, $configuration, $solrLogManager) : $queryBuilder; |
|
111 | 48 | } |
|
112 | |||
113 | /** |
||
114 | * @param bool $useCache |
||
115 | * @return bool |
||
116 | */ |
||
117 | public function getIsSolrAvailable($useCache = true) |
||
118 | { |
||
119 | $this->isSolrAvailable = $this->search->ping($useCache); |
||
120 | return $this->isSolrAvailable; |
||
121 | } |
||
122 | |||
123 | /** |
||
124 | * @return bool |
||
125 | */ |
||
126 | 31 | public function getHasSearched() |
|
127 | { |
||
128 | 31 | return $this->search->hasSearched(); |
|
129 | } |
||
130 | |||
131 | /** |
||
132 | * Retrieves the used search instance. |
||
133 | * |
||
134 | * @return Search |
||
135 | */ |
||
136 | 2 | public function getSearch() |
|
137 | { |
||
138 | 2 | return $this->search; |
|
139 | } |
||
140 | |||
141 | /** |
||
142 | * @param Query $query |
||
143 | * @param SearchRequest $searchRequest |
||
144 | */ |
||
145 | 39 | protected function initializeRegisteredSearchComponents(Query $query, SearchRequest $searchRequest) |
|
146 | { |
||
147 | 39 | $searchComponents = $this->getRegisteredSearchComponents(); |
|
148 | |||
149 | 39 | foreach ($searchComponents as $searchComponent) { |
|
150 | /** @var Search\SearchComponent $searchComponent */ |
||
151 | 34 | $searchComponent->setSearchConfiguration($this->typoScriptConfiguration->getSearchConfiguration()); |
|
152 | |||
153 | 34 | if ($searchComponent instanceof QueryAware) { |
|
154 | 34 | $searchComponent->setQuery($query); |
|
155 | } |
||
156 | |||
157 | 34 | if ($searchComponent instanceof SearchRequestAware) { |
|
158 | 33 | $searchComponent->setSearchRequest($searchRequest); |
|
159 | } |
||
160 | |||
161 | 34 | $searchComponent->initializeSearchComponent(); |
|
162 | } |
||
163 | 39 | } |
|
164 | |||
165 | /** |
||
166 | * @return string |
||
167 | */ |
||
168 | 41 | protected function getResultSetClassName() |
|
169 | { |
||
170 | 41 | return isset($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['solr']['searchResultSetClassName ']) ? |
|
171 | 41 | $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['solr']['searchResultSetClassName '] : SearchResultSet::class; |
|
172 | } |
||
173 | |||
174 | /** |
||
175 | * Performs a search and returns a SearchResultSet. |
||
176 | * |
||
177 | * @param SearchRequest $searchRequest |
||
178 | * @return SearchResultSet |
||
179 | */ |
||
180 | 41 | public function search(SearchRequest $searchRequest) |
|
181 | { |
||
182 | /** @var $resultSet SearchResultSet */ |
||
183 | 41 | $resultSetClass = $this->getResultSetClassName(); |
|
184 | 41 | $resultSet = GeneralUtility::makeInstance($resultSetClass); |
|
185 | 41 | $resultSet->setUsedSearchRequest($searchRequest); |
|
186 | 41 | $this->lastResultSet = $resultSet; |
|
187 | |||
188 | 41 | $resultSet = $this->handleSearchHook('beforeSearch', $resultSet); |
|
189 | |||
190 | 41 | if ($searchRequest->getRawUserQueryIsNull() && !$this->getInitialSearchIsConfigured()) { |
|
191 | // when no rawQuery was passed or no initialSearch is configured, we pass an empty result set |
||
192 | 2 | return $resultSet; |
|
193 | } |
||
194 | |||
195 | 39 | if ($searchRequest->getRawUserQueryIsEmptyString() && !$this->typoScriptConfiguration->getSearchQueryAllowEmptyQuery()) { |
|
196 | // the user entered an empty query string "" or " " and empty querystring is not allowed |
||
197 | return $resultSet; |
||
198 | } |
||
199 | |||
200 | 39 | $rawQuery = $searchRequest->getRawUserQuery(); |
|
201 | 39 | $resultsPerPage = (int)$searchRequest->getResultsPerPage(); |
|
202 | 39 | $query = $this->queryBuilder->buildSearchQuery($rawQuery, $resultsPerPage); |
|
203 | 39 | $this->initializeRegisteredSearchComponents($query, $searchRequest); |
|
204 | 39 | $resultSet->setUsedQuery($query); |
|
205 | |||
206 | // the offset mulitplier is page - 1 but not less then zero |
||
207 | 39 | $offsetMultiplier = max(0, $searchRequest->getPage() - 1); |
|
208 | 39 | $offSet = $offsetMultiplier * $resultsPerPage; |
|
209 | |||
210 | // performing the actual search, sending the query to the Solr server |
||
211 | 39 | $query = $this->modifyQuery($query, $searchRequest, $this->search); |
|
212 | 39 | $response = $this->doASearch($query, $offSet); |
|
213 | |||
214 | 38 | if ($resultsPerPage === 0) { |
|
215 | // when resultPerPage was forced to 0 we also set the numFound to 0 to hide results, e.g. |
||
216 | // when results for the initial search should not be shown. |
||
217 | 2 | $response->response->numFound = 0; |
|
218 | } |
||
219 | |||
220 | 38 | $resultSet->setUsedSearch($this->search); |
|
221 | 38 | $resultSet->setResponse($response); |
|
222 | |||
223 | /** @var ResultParserRegistry $parserRegistry */ |
||
224 | 38 | $parserRegistry = GeneralUtility::makeInstance(ResultParserRegistry::class, $this->typoScriptConfiguration); |
|
225 | 38 | $useRawDocuments = (bool)$this->typoScriptConfiguration->getValueByPathOrDefaultValue('plugin.tx_solr.features.useRawDocuments', false); |
|
226 | 38 | $searchResults = $parserRegistry->getParser($resultSet)->parse($resultSet, $useRawDocuments); |
|
227 | 38 | $resultSet->setSearchResults($searchResults); |
|
228 | |||
229 | 38 | $resultSet->setUsedPage((int)$searchRequest->getPage()); |
|
230 | 38 | $resultSet->setUsedResultsPerPage($resultsPerPage); |
|
231 | 38 | $resultSet->setUsedAdditionalFilters($this->queryBuilder->getAdditionalFilters()); |
|
232 | |||
233 | /** @var $variantsProcessor VariantsProcessor */ |
||
234 | 38 | $variantsProcessor = GeneralUtility::makeInstance(VariantsProcessor::class, $this->typoScriptConfiguration, $this->searchResultBuilder); |
|
235 | 38 | $variantsProcessor->process($resultSet); |
|
236 | |||
237 | /** @var $searchResultReconstitutionProcessor ResultSetReconstitutionProcessor */ |
||
238 | 38 | $searchResultReconstitutionProcessor = GeneralUtility::makeInstance(ResultSetReconstitutionProcessor::class); |
|
239 | 38 | $searchResultReconstitutionProcessor->process($resultSet); |
|
240 | |||
241 | 38 | $resultSet = $this->getAutoCorrection($resultSet); |
|
242 | |||
243 | 38 | return $this->handleSearchHook('afterSearch', $resultSet); |
|
244 | } |
||
245 | |||
246 | /** |
||
247 | * Retrieves the configuration filters from the TypoScript configuration, except the __pageSections filter. |
||
248 | * |
||
249 | * @return array |
||
250 | */ |
||
251 | 33 | public function getAdditionalFilters() |
|
252 | { |
||
253 | 33 | return $this->queryBuilder->getAdditionalFilters(); |
|
254 | } |
||
255 | |||
256 | /** |
||
257 | * Executes the search and builds a fake response for a current bug in Apache Solr 6.3 |
||
258 | * |
||
259 | * @param Query $query |
||
260 | * @param int $offSet |
||
261 | * @return \Apache_Solr_Response |
||
262 | */ |
||
263 | 39 | protected function doASearch($query, $offSet) |
|
264 | { |
||
265 | 39 | $response = $this->search->search($query, $offSet, null); |
|
266 | 38 | if($response === null) { |
|
267 | throw new SolrIncompleteResponseException('The response retrieved from solr was incomplete', 1505989678); |
||
268 | } |
||
269 | |||
270 | 38 | return $response; |
|
271 | } |
||
272 | |||
273 | /** |
||
274 | * @param SearchResultSet $searchResultSet |
||
275 | * @return SearchResultSet |
||
276 | */ |
||
277 | 38 | protected function getAutoCorrection(SearchResultSet $searchResultSet) |
|
278 | { |
||
279 | // no secondary search configured |
||
280 | 38 | if (!$this->typoScriptConfiguration->getSearchSpellcheckingSearchUsingSpellCheckerSuggestion()) { |
|
281 | 37 | return $searchResultSet; |
|
282 | } |
||
283 | |||
284 | // more then zero results |
||
285 | 1 | if ($searchResultSet->getAllResultCount() > 0) { |
|
286 | 1 | return $searchResultSet; |
|
287 | } |
||
288 | |||
289 | // no corrections present |
||
290 | 1 | if (!$searchResultSet->getHasSpellCheckingSuggestions()) { |
|
291 | return $searchResultSet; |
||
292 | } |
||
293 | |||
294 | 1 | $searchResultSet = $this->peformAutoCorrection($searchResultSet); |
|
295 | |||
296 | 1 | return $searchResultSet; |
|
297 | } |
||
298 | |||
299 | /** |
||
300 | * @param SearchResultSet $searchResultSet |
||
301 | * @return SearchResultSet |
||
302 | */ |
||
303 | 1 | protected function peformAutoCorrection(SearchResultSet $searchResultSet) |
|
304 | { |
||
305 | 1 | $searchRequest = $searchResultSet->getUsedSearchRequest(); |
|
306 | 1 | $suggestions = $searchResultSet->getSpellCheckingSuggestions(); |
|
307 | |||
308 | 1 | $maximumRuns = $this->typoScriptConfiguration->getSearchSpellcheckingNumberOfSuggestionsToTry(1); |
|
309 | 1 | $runs = 0; |
|
310 | |||
311 | 1 | foreach ($suggestions as $suggestion) { |
|
312 | 1 | $runs++; |
|
313 | |||
314 | 1 | $correction = $suggestion->getSuggestion(); |
|
315 | 1 | $initialQuery = $searchRequest->getRawUserQuery(); |
|
316 | |||
317 | 1 | $searchRequest->setRawQueryString($correction); |
|
318 | 1 | $searchResultSet = $this->search($searchRequest); |
|
319 | 1 | if ($searchResultSet->getAllResultCount() > 0) { |
|
320 | 1 | $searchResultSet->setIsAutoCorrected(true); |
|
321 | 1 | $searchResultSet->setCorrectedQueryString($correction); |
|
322 | 1 | $searchResultSet->setInitialQueryString($initialQuery); |
|
323 | 1 | break; |
|
324 | } |
||
325 | |||
326 | if ($runs > $maximumRuns) { |
||
327 | break; |
||
328 | } |
||
329 | } |
||
330 | 1 | return $searchResultSet; |
|
331 | } |
||
332 | |||
333 | /** |
||
334 | * Allows to modify a query before eventually handing it over to Solr. |
||
335 | * |
||
336 | * @param Query $query The current query before it's being handed over to Solr. |
||
337 | * @param SearchRequest $searchRequest The searchRequest, relevant in the current context |
||
338 | * @param Search $search The search, relevant in the current context |
||
339 | * @throws \UnexpectedValueException |
||
340 | * @return Query The modified query that is actually going to be given to Solr. |
||
341 | */ |
||
342 | 39 | protected function modifyQuery(Query $query, SearchRequest $searchRequest, Search $search) |
|
343 | { |
||
344 | // hook to modify the search query |
||
345 | 39 | if (is_array($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['solr']['modifySearchQuery'])) { |
|
346 | 33 | foreach ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['solr']['modifySearchQuery'] as $classReference) { |
|
347 | 33 | $queryModifier = GeneralUtility::makeInstance($classReference); |
|
348 | |||
349 | 33 | if ($queryModifier instanceof Modifier) { |
|
350 | 33 | if ($queryModifier instanceof SearchAware) { |
|
351 | $queryModifier->setSearch($search); |
||
352 | } |
||
353 | |||
354 | 33 | if ($queryModifier instanceof SearchRequestAware) { |
|
355 | 33 | $queryModifier->setSearchRequest($searchRequest); |
|
356 | } |
||
357 | |||
358 | 33 | $query = $queryModifier->modifyQuery($query); |
|
359 | } else { |
||
360 | throw new \UnexpectedValueException( |
||
361 | get_class($queryModifier) . ' must implement interface ' . Modifier::class, |
||
362 | 33 | 1310387414 |
|
363 | ); |
||
364 | } |
||
365 | } |
||
366 | } |
||
367 | |||
368 | 39 | return $query; |
|
369 | } |
||
370 | |||
371 | /** |
||
372 | * Retrieves a single document from solr by document id. |
||
373 | * |
||
374 | * @param string $documentId |
||
375 | * @return SearchResult |
||
376 | */ |
||
377 | 3 | public function getDocumentById($documentId) |
|
378 | { |
||
379 | /* @var $query Query */ |
||
380 | 3 | $query = GeneralUtility::makeInstance(Query::class, $documentId); |
|
381 | 3 | $query->setQueryFields(QueryFields::fromString('id')); |
|
382 | 3 | $response = $this->search->search($query, 0, 1); |
|
383 | 2 | $parsedData = $response->getParsedData(); |
|
384 | 2 | $resultDocument = isset($parsedData->response->docs[0]) ? $parsedData->response->docs[0] : null; |
|
385 | |||
386 | 2 | return $this->searchResultBuilder->fromApacheSolrDocument($resultDocument); |
|
387 | } |
||
388 | |||
389 | /** |
||
390 | * This method is used to call the registered hooks during the search execution. |
||
391 | * |
||
392 | * @param string $eventName |
||
393 | * @param SearchResultSet $resultSet |
||
394 | * @return SearchResultSet |
||
395 | */ |
||
396 | 41 | private function handleSearchHook($eventName, SearchResultSet $resultSet) |
|
397 | { |
||
398 | 41 | if (!is_array($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['solr'][$eventName])) { |
|
399 | 41 | return $resultSet; |
|
400 | } |
||
401 | |||
402 | 29 | foreach ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['solr'][$eventName] as $classReference) { |
|
403 | 29 | $afterSearchProcessor = GeneralUtility::makeInstance($classReference); |
|
404 | 29 | if ($afterSearchProcessor instanceof SearchResultSetProcessor) { |
|
405 | 29 | $afterSearchProcessor->process($resultSet); |
|
406 | } |
||
407 | } |
||
408 | |||
409 | 29 | return $resultSet; |
|
410 | } |
||
411 | |||
412 | /** |
||
413 | * @return SearchResultSet |
||
414 | */ |
||
415 | 30 | public function getLastResultSet() |
|
419 | |||
420 | /** |
||
421 | * This method returns true when the last search was executed with an empty query |
||
422 | * string or whitespaces only. When no search was triggered it will return false. |
||
423 | * |
||
424 | * @return bool |
||
425 | */ |
||
426 | public function getLastSearchWasExecutedWithEmptyQueryString() |
||
427 | { |
||
428 | $wasEmptyQueryString = false; |
||
429 | if ($this->lastResultSet != null) { |
||
430 | $wasEmptyQueryString = $this->lastResultSet->getUsedSearchRequest()->getRawUserQueryIsEmptyString(); |
||
431 | } |
||
432 | |||
433 | return $wasEmptyQueryString; |
||
434 | } |
||
435 | |||
436 | /** |
||
437 | * @return bool |
||
438 | */ |
||
439 | 6 | protected function getInitialSearchIsConfigured() |
|
443 | |||
444 | /** |
||
445 | * @return mixed |
||
446 | */ |
||
447 | 33 | protected function getRegisteredSearchComponents() |
|
448 | { |
||
449 | 33 | return GeneralUtility::makeInstance(SearchComponentManager::class)->getSearchComponents(); |
|
450 | } |
||
451 | } |
||
452 |
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: