Completed
Push — 2.1 ( b44a46...4c2160 )
by
unknown
12:30
created

ColumnSchema::dbTypecast()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4.0312

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 7
cts 8
cp 0.875
rs 9.2
c 0
b 0
f 0
cc 4
eloc 8
nc 4
nop 1
crap 4.0312
1
<?php
2
/**
3
 * @link http://www.yiiframework.com/
4
 * @copyright Copyright (c) 2008 Yii Software LLC
5
 * @license http://www.yiiframework.com/license/
6
 */
7
8
namespace yii\db\pgsql;
9
10
use yii\db\ArrayExpression;
11
use yii\db\ExpressionInterface;
12
use yii\db\JsonExpression;
13
14
/**
15
 * Class ColumnSchema
16
 *
17
 * @author Dmytro Naumenko <[email protected]>
18
 */
19
class ColumnSchema extends \yii\db\ColumnSchema
20
{
21
    /**
22
     * @var int the dimension of array. Defaults to 0, means this column is not an array.
23
     */
24
    public $dimension = 0;
25
26
27
    /**
28
     * {@inheritdoc}
29
     */
30 196
    public function dbTypecast($value)
31
    {
32 196
        if ($value instanceof ExpressionInterface) {
33 78
            return $value;
34
        }
35
36 195
        if ($this->dimension > 0) {
37
            return new ArrayExpression($value, $this->dbType, $this->dimension);
38
        }
39 195
        if (in_array($this->dbType, [Schema::TYPE_JSON, Schema::TYPE_JSONB], true)) {
40 2
            return new JsonExpression($value, $this->type);
41
        }
42
43 193
        return $this->typecast($value);
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49 141
    public function phpTypecast($value)
50
    {
51 141
        if ($this->dimension > 0) {
52 11
            if (!is_array($value)) {
53 11
                $value = $this->getArrayParser()->parse($value);
54
            }
55 11
            if (is_array($value)) {
56 2
                array_walk_recursive($value, function (&$val, $key) {
0 ignored issues
show
Unused Code introduced by
The parameter $key is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
57 2
                    $val = $this->phpTypecastValue($val);
58 2
                });
59
            }
60
61 11
            return new ArrayExpression($value, $this->dbType, $this->dimension);
62
        }
63
64 141
        return $this->phpTypecastValue($value);
65
    }
66
67
    /**
68
     * Casts $value after retrieving from the DBMS to PHP representation.
69
     *
70
     * @param string|null $value
71
     * @return bool|mixed|null
72
     */
73 141
    protected function phpTypecastValue($value)
74
    {
75 141
        if ($value === null) {
76 41
            return null;
77
        }
78
79 141
        switch ($this->type) {
80 141
            case Schema::TYPE_BOOLEAN:
81 56
                switch (strtolower($value)) {
82 56
                    case 't':
83 56
                    case 'true':
84
                        return true;
85 56
                    case 'f':
86 56
                    case 'false':
87
                        return false;
88
                }
89 56
                return (bool) $value;
90 141
            case Schema::TYPE_JSON:
91 33
                return json_decode($value, true);
92
        }
93
94 140
        return parent::phpTypecast($value);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (phpTypecast() instead of phpTypecastValue()). Are you sure this is correct? If so, you might want to change this to $this->phpTypecast().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
95
    }
96
97
    /**
98
     * Creates instance of ArrayParser
99
     *
100
     * @return ArrayParser
101
     */
102 11
    protected function getArrayParser()
103
    {
104 11
        static $parser = null;
105
106 11
        if ($parser === null) {
107 1
            $parser = new ArrayParser();
108
        }
109
110 11
        return $parser;
111
    }
112
}
113