Test Failed
Pull Request — dev (#93)
by Def
14:13 queued 10:50
created

ConnectionPDOSqlite::getQuoter()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 7
ccs 3
cts 3
cp 1
rs 10
cc 2
nc 2
nop 0
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Sqlite\PDO;
6
7
use PDO;
8
use Yiisoft\Db\Command\CommandInterface;
9
use Yiisoft\Db\Connection\ConnectionPDO;
10
use Yiisoft\Db\Exception\Exception;
11
use Yiisoft\Db\Exception\InvalidConfigException;
12
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...
13
use Yiisoft\Db\Schema\Quoter;
14
use Yiisoft\Db\Schema\QuoterInterface;
15
use Yiisoft\Db\Schema\SchemaInterface;
16
use Yiisoft\Db\Transaction\TransactionInterface;
17
18
use function constant;
19
use function strncmp;
20
21
/**
22
 * Database connection class prefilled for SQLite Server.
23
 */
24
final class ConnectionPDOSqlite extends ConnectionPDO
25
{
26
    /**
27
     * Reset the connection after cloning.
28 380
     */
29
    public function __clone()
30
    {
31
        $this->transaction = null;
32
33 380
        if (strncmp($this->driver->getDsn(), 'sqlite::memory:', 15) !== 0) {
34
            /** reset PDO connection, unless its sqlite in-memory, which can only have one connection */
35
            $this->pdo = null;
36 328
        }
37
    }
38 328
39
    public function createCommand(?string $sql = null, array $params = []): CommandInterface
40 328
    {
41 172
        $command = new CommandPDOSqlite($this, $this->queryCache);
0 ignored issues
show
Bug introduced by
The property queryCache is declared private in Yiisoft\Db\Connection\Connection and cannot be accessed from this context.
Loading history...
42
43
        if ($sql !== null) {
44 328
            $command->setSql($sql);
45 328
        }
46
47
        if ($this->logger !== null) {
48 328
            $command->setLogger($this->logger);
49 328
        }
50
51
        if ($this->profiler !== null) {
52 328
            $command->setProfiler($this->profiler);
53
        }
54
55 8
        return $command->bindValues($params);
56
    }
57 8
58
    public function createTransaction(): TransactionInterface
59
    {
60 9
        return new TransactionPDOSqlite($this);
61
    }
62 9
63
    public function getDriverName(): string
64
    {
65
        return 'sqlite';
66
    }
67
68 328
    /**
69
     * @throws Exception|InvalidConfigException
70 328
     */
71 328
    public function getQueryBuilder(): QueryBuilderInterface
72 328
    {
73 328
        if ($this->queryBuilder === null) {
74 328
            $this->queryBuilder = new QueryBuilderPDOSqlite(
75
                $this->createCommand(),
76
                $this->getQuoter(),
77
                $this->getSchema(),
78 328
            );
79
        }
80
81 344
        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...
82
    }
83 344
84 344
    public function getQuoter(): QuoterInterface
85
    {
86
        if ($this->quoter === null) {
87 344
            $this->quoter = new Quoter('`', '`', $this->getTablePrefix());
88
        }
89
90 342
        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...
91
    }
92 342
93 342
    public function getSchema(): SchemaInterface
94
    {
95
        if ($this->schema === null) {
96 342
            $this->schema = new SchemaPDOSqlite($this, $this->schemaCache);
0 ignored issues
show
Bug introduced by
The property schemaCache does not exist on Yiisoft\Db\Sqlite\PDO\ConnectionPDOSqlite. Did you mean schema?
Loading history...
97
        }
98
99
        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...
100
    }
101
102
    /**
103
     * Initializes the DB connection.
104
     *
105
     * This method is invoked right after the DB connection is established.
106
     *
107
     * The default implementation turns on `PDO::ATTR_EMULATE_PREPARES`.
108
     *
109
     * if {@see emulatePrepare} is true, and sets the database {@see charset} if it is not empty.
110 176
     *
111
     * It then triggers an {@see EVENT_AFTER_OPEN} event.
112 176
     */
113 176
    protected function initConnection(): void
114
    {
115 176
        $this->pdo = $this->driver->createConnection();
116
        $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
117
118
        if ($this->getEmulatePrepare() !== null && constant('PDO::ATTR_EMULATE_PREPARES')) {
119
            $this->pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, $this->getEmulatePrepare());
120
        }
121
    }
122
}
123