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

src/Driver/MasterSlavesDriver.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
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