Failed Conditions
Pull Request — master (#28)
by Dallas
08:29
created

Blueprint::numeric()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 3
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\Builders\Routines\CreateFunctionBuilder;
15
use Umbrellio\Postgres\Schema\Builders\Routines\CreateProcedureBuilder;
16
use Umbrellio\Postgres\Schema\Builders\Routines\CreateTriggerBuilder;
17
use Umbrellio\Postgres\Schema\Builders\Routines\DropFunctionBuilder;
18
use Umbrellio\Postgres\Schema\Builders\Routines\DropProcedureBuilder;
19
use Umbrellio\Postgres\Schema\Builders\Routines\DropTriggerBuilder;
20
use Umbrellio\Postgres\Schema\Definitions\Indexes\CheckDefinition;
21
use Umbrellio\Postgres\Schema\Definitions\Indexes\ExcludeDefinition;
22
use Umbrellio\Postgres\Schema\Definitions\Indexes\UniqueDefinition;
23
use Umbrellio\Postgres\Schema\Definitions\Routines\Functions\CreateFunctionDefinition;
24
use Umbrellio\Postgres\Schema\Definitions\Routines\Functions\DropFunctionDefinition;
25
use Umbrellio\Postgres\Schema\Definitions\Routines\Procedures\CreateProcedureDefinition;
26
use Umbrellio\Postgres\Schema\Definitions\Routines\Procedures\DropProcedureDefinition;
27
use Umbrellio\Postgres\Schema\Definitions\Routines\Triggers\CreateTriggerDefinition;
28
use Umbrellio\Postgres\Schema\Definitions\Routines\Triggers\DropTriggerDefinition;
29
use Umbrellio\Postgres\Schema\Definitions\Tables\AttachPartitionDefinition;
30
use Umbrellio\Postgres\Schema\Definitions\Tables\LikeDefinition;
31
use Umbrellio\Postgres\Schema\Definitions\Views\ViewDefinition;
32
33
class Blueprint extends BaseBlueprint
34
{
35
    /**
36
     * @return AttachPartitionDefinition
37
     */
38
    public function attachPartition(string $partition)
39
    {
40
        return $this->addCommand('attachPartition', compact('partition'));
0 ignored issues
show
Bug introduced by
The method addCommand() 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

40
        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...
41
    }
42
43
    public function detachPartition(string $partition): void
44
    {
45
        $this->addCommand('detachPartition', compact('partition'));
46
    }
47
48
    /**
49
     * @return LikeDefinition
50
     */
51
    public function like(string $table)
52
    {
53
        return $this->addCommand('like', compact('table'));
54
    }
55
56
    public function ifNotExists(): Fluent
57
    {
58
        return $this->addCommand('ifNotExists');
59
    }
60
61
    /**
62
     * @param array|string $columns
63
     * @return UniqueDefinition
64
     */
65
    public function uniquePartial($columns, ?string $index = null, ?string $algorithm = null)
66
    {
67
        $columns = (array) $columns;
68
69
        $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

69
        $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...
70
71
        return $this->addExtendedCommand(
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->addExtende... 'index', 'algorithm')) returns the type Umbrellio\Postgres\Schem...es\Unique\UniqueBuilder which is incompatible with the documented return type Umbrellio\Postgres\Schem...ndexes\UniqueDefinition.
Loading history...
72
            UniqueBuilder::class,
73
            'uniquePartial',
74
            compact('columns', 'index', 'algorithm')
75
        );
76
    }
77
78
    public function dropUniquePartial($index): Fluent
79
    {
80
        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

80
        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...
81
    }
82
83
    /**
84
     * @param array|string $columns
85
     * @return ExcludeDefinition
86
     */
87
    public function exclude($columns, ?string $index = null)
88
    {
89
        $columns = (array) $columns;
90
91
        $index = $index ?: $this->createIndexName('excl', $columns);
92
93
        return $this->addExtendedCommand(ExcludeBuilder::class, 'exclude', compact('columns', 'index'));
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->addExtende...ct('columns', 'index')) returns the type Umbrellio\Postgres\Schem...\Exclude\ExcludeBuilder which is incompatible with the documented return type Umbrellio\Postgres\Schem...dexes\ExcludeDefinition.
Loading history...
94
    }
95
96
    /**
97
     * @param array|string $columns
98
     * @return CheckDefinition
99
     */
100
    public function check($columns, ?string $index = null)
101
    {
102
        $columns = (array) $columns;
103
104
        $index = $index ?: $this->createIndexName('chk', $columns);
105
106
        return $this->addExtendedCommand(CheckBuilder::class, 'check', compact('columns', 'index'));
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->addExtende...ct('columns', 'index')) returns the type Umbrellio\Postgres\Schem...ints\Check\CheckBuilder which is incompatible with the documented return type Umbrellio\Postgres\Schem...Indexes\CheckDefinition.
Loading history...
107
    }
108
109
    public function dropExclude($index): Fluent
110
    {
111
        return $this->dropIndexCommand('dropUnique', 'excl', $index);
112
    }
113
114
    public function dropCheck($index): Fluent
