Complex classes like AbstractSolrService 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 AbstractSolrService, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
33 | abstract class AbstractSolrService extends \Apache_Solr_Service { |
||
34 | |||
35 | const SCHEME_HTTP = 'http'; |
||
36 | const SCHEME_HTTPS = 'https'; |
||
37 | |||
38 | /** |
||
39 | * @var array |
||
40 | */ |
||
41 | protected static $pingCache = []; |
||
42 | |||
43 | /** |
||
44 | * Server connection scheme. http or https. |
||
45 | * |
||
46 | * @var string |
||
47 | */ |
||
48 | protected $_scheme = self::SCHEME_HTTP; |
||
49 | |||
50 | /** |
||
51 | * @var TypoScriptConfiguration |
||
52 | */ |
||
53 | protected $configuration; |
||
54 | |||
55 | /** |
||
56 | * @var \ApacheSolrForTypo3\Solr\System\Logging\SolrLogManager |
||
57 | */ |
||
58 | protected $logger = null; |
||
59 | |||
60 | /** |
||
61 | * SolrAdminService constructor. |
||
62 | * @param string $host |
||
63 | * @param string $port |
||
64 | * @param string $path |
||
65 | * @param string $scheme |
||
66 | * @param TypoScriptConfiguration $typoScriptConfiguration |
||
67 | * @param SolrLogManager $logManager |
||
68 | */ |
||
69 | public function __construct($host = '', $port = '8983', $path = '/solr/', $scheme = 'http', $typoScriptConfiguration = null, $logManager = null) |
||
78 | |||
79 | /** |
||
80 | * Initializes the timeout from TypoScript when configuration is present. |
||
81 | * |
||
82 | * @return void |
||
83 | */ |
||
84 | protected function initializeTimeoutFromConfiguration() |
||
91 | |||
92 | /** |
||
93 | * Creates a string representation of the Solr connection. Specifically |
||
94 | * will return the Solr URL. |
||
95 | * |
||
96 | * @return string The Solr URL. |
||
97 | */ |
||
98 | public function __toString() |
||
102 | |||
103 | /** |
||
104 | * Returns the set scheme |
||
105 | * |
||
106 | * @return string |
||
107 | */ |
||
108 | public function getScheme() |
||
112 | |||
113 | /** |
||
114 | * Set the scheme used. If empty will fallback to constants |
||
115 | * |
||
116 | * @param string $scheme Either http or https |
||
117 | * @throws \UnexpectedValueException |
||
118 | */ |
||
119 | public function setScheme($scheme) |
||
138 | |||
139 | /** |
||
140 | * Return a valid http URL given this server's scheme, host, port, and path |
||
141 | * and a provided servlet name. |
||
142 | * |
||
143 | * @param string $servlet Servlet name |
||
144 | * @param array $params Additional URL parameters to attach to the end of the URL |
||
145 | * @return string Servlet URL |
||
146 | */ |
||
147 | protected function _constructUrl($servlet, $params = []) |
||
161 | |||
162 | /** |
||
163 | * Central method for making a get operation against this Solr Server |
||
164 | * |
||
165 | * @param string $url |
||
166 | * @param float|bool $timeout Read timeout in seconds |
||
167 | * @return \Apache_Solr_Response |
||
168 | */ |
||
169 | protected function _sendRawGet($url, $timeout = false) |
||
187 | |||
188 | /** |
||
189 | * Central method for making a HTTP DELETE operation against the Solr server |
||
190 | * |
||
191 | * @param string $url |
||
192 | * @param bool|float $timeout Read timeout in seconds |
||
193 | * @return \Apache_Solr_Response |
||
194 | */ |
||
195 | protected function _sendRawDelete($url, $timeout = false) |
||
219 | |||
220 | /** |
||
221 | * Central method for making a post operation against this Solr Server |
||
222 | * |
||
223 | * @param string $url |
||
224 | * @param string $rawPost |
||
225 | * @param float|bool $timeout Read timeout in seconds |
||
226 | * @param string $contentType |
||
227 | * @return \Apache_Solr_Response |
||
228 | */ |
||
229 | protected function _sendRawPost($url, $rawPost, $timeout = false, $contentType = 'text/xml; charset=UTF-8') |
||
247 | |||
248 | |||
249 | /** |
||
250 | * Build the log data and writes the message to the log |
||
251 | * |
||
252 | * @param integer $logSeverity |
||
253 | * @param string $message |
||
254 | * @param string $url |
||
255 | * @param \Apache_Solr_Response $solrResponse |
||
256 | * @param \Exception $exception |
||
257 | * @param string $contentSend |
||
258 | */ |
||
259 | protected function writeLog($logSeverity, $message, $url, $solrResponse, $exception = null, $contentSend = '') |
||
264 | |||
265 | /** |
||
266 | * Parses the solr information to build data for the logger. |
||
267 | * |
||
268 | * @param \Apache_Solr_Response $solrResponse |
||
269 | * @param \Exception $e |
||
270 | * @param string $url |
||
271 | * @param string $contentSend |
||
272 | * @return array |
||
273 | */ |
||
274 | protected function buildLogDataFromResponse(\Apache_Solr_Response $solrResponse, \Exception $e = null, $url = '', $contentSend = '') |
||
292 | |||
293 | |||
294 | /** |
||
295 | * Returns the core name from the configured path without the core name. |
||
296 | * |
||
297 | * @return string |
||
298 | */ |
||
299 | public function getCoreBasePath() |
||
305 | |||
306 | /** |
||
307 | * Returns the core name from the configured path. |
||
308 | * |
||
309 | * @return string |
||
310 | */ |
||
311 | public function getCoreName() |
||
316 | |||
317 | /** |
||
318 | * Call the /admin/ping servlet, can be used to quickly tell if a connection to the |
||
319 | * server is available. |
||
320 | * |
||
321 | * Simply overrides the SolrPhpClient implementation, changing ping from a |
||
322 | * HEAD to a GET request, see http://forge.typo3.org/issues/44167 |
||
323 | * |
||
324 | * Also does not report the time, see https://forge.typo3.org/issues/64551 |
||
325 | * |
||
326 | * @param int $timeout maximum time to wait for ping in seconds, -1 for unlimited (default is 2) |
||
327 | * @param boolean $useCache indicates if the ping result should be cached in the instance or not |
||
328 | * @return bool TRUE if Solr can be reached, FALSE if not |
||
329 | */ |
||
330 | public function ping($timeout = 2, $useCache = true) |
||
335 | |||
336 | /** |
||
337 | * Call the /admin/ping servlet, can be used to get the runtime of a ping request. |
||
338 | * |
||
339 | * @param int $timeout maximum time to wait for ping in seconds, -1 for unlimited (default is 2) |
||
340 | * @param boolean $useCache indicates if the ping result should be cached in the instance or not |
||
341 | * @return double runtime in milliseconds |
||
342 | * @throws \ApacheSolrForTypo3\Solr\PingFailedException |
||
343 | */ |
||
344 | public function getPingRoundTripRuntime($timeout = 2, $useCache = true) |
||
360 | |||
361 | /** |
||
362 | * Make a request to a servlet (a path) that's not a standard path. |
||
363 | * |
||
364 | * @param string $servlet Path to be added to the base Solr path. |
||
365 | * @param array $parameters Optional, additional request parameters when constructing the URL. |
||
366 | * @param string $method HTTP method to use, defaults to GET. |
||
367 | * @param array $requestHeaders Key value pairs of header names and values. Should include 'Content-Type' for POST and PUT. |
||
368 | * @param string $rawPost Must be an empty string unless method is POST or PUT. |
||
369 | * @param float|bool $timeout Read timeout in seconds, defaults to FALSE. |
||
370 | * @return \Apache_Solr_Response Response object |
||
371 | * @throws \Apache_Solr_HttpTransportException if returned HTTP status is other than 200 |
||
372 | */ |
||
373 | public function requestServlet($servlet, $parameters = [], $method = 'GET', $requestHeaders = [], $rawPost = '', $timeout = false) |
||
388 | |||
389 | /** |
||
390 | * Decides which transport method to used, depending on the request method and retrieves the response. |
||
391 | * |
||
392 | * @param string $url |
||
393 | * @param string $method |
||
394 | * @param array $requestHeaders |
||
395 | * @param string $rawPost |
||
396 | * @param float|bool $timeout |
||
397 | * @return \Apache_Solr_HttpTransport_Response |
||
398 | */ |
||
399 | protected function getResponseFromTransport($url, $method, $requestHeaders, $rawPost, $timeout) |
||
413 | |||
414 | /** |
||
415 | * Performs a ping request and returns the result. |
||
416 | * |
||
417 | * @param int $timeout |
||
418 | * @param boolean $useCache indicates if the ping result should be cached in the instance or not |
||
419 | * @return \Apache_Solr_HttpTransport_Response |
||
420 | */ |
||
421 | protected function performPingRequest($timeout = 2, $useCache = true) |
||
436 | |||
437 | /** |
||
438 | * Returns the current time in milliseconds. |
||
439 | * |
||
440 | * @return double |
||
441 | */ |
||
442 | protected function getMilliseconds() |
||
446 | } |
This check looks at variables that have been passed in as parameters and are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.