Driver::createConnection()   A
last analyzed

Complexity

Conditions 5
Paths 8

Size

Total Lines 19
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 5

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 19
ccs 10
cts 10
cp 1
rs 9.6111
c 0
b 0
f 0
cc 5
nc 8
nop 0
crap 5
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Mysql;
6
7
use PDO;
8
use Yiisoft\Db\Driver\Pdo\AbstractPdoDriver;
9
10
/**
11
 * Implements the MySQL, MariaDB driver based on the PDO (PHP Data Objects) extension.
12
 *
13
 * @link https://www.php.net/manual/en/ref.pdo-mysql.php
14
 */
15
final class Driver extends AbstractPdoDriver
16
{
17 313
    public function createConnection(): PDO
18
    {
19 313
        $this->attributes += [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION];
20
21 313
        if (PHP_VERSION_ID >= 80100) {
22 313
            $this->attributes += [PDO::ATTR_STRINGIFY_FETCHES => true];
23
        }
24
25 313
        $pdo = parent::createConnection();
26
27 313
        if ($this->charset !== null) {
28 1
            $pdo->exec('SET NAMES ' . $pdo->quote($this->charset));
29
        }
30
31 313
        if ($this->charset === null && !str_contains($this->dsn, 'charset')) {
32 2
            $pdo->exec('SET NAMES ' . $pdo->quote('utf8mb4'));
33
        }
34
35 313
        return $pdo;
36
    }
37
38 420
    public function getDriverName(): string
39
    {
40 420
        return 'mysql';
41
    }
42
}
43