Passed
Push — dev ( adb141...4e189c )
by Def
25:37 queued 22:33
created

ConnectionPDOPgsql::close()   A

Complexity

Conditions 5
Paths 12

Size

Total Lines 27
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 15
c 1
b 0
f 0
nc 12
nop 0
dl 0
loc 27
ccs 15
cts 15
cp 1
crap 5
rs 9.4555

1 Method

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

115
        $this->pdo->/** @scrutinizer ignore-call */ 
116
                    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...
116
117 200
        if ($this->getEmulatePrepare() !== null && constant('PDO::ATTR_EMULATE_PREPARES')) {
118 1
            $this->pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, $this->getEmulatePrepare());
119
        }
120
121 200
        $charset = $this->driver->getCharset();
122
123 200
        if ($charset !== null) {
124
            $this->pdo->exec('SET NAMES ' . $this->pdo->quote($charset));
125
        }
126
    }
127
}
128