Passed
Push — master ( d9f47f...0d7a1e )
by Wilmer
03:40
created

Driver   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Test Coverage

Coverage 91.67%

Importance

Changes 0
Metric Value
wmc 6
eloc 11
dl 0
loc 26
ccs 11
cts 12
cp 0.9167
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A createConnection() 0 19 5
A getDriverName() 0 3 1
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 288
    public function createConnection(): PDO
18
    {
19 288
        $this->attributes += [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION];
20
21 288
        if (PHP_VERSION_ID >= 80100) {
22
            $this->attributes += [PDO::ATTR_STRINGIFY_FETCHES => true];
23
        }
24
25 288
        $pdo = parent::createConnection();
26
27 288
        if ($this->charset !== null) {
28 1
            $pdo->exec('SET NAMES ' . $pdo->quote($this->charset));
29
        }
30
31 288
        if ($this->charset === null && !str_contains($this->dsn, 'charset')) {
32 2
            $pdo->exec('SET NAMES ' . $pdo->quote('utf8mb4'));
33
        }
34
35 288
        return $pdo;
36
    }
37
38 373
    public function getDriverName(): string
39
    {
40 373
        return 'mysql';
41
    }
42
}
43