Passed
Pull Request — main (#159)
by Andreas
02:02
created

Driver::getDatabase()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
/**
4
 * Licensed to CRATE Technology GmbH("Crate") under one or more contributor
5
 * license agreements.  See the NOTICE file distributed with this work for
6
 * additional information regarding copyright ownership.  Crate licenses
7
 * this file to you under the Apache License, Version 2.0 (the "License");
8
 * you may not use this file except in compliance with the License.  You may
9
 * obtain a copy of the License at
10
 *
11
 * http://www.apache.org/licenses/LICENSE-2.0
12
 *
13
 * Unless required by applicable law or agreed to in writing, software
14
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
16
 * License for the specific language governing permissions and limitations
17
 * under the License.
18
 *
19
 * However, if you have executed another commercial license agreement
20
 * with Crate these terms will supersede the license and you may use the
21
 * software solely pursuant to the terms of the relevant commercial agreement.
22
 */
23
24
namespace Crate\DBAL\Driver\PDOCrate;
25
26
use Crate\DBAL\Platforms\CratePlatform;
27
use Crate\DBAL\Platforms\CratePlatform1;
28
use Crate\DBAL\Platforms\CratePlatform4;
29
use Crate\DBAL\Schema\CrateSchemaManager;
30
use Doctrine\DBAL\Connection;
31
use Doctrine\DBAL\Platforms\AbstractPlatform;
32
use Doctrine\DBAL\Schema\AbstractSchemaManager;
33
use Doctrine\DBAL\VersionAwarePlatformDriver;
34
use SensitiveParameter;
0 ignored issues
show
Bug introduced by
The type SensitiveParameter was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
35
36
class Driver implements VersionAwarePlatformDriver
0 ignored issues
show
Deprecated Code introduced by
The interface Doctrine\DBAL\VersionAwarePlatformDriver has been deprecated: All drivers will have to be aware of the server version in the next major release. ( Ignorable by Annotation )

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

36
class Driver implements /** @scrutinizer ignore-deprecated */ VersionAwarePlatformDriver

This interface has been deprecated. The supplier of the interface has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the interface will be removed and what other interface to use instead.

Loading history...
37
{
38
    public const VERSION = '4.0.3';
39
    public const NAME = 'crate';
40
41
    private const VERSION_057 = '0.57.0';
42
    private const VERSION_4 = '4.0.0';
43
44
    /**
45
     * {@inheritDoc}
46
     * @return PDOConnection The database connection.
47
     */
48 16
    public function connect(
49
        #[SensitiveParameter]
50
        array $params
51
    ): PDOConnection {
52 16
        $username = $params['user'] ?? null;
53 16
        $password = $params['password'] ?? null;
54 16
        $driverOptions = $params['driver_options'] ?? [];
55 16
        return new PDOConnection($this->constructPdoDsn($params), $username, $password, $driverOptions);
56
    }
57
58
    /**
59
     * Constructs the Crate PDO DSN.
60
     *
61
     * @return string The DSN.
62
     */
63 16
    private function constructPdoDsn(array $params): string
64
    {
65 16
        $dsn = self::NAME . ':';
66 16
        if (isset($params['host']) && $params['host'] != '') {
67 16
            $dsn .= $params['host'];
68
        } else {
69
            $dsn .= 'localhost';
70
        }
71 16
        $dsn .= ':' . (isset($params['port']) ? $params['port'] : '4200');
72
73 16
        return $dsn;
74
    }
75
76
    /**
77
     * {@inheritDoc}
78
     */
79 2
    public function getDatabasePlatform(): AbstractPlatform
80
    {
81
        // FIXME: Chicken egg problem: No connection information here.
82
        //        Is this meant to "activate" the right platform *after* a server
83
        //        inquiry at all? Isn't it more likely it is NOT meant to work this way?
84
        //        Let's investigate how the other vendors are doing it.
85 2
        $conn = $this->connect();
0 ignored issues
show
Bug introduced by
The call to Crate\DBAL\Driver\PDOCrate\Driver::connect() has too few arguments starting with params. ( Ignorable by Annotation )

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

85
        /** @scrutinizer ignore-call */ 
86
        $conn = $this->connect();

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
86
        $version = $conn->getServerVersion();
87
        $platform = $this->createDatabasePlatformForVersion($version);
88
        return $platform;
89
    }
90
91
    /**
92
     * {@inheritDoc}
93
     */
94
    public function getSchemaManager(Connection $conn, AbstractPlatform $platform): AbstractSchemaManager
95
    {
96
        // Added by Doctrine 3.
97
        return new CrateSchemaManager($conn, $platform);
98
    }
99
100
    /**
101
     * {@inheritDoc}
102
     */
103
    public function getName(): string
104
    {
105
        return self::NAME;
106
    }
107
108
    /**
109
     * {@inheritDoc}
110
     */
111
    public function getDatabase(Connection $conn): string|null
0 ignored issues
show
Unused Code introduced by
The parameter $conn is not used and could be removed. ( Ignorable by Annotation )

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

111
    public function getDatabase(/** @scrutinizer ignore-unused */ Connection $conn): string|null

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
112
    {
113
        return null;
114
    }
115
116
    /**
117
     * {@inheritDoc}
118
     */
119 240
    public function createDatabasePlatformForVersion($version): AbstractPlatform
120
    {
121 240
        if (version_compare($version, self::VERSION_057, "<")) {
122 2
            return new CratePlatform();
123 238
        } elseif (version_compare($version, self::VERSION_4, "<")) {
124 2
            return new CratePlatform1();
125
        } else {
126 236
            return new CratePlatform4();
127
        }
128
    }
129
130
    /**
131
     * {@inheritDoc}
132
     */
133
    public function getExceptionConverter(): ExceptionConverter
134
    {
135
        // Added by Doctrine 3.
136
        return new ExceptionConverter();
137
    }
138
}
139