Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like RedisConnectionPool 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 RedisConnectionPool, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
41 | class RedisConnectionPool implements LoggerAwareInterface { |
||
42 | /** @var string Connection timeout in seconds */ |
||
43 | protected $connectTimeout; |
||
44 | /** @var string Read timeout in seconds */ |
||
45 | protected $readTimeout; |
||
46 | /** @var string Plaintext auth password */ |
||
47 | protected $password; |
||
48 | /** @var bool Whether connections persist */ |
||
49 | protected $persistent; |
||
50 | /** @var int Serializer to use (Redis::SERIALIZER_*) */ |
||
51 | protected $serializer; |
||
52 | |||
53 | /** @var int Current idle pool size */ |
||
54 | protected $idlePoolSize = 0; |
||
55 | |||
56 | /** @var array (server name => ((connection info array),...) */ |
||
57 | protected $connections = []; |
||
58 | /** @var array (server name => UNIX timestamp) */ |
||
59 | protected $downServers = []; |
||
60 | |||
61 | /** @var array (pool ID => RedisConnectionPool) */ |
||
62 | protected static $instances = []; |
||
63 | |||
64 | /** integer; seconds to cache servers as "down". */ |
||
65 | const SERVER_DOWN_TTL = 30; |
||
66 | |||
67 | /** |
||
68 | * @var LoggerInterface |
||
69 | */ |
||
70 | protected $logger; |
||
71 | |||
72 | /** |
||
73 | * @param array $options |
||
74 | * @throws Exception |
||
75 | */ |
||
76 | protected function __construct( array $options ) { |
||
99 | |||
100 | /** |
||
101 | * @param LoggerInterface $logger |
||
102 | * @return null |
||
103 | */ |
||
104 | public function setLogger( LoggerInterface $logger ) { |
||
107 | |||
108 | /** |
||
109 | * @param array $options |
||
110 | * @return array |
||
111 | */ |
||
112 | protected static function applyDefaultConfig( array $options ) { |
||
128 | |||
129 | /** |
||
130 | * @param array $options |
||
131 | * $options include: |
||
132 | * - connectTimeout : The timeout for new connections, in seconds. |
||
133 | * Optional, default is 1 second. |
||
134 | * - readTimeout : The timeout for operation reads, in seconds. |
||
135 | * Commands like BLPOP can fail if told to wait longer than this. |
||
136 | * Optional, default is 1 second. |
||
137 | * - persistent : Set this to true to allow connections to persist across |
||
138 | * multiple web requests. False by default. |
||
139 | * - password : The authentication password, will be sent to Redis in clear text. |
||
140 | * Optional, if it is unspecified, no AUTH command will be sent. |
||
141 | * - serializer : Set to "php", "igbinary", or "none". Default is "php". |
||
142 | * @return RedisConnectionPool |
||
143 | */ |
||
144 | public static function singleton( array $options ) { |
||
156 | |||
157 | /** |
||
158 | * Destroy all singleton() instances |
||
159 | * @since 1.27 |
||
160 | */ |
||
161 | public static function destroySingletons() { |
||
164 | |||
165 | /** |
||
166 | * Get a connection to a redis server. Based on code in RedisBagOStuff.php. |
||
167 | * |
||
168 | * @param string $server A hostname/port combination or the absolute path of a UNIX socket. |
||
169 | * If a hostname is specified but no port, port 6379 will be used. |
||
170 | * @param LoggerInterface $logger PSR-3 logger intance. [optional] |
||
171 | * @return RedisConnRef|bool Returns false on failure |
||
172 | * @throws MWException |
||
173 | */ |
||
174 | public function getConnection( $server, LoggerInterface $logger = null ) { |
||
278 | |||
279 | /** |
||
280 | * Mark a connection to a server as free to return to the pool |
||
281 | * |
||
282 | * @param string $server |
||
283 | * @param Redis $conn |
||
284 | * @return bool |
||
285 | */ |
||
286 | public function freeConnection( $server, Redis $conn ) { |
||
301 | |||
302 | /** |
||
303 | * Close any extra idle connections if there are more than the limit |
||
304 | */ |
||
305 | protected function closeExcessIdleConections() { |
||
321 | |||
322 | /** |
||
323 | * The redis extension throws an exception in response to various read, write |
||
324 | * and protocol errors. Sometimes it also closes the connection, sometimes |
||
325 | * not. The safest response for us is to explicitly destroy the connection |
||
326 | * object and let it be reopened during the next request. |
||
327 | * |
||
328 | * @param string $server |
||
329 | * @param RedisConnRef $cref |
||
330 | * @param RedisException $e |
||
331 | * @deprecated since 1.23 |
||
332 | */ |
||
333 | public function handleException( $server, RedisConnRef $cref, RedisException $e ) { |
||
336 | |||
337 | /** |
||
338 | * The redis extension throws an exception in response to various read, write |
||
339 | * and protocol errors. Sometimes it also closes the connection, sometimes |
||
340 | * not. The safest response for us is to explicitly destroy the connection |
||
341 | * object and let it be reopened during the next request. |
||
342 | * |
||
343 | * @param RedisConnRef $cref |
||
344 | * @param RedisException $e |
||
345 | */ |
||
346 | public function handleError( RedisConnRef $cref, RedisException $e ) { |
||
363 | |||
364 | /** |
||
365 | * Re-send an AUTH request to the redis server (useful after disconnects). |
||
366 | * |
||
367 | * This works around an upstream bug in phpredis. phpredis hides disconnects by transparently |
||
368 | * reconnecting, but it neglects to re-authenticate the new connection. To the user of the |
||
369 | * phpredis client API this manifests as a seemingly random tendency of connections to lose |
||
370 | * their authentication status. |
||
371 | * |
||
372 | * This method is for internal use only. |
||
373 | * |
||
374 | * @see https://github.com/nicolasff/phpredis/issues/403 |
||
375 | * |
||
376 | * @param string $server |
||
377 | * @param Redis $conn |
||
378 | * @return bool Success |
||
379 | */ |
||
380 | public function reauthenticateConnection( $server, Redis $conn ) { |
||
394 | |||
395 | /** |
||
396 | * Adjust or reset the connection handle read timeout value |
||
397 | * |
||
398 | * @param Redis $conn |
||
399 | * @param int $timeout Optional |
||
400 | */ |
||
401 | public function resetTimeout( Redis $conn, $timeout = null ) { |
||
404 | |||
405 | /** |
||
406 | * Make sure connections are closed for sanity |
||
407 | */ |
||
408 | function __destruct() { |
||
417 | } |
||
418 |
Scrutinizer analyzes your
composer.json
/composer.lock
file if available to determine the classes, and functions that are defined by your dependencies.It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.