ConnectionManager   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 55
rs 10
c 0
b 0
f 0
wmc 5
lcom 1
cbo 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 2
A getMasterConnection() 0 4 1
A getSlaveConnection() 0 4 2
1
<?php
2
3
namespace League\Database;
4
5
use League\Database\Driver\Engine;
6
use League\Database\Driver\TransactionPDO;
7
8
final class ConnectionManager
9
{
10
    /**
11
     * Master database connection
12
     *
13
     * @var TransactionPDO
14
     */
15
    private $masterConnection;
16
17
    /**
18
     * Slave database connection
19
     *
20
     * @var TransactionPDO|null
21
     */
22
    private $slaveConnection;
23
24
    /**
25
     * PDOManager constructor
26
     * Initialize connection to Database for master and slave (if applicable)
27
     *
28
     * @param string $name      Unique name for connection
29
     * @param array  $config    Configuration settings for database connection
30
     * @param array  $options   Some specific options
31
     */
32
    public function __construct(string $name, array $config, array $options = [])
33
    {
34
        $masterConf = $config['master'] ?? $config;
35
        $slaveConf = $config['slave'] ?? [];
36
37
        $this->masterConnection = Engine::setConnection($name.'_master', $masterConf, $options);
0 ignored issues
show
Documentation Bug introduced by
It seems like \League\Database\Driver\... $masterConf, $options) can also be of type false. However, the property $masterConnection is declared as type object<League\Database\Driver\TransactionPDO>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
38
        $this->slaveConnection = $slaveConf
39
            ? Engine::setConnection($name.'_slave', $slaveConf, $options)
40
            : null;
41
    }
42
43
    /**
44
     * Returns master database handler
45
     *
46
     * @return TransactionPDO
47
     */
48
    public function getMasterConnection() : TransactionPDO
49
    {
50
        return $this->masterConnection;
51
    }
52
53
    /**
54
     * Returns slave database handler
55
     *
56
     * @return TransactionPDO
57
     */
58
    public function getSlaveConnection() : TransactionPDO
59
    {
60
        return $this->slaveConnection ?: $this->masterConnection;
61
    }
62
}
63