Completed
Push — master ( ca4e1a...e63a45 )
by Dmitry
21s
created

ColumnSchema   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 130
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 91.49%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 24
c 2
b 0
f 0
lcom 1
cbo 4
dl 0
loc 130
ccs 43
cts 47
cp 0.9149
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
B dbTypecast() 0 17 6
C phpTypecast() 0 24 7
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 208
    public function dbTypecast($value)
58
    {
59 208
        if ($value instanceof ExpressionInterface) {
60 89
            return $value;
61
        }
62
63 202
        if ($this->dimension > 0) {
64 2
            return $this->disableArraySupport
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...
65
                ? (string) $value
66 2
                : new ArrayExpression($value, $this->dbType, $this->dimension);
67
        }
68 201
        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 5
            return new JsonExpression($value, $this->dbType);
70
        }
71
72 196
        return $this->typecast($value);
73
    }
74
75
    /**
76
     * {@inheritdoc}
77
     */
78 151
    public function phpTypecast($value)
79
    {
80 151
        if ($this->dimension > 0) {
81 13
            if ($this->disableArraySupport) {
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...
82
                return $value;
83
            }
84 13
            if (!is_array($value)) {
85 13
                $value = $this->getArrayParser()->parse($value);
86
            }
87 13
            if (is_array($value)) {
88 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...
89 3
                    $val = $this->phpTypecastValue($val);
90 4
                });
91 11
            } elseif ($value === null) {
92 11
                return null;
93
            }
94
95 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...
96 4
                ? new ArrayExpression($value, $this->dbType, $this->dimension)
97 4
                : $value;
98
        }
99
100 151
        return $this->phpTypecastValue($value);
101
    }
102
103
    /**
104
     * Casts $value after retrieving from the DBMS to PHP representation.
105
     *
106
     * @param string|null $value
107
     * @return bool|mixed|null
108
     */
109 151
    protected function phpTypecastValue($value)
110
    {
111 151
        if ($value === null) {
112 43
            return null;
113
        }
114
115 151
        switch ($this->type) {
116 151
            case Schema::TYPE_BOOLEAN:
117 56
                switch (strtolower($value)) {
118 56
                    case 't':
119 56
                    case 'true':
120
                        return true;
121 56
                    case 'f':
122 56
                    case 'false':
123
                        return false;
124
                }
125 56
                return (bool) $value;
126 151
            case Schema::TYPE_JSON:
127 40
                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...
128
        }
129
130 151
        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...
131
    }
132
133
    /**
134
     * Creates instance of ArrayParser
135
     *
136
     * @return ArrayParser
137
     */
138 13
    protected function getArrayParser()
139
    {
140 13
        static $parser = null;
141
142 13
        if ($parser === null) {
143 1
            $parser = new ArrayParser();
144
        }
145
146 13
        return $parser;
147
    }
148
}
149