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 | * @var \ApacheSolrForTypo3\Solr\System\Logging\SolrLogManager |
||
| 81 | */ |
||
| 82 | protected $logger = null; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Constructor |
||
| 86 | * |
||
| 87 | * @param SolrService $solrConnection The Solr connection to use for searching |
||
| 88 | */ |
||
| 89 | 49 | public function __construct(SolrService $solrConnection = null) |
|
| 103 | |||
| 104 | /** |
||
| 105 | * Gets the Solr connection used by this search. |
||
| 106 | * |
||
| 107 | * @return SolrService Solr connection |
||
| 108 | */ |
||
| 109 | public function getSolrConnection() |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Sets the Solr connection used by this search. |
||
| 116 | * |
||
| 117 | * Since ApacheSolrForTypo3\Solr\Search is a \TYPO3\CMS\Core\SingletonInterface, this is needed to |
||
| 118 | * be able to switch between multiple cores/connections during |
||
| 119 | * one request |
||
| 120 | * |
||
| 121 | * @param SolrService $solrConnection |
||
| 122 | */ |
||
| 123 | public function setSolrConnection(SolrService $solrConnection) |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Executes a query against a Solr server. |
||
| 130 | * |
||
| 131 | * 1) Gets the query string |
||
| 132 | * 2) Conducts the actual search |
||
| 133 | * 3) Checks debug settings |
||
| 134 | * |
||
| 135 | * @param Query $query The query with keywords, filters, and so on. |
||
| 136 | * @param int $offset Result offset for pagination. |
||
| 137 | * @param int $limit Maximum number of results to return. If set to NULL, this value is taken from the query object. |
||
| 138 | * @return \Apache_Solr_Response Solr response |
||
| 139 | */ |
||
| 140 | 26 | public function search(Query $query, $offset = 0, $limit = 10) |
|
| 192 | |||
| 193 | /** |
||
| 194 | * Allows to modify a query before eventually handing it over to Solr. |
||
| 195 | * |
||
| 196 | * @param Query $query The current query before it's being handed over to Solr. |
||
| 197 | * @return Query The modified query that is actually going to be given to Solr. |
||
| 198 | */ |
||
| 199 | 26 | protected function modifyQuery(Query $query) |
|
| 223 | |||
| 224 | /** |
||
| 225 | * Allows to modify a response returned from Solr before returning it to |
||
| 226 | * the rest of the extension. |
||
| 227 | * |
||
| 228 | * @param \Apache_Solr_Response $response The response as returned by Solr |
||
| 229 | * @return \Apache_Solr_Response The modified response that is actually going to be returned to the extension. |
||
| 230 | * @throws \UnexpectedValueException if a response modifier does not implement interface ApacheSolrForTypo3\Solr\Search\ResponseModifier |
||
| 231 | */ |
||
| 232 | 26 | protected function modifyResponse(\Apache_Solr_Response $response) |
|
| 259 | |||
| 260 | /** |
||
| 261 | * Sends a ping to the solr server to see whether it is available. |
||
| 262 | * |
||
| 263 | * @param bool $useCache Set to true if the cache should be used. |
||
| 264 | * @return bool Returns TRUE on successful ping. |
||
| 265 | * @throws \Exception Throws an exception in case ping was not successful. |
||
| 266 | */ |
||
| 267 | 25 | public function ping($useCache = true) |
|
| 291 | |||
| 292 | /** |
||
| 293 | * checks whether a search has been executed |
||
| 294 | * |
||
| 295 | * @return bool TRUE if there was a search, FALSE otherwise (if the user just visited the search page f.e.) |
||
| 296 | */ |
||
| 297 | 25 | public function hasSearched() |
|
| 301 | |||
| 302 | /** |
||
| 303 | * Gets the query object. |
||
| 304 | * |
||
| 305 | * @return Query Query |
||
| 306 | */ |
||
| 307 | 28 | public function getQuery() |
|
| 311 | |||
| 312 | /** |
||
| 313 | * Gets the Solr response |
||
| 314 | * |
||
| 315 | * @return \Apache_Solr_Response |
||
| 316 | */ |
||
| 317 | 18 | public function getResponse() |
|
| 321 | |||
| 322 | public function getRawResponse() |
||
| 326 | |||
| 327 | 18 | public function getResponseHeader() |
|
| 331 | |||
| 332 | 18 | public function getResponseBody() |
|
| 336 | |||
| 337 | /** |
||
| 338 | * Returns all results documents raw. Use with caution! |
||
| 339 | * |
||
| 340 | * @return \Apache_Solr_Document[] |
||
| 341 | */ |
||
| 342 | public function getResultDocumentsRaw() |
||
| 346 | |||
| 347 | /** |
||
| 348 | * Returns all result documents but applies htmlspecialchars() on all fields retrieved |
||
| 349 | * from solr except the configured fields in plugin.tx_solr.search.trustedFields |
||
| 350 | * |
||
| 351 | * @return \Apache_Solr_Document[] |
||
| 352 | */ |
||
| 353 | 18 | public function getResultDocumentsEscaped() |
|
| 357 | |||
| 358 | /** |
||
| 359 | * This method is used to apply htmlspecialchars on all document fields that |
||
| 360 | * are not configured to be secure. Secure mean that we know where the content is coming from. |
||
| 361 | * |
||
| 362 | * @param array $documents |
||
| 363 | * @return \Apache_Solr_Document[] |
||
| 364 | */ |
||
| 365 | 18 | protected function applyHtmlSpecialCharsOnAllFields(array $documents) |
|
| 386 | |||
| 387 | /** |
||
| 388 | * Applies htmlspecialchars on all items of an array of a single value. |
||
| 389 | * |
||
| 390 | * @param $fieldValue |
||
| 391 | * @return array|string |
||
| 392 | */ |
||
| 393 | 18 | protected function applyHtmlSpecialCharsOnSingleFieldValue($fieldValue) |
|
| 405 | |||
| 406 | /** |
||
| 407 | * Gets the time Solr took to execute the query and return the result. |
||
| 408 | * |
||
| 409 | * @return int Query time in milliseconds |
||
| 410 | */ |
||
| 411 | 18 | public function getQueryTime() |
|
| 415 | |||
| 416 | /** |
||
| 417 | * Gets the number of results per page. |
||
| 418 | * |
||
| 419 | * @return int Number of results per page |
||
| 420 | */ |
||
| 421 | public function getResultsPerPage() |
||
| 425 | |||
| 426 | /** |
||
| 427 | * Gets all facets with their fields, options, and counts. |
||
| 428 | * |
||
| 429 | * @return array |
||
| 430 | */ |
||
| 431 | 18 | public function getFacetCounts() |
|
| 462 | |||
| 463 | 18 | public function getFacetFieldOptions($facetField) |
|
| 474 | |||
| 475 | public function getFacetQueryOptions($facetField) |
||
| 496 | |||
| 497 | public function getFacetRangeOptions($rangeFacetField) |
||
| 501 | |||
| 502 | 20 | public function getNumberOfResults() |
|
| 506 | |||
| 507 | /** |
||
| 508 | * Gets the result offset. |
||
| 509 | * |
||
| 510 | * @return int Result offset |
||
| 511 | */ |
||
| 512 | 18 | public function getResultOffset() |
|
| 516 | |||
| 517 | 27 | public function getMaximumResultScore() |
|
| 521 | |||
| 522 | public function getDebugResponse() |
||
| 526 | |||
| 527 | 18 | public function getHighlightedContent() |
|
| 537 | |||
| 538 | 20 | public function getSpellcheckingSuggestions() |
|
| 555 | } |
||
| 556 |