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 |
||
46 | class SearchResultSetService |
||
47 | { |
||
48 | /** |
||
49 | * Additional filters, which will be added to the query, as well as to |
||
50 | * suggest queries. |
||
51 | * |
||
52 | * @var array |
||
53 | */ |
||
54 | protected $additionalFilters = []; |
||
55 | |||
56 | /** |
||
57 | * Track, if the number of results per page has been changed by the current request |
||
58 | * |
||
59 | * @var bool |
||
60 | */ |
||
61 | protected $resultsPerPageChanged = false; |
||
62 | |||
63 | /** |
||
64 | * @var \ApacheSolrForTypo3\Solr\Search |
||
65 | */ |
||
66 | protected $search; |
||
67 | |||
68 | /** |
||
69 | * @var SearchResultSet |
||
70 | */ |
||
71 | protected $lastResultSet = null; |
||
72 | |||
73 | /** |
||
74 | * @var bool |
||
75 | */ |
||
76 | protected $useQueryAwareComponents = true; |
||
77 | |||
78 | /** |
||
79 | * @var |
||
80 | */ |
||
81 | protected $isSolrAvailable = false; |
||
82 | |||
83 | /** |
||
84 | * @var TypoScriptConfiguration |
||
85 | */ |
||
86 | protected $typoScriptConfiguration; |
||
87 | |||
88 | /** |
||
89 | * @var \ApacheSolrForTypo3\Solr\System\Logging\SolrLogManager; |
||
90 | */ |
||
91 | protected $logger = null; |
||
92 | |||
93 | /** |
||
94 | * @param TypoScriptConfiguration $configuration |
||
95 | * @param Search $search |
||
96 | */ |
||
97 | 40 | public function __construct(TypoScriptConfiguration $configuration, Search $search) |
|
103 | |||
104 | /** |
||
105 | * @param bool $useCache |
||
106 | * @return bool |
||
107 | */ |
||
108 | 24 | public function getIsSolrAvailable($useCache = true) |
|
113 | |||
114 | /** |
||
115 | * @return bool |
||
116 | */ |
||
117 | 24 | public function getHasSearched() |
|
121 | |||
122 | /** |
||
123 | * Retrieves the used search instance. |
||
124 | * |
||
125 | * @return Search |
||
126 | */ |
||
127 | 2 | public function getSearch() |
|
131 | |||
132 | /** |
||
133 | * @param bool $useQueryAwareComponents |
||
134 | */ |
||
135 | public function setUseQueryAwareComponents($useQueryAwareComponents) |
||
139 | |||
140 | /** |
||
141 | * @return bool |
||
142 | */ |
||
143 | public function getUseQueryAwareComponents() |
||
147 | |||
148 | /** |
||
149 | * Initializes the Query object and SearchComponents and returns |
||
150 | * the initialized query object, when a search should be executed. |
||
151 | * |
||
152 | * @param string $rawQuery |
||
153 | * @param int $resultsPerPage |
||
154 | * @return Query |
||
155 | */ |
||
156 | 32 | protected function getPreparedQuery($rawQuery, $resultsPerPage) |
|
193 | |||
194 | /** |
||
195 | * @param Query $query |
||
196 | */ |
||
197 | 32 | protected function initializeRegisteredSearchComponents(Query $query) |
|
212 | |||
213 | /** |
||
214 | * Returns the number of results per Page. |
||
215 | * |
||
216 | * Also influences how many result documents are returned by the Solr |
||
217 | * server as the return value is used in the Solr "rows" GET parameter. |
||
218 | * |
||
219 | * @param string $rawQuery |
||
220 | * @param int|null $requestedPerPage |
||
221 | * @return int number of results to show per page |
||
222 | */ |
||
223 | 32 | protected function getNumberOfResultsPerPage($rawQuery, $requestedPerPage = null) |
|
248 | |||
249 | /** |
||
250 | * Provides a hook for other classes to process the search's response. |
||
251 | * |
||
252 | * @param string $rawQuery |
||
253 | * @param Query $query The query that has been searched for. |
||
254 | * @param \Apache_Solr_Response $response The search's response. |
||
255 | */ |
||
256 | 34 | protected function processResponse($rawQuery, Query $query, \Apache_Solr_Response $response) |
|
277 | |||
278 | /** |
||
279 | * This method is used to add documents to the expanded documents of the SearchResult |
||
280 | * when collapsing is configured. |
||
281 | * |
||
282 | * @param \Apache_Solr_Response $response |
||
283 | */ |
||
284 | 34 | protected function addExpandedDocumentsFromVariants(\Apache_Solr_Response $response) |
|
285 | { |
||
286 | 34 | if (!is_array($response->response->docs)) { |
|
287 | 5 | return; |
|
288 | } |
||
289 | |||
290 | 29 | if (!$this->typoScriptConfiguration->getSearchVariants()) { |
|
291 | 26 | return; |
|
292 | } |
||
293 | |||
294 | 3 | $variantsField = $this->typoScriptConfiguration->getSearchVariantsField(); |
|
295 | 3 | foreach ($response->response->docs as $key => $resultDocument) { |
|
296 | /** @var $resultDocument SearchResult */ |
||
297 | 2 | $variantField = $resultDocument->getField($variantsField); |
|
298 | 2 | $variantId = isset($variantField['value']) ? $variantField['value'] : null; |
|
299 | |||
300 | // when there is no value in the collapsing field, we can return |
||
301 | 2 | if ($variantId === null) { |
|
302 | continue; |
||
303 | } |
||
304 | |||
305 | 2 | $variantAccessKey = strtolower($variantId); |
|
306 | 2 | if (!isset($response->{'expanded'}) || !isset($response->{'expanded'}->{$variantAccessKey})) { |
|
307 | continue; |
||
308 | } |
||
309 | |||
310 | 2 | foreach ($response->{'expanded'}->{$variantAccessKey}->{'docs'} as $variantDocumentArray) { |
|
311 | 2 | $variantDocument = new \Apache_Solr_Document(); |
|
312 | 2 | foreach (get_object_vars($variantDocumentArray) as $propertyName => $propertyValue) { |
|
313 | 2 | $variantDocument->{$propertyName} = $propertyValue; |
|
314 | } |
||
315 | 2 | $variantSearchResult = $this->wrapApacheSolrDocumentInResultObject($variantDocument); |
|
316 | 2 | $variantSearchResult->setIsVariant(true); |
|
317 | 2 | $variantSearchResult->setVariantParent($resultDocument); |
|
318 | |||
319 | 2 | $resultDocument->addVariant($variantSearchResult); |
|
320 | } |
||
321 | } |
||
322 | 3 | } |
|
323 | |||
324 | /** |
||
325 | * Wrap all results document it a custom EXT:solr SearchResult object. |
||
326 | * |
||
327 | * Can be overwritten: |
||
328 | * |
||
329 | * $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['solr']['searchResultClassName '] = '' |
||
330 | * |
||
331 | * to use a custom result object. |
||
332 | * |
||
333 | * @param \Apache_Solr_Response $response |
||
334 | * @throws \Apache_Solr_ParserException |
||
335 | */ |
||
336 | 34 | protected function wrapResultDocumentInResultObject(\Apache_Solr_Response $response) |
|
337 | { |
||
338 | 34 | $documents = $response->response->docs; |
|
339 | |||
340 | 34 | if (!is_array($documents)) { |
|
341 | 5 | return; |
|
342 | } |
||
343 | |||
344 | 29 | foreach ($documents as $key => $originalDocument) { |
|
345 | 26 | $result = $this->wrapApacheSolrDocumentInResultObject($originalDocument); |
|
346 | 26 | $documents[$key] = $result; |
|
347 | } |
||
348 | |||
349 | 29 | $response->response->docs = $documents; |
|
350 | 29 | } |
|
351 | |||
352 | /** |
||
353 | * This method is used to wrap the \Apache_Solr_Document instance in an instance of the configured SearchResult |
||
354 | * class. |
||
355 | * |
||
356 | * @param \Apache_Solr_Document $originalDocument |
||
357 | * @throws \InvalidArgumentException |
||
358 | * @return SearchResult |
||
359 | */ |
||
360 | 26 | protected function wrapApacheSolrDocumentInResultObject(\Apache_Solr_Document $originalDocument) |
|
370 | |||
371 | /** |
||
372 | * @return string |
||
373 | */ |
||
374 | 26 | protected function getResultClassName() |
|
379 | |||
380 | /** |
||
381 | * @return string |
||
382 | */ |
||
383 | 34 | protected function getResultSetClassName() |
|
388 | |||
389 | /** |
||
390 | * Checks it the results should be hidden in the response. |
||
391 | * |
||
392 | * @param string $rawQuery |
||
393 | * @return bool |
||
394 | */ |
||
395 | 34 | protected function shouldHideResultsFromInitialSearch($rawQuery) |
|
399 | |||
400 | /** |
||
401 | * Initializes additional filters configured through TypoScript and |
||
402 | * Flexforms for use in regular queries and suggest queries. |
||
403 | * |
||
404 | * @param Query $query |
||
405 | * @return void |
||
406 | */ |
||
407 | 32 | protected function applyPageSectionsRootLineFilter(Query $query) |
|
420 | |||
421 | /** |
||
422 | * Retrieves the configuration filters from the TypoScript configuration, except the __pageSections filter. |
||
423 | * |
||
424 | * @return array |
||
425 | */ |
||
426 | 37 | public function getAdditionalFilters() |
|
462 | |||
463 | /** |
||
464 | * Performs a search and returns a SearchResultSet. |
||
465 | * |
||
466 | * @param SearchRequest $searchRequest |
||
467 | * @return SearchResultSet |
||
468 | */ |
||
469 | 34 | public function search(SearchRequest $searchRequest) |
|
520 | |||
521 | /** |
||
522 | * Retrieves a single document from solr by document id. |
||
523 | * |
||
524 | * @param string $documentId |
||
525 | * @return SearchResult |
||
526 | */ |
||
527 | 2 | public function getDocumentById($documentId) |
|
539 | |||
540 | /** |
||
541 | * This method is used to call the registered hooks during the search execution. |
||
542 | * |
||
543 | * @param string $eventName |
||
544 | * @param SearchResultSet $resultSet |
||
545 | * @return SearchResultSet |
||
546 | */ |
||
547 | 34 | private function handleSearchHook($eventName, SearchResultSet $resultSet) |
|
562 | |||
563 | /** |
||
564 | * @return SearchResultSet |
||
565 | */ |
||
566 | 23 | public function getLastResultSet() |
|
570 | |||
571 | /** |
||
572 | * This method returns true when the last search was executed with an empty query |
||
573 | * string or whitespaces only. When no search was triggered it will return false. |
||
574 | * |
||
575 | * @return bool |
||
576 | */ |
||
577 | public function getLastSearchWasExecutedWithEmptyQueryString() |
||
586 | |||
587 | /** |
||
588 | * @param int $requestedPerPage |
||
589 | */ |
||
590 | 2 | protected function setPerPageInSession($requestedPerPage) |
|
594 | |||
595 | /** |
||
596 | * @return mixed |
||
597 | */ |
||
598 | 25 | protected function getPerPageFromSession() |
|
602 | |||
603 | /** |
||
604 | * @return bool |
||
605 | */ |
||
606 | 2 | protected function getInitialSearchIsConfigured() |
|
610 | |||
611 | /** |
||
612 | * @return mixed |
||
613 | */ |
||
614 | 25 | protected function getRegisteredSearchComponents() |
|
618 | |||
619 | /** |
||
620 | * This method is used to reference the SearchResult object from the response in the SearchResultSet object. |
||
621 | * |
||
622 | * @param \Apache_Solr_Response $response |
||
623 | * @param SearchResultSet $resultSet |
||
624 | */ |
||
625 | 32 | protected function addSearchResultsToResultSet($response, $resultSet) |
|
635 | } |
||
636 |
An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.
If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.