Passed
Pull Request — master (#30)
by Mathieu
16:30 queued 06:21
created

MigrationModel::createMysqlMigrationTable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 10
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Suricate\Migrations;
6
7
use Suricate\DBObject;
8
9
class MigrationModel extends DBObject
10
{
11
    protected $tableName = 'suricate_migrations';
12
    protected $tableIndex = 'name';
13
    // FIXME: set config ?
14
15
    public function __construct()
16
    {
17
        parent::__construct();
18
19
        $this->dbVariables = [
20
            'name',
21
            'date_added',
22
        ];
23
24
        $this->readOnlyVariables = ['date_added'];
25
    }
26
27
    public function createMigrationTable(): int
28
    {
29
        $this->connectDB();
30
        if ($this->dbLink !== false) {
0 ignored issues
show
introduced by
The condition $this->dbLink !== false is always true.
Loading history...
31
32
            $dbParameters = $this->dbLink->getConfigParameters();
33
            switch ($dbParameters['type']) {
34
                case 'mysql':
35
                    if (!$this->doesMigrationTableExists('mysql')) {
0 ignored issues
show
Unused Code introduced by
The call to Suricate\Migrations\Migr...sMigrationTableExists() has too many arguments starting with 'mysql'. ( Ignorable by Annotation )

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

35
                    if (!$this->/** @scrutinizer ignore-call */ doesMigrationTableExists('mysql')) {

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
36
                        $this->createMysqlMigrationTable();
37
                        return 1;
38
                    }
39
                    return -1;
40
41
                case 'sqlite':
42
                    if (!$this->doesMigrationTableExists('sqlite')) {
43
                        $this->createSqliteMigrationTable();
44
                        return 1;
45
                    }
46
                    return -1;
47
            }
48
49
            return 0;
50
        }
0 ignored issues
show
Bug Best Practice introduced by
The function implicitly returns null when the if condition on line 30 is false. This is incompatible with the type-hinted return integer. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
51
    }
52
53
    private function createMysqlMigrationTable()
54
    {
55
        $sql = <<<EOD
56
        CREATE TABLE IF NOT EXISTS `{$this->tableName}` (
57
            `name` varchar(255) NOT NULL UNIQUE,
58
            `date_added` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP
59
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 
60
EOD;
61
62
        $this->dbLink->query($sql);
63
    }
64
65
    private function createSqliteMigrationTable()
66
    {
67
        // FIXME:
68
        $sql = <<<EOD
69
        CREATE TABLE IF NOT EXISTS "{$this->tableName}" (
70
            "name"	TEXT NOT NULL UNIQUE,
71
            "date_added" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
72
        );
73
EOD;
74
        $this->dbLink->query($sql);
75
        echo $sql;
76
        return $sql;
77
    }
78
79
    private function doesMigrationTableExists(): bool
80
    {
81
        $dbParameters = $this->dbLink->getConfigParameters();
82
        $dbType = $dbParameters['type'];
83
84
        if ($dbType === 'sqlite') {
85
            $sql = "SELECT count(*) FROM sqlite_master WHERE type='table' AND name=:table";
86
            $sqlParams = [
87
                "table" => $this->getTableName()
88
            ];
89
            // FIXME:
90
            $res = $this->dbLink->query($sql, $sqlParams)->fetchColumn();
91
92
            return ((int) $res) === 1;
93
        }
94
95
96
        if ($dbType === 'mysql') {
97
            $sql = "SELECT count(*) FROM information_schema.tables " .
98
                " WHERE table_schema=:schema " .
99
                " AND table_name =:tableName ";
100
101
            $sqlParams = [
102
                "schema" => $dbParameters['database'],
103
                "tableName" => $this->getTableName()
104
            ];
105
            $res = $this->dbLink->query($sql, $sqlParams)->fetchColumn();
106
107
            return ((int) $res) === 1;
108
        }
109
110
        return true;
111
    }
112
}
113