Passed
Pull Request — master (#752)
by Sergei
02:46
created

BigIntColumnSchema   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
c 1
b 0
f 0
dl 0
loc 28
rs 10
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A dbTypecast() 0 8 2
A __construct() 0 6 1
A phpTypecast() 0 7 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Schema\Column;
6
7
use Yiisoft\Db\Expression\ExpressionInterface;
8
use Yiisoft\Db\Schema\SchemaInterface;
9
10
use function is_int;
11
12
class BigIntColumnSchema extends AbstractColumnSchema
13
{
14
    public function __construct(string $name)
15
    {
16
        parent::__construct($name);
17
18
        $this->type(SchemaInterface::TYPE_BIGINT);
19
        $this->phpType(SchemaInterface::PHP_TYPE_INTEGER);
20
    }
21
22
    public function dbTypecast(mixed $value): int|string|ExpressionInterface|null
23
    {
24
        return match (true) {
25
            is_int($value), $value === null, $value instanceof ExpressionInterface => $value,
26
            $value === '' => null,
27
            $value === false => 0,
28
            PHP_INT_MIN <= $value && $value <= PHP_INT_MAX => (int) $value,
29
            default => (string) $value,
30
        };
31
    }
32
33
    public function phpTypecast(mixed $value): int|string|null
34
    {
35
        /** @psalm-var int|string|null $value */
36
        return match (true) {
37
            $value === null => null,
38
            PHP_INT_MIN <= $value && $value <= PHP_INT_MAX => (int) $value,
39
            default => (string) $value,
40
        };
41
    }
42
}
43