Completed
Push — master ( d2e043...09ed12 )
by Changwan
12:25
created

Manager::connect()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 12
nc 6
nop 2
dl 0
loc 17
ccs 11
cts 11
cp 1
crap 4
rs 9.2
c 0
b 0
f 0
1
<?php
2
namespace Wandu\Database;
3
4
use Wandu\Database\Connection\MysqlConnection;
5
use Wandu\Database\Contracts\ConnectionInterface;
6
use Wandu\Database\Exception\DriverNotFoundException;
7
8
class Manager
9
{
10
    /** @var \Wandu\Database\Contracts\ConnectionInterface[] */
11
    protected $connections = [];
12
13
    /**
14
     * @param array|\Wandu\Database\Configuration|\Wandu\Database\Contracts\ConnectionInterface $connection
15
     * @param string $name
16
     * @return \Wandu\Database\Contracts\ConnectionInterface
17
     */
18 17
    public function connect($connection, $name = 'default')
19
    {
20 17
        if (!$connection instanceof Configuration) {
21 17
            $connection = new Configuration($connection);
0 ignored issues
show
Bug introduced by
It seems like $connection defined by new \Wandu\Database\Configuration($connection) on line 21 can also be of type object<Wandu\Database\Co...ts\ConnectionInterface>; however, Wandu\Database\Configuration::__construct() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
22
        }
23 17
        if (!$connection instanceof ConnectionInterface) {
24 17
            switch ($connection->getDriver()) {
25 17
                case Configuration::DRIVER_MYSQL:
26 16
                    $connection = new MysqlConnection($connection);
27 16
                    break;
28
                default:
29 1
                    throw new DriverNotFoundException($connection->getDriver());
30
            }
31
        }
32 16
        $connection->connect();
33 16
        return $this->connections[$name] = $connection;
34
    }
35
36
    /**
37
     * @param string $name
38
     * @return \Wandu\Database\Contracts\ConnectionInterface
39
     */
40
    public function connection($name = 'default')
41
    {
42
        return isset($this->connections[$name]) ? $this->connections[$name] : null;
43
    }
44
}
45