CreateCompiler   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
eloc 15
dl 0
loc 38
rs 10
c 1
b 0
f 0
ccs 4
cts 4
cp 1

4 Methods

Rating   Name   Duplication   Size   Complexity  
A beforeTable() 0 3 2
A compileLike() 0 5 2
A compileColumns() 0 3 1
A compile() 0 13 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Umbrellio\Postgres\Compilers;
6
7
use Illuminate\Database\Schema\Blueprint;
8
use Illuminate\Database\Schema\Grammars\Grammar;
9
use Illuminate\Support\Fluent;
10
11
class CreateCompiler
12
{
13
    /**
14
     * @codeCoverageIgnore
15
     */
16
    public static function compile(Grammar $grammar, Blueprint $blueprint, array $columns, array $commands = []): string
17
    {
18
        $compiledCommand = sprintf(
19
            '%s table %s %s (%s)',
20
            $blueprint->temporary ? 'create temporary' : 'create',
21
            self::beforeTable($commands['ifNotExists']),
22
            $grammar->wrapTable($blueprint),
23
            $commands['like']
24
                ? self::compileLike($grammar, $commands['like'])
25
                : self::compileColumns($columns)
26
        );
27
28
        return str_replace('  ', ' ', trim($compiledCommand));
29
    }
30
31 27
    private static function beforeTable(?Fluent $command = null): string
32
    {
33 27
        return $command ? 'if not exists' : '';
34
    }
35
36
    /**
37
     * @codeCoverageIgnore
38
     */
39
    private static function compileLike(Grammar $grammar, Fluent $command): string
40
    {
41
        $table = $command->get('table');
42
        $includingAll = $command->get('includingAll') ? ' including all' : '';
43
        return "like {$grammar->wrapTable($table)}{$includingAll}";
44
    }
45
46 27
    private static function compileColumns(array $columns): string
47
    {
48 27
        return implode(', ', $columns);
49
    }
50
}
51