Passed
Pull Request — master (#40)
by Wilmer
12:48
created

MysqlConnection::initConnection()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 6
nc 5
nop 0
dl 0
loc 11
rs 9.6111
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Mysql\Connection;
6
7
use PDO;
8
use Yiisoft\Db\Command\Command;
9
use Yiisoft\Db\Connection\Connection;
10
use Yiisoft\Db\Exception\Exception;
11
use Yiisoft\Db\Exception\InvalidConfigException;
12
use Yiisoft\Db\Exception\NotSupportedException;
13
use Yiisoft\Db\Mysql\Schema\MysqlSchema;
14
15
use function constant;
16
17
/**
18
 * Database connection class prefilled for MYSQL Server.
19
 */
20
final class MysqlConnection extends Connection
21
{
22
    private ?MysqlSchema $schema = null;
23
24
    /**
25
     * Creates a command for execution.
26
     *
27
     * @param string|null $sql the SQL statement to be executed
28
     * @param array $params the parameters to be bound to the SQL statement
29
     *
30
     * @return Command the DB command
31
     * @throws Exception
32
     * @throws InvalidConfigException
33
     * @throws NotSupportedException
34
     */
35
    public function createCommand(?string $sql = null, array $params = []): Command
36
    {
37
        if ($sql !== null) {
38
            $sql = $this->quoteSql($sql);
39
        }
40
41
        $command = new Command($this->getProfiler(), $this->getLogger(), $this, $sql);
0 ignored issues
show
Bug introduced by
The method getProfiler() does not exist on Yiisoft\Db\Mysql\Connection\MysqlConnection. ( Ignorable by Annotation )

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

41
        $command = new Command($this->/** @scrutinizer ignore-call */ getProfiler(), $this->getLogger(), $this, $sql);

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...
Bug introduced by
The method getLogger() does not exist on Yiisoft\Db\Mysql\Connection\MysqlConnection. ( Ignorable by Annotation )

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

41
        $command = new Command($this->getProfiler(), $this->/** @scrutinizer ignore-call */ getLogger(), $this, $sql);

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...
42
43
        return $command->bindValues($params);
44
    }
45
46
    /**
47
     * Returns the schema information for the database opened by this connection.
48
     *
49
     * @return MysqlSchema the schema information for the database opened by this connection.
50
     */
51
    public function getSchema(): MysqlSchema
52
    {
53
        if ($this->schema !== null) {
54
            return $this->schema;
55
        }
56
57
        return $this->schema = new MysqlSchema($this);
58
    }
59
60
    /**
61
     * Creates the PDO instance.
62
     *
63
     * This method is called by {@see open} to establish a DB connection. The default implementation will create a PHP
64
     * PDO instance. You may override this method if the default PDO needs to be adapted for certain DBMS.
65
     *
66
     * @return PDO the pdo instance
67
     */
68
    protected function createPdoInstance(): PDO
69
    {
70
        return new PDO($this->getDsn(), $this->getUsername(), $this->getPassword(), $this->getAttributes());
71
    }
72
73
    /**
74
     * Initializes the DB connection.
75
     *
76
     * This method is invoked right after the DB connection is established.
77
     *
78
     * The default implementation turns on `PDO::ATTR_EMULATE_PREPARES`.
79
     *
80
     * if {@see emulatePrepare} is true, and sets the database {@see charset} if it is not empty.
81
     *
82
     * It then triggers an {@see EVENT_AFTER_OPEN} event.
83
     */
84
    protected function initConnection(): void
85
    {
86
        if ($this->getPDO() !== null) {
87
            $this->getPDO()->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
88
89
            if ($this->getEmulatePrepare() !== null && constant('PDO::ATTR_EMULATE_PREPARES')) {
0 ignored issues
show
Bug introduced by
The method getEmulatePrepare() does not exist on Yiisoft\Db\Mysql\Connection\MysqlConnection. ( Ignorable by Annotation )

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

89
            if ($this->/** @scrutinizer ignore-call */ getEmulatePrepare() !== null && constant('PDO::ATTR_EMULATE_PREPARES')) {

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...
90
                $this->getPDO()->setAttribute(PDO::ATTR_EMULATE_PREPARES, $this->getEmulatePrepare());
91
            }
92
93
            if ($this->getCharset() !== null) {
0 ignored issues
show
Bug introduced by
The method getCharset() does not exist on Yiisoft\Db\Mysql\Connection\MysqlConnection. ( Ignorable by Annotation )

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

93
            if ($this->/** @scrutinizer ignore-call */ getCharset() !== null) {

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...
94
                $this->getPDO()->exec('SET NAMES ' . $this->getPDO()->quote($this->getCharset()));
95
            }
96
        }
97
    }
98
}
99