PostgresDriverConfig::bootSchema()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 7
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 16
ccs 7
cts 7
cp 1
crap 2
rs 10
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
     *
32
     * @psalm-readonly-allow-private-mutation
33
     */
34
    public array $schema;
35
36
    /**
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
        array $options = [],
53
    ) {
54 34
        /** @psalm-suppress ArgumentTypeCoercion */
55 34
        parent::__construct(
56
            connection: $connection,
57
            driver: $driver,
58
            reconnect: $reconnect,
59
            timezone: $timezone,
60
            queryCache: $queryCache,
61
            readonlySchema: $readonlySchema,
62
            readonly: $readonly,
63
            options: $options,
64 34
        );
65 34
66
        $this->schema = $this->bootSchema($schema);
67
    }
68
69
    /**
70
     * @param iterable<non-empty-string>|non-empty-string $schema
71
     *
72 34
     * @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...
73
     */
74
    private function bootSchema(iterable|string $schema): array
75 34
    {
76 34
        // Cast any schema config variants to array
77 34
        $schema = match (true) {
78 28
            $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

78
            $schema instanceof \Traversable => \iterator_to_array(/** @scrutinizer ignore-type */ $schema),
Loading history...
79
            \is_string($schema) => [$schema],
80
            default => $schema,
81
        };
82 34
83 20
        // Fill array by default in case that result array is empty
84
        if ($schema === []) {
85
            $schema = [self::DEFAULT_SCHEMA];
86
        }
87 34
88
        // Remove schema duplications
89
        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

89
        return \array_values(\array_unique(/** @scrutinizer ignore-type */ $schema));
Loading history...
90
    }
91
}
92