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 | public function __construct(TypoScriptConfiguration $configuration, Search $search, SolrLogManager $solrLogManager = null, SearchResultBuilder $resultBuilder = null, QueryBuilder $queryBuilder = null) |
||
105 | 47 | { |
|
106 | $this->search = $search; |
||
107 | 47 | $this->typoScriptConfiguration = $configuration; |
|
108 | 47 | $this->logger = is_null($solrLogManager) ? GeneralUtility::makeInstance(SolrLogManager::class, __CLASS__) : $solrLogManager; |
|
109 | 47 | $this->searchResultBuilder = is_null($resultBuilder) ? GeneralUtility::makeInstance(SearchResultBuilder::class) : $resultBuilder; |
|
110 | 47 | $this->queryBuilder = is_null($queryBuilder) ? GeneralUtility::makeInstance(QueryBuilder::class, $configuration, $solrLogManager) : $queryBuilder; |
|
111 | 47 | } |
|
112 | |||
113 | /** |
||
114 | * @param bool $useCache |
||
115 | * @return bool |
||
116 | */ |
||
117 | public function getIsSolrAvailable($useCache = true) |
||
122 | |||
123 | /** |
||
124 | * @return bool |
||
125 | */ |
||
126 | 30 | public function getHasSearched() |
|
130 | |||
131 | /** |
||
132 | * Retrieves the used search instance. |
||
133 | * |
||
134 | * @return Search |
||
135 | */ |
||
136 | 2 | public function getSearch() |
|
140 | |||
141 | /** |
||
142 | * @param Query $query |
||
143 | * @param SearchRequest $searchRequest |
||
144 | */ |
||
145 | protected function initializeRegisteredSearchComponents(Query $query, SearchRequest $searchRequest) |
||
164 | |||
165 | /** |
||
166 | 38 | * @return string |
|
167 | */ |
||
168 | 38 | protected function getResultSetClassName() |
|
173 | |||
174 | 38 | /** |
|
175 | 3 | * Performs a search and returns a SearchResultSet. |
|
176 | * |
||
177 | * @param SearchRequest $searchRequest |
||
178 | 38 | * @return SearchResultSet |
|
179 | 2 | */ |
|
180 | public function search(SearchRequest $searchRequest) |
||
245 | 35 | ||
246 | 35 | /** |
|
247 | 35 | * Retrieves the configuration filters from the TypoScript configuration, except the __pageSections filter. |
|
248 | * |
||
249 | * @return array |
||
250 | */ |
||
251 | public function getAdditionalFilters() |
||
255 | 40 | ||
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 | protected function doASearch($query, $offSet) |
||
272 | |||
273 | 2 | /** |
|
274 | * @param SearchResultSet $searchResultSet |
||
275 | * @return SearchResultSet |
||
276 | */ |
||
277 | 2 | protected function getAutoCorrection(SearchResultSet $searchResultSet) |
|
298 | |||
299 | 2 | /** |
|
300 | * @param SearchResultSet $searchResultSet |
||
301 | 2 | * @return SearchResultSet |
|
302 | */ |
||
303 | protected function peformAutoCorrection(SearchResultSet $searchResultSet) |
||
332 | 40 | ||
333 | 40 | /** |
|
334 | * Allows to modify a query before eventually handing it over to Solr. |
||
335 | 40 | * |
|
336 | * @param Query $query The current query before it's being handed over to Solr. |
||
337 | 40 | * @param SearchRequest $searchRequest The searchRequest, relevant in the current context |
|
338 | * @param Search $search The search, relevant in the current context |
||
339 | 2 | * @throws \UnexpectedValueException |
|
340 | * @return Query The modified query that is actually going to be given to Solr. |
||
341 | */ |
||
342 | 38 | protected function modifyQuery(Query $query, SearchRequest $searchRequest, Search $search) |
|
370 | 37 | ||
371 | 37 | /** |
|
372 | 37 | * Retrieves a single document from solr by document id. |
|
373 | 37 | * |
|
374 | 37 | * @param string $documentId |
|
375 | * @return SearchResult |
||
376 | */ |
||
377 | 37 | public function getDocumentById($documentId) |
|
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 | private function handleSearchHook($eventName, SearchResultSet $resultSet) |
||
411 | |||
412 | 1 | /** |
|
413 | 1 | * @return SearchResultSet |
|
414 | 1 | */ |
|
415 | 1 | public function getLastResultSet() |
|
419 | 1 | ||
420 | 1 | /** |
|
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 | 37 | * @return bool |
|
425 | */ |
||
426 | public function getLastSearchWasExecutedWithEmptyQueryString() |
||
435 | 37 | ||
436 | /** |
||
437 | * @return bool |
||
438 | 37 | */ |
|
439 | 36 | protected function getInitialSearchIsConfigured() |
|
443 | 1 | ||
444 | 1 | /** |
|
445 | * @return mixed |
||
446 | */ |
||
447 | protected function getRegisteredSearchComponents() |
||
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: