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

BooleanColumn   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A phpTypecast() 0 7 3
A __construct() 0 5 1
A dbTypecast() 0 7 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 BooleanColumn extends Column
11
{
12
    public function __construct(
13
        string|null $type = SchemaInterface::TYPE_BOOLEAN,
14
        string|null $phpType = SchemaInterface::PHP_TYPE_BOOLEAN,
15
    ) {
16
        parent::__construct($type, $phpType);
17
    }
18
19
    public function dbTypecast(mixed $value): bool|ExpressionInterface|null
20
    {
21
        /** Optimized for performance. Do not merge cases or change order. */
22
        return match (true) {
23
            $value, $value === false, $value === null, $value instanceof ExpressionInterface => $value,
24
            $value === '' => null,
25
            default => (bool) $value,
26
        };
27
    }
28
29
    public function phpTypecast(mixed $value): bool|null
30
    {
31
        if ($value === null) {
32
            return null;
33
        }
34
35
        return $value && $value !== "\0";
36
    }
37
}
38