Failed Conditions
Pull Request — master (#58)
by Dallas
06:33
created

Blueprint::check()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 1
nop 2
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Umbrellio\Postgres\Schema;
6
7
use Illuminate\Database\Schema\Blueprint as BaseBlueprint;
8
use Illuminate\Database\Schema\ColumnDefinition;
9
use Illuminate\Support\Facades\Schema;
10
use Illuminate\Support\Fluent;
11
use Umbrellio\Postgres\Schema\Builders\Constraints\Check\CheckBuilder;
12
use Umbrellio\Postgres\Schema\Builders\Constraints\Exclude\ExcludeBuilder;
13
use Umbrellio\Postgres\Schema\Builders\Indexes\Unique\UniqueBuilder;
14
use Umbrellio\Postgres\Schema\Definitions\AttachPartitionDefinition;
15
use Umbrellio\Postgres\Schema\Definitions\CheckDefinition;
16
use Umbrellio\Postgres\Schema\Definitions\ExcludeDefinition;
17
use Umbrellio\Postgres\Schema\Definitions\LikeDefinition;
18
use Umbrellio\Postgres\Schema\Definitions\UniqueDefinition;
19
use Umbrellio\Postgres\Schema\Definitions\ViewDefinition;
20
21
class Blueprint extends BaseBlueprint
22
{
23
    protected $commands = [];
24
25
    /**
26
     * @return AttachPartitionDefinition|Fluent
27
     */
28 4
    public function attachPartition(string $partition)
29
    {
30 4
        return $this->addCommand('attachPartition', compact('partition'));
0 ignored issues
show
Bug introduced by
The method addCommand() does not exist on Umbrellio\Postgres\Schema\Blueprint. Did you maybe mean addColumn()? ( Ignorable by Annotation )

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

30
        return $this->/** @scrutinizer ignore-call */ addCommand('attachPartition', compact('partition'));

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
31
    }
32
33 1
    public function detachPartition(string $partition): void
34
    {
35 1
        $this->addCommand('detachPartition', compact('partition'));
36 1
    }
37
38
    /**
39
     * @return LikeDefinition|Fluent
40
     */
41 2
    public function like(string $table)
42
    {
43 2
        return $this->addCommand('like', compact('table'));
44
    }
45
46 1
    public function ifNotExists(): Fluent
47
    {
48 1
        return $this->addCommand('ifNotExists');
49
    }
50
51
    /**
52
     * @param array|string $columns
53
     * @return UniqueDefinition|UniqueBuilder
54
     */
55 13
    public function uniquePartial($columns, ?string $index = null, ?string $algorithm = null)
56
    {
57 13
        $columns = (array) $columns;
58
59 13
        $index = $index ?: $this->createIndexName('unique', $columns);
0 ignored issues
show
Bug introduced by
The method createIndexName() does not exist on Umbrellio\Postgres\Schema\Blueprint. ( Ignorable by Annotation )

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

59
        $index = $index ?: $this->/** @scrutinizer ignore-call */ createIndexName('unique', $columns);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
60
61 13
        return $this->addExtendedCommand(
62 13
            UniqueBuilder::class,
63 13
            'uniquePartial',
64 13
            compact('columns', 'index', 'algorithm')
65
        );
66
    }
67
68 12
    public function dropUniquePartial($index): Fluent
69
    {
70 12
        return $this->dropIndexCommand('dropIndex', 'unique', $index);
0 ignored issues
show
Bug introduced by
The method dropIndexCommand() does not exist on Umbrellio\Postgres\Schema\Blueprint. ( Ignorable by Annotation )

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

70
        return $this->/** @scrutinizer ignore-call */ dropIndexCommand('dropIndex', 'unique', $index);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
71
    }
72
73
    /**
74
     * @param array|string $columns
75
     * @return ExcludeDefinition|ExcludeBuilder
76
     */
77 6
    public function exclude($columns, ?string $index = null)
78
    {
79 6
        $columns = (array) $columns;
80
81 6
        $index = $index ?: $this->createIndexName('excl', $columns);
82
83 6
        return $this->addExtendedCommand(ExcludeBuilder::class, 'exclude', compact('columns', 'index'));
84
    }
85
86
    /**
87
     * @param array|string $columns
88
     * @return CheckDefinition|CheckBuilder
89
     */
90 3
    public function check($columns, ?string $index = null)
91
    {
92 3
        $columns = (array) $columns;
93
94 3
        $index = $index ?: $this->createIndexName('chk', $columns);
95
96 3
        return $this->addExtendedCommand(CheckBuilder::class, 'check', compact('columns', 'index'));
97
    }
98
99 1
    public function dropExclude($index): Fluent
100
    {
101 1
        return $this->dropIndexCommand('dropUnique', 'excl', $index);
102
    }
103
104 1
    public function dropCheck($index): Fluent
105
    {
106 1
        return $this->dropIndexCommand('dropUnique', 'chk', $index);
107
    }
108
109 2
    public function hasIndex($index, bool $unique = false): bool
110
    {
111 2
        if (is_array($index)) {
112 2
            $index = $this->createIndexName($unique === false ? 'index' : 'unique', $index);
113
        }
114
115 2
        return array_key_exists($index, $this->getSchemaManager()->listTableIndexes($this->getTable()));
116
    }
117
118
    /**
119
     * @return ViewDefinition|Fluent
120
     */
121 2
    public function createView(string $view, string $select, bool $materialize = false): Fluent
122
    {
123 2
        return $this->addCommand('createView', compact('view', 'select', 'materialize'));
124
    }
125
126 2
    public function dropView(string $view): Fluent
127
    {
128 2
        return $this->addCommand('dropView', compact('view'));
129
    }
130
131
    /**
132
     * Almost like 'decimal' type, but can be with variable precision (by default)
133
     */
134 3
    public function numeric(string $column, ?int $precision = null, ?int $scale = null): ColumnDefinition
135
    {
136 3
        return $this->addColumn('numeric', $column, compact('precision', 'scale'));
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->addColumn(...('precision', 'scale')) could return the type Illuminate\Support\Fluent which is incompatible with the type-hinted return Illuminate\Database\Schema\ColumnDefinition. Consider adding an additional type-check to rule them out.
Loading history...
137
    }
138
139 1
    public function tsrange(string $column): ColumnDefinition
140
    {
141 1
        return $this->addColumn('tsrange', $column);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->addColumn('tsrange', $column) could return the type Illuminate\Support\Fluent which is incompatible with the type-hinted return Illuminate\Database\Schema\ColumnDefinition. Consider adding an additional type-check to rule them out.
Loading history...
142
    }
143
144 2
    protected function getSchemaManager()
145
    {
146 2
        return Schema::getConnection()->getDoctrineSchemaManager();
147
    }
148
149 22
    private function addExtendedCommand(string $fluent, string $name, array $parameters = [])
150
    {
151 22
        $command = new $fluent(array_merge(compact('name'), $parameters));
152 22
        $this->commands[] = $command;
153 22
        return $command;
154
    }
155
}
156