Passed
Pull Request — 2.x (#83)
by
unknown
16:51
created

PostgresDriverConfig::bootSchema()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 7
nc 2
nop 1
dl 0
loc 16
ccs 8
cts 8
cp 1
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;
13
14
use Cycle\Database\Config\Postgres\ConnectionConfig;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Cycle\Database\Config\ConnectionConfig. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
15
use Cycle\Database\Driver\Postgres\PostgresDriver;
16
17
/**
18
 * @template-extends DriverConfig<ConnectionConfig>
19
 */
20
class PostgresDriverConfig extends DriverConfig
21
{
22
    /**
23
     * Default public schema name for all postgres connections.
24
     *
25
     * @var non-empty-string
26
     */
27
    public const DEFAULT_SCHEMA = 'public';
28
29
    /**
30
     * @var non-empty-array<non-empty-string>
0 ignored issues
show
Documentation Bug introduced by
The doc comment non-empty-array<non-empty-string> at position 0 could not be parsed: Unknown type name 'non-empty-array' at position 0 in non-empty-array<non-empty-string>.
Loading history...
31
     * @psalm-readonly-allow-private-mutation
32
     */
33
    public array $schema;
34
35
    /**
36
     * @param ConnectionConfig $connection
37
     * @param iterable<non-empty-string>|non-empty-string $schema List of available Postgres
38
     *        schemas for "search path" (See also {@link https://www.postgresql.org/docs/9.6/ddl-schemas.html}).
39
     *        The first parameter's item will be used as default schema.
40
     *
41
     * {@inheritDoc}
42
     */
43 34
    public function __construct(
44
        ConnectionConfig $connection,
45
        iterable|string $schema = self::DEFAULT_SCHEMA,
46
        string $driver = PostgresDriver::class,
47
        bool $reconnect = true,
48
        string $timezone = 'UTC',
49
        bool $queryCache = true,
50
        bool $readonlySchema = false,
51
        bool $readonly = false,
52
    ) {
53
        /** @psalm-suppress ArgumentTypeCoercion */
54 34
        parent::__construct(
55 34
            connection: $connection,
56
            driver: $driver,
57
            reconnect: $reconnect,
58
            timezone: $timezone,
59
            queryCache: $queryCache,
60
            readonlySchema: $readonlySchema,
61
            readonly: $readonly,
62
        );
63
64 34
        $this->schema = $this->bootSchema($schema);
65 34
    }
66
67
    /**
68
     * @param iterable<non-empty-string>|non-empty-string $schema
69
     *
70
     * @return 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...
71
     */
72 34
    private function bootSchema(iterable|string $schema): array
73
    {
74
        // Cast any schema config variants to array
75 34
        $schema = match (true) {
76 34
            $schema instanceof \Traversable => \iterator_to_array($schema),
0 ignored issues
show
Bug introduced by
It seems like $schema can also be of type string; however, parameter $iterator of iterator_to_array() does only seem to accept Traversable, maybe add an additional type check? ( Ignorable by Annotation )

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

76
            $schema instanceof \Traversable => \iterator_to_array(/** @scrutinizer ignore-type */ $schema),
Loading history...
77 34
            \is_string($schema) => [$schema],
78 28
            default => $schema
79
        };
80
81
        // Fill array by default in case that result array is empty
82 34
        if ($schema === []) {
83 20
            $schema = [self::DEFAULT_SCHEMA];
84
        }
85
86
        // Remove schema duplications
87 34
        return \array_values(\array_unique($schema));
0 ignored issues
show
Bug introduced by
It seems like $schema can also be of type iterable and string; however, parameter $array of array_unique() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

87
        return \array_values(\array_unique(/** @scrutinizer ignore-type */ $schema));
Loading history...
88
    }
89
90
    public static function __set_state(array $an_array): object
91
    {
92
        $config = parent::__set_state($an_array);
93
        $config->schema = $an_array['schema'];
94
95
        return $config;
96
    }
97
}
98