115
    {
116
        return $this->dropIndexCommand('dropUnique', 'chk', $index);
117
    }
118
119
    public function hasIndex($index, bool $unique = false): bool
120
    {
121
        if (is_array($index)) {
122
            $index = $this->createIndexName($unique === false ? 'index' : 'unique', $index);
123
        }
124
125
        return array_key_exists($index, $this->getSchemaManager()->listTableIndexes($this->getTable()));
0 ignored issues
show
Bug introduced by
The method getTable() 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

125
        return array_key_exists($index, $this->getSchemaManager()->listTableIndexes($this->/** @scrutinizer ignore-call */ getTable()));

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...
126
    }
127
128
    /**
129
     * @return ViewDefinition
130
     */
131
    public function createView(string $view, string $select, bool $materialize = false)
132
    {
133
        return $this->addCommand('createView', compact('view', 'select', 'materialize'));
134
    }
135
136
    /**
137
     * @return CreateTriggerDefinition
138
     */
139
    public function createTrigger(string $name)
140
    {
141
        return $this->addExtendedCommand(CreateTriggerBuilder::class, 'createTrigger', compact('name'));
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->addExtende...gger', compact('name')) returns the type Umbrellio\Postgres\Schem...es\CreateTriggerBuilder which is incompatible with the documented return type Umbrellio\Postgres\Schem...CreateTriggerDefinition.
Loading history...
142
    }
143
144
    /**
145
     * @return CreateFunctionDefinition
146
     */
147
    public function createFunction(string $name)
148
    {
149
        return $this->addExtendedCommand(CreateFunctionBuilder::class, 'createFunction', compact('name'));
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->addExtende...tion', compact('name')) returns the type Umbrellio\Postgres\Schem...s\CreateFunctionBuilder which is incompatible with the documented return type Umbrellio\Postgres\Schem...reateFunctionDefinition.
Loading history...
150
    }
151
152
    /**
153
     * @return CreateProcedureDefinition
154
     */
155
    public function createProcedure(string $name)
156
    {
157
        return $this->addExtendedCommand(CreateProcedureBuilder::class, 'createProcedure', compact('name'));
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->addExtende...dure', compact('name')) returns the type Umbrellio\Postgres\Schem...\CreateProcedureBuilder which is incompatible with the documented return type Umbrellio\Postgres\Schem...eateProcedureDefinition.
Loading history...
158
    }
159
160
    /**
161
     * @return DropFunctionDefinition
162
     */
163
    public function dropFunction(string $name)
164
    {
165
        return $this->addExtendedCommand(DropFunctionBuilder::class, 'dropFunction', compact('name'));
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->addExtende...tion', compact('name')) returns the type Umbrellio\Postgres\Schem...nes\DropFunctionBuilder which is incompatible with the documented return type Umbrellio\Postgres\Schem...\DropFunctionDefinition.
Loading history...
166
    }
167
168
    /**
169
     * @return DropProcedureDefinition
170
     */
171
    public function dropProcedure(string $name)
172
    {
173
        return $this->addExtendedCommand(DropProcedureBuilder::class, 'dropProcedure', compact('name'));
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->addExtende...dure', compact('name')) returns the type Umbrellio\Postgres\Schem...es\DropProcedureBuilder which is incompatible with the documented return type Umbrellio\Postgres\Schem...DropProcedureDefinition.
Loading history...
174
    }
175
176
    public function dropView(string $view): Fluent
177
    {
178
        return $this->addCommand('dropView', compact('view'));
179
    }
180
181
    /**
182
     * @return DropTriggerDefinition
183
     */
184
    public function dropTrigger(string $name, bool $dropDepends = false)
185
    {
186
        return $this->addExtendedCommand(DropTriggerBuilder::class, 'dropTrigger', compact('name', 'dropDepends'));
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->addExtende...'name', 'dropDepends')) returns the type Umbrellio\Postgres\Schem...ines\DropTriggerBuilder which is incompatible with the documented return type Umbrellio\Postgres\Schem...s\DropTriggerDefinition.
Loading history...
187
    }
188
189
    /**
190
     * Almost like 'decimal' type, but can be with variable precision (by default)
191
     */
192
    public function numeric(string $column, ?int $precision = null, ?int $scale = null): ColumnDefinition
193
    {
194
        return $this->addColumn('numeric', $column, compact('precision', 'scale'));
0 ignored issues
show
Bug introduced by
The method addColumn() 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

194
        return $this->/** @scrutinizer ignore-call */ addColumn('numeric', $column, compact('precision', 'scale'));

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...
195
    }
196
197
    public function tsrange(string $column): ColumnDefinition
198
    {
199
        return $this->addColumn('tsrange', $column);
200
    }
201
202
    protected function getSchemaManager()
203
    {
204
        return Schema::getConnection()->getDoctrineSchemaManager();
205
    }
206
207
    private function addExtendedCommand(string $fluent, string $name, array $parameters = [])
208
    {
209
        $command = new $fluent(array_merge(compact('name'), $parameters));
210
        $this->commands[] = $command;
0 ignored issues
show
Bug Best Practice introduced by
The property commands does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
211
        return $command;
212
    }
213
}
214