Completed
Push — 2.1 ( 481970...56545c )
by
unknown
11:50
created

ColumnSchema   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 123
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 92.68%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 23
c 1
b 0
f 0
lcom 1
cbo 4
dl 0
loc 123
ccs 38
cts 41
cp 0.9268
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
B dbTypecast() 0 15 6
B phpTypecast() 0 19 6
C phpTypecastValue() 0 23 9
A getArrayParser() 0 10 2
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
     * @var bool whether the column schema should OMIT using JSON support feature.
27
     * You can use this property to make upgrade to Yii 2.0.14 easier.
28
     * Default to `false`, meaning JSON support is enabled.
29
     *
30
     * @since 2.0.14.1
31
     * @deprecated Since 2.0.14.1 and will be removed in 2.1.
32
     */
33
    public $disableJsonSupport = false;
34
    /**
35
     * @var bool whether the column schema should OMIT using PgSQL Arrays support feature.
36
     * You can use this property to make upgrade to Yii 2.0.14 easier.
37
     * Default to `false`, meaning Arrays support is enabled.
38
     *
39
     * @since 2.0.14.1
40
     * @deprecated Since 2.0.14.1 and will be removed in 2.1.
41
     */
42
    public $disableArraySupport = false;
43
    /**
44
     * @var bool whether the Array column value should be unserialized to an [[ArrayExpression]] object.
45
     * You can use this property to make upgrade to Yii 2.0.14 easier.
46
     * Default to `true`, meaning arrays are unserialized to [[ArrayExpression]] objects.
47
     *
48
     * @since 2.0.14.1
49
     * @deprecated Since 2.0.14.1 and will be removed in 2.1.
50
     */
51
    public $deserializeArrayColumnToArrayExpression = true;
52
53
54
    /**
55
     * {@inheritdoc}
56
     */
57 198
    public function dbTypecast($value)
58
    {
59 198
        if ($value instanceof ExpressionInterface) {
60 83
            return $value;
61
        }
62
63 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...
64
            return new ArrayExpression($value, $this->dbType, $this->dimension);
65
        }
66 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...
67 3
            return new JsonExpression($value, $this->type);
68
        }
69
70 193
        return $this->typecast($value);
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76 143
    public function phpTypecast($value)
77
    {
78 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...
79 11
            if (!is_array($value)) {
80 11
                $value = $this->getArrayParser()->parse($value);
81
            }
82 11
            if (is_array($value)) {
83 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...
84 2
                    $val = $this->phpTypecastValue($val);
85 2
                });
86
            }
87
88 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...
89 11
                ? new ArrayExpression($value, $this->dbType, $this->dimension)
90 11
                : $value;
91
        }
92
93 143
        return $this->phpTypecastValue($value);
94
    }
95
96
    /**
97
     * Casts $value after retrieving from the DBMS to PHP representation.
98
     *
99
     * @param string|null $value
100
     * @return bool|mixed|null
101
     */
102 143
    protected function phpTypecastValue($value)
103
    {
104 143
        if ($value === null) {
105 41
            return null;
106
        }
107
108 143
        switch ($this->type) {
109 143
            case Schema::TYPE_BOOLEAN:
110 56
                switch (strtolower($value)) {
111 56
                    case 't':
112 56
                    case 'true':
113
                        return true;
114 56
                    case 'f':
115 56
                    case 'false':
116
                        return false;
117
                }
118 56
                return (bool) $value;
119 143
            case Schema::TYPE_JSON:
120 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...
121
        }
122
123 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...
124
    }
125
126
    /**
127
     * Creates instance of ArrayParser
128
     *
129
     * @return ArrayParser
130
     */
131 11
    protected function getArrayParser()
132
    {
133 11
        static $parser = null;
134
135 11
        if ($parser === null) {
136 1
            $parser = new ArrayParser();
137
        }
138
139 11
        return $parser;
140
    }
141
}
142