Passed
Pull Request — master (#808)
by Sergei
15:21 queued 13:01
created

Column::phpTypecast()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Schema\Column;
6
7
use Yiisoft\Db\Constraint\ForeignKeyConstraint;
8
use Yiisoft\Db\Expression\Expression;
9
use Yiisoft\Db\Expression\ExpressionInterface;
10
11
use function array_key_exists;
12
13
/**
14
 * Represents the metadata of a column in a database table.
15
 *
16
 * It provides information about the column's name, type, size, precision, and other details.
17
 *
18
 * The `ColumnSchema` class is used to store and retrieve metadata about a column in a database table.
19
 *
20
 * It's typically used in conjunction with the TableSchema class, which represents the metadata of a database table as a
21
 * whole.
22
 *
23
 * Here is an example of how to use the `ColumnSchema` class:
24
 *
25
 * ```php
26
 * use Yiisoft\Db\Schema\ColumnSchema;
27
 *
28
 * $column = new ColumnSchema();
29
 * $column->name('id');
30
 * $column->allowNull(false);
31
 * $column->dbType('int(11)');
32
 * $column->phpType('integer');
33
 * $column->type('integer');
34
 * $column->defaultValue(0);
35
 * $column->autoIncrement();
36
 * $column->primaryKey();
37
 * ```
38
 */
39
abstract class Column implements ColumnInterface
40
{
41
    private bool|null $allowNull = null;
42
    private bool $autoIncrement = false;
43
    private string|ExpressionInterface|null $check = null;
0 ignored issues
show
introduced by
The private property $check is not used, and could be removed.
Loading history...
44
    private string|null $comment = null;
45
    private bool $computed = false;
46
    private string|null $dbType = null;
47
    private mixed $defaultValue = null;
48
    private string|null $extra = null;
49
    private bool $primaryKey = false;
50
    private ForeignKeyConstraint|null $reference = null;
0 ignored issues
show
introduced by
The private property $reference is not used, and could be removed.
Loading history...
51
    private int|null $scale = null;
52
    private int|null $size = null;
53
    private bool $unique = false;
0 ignored issues
show
introduced by
The private property $unique is not used, and could be removed.
Loading history...
54
    private bool $unsigned = false;
55
    private array $values = [];
56
57
    public function __construct(
58
        private string|null $type = null,
59
        private string|null $phpType = null,
60
    ) {
61
    }
62
63
    public function allowNull(bool|null $value = true): static
64
    {
65
        $this->allowNull = $value;
66
        return $this;
67
    }
68
69
    public function autoIncrement(bool $value = true): static
70
    {
71
        $this->autoIncrement = $value;
72
        return $this;
73
    }
74
75
    public function comment(string $value = null): static
76
    {
77
        $this->comment = $value;
78
        return $this;
79
    }
80
81
    public function computed(bool $value = true): static
82
    {
83
        $this->computed = $value;
84
        return $this;
85
    }
86
87
    public function dbType(string $value = null): static
88
    {
89
        $this->dbType = $value;
90
        return $this;
91
    }
92
93
    public function dbTypecast(mixed $value): mixed
94
    {
95
        return $value;
96
    }
97
98
    public function defaultValue(mixed $value = null): static
99
    {
100
        $this->defaultValue = $value;
101
        return $this;
102
    }
103
104
    public function extra(string $value = null): static
105
    {
106
        $this->extra = $value;
107
        return $this;
108
    }
109
110
    public function getComment(): string|null
111
    {
112
        return $this->comment;
113
    }
114
115
    public function getDbType(): string|null
116
    {
117
        return $this->dbType;
118
    }
119
120
    public function getDefaultValue(): mixed
121
    {
122
        return $this->defaultValue;
123
    }
124
125
    public function getExtra(): string|null
126
    {
127
        return $this->extra;
128
    }
129
130
    public function getFullDbType(): string|null
131
    {
132
        if ($this->dbType === null) {
133
            return null;
134
        }
135
136
        if ($this->size === null) {
137
            return $this->dbType;
138
        }
139
140
        return "$this->dbType($this->size)";
141
    }
142
143
    public function getPhpType(): string|null
144
    {
145
        return $this->phpType;
146
    }
147
148
    public function getScale(): int|null
149
    {
150
        return $this->scale;
151
    }
152
153
    public function getSize(): int|null
154
    {
155
        return $this->size;
156
    }
157
158
    public function getType(): string
159
    {
160
        return $this->type;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->type could return the type null which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
161
    }
162
163
    public function getValues(): array
164
    {
165
        return $this->values;
166
    }
167
168
    public function isAllowNull(): bool|null
169
    {
170
        return $this->allowNull;
171
    }
172
173
    public function isAutoIncrement(): bool
174
    {
175
        return $this->autoIncrement;
176
    }
177
178
    public function isComputed(): bool
179
    {
180
        return $this->computed;
181
    }
182
183
    public function isPrimaryKey(): bool
184
    {
185
        return $this->primaryKey;
186
    }
187
188
    public function isUnsigned(): bool
189
    {
190
        return $this->unsigned;
191
    }
192
193
    public function load(array $info): static
194
    {
195
        foreach ($info as $key => $value) {
196
            match ($key) {
197
                'allow_null' => $this->allowNull($value !== null ? (bool) $value : null),
198
                'auto_increment' => $this->autoIncrement((bool) $value),
199
                'comment' => $this->comment($value !== null ? (string) $value : null),
200
                'computed' => $this->computed((bool) $value),
201
                'db_type' => $this->dbType($value !== null ? (string) $value : null),
202
                'default_value' => $this->defaultValue($value),
203
                'extra' => $this->extra($value !== null ? (string) $value : null),
204
                'primary_key' => $this->primaryKey((bool) $value),
205
                'php_type' => $this->phpType($value !== null ? (string) $value : null),
206
                'scale' => $this->scale($value !== null ? (int) $value : null),
207
                'size' => $this->size($value !== null ? (int) $value : null),
208
                'type' => $this->type($value !== null ? (string) $value : null),
209
                'unsigned' => $this->unsigned((bool) $value),
210
                'values' => $this->values(is_array($value) ? $value : null),
0 ignored issues
show
Bug introduced by
It seems like is_array($value) ? $value : null can also be of type null; however, parameter $value of Yiisoft\Db\Schema\Column\Column::values() does only seem to accept array, 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

210
                'values' => $this->values(/** @scrutinizer ignore-type */ is_array($value) ? $value : null),
Loading history...
211
                default => null,
212
            };
213
        }
214
215
        if (array_key_exists('default_value_raw', $info)) {
216
            $this->defaultValue($this->normalizeDefaultValue($info['default_value_raw']));
217
        }
218
219
        return $this;
220
    }
221
222
    public function normalizeDefaultValue(string|null $value): mixed
223
    {
224
        if ($value === null || $this->computed || preg_match("/^\(?NULL\b/i", $value) === 1) {
225
            return null;
226
        }
227
228
        if (preg_match("/^'(.*)'|^\(([^()]*)\)/s", $value, $matches) === 1) {
229
            return $this->phpTypecast($matches[2] ?? str_replace("''", "'", $matches[1]));
230
        }
231
232
        return new Expression($value);
233
    }
234
235
    public function phpType(string $value = null): static
236
    {
237
        $this->phpType = $value;
238
        return $this;
239
    }
240
241
    public function phpTypecast(mixed $value): mixed
242
    {
243
        return $value;
244
    }
245
246
    public function primaryKey(bool $value = true): static
247
    {
248
        $this->primaryKey = $value;
249
        return $this;
250
    }
251
252
    public function scale(int $value = null): static
253
    {
254
        $this->scale = $value;
255
        return $this;
256
    }
257
258
    public function size(int $value = null): static
259
    {
260
        $this->size = $value;
261
        return $this;
262
    }
263
264
    public function type(string $value = null): static
265
    {
266
        $this->type = $value;
267
        return $this;
268
    }
269
270
    public function unsigned(bool $value = true): static
271
    {
272
        $this->unsigned = $value;
273
        return $this;
274
    }
275
276
    public function values(array $value = []): static
277
    {
278
        $this->values = $value;
279
        return $this;
280
    }
281
}
282