Add   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 49
dl 0
loc 110
ccs 48
cts 48
cp 1
rs 10
c 0
b 0
f 0
wmc 11

3 Methods

Rating   Name   Duplication   Size   Complexity  
B run() 0 61 9
A getTpqlContent() 0 3 1
A getPhpContent() 0 36 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Conia\Quma\Commands;
6
7
use Conia\Cli\Opts;
8
9
class Add extends Command
10
{
11
    protected string $name = 'add-migration';
12
    protected string $group = 'Migrations';
13
    protected string $description = 'Initialize a new migrations';
14
15 18
    public function run(): string|int
16
    {
17 18
        $env = $this->env;
18 18
        $opts = new Opts();
19 18
        $fileName = $opts->get('-f', $opts->get('--file', ''));
20
21 18
        if (empty($fileName)) {
22
            // Would stop the test suit and wait for input
23
            // @codeCoverageIgnoreStart
24
            $fileName = readline('Name of the migration script: ');
25
            // @codeCoverageIgnoreEnd
26
        }
27
28 18
        $fileName = str_replace(' ', '-', $fileName);
29 18
        $fileName = str_replace('_', '-', $fileName);
30 18
        $fileName = strtolower($fileName);
31 18
        $ext = strtolower(pathinfo($fileName, PATHINFO_EXTENSION));
0 ignored issues
show
Bug introduced by
It seems like pathinfo($fileName, Coni...nds\PATHINFO_EXTENSION) can also be of type array; however, parameter $string of strtolower() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

31
        $ext = strtolower(/** @scrutinizer ignore-type */ pathinfo($fileName, PATHINFO_EXTENSION));
Loading history...
32
33 18
        if (!$ext) {
34 2
            $fileName .= '.sql';
35
        } else {
36 16
            if (!in_array($ext, ['sql', 'php', 'tpql'])) {
37 1
                echo "Wrong file extension '{$ext}'. Use 'sql', 'php' or 'tpql' instead.\nAborting.\n";
38
39 1
                return 1;
40
            }
41
        }
42
43 17
        $migrations = $env->conn->migrations();
44
45
        // Get the first migrations directory from the list (the last one added)
46
        // TODO: let the user choose the migrations dir if there are more than one
47 17
        $migrationsDir = $migrations[0];
48
49 17
        if ($migrationsDir && strpos($migrationsDir, '/vendor') !== false) {
50 1
            echo "The migrations directory is inside './vendor'.\n  -> {$migrationsDir}\nAborting.\n";
51
52 1
            return 1;
53
        }
54
55 16
        if (!is_writable($migrationsDir)) {
56 1
            echo "Migrations directory is not writable\n  -> {$migrationsDir}\nAborting. \n";
57
58 1
            return 1;
59
        }
60
61 15
        $timestamp = date('ymd-His', time());
62
63 15
        $migration = $migrationsDir . DIRECTORY_SEPARATOR . $timestamp . '-' . $fileName;
64 15
        $f = fopen($migration, 'w');
65
66 15
        if ($ext === 'php') {
67 4
            fwrite($f, $this->getPhpContent($fileName, $timestamp));
68 11
        } elseif ($ext === 'tpql') {
69 7
            fwrite($f, $this->getTpqlContent());
70
        }
71
72 15
        fclose($f);
73 15
        echo "Migration created:\n{$migration}\n";
74
75 15
        return $migration;
76
    }
77
78 4
    protected function getPhpContent(string $fileName, string $timestamp): string
79
    {
80
        // Translates what-is-up.sql into WhatIsUp
81 4
        $className = implode(
82 4
            '',
83 4
            explode(
84 4
                '-',
85 4
                explode(
86 4
                    '.',
87 4
                    ucwords($fileName, '-')
88 4
                )[0]
89 4
            )
90 4
        ) . '_' . str_replace('-', '_', $timestamp);
91
92 4
        return "<?php
93
94
declare(strict_types=1);
95
96
use \\PDO;
97
use Conia\\Quma\\Connection;
98
use Conia\\Quma\\Database;
99
use Conia\\Quma\\MigrationInterface;
100
101
102 4
class {$className} implements MigrationInterface
103
{
104
    public function run(Database \$db): bool
105
    {
106
        \$db->execute('')->run();
107
        \$result = \$db->execute('')->all(PDO::FETCH_ASSOC);
108
109
        return true;
110
    }
111
}
112
113 4
return new {$className}();";
114
    }
115
116 7
    protected function getTpqlContent(): string
117
    {
118 7
        return "<?php if (\$driver === 'pgsql') : ?>
119
120
<?php else : ?>
121
122
<?php endif ?>
123 7
";
124
    }
125
}
126