Passed
Push — env_slave ( 983dee...2fb48c )
by Akihito
01:46
created

Connection   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Importance

Changes 5
Bugs 0 Features 2
Metric Value
eloc 19
c 5
b 0
f 2
dl 0
loc 47
rs 10
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 1
A isSame() 0 5 3
A __invoke() 0 9 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Ray\AuraSqlModule;
6
7
use Aura\Sql\ExtendedPdo;
8
9
class Connection
10
{
11
    private string $dsn;
12
    private string $username;
13
    private string $password;
14
15
    /** @var array<string> */
16
    private array $options;
17
18
    /** @var array<string> */
19
    private array $queries;
20
    private ?ExtendedPdo $pdo = null;
21
22
    /**
23
     * @phpstan-param array<string> $options
24
     * @phpstan-param array<string> $queries
25
     */
26
    public function __construct(
27
        string $dsn,
28
        string $username = '',
29
        string $password = '',
30
        array $options = [],
31
        array $queries = []
32
    ) {
33
        $this->dsn = $dsn;
34
        $this->username = $username;
35
        $this->password = $password;
36
        $this->options = $options;
37
        $this->queries = $queries;
38
    }
39
40
    public function __invoke(): ExtendedPdo
41
    {
42
        if ($this->pdo instanceof ExtendedPdo) {
43
            return $this->pdo;
44
        }
45
46
        $this->pdo = new ExtendedPdo($this->dsn, $this->username, $this->password, $this->options, $this->queries);
47
48
        return $this->pdo;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->pdo returns the type null which is incompatible with the type-hinted return Aura\Sql\ExtendedPdo.
Loading history...
49
    }
50
51
    public function isSame(string $dsn, string $username, string $password): bool
52
    {
53
        return $dsn === $this->dsn &&
54
            $username === $this->username &&
55
            $password === $this->password;
56
    }
57
}
58