Passed
Push — 2.x ( d27db5...1d7bfe )
by Aleksei
21:55 queued 01:54
created

ConnectionConfig::__set_state()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 3
nop 1
dl 0
loc 12
ccs 0
cts 0
cp 0
crap 12
rs 10
c 0
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;
13
14
abstract class ConnectionConfig
15
{
16
    /**
17
     * @var array<non-empty-string>
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<non-empty-string> at position 2 could not be parsed: Unknown type name 'non-empty-string' at position 2 in array<non-empty-string>.
Loading history...
18
     */
19
    protected array $nonPrintableOptions = [
20
        // Postgres and MySQL
21
        'password',
22
        // IBM, ODBC and DB2
23
        'PWD',
24
    ];
25
26
    /**
27
     * @param non-empty-string|null $user
0 ignored issues
show
Documentation Bug introduced by
The doc comment non-empty-string|null at position 0 could not be parsed: Unknown type name 'non-empty-string' at position 0 in non-empty-string|null.
Loading history...
28
     * @param non-empty-string|null $password
29
     */
30 60
    public function __construct(
31
        public ?string $user = null,
32
        public ?string $password = null,
33
    ) {
34 60
    }
35
36
    /**
37
     * @return non-empty-string|null
0 ignored issues
show
Documentation Bug introduced by
The doc comment non-empty-string|null at position 0 could not be parsed: Unknown type name 'non-empty-string' at position 0 in non-empty-string|null.
Loading history...
38
     */
39 62
    public function getUsername(): ?string
40
    {
41 62
        return $this->user;
42
    }
43
44
    /**
45
     * @return non-empty-string|null
0 ignored issues
show
Documentation Bug introduced by
The doc comment non-empty-string|null at position 0 could not be parsed: Unknown type name 'non-empty-string' at position 0 in non-empty-string|null.
Loading history...
46
     */
47 62
    public function getPassword(): ?string
48
    {
49 62
        return $this->password;
50
    }
51
52
    /**
53
     * @param bool $secure
54
     *
55
     * @return array
56
     */
57
    protected function toArray(bool $secure = true): array
58
    {
59
        $options = \get_object_vars($this);
60
61
        foreach ($options as $key => $value) {
62
            if ($secure && \in_array($key, $this->nonPrintableOptions, true)) {
63
                $value = '<hidden>';
64
            }
65
66
            $options[$key] = $value;
67
        }
68
69
        return $options;
70
    }
71
72
    /**
73
     * @return array
74
     */
75
    public function __debugInfo(): array
76
    {
77
        return $this->toArray();
78
    }
79
80
    /**
81
     * @throws \ReflectionException
82
     */
83
    public static function __set_state(array $properties): static
84
    {
85
        $ref = new \ReflectionClass(static::class);
86
87
        $arguments = [];
88
        foreach ($ref->getConstructor()?->getParameters() ?? [] as $parameter) {
89
            $arguments[$parameter->getName()] = \array_key_exists($parameter->getName(), $properties)
90
                ? $properties[$parameter->getName()]
91
                : $parameter->getDefaultValue();
92
        }
93
94
        return new static(...$arguments);
0 ignored issues
show
Bug introduced by
$arguments is expanded, but the parameter $user of Cycle\Database\Config\Co...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

94
        return new static(/** @scrutinizer ignore-type */ ...$arguments);
Loading history...
95
    }
96
}
97