Passed
Pull Request — 2.x (#83)
by Maxim
36:42 queued 16:48
created

DsnConnectionConfig::getSourceString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 4
ccs 0
cts 2
cp 0
crap 2
rs 10
c 1
b 0
f 0
1
<?php
2
3
/**
4
 * This file is part of Cycle Database package.
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
declare(strict_types=1);
11
12
namespace Cycle\Database\Config\MySQL;
13
14
use Cycle\Database\Config\ProvidesSourceString;
15
use Cycle\Database\Config\Support\DataSourceName;
16
17
class DsnConnectionConfig extends ConnectionConfig implements ProvidesSourceString
18
{
19
    /**
20
     * @var string|null
21
     */
22
    private ?string $database = null;
23
24
    /**
25
     * @var non-empty-string
0 ignored issues
show
Documentation Bug introduced by
The doc comment non-empty-string at position 0 could not be parsed: Unknown type name 'non-empty-string' at position 0 in non-empty-string.
Loading history...
26
     * @psalm-allow-private-mutation
27
     */
28
    public string $dsn;
29
30
    /**
31
     * @param non-empty-string|\Stringable $dsn
0 ignored issues
show
Documentation Bug introduced by
The doc comment non-empty-string|\Stringable at position 0 could not be parsed: Unknown type name 'non-empty-string' at position 0 in non-empty-string|\Stringable.
Loading history...
32
     * @param non-empty-string|null $user
33
     * @param non-empty-string|null $password
34
     * @param array $options
35
     */
36
    public function __construct(
37
        string|\Stringable $dsn,
38
        ?string $user = null,
39
        ?string $password = null,
40
        array $options = []
41
    ) {
42
        parent::__construct($user, $password, $options);
43
44
        /** @psalm-suppress ArgumentTypeCoercion */
45
        $this->dsn = DataSourceName::normalize((string)$dsn, $this->getName());
46
    }
47
48
    /**
49
     * {@inheritDoc}
50
     */
51
    public function getSourceString(): string
52
    {
53
        /** @psalm-suppress ArgumentTypeCoercion */
54
        return $this->database ??= DataSourceName::read($this->getDsn(), 'dbname') ?? '*';
55
    }
56
57
    /**
58
     * {@inheritDoc}
59
     */
60
    public function getDsn(): string
61
    {
62
        return $this->dsn;
63
    }
64
65
    public static function __set_state(array $state): self
66
    {
67
        return new self(...$state);
0 ignored issues
show
Bug introduced by
$state is expanded, but the parameter $dsn of Cycle\Database\Config\My...onConfig::__construct() does not expect variable arguments. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

67
        return new self(/** @scrutinizer ignore-type */ ...$state);
Loading history...
68
    }
69
}
70