Complex classes like SolrService 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 SolrService, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 37 | class SolrService extends \Apache_Solr_Service |
||
| 38 | { |
||
| 39 | |||
| 40 | const LUKE_SERVLET = 'admin/luke'; |
||
| 41 | const SYSTEM_SERVLET = 'admin/system'; |
||
| 42 | const PLUGINS_SERVLET = 'admin/plugins'; |
||
| 43 | const CORES_SERVLET = 'admin/cores'; |
||
| 44 | const SCHEMA_SERVLET = 'schema'; |
||
| 45 | const SYNONYMS_SERVLET = 'schema/analysis/synonyms/'; |
||
| 46 | const STOPWORDS_SERVLET = 'schema/analysis/stopwords/'; |
||
| 47 | |||
| 48 | const SCHEME_HTTP = 'http'; |
||
| 49 | const SCHEME_HTTPS = 'https'; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Server connection scheme. http or https. |
||
| 53 | * |
||
| 54 | * @var string |
||
| 55 | */ |
||
| 56 | protected $_scheme = self::SCHEME_HTTP; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Constructed servlet URL for Luke |
||
| 60 | * |
||
| 61 | * @var string |
||
| 62 | */ |
||
| 63 | protected $_lukeUrl; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Constructed servlet URL for plugin information |
||
| 67 | * |
||
| 68 | * @var string |
||
| 69 | */ |
||
| 70 | protected $_pluginsUrl; |
||
| 71 | |||
| 72 | protected $_coresUrl; |
||
| 73 | |||
| 74 | protected $_extractUrl; |
||
| 75 | |||
| 76 | protected $_synonymsUrl; |
||
| 77 | |||
| 78 | protected $_stopWordsUrl; |
||
| 79 | |||
| 80 | protected $_schemaUrl; |
||
| 81 | |||
| 82 | protected $debug; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @var \Apache_Solr_Response |
||
| 86 | */ |
||
| 87 | protected $responseCache = null; |
||
| 88 | protected $hasSearched = false; |
||
| 89 | |||
| 90 | protected $lukeData = array(); |
||
| 91 | protected $systemData = null; |
||
| 92 | protected $pluginsData = null; |
||
| 93 | |||
| 94 | protected $schemaName = null; |
||
| 95 | protected $solrconfigName = null; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @var array |
||
| 99 | */ |
||
| 100 | protected $configuration; |
||
| 101 | |||
| 102 | |||
| 103 | /** |
||
| 104 | * Constructor |
||
| 105 | * |
||
| 106 | * @param string $host Solr host |
||
| 107 | * @param string $port Solr port |
||
| 108 | * @param string $path Solr path |
||
| 109 | * @param string $scheme Scheme, defaults to http, can be https |
||
| 110 | */ |
||
| 111 | public function __construct( |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Creates a string representation of the Solr connection. Specifically |
||
| 125 | * will return the Solr URL. |
||
| 126 | * |
||
| 127 | * @return string The Solr URL. |
||
| 128 | */ |
||
| 129 | public function __toString() |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Returns the current time in milliseconds. |
||
| 136 | * |
||
| 137 | * @return integer |
||
| 138 | */ |
||
| 139 | protected function getMilliseconds() |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Performs a search. |
||
| 146 | * |
||
| 147 | * @param string $query query string / search term |
||
| 148 | * @param integer $offset result offset for pagination |
||
| 149 | * @param integer $limit number of results to retrieve |
||
| 150 | * @param array $params additional HTTP GET parameters |
||
| 151 | * @param string $method The HTTP method (Apache_Solr_Service::METHOD_GET or Apache_Solr_Service::METHOD::POST) |
||
| 152 | * @return \Apache_Solr_Response Solr response |
||
| 153 | * @throws \RuntimeException if Solr returns a HTTP status code other than 200 |
||
| 154 | */ |
||
| 155 | public function search($query, $offset = 0, $limit = 10, $params = array(), $method = self::METHOD_GET) |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Call the /admin/ping servlet, can be used to quickly tell if a connection to the |
||
| 176 | * server is available. |
||
| 177 | * |
||
| 178 | * Simply overrides the SolrPhpClient implementation, changing ping from a |
||
| 179 | * HEAD to a GET request, see http://forge.typo3.org/issues/44167 |
||
| 180 | * |
||
| 181 | * Also does not report the time, see https://forge.typo3.org/issues/64551 |
||
| 182 | * |
||
| 183 | * @param float|integer $timeout maximum time to wait for ping in seconds, -1 for unlimited (default is 2) |
||
| 184 | * @return bool TRUE if Solr can be reached, FALSE if not |
||
| 185 | */ |
||
| 186 | public function ping($timeout = 2) |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Call the /admin/ping servlet, can be used to get the runtime of a ping request. |
||
| 194 | * |
||
| 195 | * @param float|integer $timeout maximum time to wait for ping in seconds, -1 for unlimited (default is 2) |
||
| 196 | * @return int runtime in milliseconds |
||
| 197 | * @throws \ApacheSolrForTypo3\Solr\PingFailedException |
||
| 198 | */ |
||
| 199 | public function getPingRoundTripRuntime($timeout = 2) |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Performs a ping request and returns the result. |
||
| 218 | * |
||
| 219 | * @param int $timeout |
||
| 220 | * @return \Apache_Solr_HttpTransport_Response |
||
| 221 | */ |
||
| 222 | protected function performPingRequest($timeout = 2) |
||
| 226 | |||
| 227 | /** |
||
| 228 | * Performs a content and meta data extraction request. |
||
| 229 | * |
||
| 230 | * @param ExtractingQuery $query An extraction query |
||
| 231 | * @return array An array containing the extracted content [0] and meta data [1] |
||
| 232 | */ |
||
| 233 | public function extractByQuery(ExtractingQuery $query) |
||
| 264 | |||
| 265 | /** |
||
| 266 | * Make a request to a servlet (a path) that's not a standard path. |
||
| 267 | * |
||
| 268 | * @param string $servlet Path to be added to the base Solr path. |
||
| 269 | * @param array $parameters Optional, additional request parameters when constructing the URL. |
||
| 270 | * @param string $method HTTP method to use, defaults to GET. |
||
| 271 | * @param array $requestHeaders Key value pairs of header names and values. Should include 'Content-Type' for POST and PUT. |
||
| 272 | * @param string $rawPost Must be an empty string unless method is POST or PUT. |
||
| 273 | * @param float|boolean $timeout Read timeout in seconds, defaults to FALSE. |
||
| 274 | * @return \Apache_Solr_Response Response object |
||
| 275 | * @throws \Apache_Solr_HttpTransportException if returned HTTP status is other than 200 |
||
| 276 | */ |
||
| 277 | public function requestServlet( |
||
| 315 | |||
| 316 | /** |
||
| 317 | * Return a valid http URL given this server's scheme, host, port, and path |
||
| 318 | * and a provided servlet name. |
||
| 319 | * |
||
| 320 | * @param string $servlet Servlet name |
||
| 321 | * @param array $params Additional URL parameters to attach to the end of the URL |
||
| 322 | * @return string Servlet URL |
||
| 323 | */ |
||
| 324 | protected function _constructUrl($servlet, $params = array()) |
||
| 338 | |||
| 339 | /** |
||
| 340 | * Returns the set scheme |
||
| 341 | * |
||
| 342 | * @return string |
||
| 343 | */ |
||
| 344 | public function getScheme() |
||
| 348 | |||
| 349 | /** |
||
| 350 | * Set the scheme used. If empty will fallback to constants |
||
| 351 | * |
||
| 352 | * @param string $scheme Either http or https |
||
| 353 | * @throws \UnexpectedValueException |
||
| 354 | */ |
||
| 355 | public function setScheme($scheme) |
||
| 375 | |||
| 376 | /** |
||
| 377 | * get field meta data for the index |
||
| 378 | * |
||
| 379 | * @param integer $numberOfTerms Number of top terms to fetch for each field |
||
| 380 | * @return array |
||
| 381 | */ |
||
| 382 | public function getFieldsMetaData($numberOfTerms = 0) |
||
| 386 | |||
| 387 | /** |
||
| 388 | * Retrieves meta data about the index from the luke request handler |
||
| 389 | * |
||
| 390 | * @param integer $numberOfTerms Number of top terms to fetch for each field |
||
| 391 | * @return \Apache_Solr_Response Index meta data |
||
| 392 | */ |
||
| 393 | public function getLukeMetaData($numberOfTerms = 0) |
||
| 410 | |||
| 411 | /** |
||
| 412 | * Central method for making a get operation against this Solr Server |
||
| 413 | * |
||
| 414 | * @param string $url |
||
| 415 | * @param float|boolean $timeout Read timeout in seconds |
||
| 416 | * @return \Apache_Solr_Response |
||
| 417 | */ |
||
| 418 | protected function _sendRawGet($url, $timeout = false) |
||
| 419 | { |
||
| 420 | $logSeverity = 0; // info |
||
| 421 | |||
| 422 | try { |
||
| 423 | $response = parent::_sendRawGet($url, $timeout); |
||
| 424 | } catch (\Apache_Solr_HttpTransportException $e) { |
||
| 425 | $logSeverity = 3; // fatal error |
||
| 426 | $response = $e->getResponse(); |
||
| 427 | } |
||
| 428 | |||
| 429 | if ($this->configuration->getLoggingQueryRawGet() || $response->getHttpStatus() != 200) { |
||
| 430 | $logData = array( |
||
| 431 | 'query url' => $url, |
||
| 432 | 'response' => (array)$response |
||
| 433 | ); |
||
| 434 | |||
| 435 | if (!empty($e)) { |
||
| 436 | $logData['exception'] = $e->__toString(); |
||
| 437 | } else { |
||
| 438 | // trigger data parsing |
||
| 439 | $response->response; |
||
| 440 | $logData['response data'] = print_r($response, true); |
||
| 441 | } |
||
| 442 | |||
| 443 | GeneralUtility::devLog('Querying Solr using GET', 'solr', |
||
| 444 | $logSeverity, $logData); |
||
| 445 | } |
||
| 446 | |||
| 447 | return $response; |
||
| 448 | } |
||
| 449 | |||
| 450 | /** |
||
| 451 | * Returns whether a search has been executed or not. |
||
| 452 | * |
||
| 453 | * @return bool TRUE if a search has been executed, FALSE otherwise |
||
| 454 | */ |
||
| 455 | public function hasSearched() |
||
| 459 | |||
| 460 | /** |
||
| 461 | * Gets the most recent response (if any) |
||
| 462 | * |
||
| 463 | * @return \Apache_Solr_Response Most recent response, or NULL if a search has not been executed yet. |
||
| 464 | */ |
||
| 465 | public function getResponse() |
||
| 469 | |||
| 470 | /** |
||
| 471 | * Enable/Disable debug mode |
||
| 472 | * |
||
| 473 | * @param boolean $debug TRUE to enable debug mode, FALSE to turn off, off by default |
||
| 474 | */ |
||
| 475 | public function setDebug($debug) |
||
| 479 | |||
| 480 | /** |
||
| 481 | * Gets information about the plugins installed in Solr |
||
| 482 | * |
||
| 483 | * @return array A nested array of plugin data. |
||
| 484 | */ |
||
| 485 | public function getPluginsInformation() |
||
| 497 | |||
| 498 | /** |
||
| 499 | * Gets the name of the schema.xml file installed and in use on the Solr |
||
| 500 | * server. |
||
| 501 | * |
||
| 502 | * @return string Name of the active schema.xml |
||
| 503 | */ |
||
| 504 | public function getSchemaName() |
||
| 513 | |||
| 514 | /** |
||
| 515 | * Gets information about the Solr server |
||
| 516 | * |
||
| 517 | * @return array A nested array of system data. |
||
| 518 | */ |
||
| 519 | public function getSystemInformation() |
||
| 531 | |||
| 532 | /** |
||
| 533 | * Gets the name of the solrconfig.xml file installed and in use on the Solr |
||
| 534 | * server. |
||
| 535 | * |
||
| 536 | * @return string Name of the active solrconfig.xml |
||
| 537 | */ |
||
| 538 | public function getSolrconfigName() |
||
| 554 | |||
| 555 | /** |
||
| 556 | * Gets the Solr server's version number. |
||
| 557 | * |
||
| 558 | * @return string Solr version number |
||
| 559 | */ |
||
| 560 | public function getSolrServerVersion() |
||
| 568 | |||
| 569 | /** |
||
| 570 | * Deletes all index documents of a certain type and does a commit |
||
| 571 | * afterwards. |
||
| 572 | * |
||
| 573 | * @param string $type The type of documents to delete, usually a table name. |
||
| 574 | * @param boolean $commit Will commit immediately after deleting the documents if set, defaults to TRUE |
||
| 575 | */ |
||
| 576 | public function deleteByType($type, $commit = true) |
||
| 584 | |||
| 585 | /** |
||
| 586 | * Raw Delete Method. Takes a raw post body and sends it to the update service. Body should be |
||
| 587 | * a complete and well formed "delete" xml document |
||
| 588 | * |
||
| 589 | * @param string $rawPost Expected to be utf-8 encoded xml document |
||
| 590 | * @param float|integer $timeout Maximum expected duration of the delete operation on the server (otherwise, will throw a communication exception) |
||
| 591 | * @return \Apache_Solr_Response |
||
| 592 | */ |
||
| 593 | public function delete($rawPost, $timeout = 3600) |
||
| 610 | |||
| 611 | /** |
||
| 612 | * Central method for making a post operation against this Solr Server |
||
| 613 | * |
||
| 614 | * @param string $url |
||
| 615 | * @param string $rawPost |
||
| 616 | * @param float|boolean $timeout Read timeout in seconds |
||
| 617 | * @param string $contentType |
||
| 618 | * @return \Apache_Solr_Response |
||
| 619 | */ |
||
| 620 | protected function _sendRawPost( |
||
| 653 | |||
| 654 | /** |
||
| 655 | * Get currently configured synonyms |
||
| 656 | * |
||
| 657 | * @param string $baseWord If given a base word, retrieves the synonyms for that word only |
||
| 658 | * @return array |
||
| 659 | */ |
||
| 660 | public function getSynonyms($baseWord = '') |
||
| 683 | |||
| 684 | /** |
||
| 685 | * Add list of synonyms for base word to managed synonyms map |
||
| 686 | * |
||
| 687 | * @param $baseWord |
||
| 688 | * @param array $synonyms |
||
| 689 | * |
||
| 690 | * @return \Apache_Solr_Response |
||
| 691 | * |
||
| 692 | * @throws \Apache_Solr_InvalidArgumentException If $baseWord or $synonyms are empty |
||
| 693 | */ |
||
| 694 | public function addSynonym($baseWord, array $synonyms) |
||
| 704 | |||
| 705 | /** |
||
| 706 | * Remove a synonym from the synonyms map |
||
| 707 | * |
||
| 708 | * @param $baseWord |
||
| 709 | * @return \Apache_Solr_Response |
||
| 710 | * @throws \Apache_Solr_InvalidArgumentException |
||
| 711 | */ |
||
| 712 | public function deleteSynonym($baseWord) |
||
| 720 | |||
| 721 | /** |
||
| 722 | * Central method for making a HTTP DELETE operation against the Solr server |
||
| 723 | * |
||
| 724 | * @param $url |
||
| 725 | * @param bool|float $timeout Read timeout in seconds |
||
| 726 | * @return \Apache_Solr_Response |
||
| 727 | */ |
||
| 728 | protected function _sendRawDelete($url, $timeout = false) |
||
| 729 | { |
||
| 730 | $logSeverity = 0; // info |
||
| 731 | |||
| 732 | try { |
||
| 733 | $httpTransport = $this->getHttpTransport(); |
||
| 734 | |||
| 735 | $httpResponse = $httpTransport->performDeleteRequest($url, |
||
| 736 | $timeout); |
||
| 737 | $solrResponse = new \Apache_Solr_Response($httpResponse, |
||
| 738 | $this->_createDocuments, $this->_collapseSingleValueArrays); |
||
| 739 | |||
| 740 | if ($solrResponse->getHttpStatus() != 200) { |
||
| 741 | throw new \Apache_Solr_HttpTransportException($solrResponse); |
||
| 742 | } |
||
| 743 | } catch (\Apache_Solr_HttpTransportException $e) { |
||
| 744 | $logSeverity = 3; // fatal error |
||
| 745 | $solrResponse = $e->getResponse(); |
||
| 746 | } |
||
| 747 | |||
| 748 | if ($this->configuration->getLoggingQueryRawDelete() || $solrResponse->getHttpStatus() != 200) { |
||
| 749 | $logData = array( |
||
| 750 | 'query url' => $url, |
||
| 751 | 'response' => (array)$solrResponse |
||
| 752 | ); |
||
| 753 | |||
| 754 | if (!empty($e)) { |
||
| 755 | $logData['exception'] = $e->__toString(); |
||
| 756 | } else { |
||
| 757 | // trigger data parsing |
||
| 758 | $solrResponse->response; |
||
| 759 | $logData['response data'] = print_r($solrResponse, true); |
||
| 760 | } |
||
| 761 | |||
| 762 | GeneralUtility::devLog('Querying Solr using DELETE', 'solr', |
||
| 763 | $logSeverity, $logData); |
||
| 764 | } |
||
| 765 | |||
| 766 | return $solrResponse; |
||
| 767 | } |
||
| 768 | |||
| 769 | /** |
||
| 770 | * Get currently configured stop words |
||
| 771 | * |
||
| 772 | * @return array |
||
| 773 | */ |
||
| 774 | public function getStopWords() |
||
| 787 | |||
| 788 | /** |
||
| 789 | * Adds stop words to the managed stop word list |
||
| 790 | * |
||
| 791 | * @param array|string $stopWords string for a single word, array for multiple words |
||
| 792 | * @return \Apache_Solr_Response |
||
| 793 | * @throws \Apache_Solr_InvalidArgumentException If $stopWords is empty |
||
| 794 | */ |
||
| 795 | public function addStopWords($stopWords) |
||
| 810 | |||
| 811 | /** |
||
| 812 | * Deletes a words from the managed stop word list |
||
| 813 | * |
||
| 814 | * @param string $stopWord stop word to delete |
||
| 815 | * @return \Apache_Solr_Response |
||
| 816 | * @throws \Apache_Solr_InvalidArgumentException If $stopWords is empty |
||
| 817 | */ |
||
| 818 | public function deleteStopWord($stopWord) |
||
| 826 | |||
| 827 | /** |
||
| 828 | * Reloads the current core |
||
| 829 | * |
||
| 830 | * @return \Apache_Solr_Response |
||
| 831 | */ |
||
| 832 | public function reloadCore() |
||
| 839 | |||
| 840 | /** |
||
| 841 | * initializes various URLs, including the Luke URL |
||
| 842 | * |
||
| 843 | * @return void |
||
| 844 | */ |
||
| 845 | protected function _initUrls() |
||
| 880 | |||
| 881 | /** |
||
| 882 | * Get the language map name for the text field. |
||
| 883 | * This is necessary to select the correct synonym map. |
||
| 884 | * |
||
| 885 | * @return string |
||
| 886 | */ |
||
| 887 | protected function getManagedLanguage() |
||
| 907 | |||
| 908 | /** |
||
| 909 | * Get the configured schema for the current core |
||
| 910 | * |
||
| 911 | * @return \stdClass |
||
| 912 | */ |
||
| 913 | protected function getSchema() |
||
| 918 | } |
||
| 919 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..