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 |
||
34 | class SolrService extends \Apache_Solr_Service |
||
35 | { |
||
36 | const LUKE_SERVLET = 'admin/luke'; |
||
37 | const SYSTEM_SERVLET = 'admin/system'; |
||
38 | const PLUGINS_SERVLET = 'admin/plugins'; |
||
39 | const CORES_SERVLET = 'admin/cores'; |
||
40 | const SCHEMA_SERVLET = 'schema'; |
||
41 | const SYNONYMS_SERVLET = 'schema/analysis/synonyms/'; |
||
42 | const STOPWORDS_SERVLET = 'schema/analysis/stopwords/'; |
||
43 | |||
44 | const SCHEME_HTTP = 'http'; |
||
45 | const SCHEME_HTTPS = 'https'; |
||
46 | |||
47 | /** |
||
48 | * Server connection scheme. http or https. |
||
49 | * |
||
50 | * @var string |
||
51 | */ |
||
52 | protected $_scheme = self::SCHEME_HTTP; |
||
53 | |||
54 | /** |
||
55 | * Constructed servlet URL for Luke |
||
56 | * |
||
57 | * @var string |
||
58 | */ |
||
59 | protected $_lukeUrl; |
||
60 | |||
61 | /** |
||
62 | * Constructed servlet URL for plugin information |
||
63 | * |
||
64 | * @var string |
||
65 | */ |
||
66 | protected $_pluginsUrl; |
||
67 | |||
68 | protected $_coresUrl; |
||
69 | |||
70 | protected $_extractUrl; |
||
71 | |||
72 | protected $_synonymsUrl; |
||
73 | |||
74 | protected $_stopWordsUrl; |
||
75 | |||
76 | protected $_schemaUrl; |
||
77 | |||
78 | protected $debug; |
||
79 | |||
80 | /** |
||
81 | * @var \Apache_Solr_Response |
||
82 | */ |
||
83 | protected $responseCache = null; |
||
84 | protected $hasSearched = false; |
||
85 | |||
86 | protected $lukeData = array(); |
||
87 | protected $systemData = null; |
||
88 | protected $pluginsData = null; |
||
89 | |||
90 | protected $schemaName = null; |
||
91 | protected $solrconfigName = null; |
||
92 | |||
93 | /** |
||
94 | * @var array |
||
95 | */ |
||
96 | protected $configuration; |
||
97 | |||
98 | /** |
||
99 | * Constructor |
||
100 | * |
||
101 | * @param string $host Solr host |
||
102 | * @param string $port Solr port |
||
103 | * @param string $path Solr path |
||
104 | * @param string $scheme Scheme, defaults to http, can be https |
||
105 | */ |
||
106 | 54 | public function __construct( |
|
117 | |||
118 | /** |
||
119 | * Creates a string representation of the Solr connection. Specifically |
||
120 | * will return the Solr URL. |
||
121 | * |
||
122 | * @return string The Solr URL. |
||
123 | */ |
||
124 | 4 | public function __toString() |
|
128 | |||
129 | /** |
||
130 | * Returns the current time in milliseconds. |
||
131 | * |
||
132 | * @return int |
||
133 | */ |
||
134 | protected function getMilliseconds() |
||
138 | |||
139 | /** |
||
140 | * Performs a search. |
||
141 | * |
||
142 | * @param string $query query string / search term |
||
143 | * @param int $offset result offset for pagination |
||
144 | * @param int $limit number of results to retrieve |
||
145 | * @param array $params additional HTTP GET parameters |
||
146 | * @param string $method The HTTP method (Apache_Solr_Service::METHOD_GET or Apache_Solr_Service::METHOD::POST) |
||
147 | * @return \Apache_Solr_Response Solr response |
||
148 | * @throws \RuntimeException if Solr returns a HTTP status code other than 200 |
||
149 | */ |
||
150 | 25 | public function search($query, $offset = 0, $limit = 10, $params = array(), $method = self::METHOD_GET) |
|
168 | |||
169 | /** |
||
170 | * Call the /admin/ping servlet, can be used to quickly tell if a connection to the |
||
171 | * server is available. |
||
172 | * |
||
173 | * Simply overrides the SolrPhpClient implementation, changing ping from a |
||
174 | * HEAD to a GET request, see http://forge.typo3.org/issues/44167 |
||
175 | * |
||
176 | * Also does not report the time, see https://forge.typo3.org/issues/64551 |
||
177 | * |
||
178 | * @param float|int $timeout maximum time to wait for ping in seconds, -1 for unlimited (default is 2) |
||
179 | * @return bool TRUE if Solr can be reached, FALSE if not |
||
180 | */ |
||
181 | 35 | public function ping($timeout = 2) |
|
186 | |||
187 | /** |
||
188 | * Call the /admin/ping servlet, can be used to get the runtime of a ping request. |
||
189 | * |
||
190 | * @param float|int $timeout maximum time to wait for ping in seconds, -1 for unlimited (default is 2) |
||
191 | * @return int runtime in milliseconds |
||
192 | * @throws \ApacheSolrForTypo3\Solr\PingFailedException |
||
193 | */ |
||
194 | public function getPingRoundTripRuntime($timeout = 2) |
||
210 | |||
211 | /** |
||
212 | * Performs a ping request and returns the result. |
||
213 | * |
||
214 | * @param int $timeout |
||
215 | * @return \Apache_Solr_HttpTransport_Response |
||
216 | */ |
||
217 | 35 | protected function performPingRequest($timeout = 2) |
|
221 | |||
222 | /** |
||
223 | * Performs a content and meta data extraction request. |
||
224 | * |
||
225 | * @param ExtractingQuery $query An extraction query |
||
226 | * @return array An array containing the extracted content [0] and meta data [1] |
||
227 | */ |
||
228 | 1 | public function extractByQuery(ExtractingQuery $query) |
|
259 | |||
260 | /** |
||
261 | * Make a request to a servlet (a path) that's not a standard path. |
||
262 | * |
||
263 | * @param string $servlet Path to be added to the base Solr path. |
||
264 | * @param array $parameters Optional, additional request parameters when constructing the URL. |
||
265 | * @param string $method HTTP method to use, defaults to GET. |
||
266 | * @param array $requestHeaders Key value pairs of header names and values. Should include 'Content-Type' for POST and PUT. |
||
267 | * @param string $rawPost Must be an empty string unless method is POST or PUT. |
||
268 | * @param float|bool $timeout Read timeout in seconds, defaults to FALSE. |
||
269 | * @return \Apache_Solr_Response Response object |
||
270 | * @throws \Apache_Solr_HttpTransportException if returned HTTP status is other than 200 |
||
271 | */ |
||
272 | 1 | public function requestServlet( |
|
310 | |||
311 | /** |
||
312 | * Return a valid http URL given this server's scheme, host, port, and path |
||
313 | * and a provided servlet name. |
||
314 | * |
||
315 | * @param string $servlet Servlet name |
||
316 | * @param array $params Additional URL parameters to attach to the end of the URL |
||
317 | * @return string Servlet URL |
||
318 | */ |
||
319 | 53 | protected function _constructUrl($servlet, $params = array()) |
|
333 | |||
334 | /** |
||
335 | * Returns the set scheme |
||
336 | * |
||
337 | * @return string |
||
338 | */ |
||
339 | public function getScheme() |
||
343 | |||
344 | /** |
||
345 | * Set the scheme used. If empty will fallback to constants |
||
346 | * |
||
347 | * @param string $scheme Either http or https |
||
348 | * @throws \UnexpectedValueException |
||
349 | */ |
||
350 | 54 | public function setScheme($scheme) |
|
370 | |||
371 | /** |
||
372 | * get field meta data for the index |
||
373 | * |
||
374 | * @param int $numberOfTerms Number of top terms to fetch for each field |
||
375 | * @return array |
||
376 | */ |
||
377 | public function getFieldsMetaData($numberOfTerms = 0) |
||
381 | |||
382 | /** |
||
383 | * Retrieves meta data about the index from the luke request handler |
||
384 | * |
||
385 | * @param int $numberOfTerms Number of top terms to fetch for each field |
||
386 | * @return \Apache_Solr_Response Index meta data |
||
387 | */ |
||
388 | public function getLukeMetaData($numberOfTerms = 0) |
||
405 | |||
406 | /** |
||
407 | * Central method for making a get operation against this Solr Server |
||
408 | * |
||
409 | * @param string $url |
||
410 | * @param float|bool $timeout Read timeout in seconds |
||
411 | * @return \Apache_Solr_Response |
||
412 | */ |
||
413 | 53 | protected function _sendRawGet($url, $timeout = false) |
|
444 | |||
445 | /** |
||
446 | * Returns whether a search has been executed or not. |
||
447 | * |
||
448 | * @return bool TRUE if a search has been executed, FALSE otherwise |
||
449 | */ |
||
450 | public function hasSearched() |
||
454 | |||
455 | /** |
||
456 | * Gets the most recent response (if any) |
||
457 | * |
||
458 | * @return \Apache_Solr_Response Most recent response, or NULL if a search has not been executed yet. |
||
459 | */ |
||
460 | public function getResponse() |
||
464 | |||
465 | /** |
||
466 | * Enable/Disable debug mode |
||
467 | * |
||
468 | * @param bool $debug TRUE to enable debug mode, FALSE to turn off, off by default |
||
469 | */ |
||
470 | public function setDebug($debug) |
||
474 | |||
475 | /** |
||
476 | * Gets information about the plugins installed in Solr |
||
477 | * |
||
478 | * @return array A nested array of plugin data. |
||
479 | */ |
||
480 | public function getPluginsInformation() |
||
492 | |||
493 | /** |
||
494 | * Gets the name of the schema.xml file installed and in use on the Solr |
||
495 | * server. |
||
496 | * |
||
497 | * @return string Name of the active schema.xml |
||
498 | */ |
||
499 | 1 | public function getSchemaName() |
|
500 | { |
||
501 | 1 | if (is_null($this->schemaName)) { |
|
502 | 1 | $systemInformation = $this->getSystemInformation(); |
|
503 | 1 | $this->schemaName = $systemInformation->core->schema; |
|
504 | 1 | } |
|
505 | |||
506 | 1 | return $this->schemaName; |
|
507 | } |
||
508 | |||
509 | /** |
||
510 | * Gets information about the Solr server |
||
511 | * |
||
512 | * @return array A nested array of system data. |
||
513 | */ |
||
514 | 2 | public function getSystemInformation() |
|
515 | { |
||
516 | 2 | if (empty($this->systemData)) { |
|
517 | 2 | $systemInformation = $this->system(); |
|
518 | |||
519 | // access a random property to trigger response parsing |
||
520 | 2 | $systemInformation->responseHeader; |
|
521 | 2 | $this->systemData = $systemInformation; |
|
522 | 2 | } |
|
523 | |||
524 | 2 | return $this->systemData; |
|
525 | } |
||
526 | |||
527 | /** |
||
528 | * Gets the name of the solrconfig.xml file installed and in use on the Solr |
||
529 | * server. |
||
530 | * |
||
531 | * @return string Name of the active solrconfig.xml |
||
532 | */ |
||
533 | 1 | public function getSolrconfigName() |
|
534 | { |
||
535 | 1 | if (is_null($this->solrconfigName)) { |
|
536 | 1 | $solrconfigXmlUrl = $this->_scheme . '://' |
|
537 | 1 | . $this->_host . ':' . $this->_port |
|
538 | 1 | . $this->_path . 'admin/file/?file=solrconfig.xml'; |
|
539 | |||
540 | 1 | $solrconfigXml = simplexml_load_file($solrconfigXmlUrl); |
|
541 | 1 | if ($solrconfigXml === false) { |
|
542 | throw new \InvalidArgumentException('No valid xml response from schema file: ' . $solrconfigXmlUrl); |
||
543 | } |
||
544 | 1 | $this->solrconfigName = (string)$solrconfigXml->attributes()->name; |
|
545 | 1 | } |
|
546 | |||
547 | 1 | return $this->solrconfigName; |
|
548 | } |
||
549 | |||
550 | /** |
||
551 | * Gets the Solr server's version number. |
||
552 | * |
||
553 | * @return string Solr version number |
||
554 | */ |
||
555 | 1 | public function getSolrServerVersion() |
|
556 | { |
||
557 | 1 | $systemInformation = $this->getSystemInformation(); |
|
558 | |||
559 | // don't know why $systemInformation->lucene->solr-spec-version won't work |
||
560 | 1 | $luceneInformation = (array)$systemInformation->lucene; |
|
561 | 1 | return $luceneInformation['solr-spec-version']; |
|
562 | } |
||
563 | |||
564 | /** |
||
565 | * Deletes all index documents of a certain type and does a commit |
||
566 | * afterwards. |
||
567 | * |
||
568 | * @param string $type The type of documents to delete, usually a table name. |
||
569 | * @param bool $commit Will commit immediately after deleting the documents if set, defaults to TRUE |
||
570 | */ |
||
571 | public function deleteByType($type, $commit = true) |
||
579 | |||
580 | /** |
||
581 | * Raw Delete Method. Takes a raw post body and sends it to the update service. Body should be |
||
582 | * a complete and well formed "delete" xml document |
||
583 | * |
||
584 | * @param string $rawPost Expected to be utf-8 encoded xml document |
||
585 | * @param float|int $timeout Maximum expected duration of the delete operation on the server (otherwise, will throw a communication exception) |
||
586 | * @return \Apache_Solr_Response |
||
587 | */ |
||
588 | 5 | public function delete($rawPost, $timeout = 3600) |
|
605 | |||
606 | /** |
||
607 | * Central method for making a post operation against this Solr Server |
||
608 | * |
||
609 | * @param string $url |
||
610 | * @param string $rawPost |
||
611 | * @param float|bool $timeout Read timeout in seconds |
||
612 | * @param string $contentType |
||
613 | * @return \Apache_Solr_Response |
||
614 | */ |
||
615 | 46 | protected function _sendRawPost( |
|
648 | |||
649 | /** |
||
650 | * Get currently configured synonyms |
||
651 | * |
||
652 | * @param string $baseWord If given a base word, retrieves the synonyms for that word only |
||
653 | * @return array |
||
654 | */ |
||
655 | public function getSynonyms($baseWord = '') |
||
678 | |||
679 | /** |
||
680 | * Add list of synonyms for base word to managed synonyms map |
||
681 | * |
||
682 | * @param $baseWord |
||
683 | * @param array $synonyms |
||
684 | * |
||
685 | * @return \Apache_Solr_Response |
||
686 | * |
||
687 | * @throws \Apache_Solr_InvalidArgumentException If $baseWord or $synonyms are empty |
||
688 | */ |
||
689 | public function addSynonym($baseWord, array $synonyms) |
||
699 | |||
700 | /** |
||
701 | * Remove a synonym from the synonyms map |
||
702 | * |
||
703 | * @param $baseWord |
||
704 | * @return \Apache_Solr_Response |
||
705 | * @throws \Apache_Solr_InvalidArgumentException |
||
706 | */ |
||
707 | public function deleteSynonym($baseWord) |
||
715 | |||
716 | /** |
||
717 | * Central method for making a HTTP DELETE operation against the Solr server |
||
718 | * |
||
719 | * @param $url |
||
720 | * @param bool|float $timeout Read timeout in seconds |
||
721 | * @return \Apache_Solr_Response |
||
722 | */ |
||
723 | protected function _sendRawDelete($url, $timeout = false) |
||
763 | |||
764 | /** |
||
765 | * Get currently configured stop words |
||
766 | * |
||
767 | * @return array |
||
768 | */ |
||
769 | public function getStopWords() |
||
782 | |||
783 | /** |
||
784 | * Adds stop words to the managed stop word list |
||
785 | * |
||
786 | * @param array|string $stopWords string for a single word, array for multiple words |
||
787 | * @return \Apache_Solr_Response |
||
788 | * @throws \Apache_Solr_InvalidArgumentException If $stopWords is empty |
||
789 | */ |
||
790 | public function addStopWords($stopWords) |
||
805 | |||
806 | /** |
||
807 | * Deletes a words from the managed stop word list |
||
808 | * |
||
809 | * @param string $stopWord stop word to delete |
||
810 | * @return \Apache_Solr_Response |
||
811 | * @throws \Apache_Solr_InvalidArgumentException If $stopWords is empty |
||
812 | */ |
||
813 | public function deleteStopWord($stopWord) |
||
821 | |||
822 | /** |
||
823 | * Reloads the current core |
||
824 | * |
||
825 | * @return \Apache_Solr_Response |
||
826 | */ |
||
827 | public function reloadCore() |
||
834 | |||
835 | /** |
||
836 | * initializes various URLs, including the Luke URL |
||
837 | * |
||
838 | * @return void |
||
839 | */ |
||
840 | 53 | protected function _initUrls() |
|
875 | |||
876 | /** |
||
877 | * Get the language map name for the text field. |
||
878 | * This is necessary to select the correct synonym map. |
||
879 | * |
||
880 | * @return string |
||
881 | */ |
||
882 | 53 | protected function getManagedLanguage() |
|
902 | |||
903 | /** |
||
904 | * Get the configured schema for the current core |
||
905 | * |
||
906 | * @return \stdClass |
||
907 | */ |
||
908 | 53 | protected function getSchema() |
|
913 | } |
||
914 |
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..