Passed
Pull Request — 2.x (#91)
by Aleksei
18:20
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
    use StateTrait;
17
18
    /**
19
     * @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...
20
     */
21
    protected array $nonPrintableOptions = [
22
        // Postgres and MySQL
23
        'password',
24
        // IBM, ODBC and DB2
25
        'PWD',
26
    ];
27
28
    /**
29
     * @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...
30 60
     * @param non-empty-string|null $password
31
     */
32
    public function __construct(
33
        public ?string $user = null,
34 60
        public ?string $password = null,
35
    ) {
36
    }
37
38
    /**
39 62
     * @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...
40
     */
41 62
    public function getUsername(): ?string
42
    {
43
        return $this->user;
44
    }
45
46
    /**
47 62
     * @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...
48
     */
49 62
    public function getPassword(): ?string
50
    {
51
        return $this->password;
52
    }
53
54
    /**
55
     * @param bool $secure
56
     *
57
     * @return array
58
     */
59
    protected function toArray(bool $secure = true): array
60
    {
61
        $options = \get_object_vars($this);
62
63
        foreach ($options as $key => $value) {
64
            if ($secure && \in_array($key, $this->nonPrintableOptions, true)) {
65
                $value = '<hidden>';
66
            }
67
68
            $options[$key] = $value;
69
        }
70
71
        return $options;
72
    }
73
74
    /**
75
     * @return array
76
     */
77
    public function __debugInfo(): array
78
    {
79
        return $this->toArray();
80
    }
81
}
82