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 |
||
38 | class Search |
||
39 | { |
||
40 | |||
41 | /** |
||
42 | * An instance of the Solr service |
||
43 | * |
||
44 | * @var SolrService |
||
45 | */ |
||
46 | protected $solr = null; |
||
47 | |||
48 | /** |
||
49 | * The search query |
||
50 | * |
||
51 | * @var Query |
||
52 | */ |
||
53 | protected $query = null; |
||
54 | |||
55 | /** |
||
56 | * The search response |
||
57 | * |
||
58 | * @var \Apache_Solr_Response |
||
59 | */ |
||
60 | protected $response = null; |
||
61 | |||
62 | /** |
||
63 | * Flag for marking a search |
||
64 | * |
||
65 | * @var bool |
||
66 | */ |
||
67 | protected $hasSearched = false; |
||
68 | |||
69 | /** |
||
70 | * @var TypoScriptConfiguration |
||
71 | */ |
||
72 | protected $configuration; |
||
73 | |||
74 | // TODO Override __clone to reset $response and $hasSearched |
||
75 | |||
76 | /** |
||
77 | * @var \ApacheSolrForTypo3\Solr\System\Logging\SolrLogManager |
||
78 | */ |
||
79 | protected $logger = null; |
||
80 | |||
81 | /** |
||
82 | * Constructor |
||
83 | * |
||
84 | * @param SolrService $solrConnection The Solr connection to use for searching |
||
85 | */ |
||
86 | public function __construct(SolrService $solrConnection = null) |
||
100 | |||
101 | 36 | /** |
|
102 | 36 | * Gets the Solr connection used by this search. |
|
103 | * |
||
104 | * @return SolrService Solr connection |
||
105 | */ |
||
106 | public function getSolrConnection() |
||
110 | |||
111 | /** |
||
112 | * Sets the Solr connection used by this search. |
||
113 | * |
||
114 | * Since ApacheSolrForTypo3\Solr\Search is a \TYPO3\CMS\Core\SingletonInterface, this is needed to |
||
115 | * be able to switch between multiple cores/connections during |
||
116 | * one request |
||
117 | * |
||
118 | * @param SolrService $solrConnection |
||
119 | */ |
||
120 | public function setSolrConnection(SolrService $solrConnection) |
||
124 | |||
125 | /** |
||
126 | * Executes a query against a Solr server. |
||
127 | * |
||
128 | * 1) Gets the query string |
||
129 | * 2) Conducts the actual search |
||
130 | * 3) Checks debug settings |
||
131 | * |
||
132 | * @param Query $query The query with keywords, filters, and so on. |
||
133 | * @param int $offset Result offset for pagination. |
||
134 | * @param int $limit Maximum number of results to return. If set to NULL, this value is taken from the query object. |
||
135 | * @return \Apache_Solr_Response Solr response |
||
136 | */ |
||
137 | public function search(Query $query, $offset = 0, $limit = 10) |
||
187 | 30 | ||
188 | 30 | /** |
|
189 | * Sends a ping to the solr server to see whether it is available. |
||
190 | 30 | * |
|
191 | * @param bool $useCache Set to true if the cache should be used. |
||
192 | * @return bool Returns TRUE on successful ping. |
||
193 | * @throws \Exception Throws an exception in case ping was not successful. |
||
194 | */ |
||
195 | public function ping($useCache = true) |
||
219 | |||
220 | /** |
||
221 | 30 | * checks whether a search has been executed |
|
222 | * |
||
223 | * @return bool TRUE if there was a search, FALSE otherwise (if the user just visited the search page f.e.) |
||
224 | */ |
||
225 | public function hasSearched() |
||
229 | |||
230 | /** |
||
231 | * Gets the query object. |
||
232 | 30 | * |
|
233 | * @return Query Query |
||
234 | */ |
||
235 | 30 | public function getQuery() |
|
239 | |||
240 | /** |
||
241 | * Gets the Solr response |
||
242 | * |
||
243 | * @return \Apache_Solr_Response |
||
244 | */ |
||
245 | public function getResponse() |
||
249 | |||
250 | public function getRawResponse() |
||
254 | |||
255 | public function getResponseHeader() |
||
259 | |||
260 | public function getResponseBody() |
||
264 | |||
265 | /** |
||
266 | * Returns all results documents raw. Use with caution! |
||
267 | 25 | * |
|
268 | * @return \Apache_Solr_Document[] |
||
269 | 25 | */ |
|
270 | public function getResultDocumentsRaw() |
||
274 | |||
275 | /** |
||
276 | 25 | * Returns all result documents but applies htmlspecialchars() on all fields retrieved |
|
277 | * from solr except the configured fields in plugin.tx_solr.search.trustedFields |
||
278 | * |
||
279 | * @return \Apache_Solr_Document[] |
||
280 | */ |
||
281 | public function getResultDocumentsEscaped() |
||
285 | |||
286 | /** |
||
287 | * This method is used to apply htmlspecialchars on all document fields that |
||
288 | * are not configured to be secure. Secure mean that we know where the content is coming from. |
||
289 | 25 | * |
|
290 | * @param array $documents |
||
291 | * @return \Apache_Solr_Document[] |
||
292 | */ |
||
293 | protected function applyHtmlSpecialCharsOnAllFields(array $documents) |
||
314 | |||
315 | /** |
||
316 | * Applies htmlspecialchars on all items of an array of a single value. |
||
317 | 23 | * |
|
318 | * @param $fieldValue |
||
319 | 23 | * @return array|string |
|
320 | */ |
||
321 | protected function applyHtmlSpecialCharsOnSingleFieldValue($fieldValue) |
||
333 | |||
334 | 23 | /** |
|
335 | * Gets the time Solr took to execute the query and return the result. |
||
336 | * |
||
337 | * @return int Query time in milliseconds |
||
338 | */ |
||
339 | public function getQueryTime() |
||
343 | |||
344 | 22 | /** |
|
345 | * Gets the number of results per page. |
||
346 | * |
||
347 | * @return int Number of results per page |
||
348 | */ |
||
349 | public function getResultsPerPage() |
||
353 | 3 | ||
354 | /** |
||
355 | 3 | * Gets all facets with their fields, options, and counts. |
|
356 | * |
||
357 | * @return array |
||
358 | */ |
||
359 | public function getFacetCounts() |
||
390 | |||
391 | public function getFacetFieldOptions($facetField) |
||
402 | |||
403 | 3 | public function getFacetQueryOptions($facetField) |
|
424 | |||
425 | public function getFacetRangeOptions($rangeFacetField) |
||
429 | |||
430 | public function getNumberOfResults() |
||
434 | |||
435 | /** |
||
436 | * Gets the result offset. |
||
437 | * |
||
438 | * @return int Result offset |
||
439 | */ |
||
440 | public function getResultOffset() |
||
444 | |||
445 | public function getMaximumResultScore() |
||
449 | |||
450 | public function getDebugResponse() |
||
454 | |||
455 | public function getHighlightedContent() |
||
465 | |||
466 | public function getSpellcheckingSuggestions() |
||
483 | } |
||
484 |