Passed
Push — dev ( 34f311...866386 )
by Def
05:49 queued 03:02
created

ConnectionPDOPgsql::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 6
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Pgsql\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
/**
19
 * Database connection class prefilled for PgSQL Server.
20
 * The class Connection represents a connection to a database via [PDO](https://secure.php.net/manual/en/book.pdo.php).
21
 */
22
final class ConnectionPDOPgsql extends ConnectionPDO
23
{
24 406
    public function createCommand(?string $sql = null, array $params = []): CommandInterface
25
    {
26 406
        $command = new CommandPDOPgsql($this, $this->queryCache);
27
28 406
        if ($sql !== null) {
29 196
            $command->setSql($sql);
30
        }
31
32 406
        if ($this->logger !== null) {
33 406
            $command->setLogger($this->logger);
34
        }
35
36 406
        if ($this->profiler !== null) {
37 406
            $command->setProfiler($this->profiler);
38
        }
39
40 406
        return $command->bindValues($params);
41
    }
42
43 8
    public function createTransaction(): TransactionInterface
44
    {
45 8
        return new TransactionPDOPgsql($this);
46
    }
47
48 11
    public function getDriverName(): string
49
    {
50 11
        return 'pgsql';
51
    }
52
53
    /**
54
     * @throws Exception|InvalidConfigException
55
     */
56 406
    public function getQueryBuilder(): QueryBuilderInterface
57
    {
58 406
        if ($this->queryBuilder === null) {
59 406
            $this->queryBuilder = new QueryBuilderPDOPgsql(
60 406
                $this->createCommand(),
61 406
                $this->getQuoter(),
62 406
                $this->getSchema(),
63
            );
64
        }
65
66 406
        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...
67
    }
68
69 419
    public function getQuoter(): QuoterInterface
70
    {
71 419
        if ($this->quoter === null) {
72 419
            $this->quoter = new Quoter('"', '"', $this->getTablePrefix());
73
        }
74
75 419
        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...
76
    }
77
78 420
    public function getSchema(): SchemaInterface
79
    {
80 420
        if ($this->schema === null) {
81 420
            $this->schema = new SchemaPDOPgsql($this, $this->schemaCache);
82
        }
83
84 420
        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...
85
    }
86
87
    /**
88
     * Initializes the DB connection.
89
     *
90
     * This method is invoked right after the DB connection is established.
91
     *
92
     * The default implementation turns on `PDO::ATTR_EMULATE_PREPARES`.
93
     *
94
     * if {@see emulatePrepare} is true, and sets the database {@see charset} if it is not empty.
95
     *
96
     * It then triggers an {@see EVENT_AFTER_OPEN} event.
97
     */
98 200
    protected function initConnection(): void
99
    {
100 200
        if ($this->pdo === null) {
101 200
            $this->pdo = $this->driver->createConnection();
102
        }
103
104 200
        $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

104
        $this->pdo->/** @scrutinizer ignore-call */ 
105
                    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...
105
106 200
        if ($this->getEmulatePrepare() !== null && constant('PDO::ATTR_EMULATE_PREPARES')) {
107 1
            $this->pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, $this->getEmulatePrepare());
108
        }
109
110 200
        $charset = $this->driver->getCharset();
111
112 200
        if ($charset !== null) {
113
            $this->pdo->exec('SET NAMES ' . $this->pdo->quote($charset));
114
        }
115
    }
116
}
117