| Conditions | 13 |
| Paths | 26 |
| Total Lines | 36 |
| Code Lines | 20 |
| 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 |
||
| 30 | public function getNativeRedisConnection(?string $connection_name = null): array |
||
| 31 | { |
||
| 32 | $isCluster = config('database.redis.clusters.' . ($connection_name ?? 'default')) !== null ? true : false; |
||
| 33 | if ($isCluster) { |
||
| 34 | $config = config('database.redis.clusters.' . ($connection_name ?? 'default')); |
||
| 35 | $url = $config[0]['host'] . ':' . $config[0]['port']; |
||
| 36 | $nativeRedisCluster = new \RedisCluster( |
||
| 37 | null, // Nome del cluster (può essere null) |
||
| 38 | [$url], // Nodo master |
||
| 39 | -1, // Timeout connessione |
||
| 40 | -1, // Timeout lettura |
||
| 41 | true, // Persistente |
||
| 42 | ($config[0]['password'] !== null && $config[0]['password'] !== '' ? $config[0]['password'] : null) // Password se necessaria |
||
| 43 | ); |
||
| 44 | |||
| 45 | // Nel cluster c'è sempre un unico databse |
||
| 46 | return ['connection' => $nativeRedisCluster, 'database' => 0]; |
||
| 47 | } |
||
| 48 | // Crea una nuova connessione nativa Redis |
||
| 49 | $nativeRedis = new \Redis(); |
||
| 50 | // Connessione al server Redis (no cluster) |
||
| 51 | $config = config('database.redis.' . ($connection_name ?? 'default')); |
||
| 52 | $nativeRedis->connect($config['host'], $config['port']); |
||
| 53 | |||
| 54 | // Autenticazione con username e password (se configurati) |
||
| 55 | if ($config['username'] !== null && $config['password'] !== null && $config['password'] !== '' && $config['username'] !== '') { |
||
| 56 | $nativeRedis->auth([$config['username'], $config['password']]); |
||
|
|
|||
| 57 | } elseif ($config['password'] !== null && $config['password'] !== '') { |
||
| 58 | $nativeRedis->auth($config['password']); // Per versioni Redis senza ACL |
||
| 59 | } |
||
| 60 | |||
| 61 | // Seleziono il database corretto |
||
| 62 | $database = ($config['database'] !== null && $config['database'] !== '') ? (int) $config['database'] : 0; |
||
| 63 | $nativeRedis->select($database); |
||
| 64 | |||
| 65 | return ['connection' => $nativeRedis, 'database' => $database]; |
||
| 66 | } |
||
| 74 |