| Conditions | 15 | 
| Paths | 49 | 
| Total Lines | 34 | 
| Code Lines | 17 | 
| Lines | 0 | 
| Ratio | 0 % | 
| Changes | 4 | ||
| Bugs | 2 | 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 | ||
| 25 | public function getNativeRedisConnection(?string $connection_name = null, ?string $host = null, ?string $port = null): ?array | ||
| 26 |     { | ||
| 27 | // Crea una nuova connessione nativa Redis | ||
| 28 |         $config = config('database.redis.clusters.' . ($connection_name ?? config('supercache.connection'))); | ||
| 29 |         if ($config !== null && ($host === null || $port === null)) { | ||
| 30 | // Sono nel caso del cluster, host e port sono obbligatori in quanto le connessioni vanno stabilite per ogni nodo del cluster | ||
| 31 |             throw new \RuntimeException('Host e port non possono essere null per le connessioni in cluster'); | ||
| 32 | } | ||
| 33 |         if ($config === null) { | ||
| 34 | // Sono nel caso di una connessione standalone | ||
| 35 |             $config = config('database.redis.' . ($connection_name ?? config('supercache.connection'))); | ||
| 36 | } | ||
| 37 | $nativeRedis = new \Redis(); | ||
| 38 |         if ($host !== null && $port !== null) { | ||
| 39 | // Se ho host e port (caso del cluster) uso questi | ||
| 40 | $nativeRedis->connect($host, $port); | ||
|  | |||
| 41 |         } else { | ||
| 42 | // Altrimenti utilizzo host e port dalla configurazione della connessione standalone | ||
| 43 | $nativeRedis->connect($config['host'], $config['port']); | ||
| 44 | } | ||
| 45 | |||
| 46 | // Autenticazione con username e password (se configurati) | ||
| 47 |         if (array_key_exists('username', $config) && $config['username'] !== '' && array_key_exists('password', $config) && $config['password'] !== '') { | ||
| 48 | $nativeRedis->auth([$config['username'], $config['password']]); | ||
| 49 |         } elseif (array_key_exists('password', $config) && $config['password'] !== '') { | ||
| 50 | $nativeRedis->auth($config['password']); // Per versioni Redis senza ACL | ||
| 51 | } | ||
| 52 | |||
| 53 | // Seleziono il database corretto (Per il cluster è sempre 0) | ||
| 54 |         $database = (array_key_exists('database', $config) && $config['database'] !== '') ? (int) $config['database'] : 0; | ||
| 55 | $nativeRedis->select($database); | ||
| 56 | //$nativeRedis->setOption(\Redis::OPT_READ_TIMEOUT, -1); | ||
| 57 | |||
| 58 | return ['connection' => $nativeRedis, 'database' => $database]; | ||
| 59 | } | ||
| 67 |