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
|
|||
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 |
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:
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
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: