Test Failed
Pull Request — master (#188)
by Def
15:41 queued 02:37
created

ColumnSchema::defaultPhpTypecast()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 13
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 5
c 0
b 0
f 0
nc 5
nop 1
dl 0
loc 13
ccs 6
cts 6
cp 1
crap 4
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Mssql;
6
7
use Yiisoft\Db\Command\ParamInterface;
8
use Yiisoft\Db\Expression\Expression;
9
use Yiisoft\Db\Schema\AbstractColumnSchema;
10
use Yiisoft\Db\Schema\SchemaInterface;
11
12
use function bin2hex;
13
use function is_string;
14
use function substr;
15
16
/**
17
 * Class ColumnSchema for MSSQL database
18
 */
19
final class ColumnSchema extends AbstractColumnSchema
20
{
21
    public function dbTypecast(mixed $value): mixed
22
    {
23
        if ($this->getType() === SchemaInterface::TYPE_BINARY && $this->getDbType() === 'varbinary') {
24
            if ($value instanceof ParamInterface && is_string($value->getValue())) {
25
                $value = (string) $value->getValue();
26
            }
27
            if (is_string($value)) {
28 133
                return new Expression('CONVERT(VARBINARY(MAX), ' . ('0x' . bin2hex($value)) . ')');
29
            }
30 133
        }
31
32 80
        return parent::dbTypecast($value);
33
    }
34
}
35