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 |
||
| 35 | class SolrService extends \Apache_Solr_Service |
||
| 36 | { |
||
| 37 | const LUKE_SERVLET = 'admin/luke'; |
||
| 38 | const SYSTEM_SERVLET = 'admin/system'; |
||
| 39 | const PLUGINS_SERVLET = 'admin/plugins'; |
||
| 40 | const CORES_SERVLET = 'admin/cores'; |
||
| 41 | const SCHEMA_SERVLET = 'schema'; |
||
| 42 | const SYNONYMS_SERVLET = 'schema/analysis/synonyms/'; |
||
| 43 | const STOPWORDS_SERVLET = 'schema/analysis/stopwords/'; |
||
| 44 | |||
| 45 | const SCHEME_HTTP = 'http'; |
||
| 46 | const SCHEME_HTTPS = 'https'; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Server connection scheme. http or https. |
||
| 50 | * |
||
| 51 | * @var string |
||
| 52 | */ |
||
| 53 | protected $_scheme = self::SCHEME_HTTP; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Constructed servlet URL for Luke |
||
| 57 | * |
||
| 58 | * @var string |
||
| 59 | */ |
||
| 60 | protected $_lukeUrl; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Constructed servlet URL for plugin information |
||
| 64 | * |
||
| 65 | * @var string |
||
| 66 | */ |
||
| 67 | protected $_pluginsUrl; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @var string |
||
| 71 | */ |
||
| 72 | protected $_coresUrl; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @var string |
||
| 76 | */ |
||
| 77 | protected $_extractUrl; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @var string |
||
| 81 | */ |
||
| 82 | protected $_synonymsUrl; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @var string |
||
| 86 | */ |
||
| 87 | protected $_stopWordsUrl; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @var string |
||
| 91 | */ |
||
| 92 | protected $_schemaUrl; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @var bool |
||
| 96 | */ |
||
| 97 | protected $debug = false; |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @var \Apache_Solr_Response |
||
| 101 | */ |
||
| 102 | protected $responseCache = null; |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @var bool |
||
| 106 | */ |
||
| 107 | protected $hasSearched = false; |
||
| 108 | |||
| 109 | /** |
||
| 110 | * @var array |
||
| 111 | */ |
||
| 112 | protected $lukeData = []; |
||
| 113 | |||
| 114 | protected $systemData = null; |
||
| 115 | protected $pluginsData = null; |
||
| 116 | |||
| 117 | protected $schemaName = null; |
||
| 118 | protected $solrconfigName = null; |
||
| 119 | |||
| 120 | /** |
||
| 121 | * @var TypoScriptConfiguration |
||
| 122 | */ |
||
| 123 | protected $configuration; |
||
| 124 | |||
| 125 | /** |
||
| 126 | * @var array |
||
| 127 | */ |
||
| 128 | protected static $pingCache = []; |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Constructor |
||
| 132 | * |
||
| 133 | * @param string $host Solr host |
||
| 134 | * @param string $port Solr port |
||
| 135 | * @param string $path Solr path |
||
| 136 | * @param string $scheme Scheme, defaults to http, can be https |
||
| 137 | * @param TypoScriptConfiguration $typoScriptConfiguration |
||
| 138 | */ |
||
| 139 | 56 | public function __construct( |
|
| 151 | |||
| 152 | /** |
||
| 153 | * Creates a string representation of the Solr connection. Specifically |
||
| 154 | * will return the Solr URL. |
||
| 155 | * |
||
| 156 | * @return string The Solr URL. |
||
| 157 | */ |
||
| 158 | 39 | public function __toString() |
|
| 162 | |||
| 163 | /** |
||
| 164 | * Returns the current time in milliseconds. |
||
| 165 | * |
||
| 166 | * @return int |
||
| 167 | */ |
||
| 168 | protected function getMilliseconds() |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Performs a search. |
||
| 175 | * |
||
| 176 | * @param string $query query string / search term |
||
| 177 | * @param int $offset result offset for pagination |
||
| 178 | * @param int $limit number of results to retrieve |
||
| 179 | * @param array $params additional HTTP GET parameters |
||
| 180 | * @param string $method The HTTP method (Apache_Solr_Service::METHOD_GET or Apache_Solr_Service::METHOD::POST) |
||
| 181 | * @return \Apache_Solr_Response Solr response |
||
| 182 | * @throws \RuntimeException if Solr returns a HTTP status code other than 200 |
||
| 183 | */ |
||
| 184 | 25 | public function search($query, $offset = 0, $limit = 10, $params = array(), $method = self::METHOD_GET) |
|
| 202 | |||
| 203 | /** |
||
| 204 | * Call the /admin/ping servlet, can be used to quickly tell if a connection to the |
||
| 205 | * server is available. |
||
| 206 | * |
||
| 207 | * Simply overrides the SolrPhpClient implementation, changing ping from a |
||
| 208 | * HEAD to a GET request, see http://forge.typo3.org/issues/44167 |
||
| 209 | * |
||
| 210 | * Also does not report the time, see https://forge.typo3.org/issues/64551 |
||
| 211 | * |
||
| 212 | * @param float|int $timeout maximum time to wait for ping in seconds, -1 for unlimited (default is 2) |
||
| 213 | * @param boolean $useCache indicates if the ping result should be cached in the instance or not |
||
| 214 | * @return bool TRUE if Solr can be reached, FALSE if not |
||
| 215 | */ |
||
| 216 | 37 | public function ping($timeout = 2, $useCache = true) |
|
| 221 | |||
| 222 | /** |
||
| 223 | * Call the /admin/ping servlet, can be used to get the runtime of a ping request. |
||
| 224 | * |
||
| 225 | * @param float|int $timeout maximum time to wait for ping in seconds, -1 for unlimited (default is 2) |
||
| 226 | * @param boolean $useCache indicates if the ping result should be cached in the instance or not |
||
| 227 | * @return int runtime in milliseconds |
||
| 228 | * @throws \ApacheSolrForTypo3\Solr\PingFailedException |
||
| 229 | */ |
||
| 230 | public function getPingRoundTripRuntime($timeout = 2, $useCache = true) |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Performs a ping request and returns the result. |
||
| 249 | * |
||
| 250 | * @param int $timeout |
||
| 251 | * @param boolean $useCache indicates if the ping result should be cached in the instance or not |
||
| 252 | * @return \Apache_Solr_HttpTransport_Response |
||
| 253 | */ |
||
| 254 | 37 | protected function performPingRequest($timeout = 2, $useCache = true) |
|
| 269 | |||
| 270 | /** |
||
| 271 | * Performs a content and meta data extraction request. |
||
| 272 | * |
||
| 273 | * @param ExtractingQuery $query An extraction query |
||
| 274 | * @return array An array containing the extracted content [0] and meta data [1] |
||
| 275 | */ |
||
| 276 | 1 | public function extractByQuery(ExtractingQuery $query) |
|
| 307 | |||
| 308 | /** |
||
| 309 | * Make a request to a servlet (a path) that's not a standard path. |
||
| 310 | * |
||
| 311 | * @param string $servlet Path to be added to the base Solr path. |
||
| 312 | * @param array $parameters Optional, additional request parameters when constructing the URL. |
||
| 313 | * @param string $method HTTP method to use, defaults to GET. |
||
| 314 | * @param array $requestHeaders Key value pairs of header names and values. Should include 'Content-Type' for POST and PUT. |
||
| 315 | * @param string $rawPost Must be an empty string unless method is POST or PUT. |
||
| 316 | * @param float|bool $timeout Read timeout in seconds, defaults to FALSE. |
||
| 317 | * @return \Apache_Solr_Response Response object |
||
| 318 | * @throws \Apache_Solr_HttpTransportException if returned HTTP status is other than 200 |
||
| 319 | */ |
||
| 320 | 1 | public function requestServlet( |
|
| 361 | |||
| 362 | /** |
||
| 363 | * Return a valid http URL given this server's scheme, host, port, and path |
||
| 364 | * and a provided servlet name. |
||
| 365 | * |
||
| 366 | * @param string $servlet Servlet name |
||
| 367 | * @param array $params Additional URL parameters to attach to the end of the URL |
||
| 368 | * @return string Servlet URL |
||
| 369 | */ |
||
| 370 | 53 | protected function _constructUrl($servlet, $params = array()) |
|
| 384 | |||
| 385 | /** |
||
| 386 | * Returns the set scheme |
||
| 387 | * |
||
| 388 | * @return string |
||
| 389 | */ |
||
| 390 | public function getScheme() |
||
| 394 | |||
| 395 | /** |
||
| 396 | * Set the scheme used. If empty will fallback to constants |
||
| 397 | * |
||
| 398 | * @param string $scheme Either http or https |
||
| 399 | * @throws \UnexpectedValueException |
||
| 400 | */ |
||
| 401 | 56 | public function setScheme($scheme) |
|
| 421 | |||
| 422 | /** |
||
| 423 | * get field meta data for the index |
||
| 424 | * |
||
| 425 | * @param int $numberOfTerms Number of top terms to fetch for each field |
||
| 426 | * @return array |
||
| 427 | */ |
||
| 428 | public function getFieldsMetaData($numberOfTerms = 0) |
||
| 432 | |||
| 433 | /** |
||
| 434 | * Retrieves meta data about the index from the luke request handler |
||
| 435 | * |
||
| 436 | * @param int $numberOfTerms Number of top terms to fetch for each field |
||
| 437 | * @return \Apache_Solr_Response Index meta data |
||
| 438 | */ |
||
| 439 | public function getLukeMetaData($numberOfTerms = 0) |
||
| 456 | |||
| 457 | /** |
||
| 458 | * Central method for making a get operation against this Solr Server |
||
| 459 | * |
||
| 460 | * @param string $url |
||
| 461 | * @param float|bool $timeout Read timeout in seconds |
||
| 462 | * @return \Apache_Solr_Response |
||
| 463 | */ |
||
| 464 | 53 | protected function _sendRawGet($url, $timeout = false) |
|
| 495 | |||
| 496 | /** |
||
| 497 | * Returns whether a search has been executed or not. |
||
| 498 | * |
||
| 499 | * @return bool TRUE if a search has been executed, FALSE otherwise |
||
| 500 | */ |
||
| 501 | public function hasSearched() |
||
| 505 | |||
| 506 | /** |
||
| 507 | * Gets the most recent response (if any) |
||
| 508 | * |
||
| 509 | * @return \Apache_Solr_Response Most recent response, or NULL if a search has not been executed yet. |
||
| 510 | */ |
||
| 511 | public function getResponse() |
||
| 515 | |||
| 516 | /** |
||
| 517 | * Enable/Disable debug mode |
||
| 518 | * |
||
| 519 | * @param bool $debug TRUE to enable debug mode, FALSE to turn off, off by default |
||
| 520 | */ |
||
| 521 | public function setDebug($debug) |
||
| 525 | |||
| 526 | /** |
||
| 527 | * Gets information about the plugins installed in Solr |
||
| 528 | * |
||
| 529 | * @return array A nested array of plugin data. |
||
| 530 | */ |
||
| 531 | public function getPluginsInformation() |
||
| 543 | |||
| 544 | /** |
||
| 545 | * Gets the name of the schema.xml file installed and in use on the Solr |
||
| 546 | * server. |
||
| 547 | * |
||
| 548 | * @return string Name of the active schema.xml |
||
| 549 | */ |
||
| 550 | 1 | public function getSchemaName() |
|
| 559 | |||
| 560 | /** |
||
| 561 | * Gets information about the Solr server |
||
| 562 | * |
||
| 563 | * @return array A nested array of system data. |
||
| 564 | */ |
||
| 565 | 2 | public function getSystemInformation() |
|
| 577 | |||
| 578 | /** |
||
| 579 | * Gets the name of the solrconfig.xml file installed and in use on the Solr |
||
| 580 | * server. |
||
| 581 | * |
||
| 582 | * @return string Name of the active solrconfig.xml |
||
| 583 | */ |
||
| 584 | 1 | public function getSolrconfigName() |
|
| 600 | |||
| 601 | /** |
||
| 602 | * Gets the Solr server's version number. |
||
| 603 | * |
||
| 604 | * @return string Solr version number |
||
| 605 | */ |
||
| 606 | 1 | public function getSolrServerVersion() |
|
| 614 | |||
| 615 | /** |
||
| 616 | * Deletes all index documents of a certain type and does a commit |
||
| 617 | * afterwards. |
||
| 618 | * |
||
| 619 | * @param string $type The type of documents to delete, usually a table name. |
||
| 620 | * @param bool $commit Will commit immediately after deleting the documents if set, defaults to TRUE |
||
| 621 | */ |
||
| 622 | public function deleteByType($type, $commit = true) |
||
| 630 | |||
| 631 | /** |
||
| 632 | * Raw Delete Method. Takes a raw post body and sends it to the update service. Body should be |
||
| 633 | * a complete and well formed "delete" xml document |
||
| 634 | * |
||
| 635 | * @param string $rawPost Expected to be utf-8 encoded xml document |
||
| 636 | * @param float|int $timeout Maximum expected duration of the delete operation on the server (otherwise, will throw a communication exception) |
||
| 637 | * @return \Apache_Solr_Response |
||
| 638 | */ |
||
| 639 | 5 | public function delete($rawPost, $timeout = 3600) |
|
| 656 | |||
| 657 | /** |
||
| 658 | * Central method for making a post operation against this Solr Server |
||
| 659 | * |
||
| 660 | * @param string $url |
||
| 661 | * @param string $rawPost |
||
| 662 | * @param float|bool $timeout Read timeout in seconds |
||
| 663 | * @param string $contentType |
||
| 664 | * @return \Apache_Solr_Response |
||
| 665 | */ |
||
| 666 | 46 | protected function _sendRawPost( |
|
| 699 | |||
| 700 | /** |
||
| 701 | * Get currently configured synonyms |
||
| 702 | * |
||
| 703 | * @param string $baseWord If given a base word, retrieves the synonyms for that word only |
||
| 704 | * @return array |
||
| 705 | */ |
||
| 706 | public function getSynonyms($baseWord = '') |
||
| 729 | |||
| 730 | /** |
||
| 731 | * Add list of synonyms for base word to managed synonyms map |
||
| 732 | * |
||
| 733 | * @param $baseWord |
||
| 734 | * @param array $synonyms |
||
| 735 | * |
||
| 736 | * @return \Apache_Solr_Response |
||
| 737 | * |
||
| 738 | * @throws \Apache_Solr_InvalidArgumentException If $baseWord or $synonyms are empty |
||
| 739 | */ |
||
| 740 | public function addSynonym($baseWord, array $synonyms) |
||
| 750 | |||
| 751 | /** |
||
| 752 | * Remove a synonym from the synonyms map |
||
| 753 | * |
||
| 754 | * @param $baseWord |
||
| 755 | * @return \Apache_Solr_Response |
||
| 756 | * @throws \Apache_Solr_InvalidArgumentException |
||
| 757 | */ |
||
| 758 | public function deleteSynonym($baseWord) |
||
| 766 | |||
| 767 | /** |
||
| 768 | * Central method for making a HTTP DELETE operation against the Solr server |
||
| 769 | * |
||
| 770 | * @param $url |
||
| 771 | * @param bool|float $timeout Read timeout in seconds |
||
| 772 | * @return \Apache_Solr_Response |
||
| 773 | */ |
||
| 774 | protected function _sendRawDelete($url, $timeout = false) |
||
| 814 | |||
| 815 | /** |
||
| 816 | * Get currently configured stop words |
||
| 817 | * |
||
| 818 | * @return array |
||
| 819 | */ |
||
| 820 | public function getStopWords() |
||
| 833 | |||
| 834 | /** |
||
| 835 | * Adds stop words to the managed stop word list |
||
| 836 | * |
||
| 837 | * @param array|string $stopWords string for a single word, array for multiple words |
||
| 838 | * @return \Apache_Solr_Response |
||
| 839 | * @throws \Apache_Solr_InvalidArgumentException If $stopWords is empty |
||
| 840 | */ |
||
| 841 | public function addStopWords($stopWords) |
||
| 856 | |||
| 857 | /** |
||
| 858 | * Deletes a words from the managed stop word list |
||
| 859 | * |
||
| 860 | * @param string $stopWord stop word to delete |
||
| 861 | * @return \Apache_Solr_Response |
||
| 862 | * @throws \Apache_Solr_InvalidArgumentException If $stopWords is empty |
||
| 863 | */ |
||
| 864 | public function deleteStopWord($stopWord) |
||
| 872 | |||
| 873 | /** |
||
| 874 | * Reloads the current core |
||
| 875 | * |
||
| 876 | * @return \Apache_Solr_Response |
||
| 877 | */ |
||
| 878 | public function reloadCore() |
||
| 885 | |||
| 886 | /** |
||
| 887 | * initializes various URLs, including the Luke URL |
||
| 888 | * |
||
| 889 | * @return void |
||
| 890 | */ |
||
| 891 | 53 | protected function _initUrls() |
|
| 927 | |||
| 928 | /** |
||
| 929 | * Get the language map name for the text field. |
||
| 930 | * This is necessary to select the correct synonym map. |
||
| 931 | * |
||
| 932 | * @return string |
||
| 933 | */ |
||
| 934 | 53 | protected function getManagedLanguage() |
|
| 954 | |||
| 955 | /** |
||
| 956 | * Get the configured schema for the current core |
||
| 957 | * |
||
| 958 | * @return \stdClass |
||
| 959 | */ |
||
| 960 | 53 | protected function getSchema() |
|
| 965 | } |
||
| 966 |
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: