Conditions | 20 |
Paths | 1274 |
Total Lines | 98 |
Code Lines | 54 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
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 |
||
52 | private function create() { |
||
53 | $isCluster = in_array('redis.cluster', $this->config->getKeys(), true); |
||
54 | $config = $isCluster |
||
55 | ? $this->config->getValue('redis.cluster', []) |
||
56 | : $this->config->getValue('redis', []); |
||
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']) && (string)$config['password'] !== '') { |
||
76 | if (isset($config['user']) && (string)$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 | if (!isset($config['seeds'])) { |
||
90 | throw new \Exception('Redis cluster config is missing the "seeds" attribute'); |
||
91 | } |
||
92 | |||
93 | // Support for older phpredis versions not supporting connectionParameters |
||
94 | if ($connectionParameters !== null) { |
||
95 | $this->instance = new \RedisCluster(null, $config['seeds'], $timeout, $readTimeout, true, $auth, $connectionParameters); |
||
|
|||
96 | } else { |
||
97 | $this->instance = new \RedisCluster(null, $config['seeds'], $timeout, $readTimeout, true, $auth); |
||
98 | } |
||
99 | |||
100 | if (isset($config['failover_mode'])) { |
||
101 | $this->instance->setOption(\RedisCluster::OPT_SLAVE_FAILOVER, $config['failover_mode']); |
||
102 | } |
||
103 | } else { |
||
104 | $this->instance = new \Redis(); |
||
105 | |||
106 | if (isset($config['host'])) { |
||
107 | $host = $config['host']; |
||
108 | } else { |
||
109 | $host = '127.0.0.1'; |
||
110 | } |
||
111 | |||
112 | if (isset($config['port'])) { |
||
113 | $port = $config['port']; |
||
114 | } elseif ($host[0] !== '/') { |
||
115 | $port = 6379; |
||
116 | } else { |
||
117 | $port = null; |
||
118 | } |
||
119 | |||
120 | $this->eventLogger->start('connect:redis', 'Connect to redis and send AUTH, SELECT'); |
||
121 | // Support for older phpredis versions not supporting connectionParameters |
||
122 | if ($connectionParameters !== null) { |
||
123 | // Non-clustered redis requires connection parameters to be wrapped inside `stream` |
||
124 | $connectionParameters = [ |
||
125 | 'stream' => $this->getSslContext($config) |
||
126 | ]; |
||
127 | /** |
||
128 | * even though the stubs and documentation don't want you to know this, |
||
129 | * pconnect does have the same $connectionParameters argument connect has |
||
130 | * |
||
131 | * https://github.com/phpredis/phpredis/blob/0264de1824b03fb2d0ad515b4d4ec019cd2dae70/redis.c#L710-L730 |
||
132 | * |
||
133 | * @psalm-suppress TooManyArguments |
||
134 | */ |
||
135 | $this->instance->pconnect($host, $port, $timeout, null, 0, $readTimeout, $connectionParameters); |
||
136 | } else { |
||
137 | $this->instance->pconnect($host, $port, $timeout, null, 0, $readTimeout); |
||
138 | } |
||
139 | |||
140 | |||
141 | // Auth if configured |
||
142 | if ($auth !== null) { |
||
143 | $this->instance->auth($auth); |
||
144 | } |
||
145 | |||
146 | if (isset($config['dbindex'])) { |
||
147 | $this->instance->select($config['dbindex']); |
||
148 | } |
||
149 | $this->eventLogger->end('connect:redis'); |
||
150 | } |
||
200 |
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.