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

TcpConnectionConfig   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
dl 0
loc 55
ccs 12
cts 12
cp 1
rs 10
c 1
b 0
f 0
wmc 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getSourceString() 0 3 1
A __construct() 0 10 1
A getDsn() 0 10 1
A __set_state() 0 3 1
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
16
class TcpConnectionConfig extends ConnectionConfig implements ProvidesSourceString
17
{
18
    /**
19
     * @param non-empty-string $host
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...
20
     * @param positive-int $port
21
     * @param non-empty-string $database
22
     * @param non-empty-string|null $charset
23
     * @param non-empty-string|null $user
24
     * @param non-empty-string|null $password
25
     * @param array<int, non-empty-string|non-empty-string> $options
26
     */
27 4
    public function __construct(
28
        public string $database,
29
        public string $host = 'localhost',
30
        public int $port = 3307,
31
        public ?string $charset = null,
32
        ?string $user = null,
33
        ?string $password = null,
34
        array $options = [],
35
    ) {
36 4
        parent::__construct($user, $password, $options);
37 4
    }
38
39
    /**
40
     * {@inheritDoc}
41
     */
42 484
    public function getSourceString(): string
43
    {
44 484
        return $this->database;
45
    }
46
47
    /**
48
     * Returns the MySQL-specific PDO DataSourceName with connection URI,
49
     * that looks like:
50
     * <code>
51
     *  mysql:host=localhost;port=3307;dbname=dbname
52
     * </code>
53
     *
54
     * {@inheritDoc}
55
     */
56 8
    public function getDsn(): string
57
    {
58 8
        $config = [
59 8
            'host' => $this->host,
60 8
            'port' => $this->port,
61 8
            'dbname' => $this->database,
62 8
            'charset' => $this->charset,
63
        ];
64
65 8
        return \sprintf('%s:%s', $this->getName(), $this->dsn($config));
66
    }
67
68
    public static function __set_state(array $state): self
69
    {
70
        return new self(...$state);
0 ignored issues
show
Bug introduced by
$state is expanded, but the parameter $database 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

70
        return new self(/** @scrutinizer ignore-type */ ...$state);
Loading history...
71
    }
72
}
73