SQLite::createTableIfNotExists()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Lloople\PHPUnitExtensions\Runners\SlowestTests;
4
5
use PDO;
6
7
class SQLite extends MySQL
8
{
9
    /**
10
     * The credentials needed to connect to the database.
11
     *
12
     * @var array
13
     */
14
    protected $credentials = [
15
        'database' => 'phpunit_results.db',
16
        'table' => 'default',
17
    ];
18
19
    protected function connect(): void
20
    {
21
        $this->connection = new PDO("sqlite:{$this->credentials['database']}");
22
        
23
        $this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
24
    }
25
26
    protected function createTableIfNotExists(): void
27
    {
28
        $this->connection->prepare(
29
            "CREATE TABLE IF NOT EXISTS `{$this->credentials['table']}` (
30
                `time` float DEFAULT NULL,
31
                `name` varchar(255) NOT NULL,
32
                `method` varchar(255) DEFAULT NULL,
33
                `class` varchar(255) DEFAULT NULL,
34
                PRIMARY KEY (`name`)
35
            );"
36
        )->execute();
37
    }
38
39
    protected function insert(string $test, string $time): void
40
    {
41
        [$class, $method] = explode('::', $test);
0 ignored issues
show
Bug introduced by
The variable $class does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $method does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
42
43
        $this->connection
44
            ->prepare(
45
                "INSERT INTO `{$this->credentials['table']}` (time, method, class, name) 
46
                VALUES(:time, :method, :class, :name) 
47
                ON CONFLICT(name) DO UPDATE SET time = :time;"
48
            )
49
            ->execute([
50
                'time' => $time,
51
                'method' => $method,
52
                'class' => $class,
53
                'name' => $test,
54
            ]);
55
    }
56
}
57