Passed
Push — 2.2 ( c9917f...37714b )
by Alexander
02:01 queued 17s
created

ColumnSchema::phpTypecast()   B

Complexity

Conditions 7
Paths 12

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 11.1032

Importance

Changes 0
Metric Value
cc 7
eloc 14
nc 12
nop 1
dl 0
loc 23
ccs 9
cts 16
cp 0.5625
crap 11.1032
rs 8.8333
c 0
b 0
f 0
1
<?php
2
/**
3
 * @link https://www.yiiframework.com/
4
 * @copyright Copyright (c) 2008 Yii Software LLC
5
 * @license https://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
     * @var string name of associated sequence if column is auto-incremental
54
     * @since 2.0.29
55
     */
56
    public $sequenceName;
57
58
59
    /**
60
     * {@inheritdoc}
61
     */
62 5
    public function dbTypecast($value)
63
    {
64 5
        if ($value === null) {
65
            return $value;
66
        }
67
68 5
        if ($value instanceof ExpressionInterface) {
69 5
            return $value;
70
        }
71
72
        if ($this->dimension > 0) {
73
            return $this->disableArraySupport
0 ignored issues
show
Deprecated Code introduced by
The property yii\db\pgsql\ColumnSchema::$disableArraySupport has been deprecated: Since 2.0.14.1 and will be removed in 2.1. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

73
            return /** @scrutinizer ignore-deprecated */ $this->disableArraySupport

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...
74
                ? (string) $value
75
                : new ArrayExpression($value, $this->dbType, $this->dimension);
76
        }
77
        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: Since 2.0.14.1 and will be removed in 2.1. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

77
        if (!/** @scrutinizer ignore-deprecated */ $this->disableJsonSupport && in_array($this->dbType, [Schema::TYPE_JSON, Schema::TYPE_JSONB], true)) {

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...
Bug introduced by
The type yii\db\pgsql\Schema was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
78
            return new JsonExpression($value, $this->dbType);
79
        }
80
81
        return $this->typecast($value);
82
    }
83
84
    /**
85
     * {@inheritdoc}
86
     */
87 5
    public function phpTypecast($value)
88
    {
89 5
        if ($this->dimension > 0) {
90 5
            if ($this->disableArraySupport) {
0 ignored issues
show
Deprecated Code introduced by
The property yii\db\pgsql\ColumnSchema::$disableArraySupport has been deprecated: Since 2.0.14.1 and will be removed in 2.1. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

90
            if (/** @scrutinizer ignore-deprecated */ $this->disableArraySupport) {

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
                return $value;
92
            }
93 5
            if (!is_array($value)) {
94 5
                $value = $this->getArrayParser()->parse($value);
95
            }
96 5
            if (is_array($value)) {
97
                array_walk_recursive($value, function (&$val, $key) {
98
                    $val = $this->phpTypecastValue($val);
99
                });
100 5
            } elseif ($value === null) {
0 ignored issues
show
introduced by
The condition $value === null is always true.
Loading history...
101 5
                return null;
102
            }
103
104
            return $this->deserializeArrayColumnToArrayExpression
0 ignored issues
show
Deprecated Code introduced by
The property yii\db\pgsql\ColumnSchem...ColumnToArrayExpression has been deprecated: Since 2.0.14.1 and will be removed in 2.1. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

104
            return /** @scrutinizer ignore-deprecated */ $this->deserializeArrayColumnToArrayExpression

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...
105
                ? new ArrayExpression($value, $this->dbType, $this->dimension)
106
                : $value;
107
        }
108
109 5
        return $this->phpTypecastValue($value);
110
    }
111
112
    /**
113
     * Casts $value after retrieving from the DBMS to PHP representation.
114
     *
115
     * @param string|null $value
116
     * @return bool|mixed|null
117
     */
118 5
    protected function phpTypecastValue($value)
119
    {
120 5
        if ($value === null) {
121 5
            return null;
122
        }
123
124 5
        switch ($this->type) {
125
            case Schema::TYPE_BOOLEAN:
126
                switch (strtolower($value)) {
127
                    case 't':
128
                    case 'true':
129
                        return true;
130
                    case 'f':
131
                    case 'false':
132
                        return false;
133
                }
134
                return (bool) $value;
135
            case Schema::TYPE_JSON:
136 5
                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: Since 2.0.14.1 and will be removed in 2.1. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

136
                return /** @scrutinizer ignore-deprecated */ $this->disableJsonSupport ? $value : json_decode($value, true);

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...
137
        }
138
139 5
        return parent::phpTypecast($value);
140
    }
141
142
    /**
143
     * Creates instance of ArrayParser
144
     *
145
     * @return ArrayParser
146
     */
147 5
    protected function getArrayParser()
148
    {
149 5
        static $parser = null;
150
151 5
        if ($parser === null) {
152 1
            $parser = new ArrayParser();
153
        }
154
155 5
        return $parser;
156
    }
157
}
158