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 |
||
| 41 | class Search implements SingletonInterface |
||
| 42 | { |
||
| 43 | |||
| 44 | /** |
||
| 45 | * An instance of the Solr service |
||
| 46 | * |
||
| 47 | * @var SolrService |
||
| 48 | */ |
||
| 49 | protected $solr = null; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * The search query |
||
| 53 | * |
||
| 54 | * @var Query |
||
| 55 | */ |
||
| 56 | protected $query = null; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * The search response |
||
| 60 | * |
||
| 61 | * @var \Apache_Solr_Response |
||
| 62 | */ |
||
| 63 | protected $response = null; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Flag for marking a search |
||
| 67 | * |
||
| 68 | * @var bool |
||
| 69 | */ |
||
| 70 | protected $hasSearched = false; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @var TypoScriptConfiguration |
||
| 74 | */ |
||
| 75 | protected $configuration; |
||
| 76 | |||
| 77 | // TODO Override __clone to reset $response and $hasSearched |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Constructor |
||
| 81 | * |
||
| 82 | * @param SolrService $solrConnection The Solr connection to use for searching |
||
| 83 | */ |
||
| 84 | 48 | public function __construct(SolrService $solrConnection = null) |
|
| 85 | { |
||
| 86 | 48 | $this->solr = $solrConnection; |
|
| 87 | |||
| 88 | 48 | if (is_null($solrConnection)) { |
|
| 89 | /** @var $connectionManager ConnectionManager */ |
||
| 90 | 1 | $connectionManager = GeneralUtility::makeInstance(ConnectionManager::class); |
|
| 91 | 1 | $this->solr = $connectionManager->getConnectionByPageId($GLOBALS['TSFE']->id, $GLOBALS['TSFE']->sys_language_uid); |
|
| 92 | } |
||
| 93 | |||
| 94 | 48 | $this->configuration = Util::getSolrConfiguration(); |
|
| 95 | 48 | } |
|
| 96 | |||
| 97 | /** |
||
| 98 | * Gets the Solr connection used by this search. |
||
| 99 | * |
||
| 100 | * @return SolrService Solr connection |
||
| 101 | */ |
||
| 102 | public function getSolrConnection() |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Sets the Solr connection used by this search. |
||
| 109 | * |
||
| 110 | * Since ApacheSolrForTypo3\Solr\Search is a \TYPO3\CMS\Core\SingletonInterface, this is needed to |
||
| 111 | * be able to switch between multiple cores/connections during |
||
| 112 | * one request |
||
| 113 | * |
||
| 114 | * @param SolrService $solrConnection |
||
| 115 | */ |
||
| 116 | public function setSolrConnection(SolrService $solrConnection) |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Executes a query against a Solr server. |
||
| 123 | * |
||
| 124 | * 1) Gets the query string |
||
| 125 | * 2) Conducts the actual search |
||
| 126 | * 3) Checks debug settings |
||
| 127 | * |
||
| 128 | * @param Query $query The query with keywords, filters, and so on. |
||
| 129 | * @param int $offset Result offset for pagination. |
||
| 130 | * @param int $limit Maximum number of results to return. If set to NULL, this value is taken from the query object. |
||
| 131 | * @return \Apache_Solr_Response Solr response |
||
| 132 | */ |
||
| 133 | 25 | public function search(Query $query, $offset = 0, $limit = 10) |
|
| 179 | |||
| 180 | /** |
||
| 181 | * Allows to modify a query before eventually handing it over to Solr. |
||
| 182 | * |
||
| 183 | * @param Query $query The current query before it's being handed over to Solr. |
||
| 184 | * @return Query The modified query that is actually going to be given to Solr. |
||
| 185 | */ |
||
| 186 | 25 | protected function modifyQuery(Query $query) |
|
| 210 | |||
| 211 | /** |
||
| 212 | * Allows to modify a response returned from Solr before returning it to |
||
| 213 | * the rest of the extension. |
||
| 214 | * |
||
| 215 | * @param \Apache_Solr_Response $response The response as returned by Solr |
||
| 216 | * @return \Apache_Solr_Response The modified response that is actually going to be returned to the extension. |
||
| 217 | * @throws \UnexpectedValueException if a response modifier does not implement interface ApacheSolrForTypo3\Solr\Search\ResponseModifier |
||
| 218 | */ |
||
| 219 | 25 | protected function modifyResponse(\Apache_Solr_Response $response) |
|
| 246 | |||
| 247 | /** |
||
| 248 | * Sends a ping to the solr server to see whether it is available. |
||
| 249 | * |
||
| 250 | * @param bool $useCache Set to true if the cache should be used. |
||
| 251 | * @return bool Returns TRUE on successful ping. |
||
| 252 | * @throws \Exception Throws an exception in case ping was not successful. |
||
| 253 | */ |
||
| 254 | 25 | public function ping($useCache = true) |
|
| 275 | |||
| 276 | /** |
||
| 277 | * checks whether a search has been executed |
||
| 278 | * |
||
| 279 | * @return bool TRUE if there was a search, FALSE otherwise (if the user just visited the search page f.e.) |
||
| 280 | */ |
||
| 281 | 25 | public function hasSearched() |
|
| 285 | |||
| 286 | /** |
||
| 287 | * Gets the query object. |
||
| 288 | * |
||
| 289 | * @return Query Query |
||
| 290 | */ |
||
| 291 | 28 | public function getQuery() |
|
| 295 | |||
| 296 | /** |
||
| 297 | * Gets the Solr response |
||
| 298 | * |
||
| 299 | * @return \Apache_Solr_Response |
||
| 300 | */ |
||
| 301 | 18 | public function getResponse() |
|
| 305 | |||
| 306 | public function getRawResponse() |
||
| 310 | |||
| 311 | 18 | public function getResponseHeader() |
|
| 315 | |||
| 316 | 18 | public function getResponseBody() |
|
| 320 | |||
| 321 | /** |
||
| 322 | * Returns all results documents raw. Use with caution! |
||
| 323 | * |
||
| 324 | * @return \Apache_Solr_Document[] |
||
| 325 | */ |
||
| 326 | public function getResultDocumentsRaw() |
||
| 330 | |||
| 331 | /** |
||
| 332 | * Returns all result documents but applies htmlspecialchars() on all fields retrieved |
||
| 333 | * from solr except the configured fields in plugin.tx_solr.search.trustedFields |
||
| 334 | * |
||
| 335 | * @return \Apache_Solr_Document[] |
||
| 336 | */ |
||
| 337 | 18 | public function getResultDocumentsEscaped() |
|
| 341 | |||
| 342 | /** |
||
| 343 | * This method is used to apply htmlspecialchars on all document fields that |
||
| 344 | * are not configured to be secure. Secure mean that we know where the content is coming from. |
||
| 345 | * |
||
| 346 | * @param array $documents |
||
| 347 | * @return \Apache_Solr_Document[] |
||
| 348 | */ |
||
| 349 | 18 | protected function applyHtmlSpecialCharsOnAllFields(array $documents) |
|
| 370 | |||
| 371 | /** |
||
| 372 | * Applies htmlspecialchars on all items of an array of a single value. |
||
| 373 | * |
||
| 374 | * @param $fieldValue |
||
| 375 | * @return array|string |
||
| 376 | */ |
||
| 377 | 18 | protected function applyHtmlSpecialCharsOnSingleFieldValue($fieldValue) |
|
| 389 | |||
| 390 | /** |
||
| 391 | * Gets the time Solr took to execute the query and return the result. |
||
| 392 | * |
||
| 393 | * @return int Query time in milliseconds |
||
| 394 | */ |
||
| 395 | 18 | public function getQueryTime() |
|
| 399 | |||
| 400 | /** |
||
| 401 | * Gets the number of results per page. |
||
| 402 | * |
||
| 403 | * @return int Number of results per page |
||
| 404 | */ |
||
| 405 | public function getResultsPerPage() |
||
| 409 | |||
| 410 | /** |
||
| 411 | * Gets all facets with their fields, options, and counts. |
||
| 412 | * |
||
| 413 | * @return array |
||
| 414 | */ |
||
| 415 | 18 | public function getFacetCounts() |
|
| 446 | |||
| 447 | 18 | public function getFacetFieldOptions($facetField) |
|
| 458 | |||
| 459 | public function getFacetQueryOptions($facetField) |
||
| 480 | |||
| 481 | public function getFacetRangeOptions($rangeFacetField) |
||
| 485 | |||
| 486 | 20 | public function getNumberOfResults() |
|
| 490 | |||
| 491 | /** |
||
| 492 | * Gets the result offset. |
||
| 493 | * |
||
| 494 | * @return int Result offset |
||
| 495 | */ |
||
| 496 | 18 | public function getResultOffset() |
|
| 500 | |||
| 501 | 27 | public function getMaximumResultScore() |
|
| 505 | |||
| 506 | public function getDebugResponse() |
||
| 510 | |||
| 511 | 18 | public function getHighlightedContent() |
|
| 521 | |||
| 522 | 20 | public function getSpellcheckingSuggestions() |
|
| 539 | } |
||
| 540 |