| Conditions | 20 |
| Paths | 1252 |
| Total Lines | 87 |
| Code Lines | 52 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 48 | private function create() { |
||
| 49 | $isCluster = in_array('redis.cluster', $this->config->getKeys()); |
||
| 50 | $config = $isCluster |
||
| 51 | ? $this->config->getValue('redis.cluster', []) |
||
| 52 | : $this->config->getValue('redis', []); |
||
| 53 | |||
| 54 | if (empty($config)) { |
||
| 55 | throw new \Exception('Redis config is empty'); |
||
| 56 | } |
||
| 57 | |||
| 58 | if ($isCluster && !class_exists('RedisCluster')) { |
||
| 59 | throw new \Exception('Redis Cluster support is not available'); |
||
| 60 | } |
||
| 61 | |||
| 62 | if (isset($config['timeout'])) { |
||
| 63 | $timeout = $config['timeout']; |
||
| 64 | } else { |
||
| 65 | $timeout = 0.0; |
||
| 66 | } |
||
| 67 | |||
| 68 | if (isset($config['read_timeout'])) { |
||
| 69 | $readTimeout = $config['read_timeout']; |
||
| 70 | } else { |
||
| 71 | $readTimeout = 0.0; |
||
| 72 | } |
||
| 73 | |||
| 74 | $auth = null; |
||
| 75 | if (isset($config['password']) && $config['password'] !== '') { |
||
| 76 | if (isset($config['user']) && $config['user'] !== '') { |
||
| 77 | $auth = [$config['user'], $config['password']]; |
||
| 78 | } else { |
||
| 79 | $auth = $config['password']; |
||
| 80 | } |
||
| 81 | } |
||
| 82 | |||
| 83 | // # TLS support |
||
| 84 | // # https://github.com/phpredis/phpredis/issues/1600 |
||
| 85 | $connectionParameters = $this->getSslContext($config); |
||
| 86 | |||
| 87 | // cluster config |
||
| 88 | if ($isCluster) { |
||
| 89 | // Support for older phpredis versions not supporting connectionParameters |
||
| 90 | if ($connectionParameters !== null) { |
||
| 91 | $this->instance = new \RedisCluster(null, $config['seeds'], $timeout, $readTimeout, false, $auth, $connectionParameters); |
||
|
|
|||
| 92 | } else { |
||
| 93 | $this->instance = new \RedisCluster(null, $config['seeds'], $timeout, $readTimeout, false, $auth); |
||
| 94 | } |
||
| 95 | |||
| 96 | if (isset($config['failover_mode'])) { |
||
| 97 | $this->instance->setOption(\RedisCluster::OPT_SLAVE_FAILOVER, $config['failover_mode']); |
||
| 98 | } |
||
| 99 | } else { |
||
| 100 | $this->instance = new \Redis(); |
||
| 101 | |||
| 102 | if (isset($config['host'])) { |
||
| 103 | $host = $config['host']; |
||
| 104 | } else { |
||
| 105 | $host = '127.0.0.1'; |
||
| 106 | } |
||
| 107 | |||
| 108 | if (isset($config['port'])) { |
||
| 109 | $port = $config['port']; |
||
| 110 | } elseif ($host[0] !== '/') { |
||
| 111 | $port = 6379; |
||
| 112 | } else { |
||
| 113 | $port = null; |
||
| 114 | } |
||
| 115 | |||
| 116 | // Support for older phpredis versions not supporting connectionParameters |
||
| 117 | if ($connectionParameters !== null) { |
||
| 118 | // Non-clustered redis requires connection parameters to be wrapped inside `stream` |
||
| 119 | $connectionParameters = [ |
||
| 120 | 'stream' => $this->getSslContext($config) |
||
| 121 | ]; |
||
| 122 | $this->instance->connect($host, $port, $timeout, null, 0, $readTimeout, $connectionParameters); |
||
| 123 | } else { |
||
| 124 | $this->instance->connect($host, $port, $timeout, null, 0, $readTimeout); |
||
| 125 | } |
||
| 126 | |||
| 127 | |||
| 128 | // Auth if configured |
||
| 129 | if ($auth !== null) { |
||
| 130 | $this->instance->auth($auth); |
||
| 131 | } |
||
| 132 | |||
| 133 | if (isset($config['dbindex'])) { |
||
| 134 | $this->instance->select($config['dbindex']); |
||
| 135 | } |
||
| 186 |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.