Test Failed
Pull Request — master (#315)
by Sergei
15:26 queued 02:13
created

BitColumnSchema   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 4
eloc 12
c 2
b 0
f 0
dl 0
loc 31
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A dbTypecast() 0 8 1
A phpTypecast() 0 9 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Pgsql\Column;
6
7
use Yiisoft\Db\Pgsql\Schema;
8
use Yiisoft\Db\Schema\Column\AbstractColumnSchema;
0 ignored issues
show
Bug introduced by
The type Yiisoft\Db\Schema\Column\AbstractColumnSchema was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
use Yiisoft\Db\Schema\SchemaInterface;
10
11
use function bindec;
12
use function decbin;
13
use function is_int;
14
use function str_pad;
15
16
final class BitColumnSchema extends AbstractColumnSchema
17
{
18
    public function __construct(string $name)
19
    {
20
        parent::__construct($name);
21
22
        $this->type(Schema::TYPE_BIT);
23
        $this->phpType(SchemaInterface::PHP_TYPE_INTEGER);
24
    }
25
26
    /** @psalm-suppress RedundantCast */
27
    public function dbTypecast(mixed $value): mixed
28
    {
29
        return match (true) {
30
            is_int($value), is_float($value) => str_pad(decbin((int) $value), (int) $this->getSize(), '0', STR_PAD_LEFT),
31
            $value === null, $value === '' => null,
32
            $value => '1',
33
            $value === false => '0',
34
            default => $value,
35
        };
36
    }
37
38
    public function phpTypecast(mixed $value): int|null
39
    {
40
        /** @psalm-var int|string|null $value */
41
        if (is_string($value)) {
42
            /** @psalm-var int */
43
            return bindec($value);
0 ignored issues
show
Bug Best Practice introduced by
The expression return bindec($value) could return the type double which is incompatible with the type-hinted return integer|null. Consider adding an additional type-check to rule them out.
Loading history...
44
        }
45
46
        return $value;
47
    }
48
}
49