Complex classes like Search 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 Search, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
40 | class Search |
||
41 | { |
||
42 | |||
43 | /** |
||
44 | * An instance of the Solr service |
||
45 | * |
||
46 | * @var SolrService |
||
47 | */ |
||
48 | protected $solr = null; |
||
49 | |||
50 | /** |
||
51 | * The search query |
||
52 | * |
||
53 | * @var Query |
||
54 | */ |
||
55 | protected $query = null; |
||
56 | |||
57 | /** |
||
58 | * The search response |
||
59 | * |
||
60 | * @var \Apache_Solr_Response |
||
61 | */ |
||
62 | protected $response = null; |
||
63 | |||
64 | /** |
||
65 | * Flag for marking a search |
||
66 | * |
||
67 | * @var bool |
||
68 | */ |
||
69 | protected $hasSearched = false; |
||
70 | |||
71 | /** |
||
72 | * @var TypoScriptConfiguration |
||
73 | */ |
||
74 | protected $configuration; |
||
75 | |||
76 | // TODO Override __clone to reset $response and $hasSearched |
||
77 | |||
78 | /** |
||
79 | * @var \ApacheSolrForTypo3\Solr\System\Logging\SolrLogManager |
||
80 | */ |
||
81 | protected $logger = null; |
||
82 | |||
83 | /** |
||
84 | * Constructor |
||
85 | * |
||
86 | * @param SolrService $solrConnection The Solr connection to use for searching |
||
87 | 43 | */ |
|
88 | public function __construct(SolrService $solrConnection = null) |
||
102 | |||
103 | /** |
||
104 | * Gets the Solr connection used by this search. |
||
105 | * |
||
106 | * @return SolrService Solr connection |
||
107 | */ |
||
108 | public function getSolrConnection() |
||
112 | |||
113 | /** |
||
114 | * Sets the Solr connection used by this search. |
||
115 | * |
||
116 | * Since ApacheSolrForTypo3\Solr\Search is a \TYPO3\CMS\Core\SingletonInterface, this is needed to |
||
117 | * be able to switch between multiple cores/connections during |
||
118 | * one request |
||
119 | * |
||
120 | * @param SolrService $solrConnection |
||
121 | */ |
||
122 | public function setSolrConnection(SolrService $solrConnection) |
||
126 | |||
127 | /** |
||
128 | * Executes a query against a Solr server. |
||
129 | * |
||
130 | * 1) Gets the query string |
||
131 | * 2) Conducts the actual search |
||
132 | * 3) Checks debug settings |
||
133 | * |
||
134 | * @param Query $query The query with keywords, filters, and so on. |
||
135 | * @param int $offset Result offset for pagination. |
||
136 | * @param int $limit Maximum number of results to return. If set to NULL, this value is taken from the query object. |
||
137 | * @return \Apache_Solr_Response Solr response |
||
138 | 37 | */ |
|
139 | public function search(Query $query, $offset = 0, $limit = 10) |
||
189 | |||
190 | /** |
||
191 | * Sends a ping to the solr server to see whether it is available. |
||
192 | * |
||
193 | * @param bool $useCache Set to true if the cache should be used. |
||
194 | * @return bool Returns TRUE on successful ping. |
||
195 | * @throws \Exception Throws an exception in case ping was not successful. |
||
196 | */ |
||
197 | public function ping($useCache = true) |
||
221 | |||
222 | /** |
||
223 | * checks whether a search has been executed |
||
224 | * |
||
225 | * @return bool TRUE if there was a search, FALSE otherwise (if the user just visited the search page f.e.) |
||
226 | 30 | */ |
|
227 | public function hasSearched() |
||
231 | |||
232 | /** |
||
233 | * Gets the query object. |
||
234 | * |
||
235 | * @return Query Query |
||
236 | 27 | */ |
|
237 | public function getQuery() |
||
241 | |||
242 | /** |
||
243 | * Gets the Solr response |
||
244 | * |
||
245 | * @return \Apache_Solr_Response |
||
246 | 28 | */ |
|
247 | public function getResponse() |
||
251 | |||
252 | public function getRawResponse() |
||
256 | 22 | ||
257 | public function getResponseHeader() |
||
261 | 28 | ||
262 | public function getResponseBody() |
||
266 | |||
267 | /** |
||
268 | * Returns all results documents raw. Use with caution! |
||
269 | * |
||
270 | * @deprecated Since 8.0.0 will be removed in 9.0.0. Use $resultSet->getSearchResults() this will be initialized by the parser depending on the settings |
||
271 | 27 | * @return \Apache_Solr_Document[] |
|
272 | */ |
||
273 | 27 | public function getResultDocumentsRaw() |
|
278 | |||
279 | /** |
||
280 | * Returns all result documents but applies htmlspecialchars() on all fields retrieved |
||
281 | * from solr except the configured fields in plugin.tx_solr.search.trustedFields |
||
282 | 3 | * |
|
283 | * @deprecated Since 8.0.0 will be removed in 9.0.0. Use DocumentEscapeService or |
||
284 | 3 | * $resultSet->getSearchResults() this will be initialized by the parser depending on the settings. |
|
285 | * @return \Apache_Solr_Document[] |
||
286 | */ |
||
287 | public function getResultDocumentsEscaped() |
||
294 | 3 | ||
295 | /** |
||
296 | 3 | * Gets the time Solr took to execute the query and return the result. |
|
297 | * |
||
298 | 3 | * @return int Query time in milliseconds |
|
299 | 3 | */ |
|
300 | public function getQueryTime() |
||
304 | 3 | ||
305 | /** |
||
306 | * Gets the number of results per page. |
||
307 | 3 | * |
|
308 | * @return int Number of results per page |
||
309 | */ |
||
310 | 3 | public function getResultsPerPage() |
|
314 | |||
315 | /** |
||
316 | * Gets all facets with their fields, options, and counts. |
||
317 | * |
||
318 | * @return array |
||
319 | */ |
||
320 | public function getFacetCounts() |
||
351 | |||
352 | public function getFacetFieldOptions($facetField) |
||
363 | |||
364 | public function getFacetQueryOptions($facetField) |
||
385 | |||
386 | public function getFacetRangeOptions($rangeFacetField) |
||
390 | |||
391 | public function getNumberOfResults() |
||
395 | |||
396 | /** |
||
397 | * Gets the result offset. |
||
398 | * |
||
399 | * @return int Result offset |
||
400 | */ |
||
401 | public function getResultOffset() |
||
405 | |||
406 | public function getMaximumResultScore() |
||
410 | |||
411 | public function getDebugResponse() |
||
415 | |||
416 | public function getHighlightedContent() |
||
426 | |||
427 | public function getSpellcheckingSuggestions() |
||
444 | } |
||
445 |