Passed
Push — master ( 1f0d9b...6a4b0f )
by Wilmer
12:11 queued 10:38
created

ColumnSchema::dbTypecast()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4.0312

Importance

Changes 0
Metric Value
cc 4
eloc 7
nc 4
nop 1
dl 0
loc 15
ccs 7
cts 8
cp 0.875
crap 4.0312
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Mysql\Schema;
6
7
use Yiisoft\Db\Expression\ExpressionInterface;
8
use Yiisoft\Db\Expression\JsonExpression;
9
use Yiisoft\Db\Schema\ColumnSchema as AbstractColumnSchema;
10
11
/**
12
 * Class ColumnSchema for MySQL database.
13
 */
14
class ColumnSchema extends AbstractColumnSchema
15
{
16
    /**
17
     * {@inheritdoc}
18
     */
19 44
    public function dbTypecast($value)
20
    {
21 44
        if ($value === null) {
22 5
            return $value;
23
        }
24
25 44
        if ($value instanceof ExpressionInterface) {
26 6
            return $value;
27
        }
28
29 42
        if ($this->getDbType() === Schema::TYPE_JSON) {
30
            return new JsonExpression($value, $this->getType());
31
        }
32
33 42
        return $this->typecast($value);
34
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39 80
    public function phpTypecast($value)
40
    {
41 80
        if ($value === null) {
42 79
            return;
43
        }
44
45 61
        if ($this->getType() === Schema::TYPE_JSON) {
46
            return \json_decode($value, true);
47
        }
48
49 61
        return parent::phpTypecast($value);
50
    }
51
}
52