Test Failed
Pull Request — master (#44)
by Wilmer
13:17 queued 09:47
created

ConnectionPDOOracle::close()   A

Complexity

Conditions 5
Paths 12

Size

Total Lines 27
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 15
nc 12
nop 0
dl 0
loc 27
rs 9.4555
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Oracle\PDO;
6
7
use PDO;
8
use PDOException;
9
use Psr\Log\LogLevel;
10
use Yiisoft\Db\Cache\QueryCache;
11
use Yiisoft\Db\Cache\SchemaCache;
12
use Yiisoft\Db\Connection\Connection;
13
use Yiisoft\Db\Connection\ConnectionPDOInterface;
0 ignored issues
show
Bug introduced by
The type Yiisoft\Db\Connection\ConnectionPDOInterface 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...
14
use Yiisoft\Db\Driver\PDODriver;
0 ignored issues
show
Bug introduced by
The type Yiisoft\Db\Driver\PDODriver 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...
15
use Yiisoft\Db\Exception\Exception;
16
use Yiisoft\Db\Exception\InvalidConfigException;
17
use Yiisoft\Db\Query\Query;
18
use Yiisoft\Db\Query\QueryBuilderInterface;
0 ignored issues
show
Bug introduced by
The type Yiisoft\Db\Query\QueryBuilderInterface 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...
19
use Yiisoft\Db\Schema\Quoter;
0 ignored issues
show
Bug introduced by
The type Yiisoft\Db\Schema\Quoter 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...
20
use Yiisoft\Db\Schema\QuoterInterface;
0 ignored issues
show
Bug introduced by
The type Yiisoft\Db\Schema\QuoterInterface 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...
21
use Yiisoft\Db\Schema\SchemaInterface;
22
use Yiisoft\Db\Transaction\TransactionInterface;
0 ignored issues
show
Bug introduced by
The type Yiisoft\Db\Transaction\TransactionInterface 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...
23
24
use function constant;
25
26
/**
27
 * The class Connection represents a connection to a database via [PDO](https://secure.php.net/manual/en/book.pdo.php).
28
 */
29
final class ConnectionPDOOracle extends Connection implements ConnectionPDOInterface
30
{
31
    private ?PDO $pdo = null;
32
    private ?Query $query = null;
33
    private ?QueryBuilderInterface $queryBuilder = null;
34
    private ?QuoterInterface $quoter = null;
35
    private ?SchemaInterface $schema = null;
36
    private string $serverVersion = '';
37
38
    public function __construct(
39
        private PDODriver $driver,
40
        private QueryCache $queryCache,
41
        private SchemaCache $schemaCache
42
    ) {
43
        parent::__construct($queryCache);
0 ignored issues
show
Bug introduced by
The call to Yiisoft\Db\Connection\Connection::__construct() has too few arguments starting with queryCache. ( Ignorable by Annotation )

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

43
        parent::/** @scrutinizer ignore-call */ 
44
                __construct($queryCache);

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...
Bug introduced by
$queryCache of type Yiisoft\Db\Cache\QueryCache is incompatible with the type string expected by parameter $dsn of Yiisoft\Db\Connection\Connection::__construct(). ( Ignorable by Annotation )

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

43
        parent::__construct(/** @scrutinizer ignore-type */ $queryCache);
Loading history...
44
    }
45
46
    /**
47
     * Reset the connection after cloning.
48
     */
49
    public function __clone()
50
    {
51
        $this->master = null;
52
        $this->slave = null;
53
        $this->transaction = null;
54
55
        if (strncmp($this->driver->getDsn(), 'sqlite::memory:', 15) !== 0) {
56
            /** reset PDO connection, unless its sqlite in-memory, which can only have one connection */
57
            $this->pdo = null;
58
        }
59
    }
60
61
    /**
62
     * Close the connection before serializing.
63
     *
64
     * @return array
65
     */
66
    public function __sleep(): array
67
    {
68
        $fields = (array) $this;
69
70
        unset(
71
            $fields["\000" . __CLASS__ . "\000" . 'pdo'],
72
            $fields["\000" . __CLASS__ . "\000" . 'master'],
73
            $fields["\000" . __CLASS__ . "\000" . 'slave'],
74
            $fields["\000" . __CLASS__ . "\000" . 'transaction'],
75
            $fields["\000" . __CLASS__ . "\000" . 'schema']
76
        );
77
78
        return array_keys($fields);
79
    }
80
81
    public function createCommand(?string $sql = null, array $params = []): CommandPDOOracle
82
    {
83
        $command = new CommandPDOOracle($this, $this->queryCache);
84
85
        if ($sql !== null) {
86
            $command->setSql($sql);
87
        }
88
89
        if ($this->logger !== null) {
90
            $command->setLogger($this->logger);
91
        }
92
93
        if ($this->profiler !== null) {
94
            $command->setProfiler($this->profiler);
95
        }
96
97
        return $command->bindValues($params);
98
    }
99
100
    public function createTransaction(): TransactionInterface
101
    {
102
        return new TransactionPDOOracle($this);
103
    }
104
105
    public function close(): void
106
    {
107
        if (!empty($this->master)) {
108
            /** @var ConnectionPDOOracle */
109
            $db = $this->master;
110
111
            if ($this->pdo === $db->getPDO()) {
112
                $this->pdo = null;
113
            }
114
115
            $db->close();
116
            $this->master = null;
117
        }
118
119
        if ($this->pdo !== null) {
120
            $this->logger?->log(
121
                LogLevel::DEBUG,
122
                'Closing DB connection: ' . $this->driver->getDsn() . ' ' . __METHOD__,
123
            );
124
125
            $this->pdo = null;
126
            $this->transaction = null;
127
        }
128
129
        if (!empty($this->slave)) {
130
            $this->slave->close();
131
            $this->slave = null;
132
        }
133
    }
134
135
    public function getDriver(): PDODriver
136
    {
137
        return $this->driver;
138
    }
139
140
    public function getDriverName(): string
141
    {
142
        return 'oci';
143
    }
144
145
    public function getMasterPdo(): PDO|null
146
    {
147
        $this->open();
148
        return $this->pdo;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->pdo also could return the type null which is incompatible with the return type mandated by Yiisoft\Db\Connection\Connection::getMasterPdo() of PDO.
Loading history...
149
    }
150
151
    public function getPDO(): ?PDO
152
    {
153
        return $this->pdo;
154
    }
155
156
    public function getQuery(): Query
157
    {
158
        if ($this->query === null) {
159
            $this->query = new Query($this);
160
        }
161
162
        return $this->query;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->query could return the type null which is incompatible with the type-hinted return Yiisoft\Db\Query\Query. Consider adding an additional type-check to rule them out.
Loading history...
163
    }
164
165
    /**
166
     * @throws Exception|InvalidConfigException
167
     */
168
    public function getQueryBuilder(): QueryBuilderInterface
169
    {
170
        if ($this->queryBuilder === null) {
171
            $this->queryBuilder = new QueryBuilderPDOOracle(
172
                $this->createCommand(),
173
                $this->getQuery(),
174
                $this->getQuoter(),
175
                $this->getSchema(),
176
            );
177
        }
178
179
        return $this->queryBuilder;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->queryBuilder could return the type null which is incompatible with the type-hinted return Yiisoft\Db\Query\QueryBuilderInterface. Consider adding an additional type-check to rule them out.
Loading history...
180
    }
181
182
    public function getQuoter(): QuoterInterface
183
    {
184
        if ($this->quoter === null) {
185
            $this->quoter = new Quoter('"', '"', $this->getTablePrefix());
186
        }
187
188
        return $this->quoter;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->quoter could return the type null which is incompatible with the type-hinted return Yiisoft\Db\Schema\QuoterInterface. Consider adding an additional type-check to rule them out.
Loading history...
189
    }
190
191
    /**
192
     * @throws Exception
193
     */
194
    public function getServerVersion(): string
195
    {
196
        if ($this->serverVersion === '') {
197
            /** @var mixed */
198
            $version = $this->getSlavePDO()?->getAttribute(PDO::ATTR_SERVER_VERSION);
199
            $this->serverVersion = is_string($version) ? $version : 'Version could not be determined.';
200
        }
201
202
        return $this->serverVersion;
203
    }
204
205
    public function getSchema(): SchemaInterface
206
    {
207
        if ($this->schema === null) {
208
            $this->schema = new SchemaPDOOracle($this, $this->schemaCache);
0 ignored issues
show
Bug introduced by
The type Yiisoft\Db\Oracle\PDO\SchemaPDOOracle 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...
209
        }
210
211
        return $this->schema;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->schema could return the type null which is incompatible with the type-hinted return Yiisoft\Db\Schema\SchemaInterface. Consider adding an additional type-check to rule them out.
Loading history...
212
    }
213
214
    public function getSlavePdo(bool $fallbackToMaster = true): ?PDO
215
    {
216
        /** @var ConnectionPDOOracle|null $db */
217
        $db = $this->getSlave(false);
218
219
        if ($db === null) {
220
            return $fallbackToMaster ? $this->getMasterPdo() : null;
221
        }
222
223
        return $db->getPDO();
224
    }
225
226
    public function isActive(): bool
227
    {
228
        return $this->pdo !== null;
229
    }
230
231
    public function open(): void
232
    {
233
        if (!empty($this->pdo)) {
234
            return;
235
        }
236
237
        if (!empty($this->masters)) {
0 ignored issues
show
Bug introduced by
The property masters is declared private in Yiisoft\Db\Connection\Connection and cannot be accessed from this context.
Loading history...
238
            /** @var ConnectionPDOOracle|null */
239
            $db = $this->getMaster();
240
241
            if ($db !== null) {
242
                $this->pdo = $db->getPDO();
243
                return;
244
            }
245
246
            throw new InvalidConfigException('None of the master DB servers is available.');
247
        }
248
249
        if (empty($this->driver->getDsn())) {
250
            throw new InvalidConfigException('Connection::dsn cannot be empty.');
251
        }
252
253
        $token = 'Opening DB connection: ' . $this->driver->getDsn();
254
255
        try {
256
            $this->logger?->log(LogLevel::INFO, $token);
257
            $this->profiler?->begin($token, [__METHOD__]);
258
            $this->initConnection();
259
            $this->profiler?->end($token, [__METHOD__]);
260
        } catch (PDOException $e) {
261
            $this->profiler?->end($token, [__METHOD__]);
262
            $this->logger?->log(LogLevel::ERROR, $token);
263
264
            throw new Exception($e->getMessage(), (array) $e->errorInfo, $e);
265
        }
266
    }
267
268
    /**
269
     * Initializes the DB connection.
270
     *
271
     * This method is invoked right after the DB connection is established.
272
     *
273
     * The default implementation turns on `PDO::ATTR_EMULATE_PREPARES`.
274
     *
275
     * if {@see emulatePrepare} is true, and sets the database {@see charset} if it is not empty.
276
     *
277
     * It then triggers an {@see EVENT_AFTER_OPEN} event.
278
     */
279
    protected function initConnection(): void
280
    {
281
        $this->pdo = $this->driver->createConnection();
282
        $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
0 ignored issues
show
Bug introduced by
The method setAttribute() does not exist on null. ( Ignorable by Annotation )

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

282
        $this->pdo->/** @scrutinizer ignore-call */ 
283
                    setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
283
284
        if ($this->getEmulatePrepare() !== null && constant('PDO::ATTR_EMULATE_PREPARES')) {
285
            $this->pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, $this->getEmulatePrepare());
286
        }
287
    }
288
}
289