Completed
Push — master ( 455b1f...25d176 )
by Dmitry
12:07
created

ColumnSchema::dbTypecast()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 6.0702

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 15
ccs 7
cts 8
cp 0.875
rs 8.8571
cc 6
eloc 8
nc 4
nop 1
crap 6.0702
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 for PostgreSQL database.
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
     * @var bool whether the column schema should OMIT using JSON support feature.
28
     * You can use this property to make upgrade to Yii 2.0.14 easier.
29
     * Default to `false`, meaning JSON support is enabled.
30
     *
31
     * @since 2.0.14.1
32
     * @deprecated Since 2.0.14.1 and will be removed in 2.1.
33
     */
34
    public $disableJsonSupport = false;
35
36
    /**
37
     * @var bool whether the column schema should OMIT using PgSQL Arrays support feature.
38
     * You can use this property to make upgrade to Yii 2.0.14 easier.
39
     * Default to `false`, meaning Arrays support is enabled.
40
     *
41
     * @since 2.0.14.1
42
     * @deprecated Since 2.0.14.1 and will be removed in 2.1.
43
     */
44
    public $disableArraySupport = false;
45
46
    /**
47
     * @var bool whether the Array column value should be unserialized to an [[ArrayExpression]] object.
48
     * You can use this property to make upgrade to Yii 2.0.14 easier.
49
     * Default to `true`, meaning arrays are unserilized to [[ArrayExpression]] objects.
50
     *
51
     * @since 2.0.14.1
52
     * @deprecated Since 2.0.14.1 and will be removed in 2.1.
53
     */
54
    public $deserializeArrayColumnToArrayExpression = true;
55
56
    /**
57
     * {@inheritdoc}
58
     */
59 198
    public function dbTypecast($value)
60
    {
61 198
        if ($value instanceof ExpressionInterface) {
62 83
            return $value;
63
        }
64
65 196
        if (!$this->disableArraySupport && $this->dimension > 0) {
0 ignored issues
show
Deprecated Code introduced by
The property yii\db\pgsql\ColumnSchema::$disableArraySupport has been deprecated with message: Since 2.0.14.1 and will be removed in 2.1.

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
66
            return new ArrayExpression($value, $this->dbType, $this->dimension);
67
        }
68 196
        if (!$this->disableJsonSupport && in_array($this->dbType, [Schema::TYPE_JSON, Schema::TYPE_JSONB], true)) {
0 ignored issues
show
Deprecated Code introduced by
The property yii\db\pgsql\ColumnSchema::$disableJsonSupport has been deprecated with message: Since 2.0.14.1 and will be removed in 2.1.

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
69 3
            return new JsonExpression($value, $this->type);
70
        }
71
72 193
        return $this->typecast($value);
73
    }
74
75
    /**
76
     * {@inheritdoc}
77
     */
78 143
    public function phpTypecast($value)
79
    {
80 143
        if (!$this->disableArraySupport && $this->dimension > 0) {
0 ignored issues
show
Deprecated Code introduced by
The property yii\db\pgsql\ColumnSchema::$disableArraySupport has been deprecated with message: Since 2.0.14.1 and will be removed in 2.1.

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
81 11
            if (!is_array($value)) {
82 11
                $value = $this->getArrayParser()->parse($value);
83
            }
84 11
            if (is_array($value)) {
85 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...
86 2
                    $val = $this->phpTypecastValue($val);
87 2
                });
88
            }
89
90 11
            return $this->deserializeArrayColumnToArrayExpression
0 ignored issues
show
Deprecated Code introduced by
The property yii\db\pgsql\ColumnSchem...ColumnToArrayExpression has been deprecated with message: Since 2.0.14.1 and will be removed in 2.1.

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
91 11
                ? new ArrayExpression($value, $this->dbType, $this->dimension)
92 11
                : $value;
93
        }
94
95 143
        return $this->phpTypecastValue($value);
96
    }
97
98
    /**
99
     * Casts $value after retrieving from the DBMS to PHP representation.
100
     *
101
     * @param string|null $value
102
     * @return bool|mixed|null
103
     */
104 143
    protected function phpTypecastValue($value)
105
    {
106 143
        if ($value === null) {
107 41
            return null;
108
        }
109
110 143
        switch ($this->type) {
111 143
            case Schema::TYPE_BOOLEAN:
112 56
                switch (strtolower($value)) {
113 56
                    case 't':
114 56
                    case 'true':
115
                        return true;
116 56
                    case 'f':
117 56
                    case 'false':
118
                        return false;
119
                }
120 56
                return (bool) $value;
121 143
            case Schema::TYPE_JSON:
122 33
                return $this->disableJsonSupport ? $value : json_decode($value, true);
0 ignored issues
show
Deprecated Code introduced by
The property yii\db\pgsql\ColumnSchema::$disableJsonSupport has been deprecated with message: Since 2.0.14.1 and will be removed in 2.1.

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
123
        }
124
125 143
        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...
126
    }
127
128
    /**
129
     * Creates instance of ArrayParser
130
     *
131
     * @return ArrayParser
132
     */
133 11
    protected function getArrayParser()
134
    {
135 11
        static $parser = null;
136
137 11
        if ($parser === null) {
138 1
            $parser = new ArrayParser();
139
        }
140
141 11
        return $parser;
142
    }
143
}
144