Passed
Pull Request — master (#808)
by Sergei
02:31
created

IntegerColumn   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A phpTypecast() 0 7 2
A dbTypecast() 0 9 2
A __construct() 0 5 1
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
class IntegerColumn extends Column
11
{
12
    public function __construct(
13
        string|null $type = SchemaInterface::TYPE_INTEGER,
14
        string|null $phpType = SchemaInterface::PHP_TYPE_INTEGER,
15
    ) {
16
        parent::__construct($type, $phpType);
17
    }
18
19
    public function dbTypecast(mixed $value): int|ExpressionInterface|null
20
    {
21
        if ($value instanceof ExpressionInterface) {
22
            return $value;
23
        }
24
25
        return match ($value) {
26
            null, '' => null,
27
            default => (int) $value,
28
        };
29
    }
30
31
    public function phpTypecast(mixed $value): int|null
32
    {
33
        if ($value === null) {
34
            return null;
35
        }
36
37
        return (int) $value;
38
    }
39
}
40