ExtendedServer::__construct()   B
last analyzed

Complexity

Conditions 10
Paths 10

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 7.6666
c 0
b 0
f 0
cc 10
nc 10
nop 1

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Ez\DbLinker;
4
5
6
use Doctrine\DBAL\Connection;
7
use Doctrine\DBAL\DriverManager;
8
9
abstract class ExtendedServer
10
{
11
    protected $host;
12
    protected $port;
13
    protected $user;
14
    protected $password;
15
    protected $dbname;
16
    protected $extraParams = [];
17
    protected $driver;
18
    protected $driverOptions;
19
20
    /**
21
     * @var Connection
22
     */
23
    protected $connection;
24
25
    public function __construct(array $params)
26
    {
27
        foreach ($params as $key => $value) {
28
            switch ($key) {
29
                case 'host':
30
                case 'port':
31
                case 'user':
32
                case 'password':
33
                case 'dbname':
34
                case 'weight':
35
                case 'driver':
36
                case 'driverOptions':
37
                    $this->$key = $value;
38
                break;
39
            default:
40
                throw new \Exception($key . ' = ' . $value);
41
                $extraParams[$key] = $value;
0 ignored issues
show
Unused Code introduced by
$extraParams[$key] = $value; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
42
            }
43
        }
44
    }
45
46
    public function dbalConfig(): array {
47
        return [
48
            'host' => $this->host,
49
            'port' => $this->port,
50
            'user' => $this->user,
51
            'password' => $this->password,
52
            'dbname' => $this->dbname,
53
            'driver' => $this->driver,
54
            'driverOptions' => $this->driverOptions,
55
        ];
56
    }
57
58
    public function isConnected(): bool {
59
        return $this->connection !== null;
60
    }
61
62
    public function __toString()
63
    {
64
        return get_class($this) . ' - ' . $this->user . '@' . $this->host . ':' . $this->port . '/' . $this->dbname;
65
    }
66
67
    public function close() {
68
        if ($this->isConnected()) {
69
//            echo \get_class($this) . ' ' . $this . PHP_EOL;
70
            $this->connection->close();
71
        }
72
    }
73
74
    /**
75
     * @return \Doctrine\DBAL\Connection
76
     * @throws \Doctrine\DBAL\DBALException
77
     */
78
    public function connection(): Connection {
79
        if (!$this->connection) {
80
            $this->connection = DriverManager::getConnection($this->dbalConfig());
81
        }
82
        return $this->connection;
83
    }
84
85
    public function __destruct()
86
    {
87
        $this->close();
88
    }
89
}