Passed
Pull Request — 2.2 (#20357)
by Wilmer
13:33 queued 05:55
created

ColumnSchema   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 13
c 0
b 0
f 0
dl 0
loc 36
ccs 0
cts 14
cp 0
rs 10
wmc 7

2 Methods

Rating   Name   Duplication   Size   Complexity  
A phpTypecast() 0 11 3
A dbTypecast() 0 15 4
1
<?php
2
3
/**
4
 * @link https://www.yiiframework.com/
5
 * @copyright Copyright (c) 2008 Yii Software LLC
6
 * @license https://www.yiiframework.com/license/
7
 */
8
9
namespace yii\db\mysql;
10
11
use yii\db\ExpressionInterface;
12
use yii\db\JsonExpression;
13
14
/**
15
 * Class ColumnSchema for MySQL database
16
 *
17
 * @author Dmytro Naumenko <[email protected]>
18
 * @since 2.0.14.1
19
 */
20
class ColumnSchema extends \yii\db\ColumnSchema
21
{
22
    /**
23
     * {@inheritdoc}
24
     */
25
    public function dbTypecast($value)
26
    {
27
        if ($value === null) {
28
            return $value;
29
        }
30
31
        if ($value instanceof ExpressionInterface) {
32
            return $value;
33
        }
34
35
        if ($this->dbType === Schema::TYPE_JSON) {
36
            return new JsonExpression($value, $this->type);
37
        }
38
39
        return $this->typecast($value);
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45
    public function phpTypecast($value)
46
    {
47
        if ($value === null) {
48
            return null;
49
        }
50
51
        if ($this->type === Schema::TYPE_JSON) {
52
            return json_decode($value, true);
53
        }
54
55
        return parent::phpTypecast($value);
56
    }
57
}
58