Completed
Push — master ( b4f1a5...4c1600 )
by Carsten
80:08 queued 71:13
created

framework/db/pgsql/ColumnSchema.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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