Completed
Push — master ( 233eeb...d5d4b8 )
by Dmitry
12:08
created

ColumnSchema   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 125
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 95.35%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 24
c 1
b 0
f 0
lcom 1
cbo 4
dl 0
loc 125
ccs 41
cts 43
cp 0.9535
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
C phpTypecastValue() 0 23 9
A getArrayParser() 0 10 2
B dbTypecast() 0 15 6
B phpTypecast() 0 21 7
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 200
    public function dbTypecast($value)
58
    {
59 200
        if ($value instanceof ExpressionInterface) {
60 85
            return $value;
61
        }
62
63 198
        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 2
            return new ArrayExpression($value, $this->dbType, $this->dimension);
65
        }
66 197
        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 4
            return new JsonExpression($value, $this->type);
68
        }
69
70 193
        return $this->typecast($value);
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76 145
    public function phpTypecast($value)
77
    {
78 145
        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 13
            if (!is_array($value)) {
80 13
                $value = $this->getArrayParser()->parse($value);
81
            }
82 13
            if (is_array($value)) {
83 4
                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 3
                    $val = $this->phpTypecastValue($val);
85 4
                });
86 11
            } elseif ($value === null) {
87 11
                return null;
88
            }
89
90 4
            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 4
                ? new ArrayExpression($value, $this->dbType, $this->dimension)
92 4
                : $value;
93
        }
94
95 145
        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 145
    protected function phpTypecastValue($value)
105
    {
106 145
        if ($value === null) {
107 43
            return null;
108
        }
109
110 145
        switch ($this->type) {
111 145
            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 145
            case Schema::TYPE_JSON:
122 34
                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 145
        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 13
    protected function getArrayParser()
134
    {
135 13
        static $parser = null;
136
137 13
        if ($parser === null) {
138 1
            $parser = new ArrayParser();
139
        }
140
141 13
        return $parser;
142
    }
143
}
144