Failed Conditions
Pull Request — master (#38)
by Mathieu
02:09
created

MasterSlavesDriver::connect()   B

Complexity

Conditions 5
Paths 16

Size

Total Lines 24
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 8.5125
c 0
b 0
f 0
cc 5
eloc 15
nc 16
nop 4
1
<?php
2
3
namespace Ez\DbLinker\Driver;
4
5
use Ez\DbLinker\Driver\Connection\MasterSlavesConnection;
6
7
trait MasterSlavesDriver
8
{
9
    use WrapperDriver;
10
11
    /**
12
     * Attempts to create a connection with the database.
13
     *
14
     * @param array       $params        All connection parameters passed by the user.
15
     * @param string|null $username      The username to use when connecting.
16
     * @param string|null $password      The password to use when connecting.
17
     * @param array       $driverOptions The driver options to use when connecting.
18
     *
19
     * @return \Doctrine\DBAL\Driver\Connection The database connection.
20
     */
21
    public function connect(Array $params, $username = null, $password = null, Array $driverOptions = []) {
22
        $cache = array_key_exists("config_cache", $driverOptions) ? $driverOptions["config_cache"] : null;
23
24
        $configParams = array_intersect_key($params, array_flip(["master", "slaves"]));
25
        $key = "dblinker.master-slave-config.".hash("sha256", serialize($configParams));
26
27
        $config = null;
28
        if ($cache !== null) {
29
            assert($driverOptions["config_cache"] instanceof \Psr\Cache\CacheItemPoolInterface);
30
            $cacheItem = $cache->getItem($key);
31
            $config = $cacheItem->get();
32
        }
33
34
        if ($config === null) {
35
            $config = $this->config($configParams, $driverOptions);
36
        }
37
38
        if ($cache !== null) {
39
            $cacheItem->set($config);
0 ignored issues
show
Bug introduced by
The variable $cacheItem does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
40
            $cache->save($cacheItem);
41
        }
42
43
        return new MasterSlavesConnection($config["master"], $config["slaves"]);
44
    }
45
46
    private function config(array $params, array $driverOptions)
47
    {
48
        $driverKey = array_key_exists('driverClass', $params['master']) ? 'driverClass' : 'driver';
49
50
        $driverValue = $params['master'][$driverKey];
51
52
        $slaves = [];
53
        foreach ($params['slaves'] as $slaveParams) {
54
            $slaveParams[$driverKey] = $driverValue;
55
            $slaves[] = $slaveParams;
56
        }
57
58
        $config = [
59
            "master" => $params["master"],
60
            "slaves" => $slaves,
61
        ];
62
63
        if (!array_key_exists("config_transform", $driverOptions)) {
64
            return $config;
65
        }
66
67
        assert(is_callable($driverOptions["config_transform"]));
68
        return $driverOptions["config_transform"]($config);
69
    }
70
}
71