Passed
Pull Request — master (#20362)
by Wilmer
05:37
created

ColumnSchema::phpTypecast()   A

Complexity

Conditions 5
Paths 7

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 10
c 1
b 0
f 0
nc 7
nop 1
dl 0
loc 18
ccs 0
cts 12
cp 0
crap 30
rs 9.6111
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 string name of associated sequence if column is auto-incremental
27
     * @since 2.0.29
28
     */
29
    public $sequenceName;
30
31
32
    /**
33
     * {@inheritdoc}
34
     */
35
    public function dbTypecast($value)
36
    {
37
        if ($value === null) {
38
            return $value;
39
        }
40
41
        if ($value instanceof ExpressionInterface) {
42
            return $value;
43
        }
44
45
        if ($this->dimension > 0) {
46
            return new ArrayExpression($value, $this->dbType, $this->dimension);
47
        }
48
        if (in_array($this->dbType, [Schema::TYPE_JSON, Schema::TYPE_JSONB], true)) {
0 ignored issues
show
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...
49
            return new JsonExpression($value, $this->dbType);
50
        }
51
52
        return $this->typecast($value);
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58
    public function phpTypecast($value)
59
    {
60
        if ($this->dimension > 0) {
61
            if (!is_array($value)) {
62
                $value = $this->getArrayParser()->parse($value);
63
            }
64
            if (is_array($value)) {
65
                array_walk_recursive($value, function (&$val, $key) {
66
                    $val = $this->phpTypecastValue($val);
67
                });
68
            } elseif ($value === null) {
0 ignored issues
show
introduced by
The condition $value === null is always true.
Loading history...
69
                return null;
70
            }
71
72
            return $value;
73
        }
74
75
        return $this->phpTypecastValue($value);
76
    }
77
78
    /**
79
     * Casts $value after retrieving from the DBMS to PHP representation.
80
     *
81
     * @param string|null $value
82
     * @return bool|mixed|null
83
     */
84
    protected function phpTypecastValue($value)
85
    {
86
        if ($value === null) {
87
            return null;
88
        }
89
90
        switch ($this->type) {
91
            case Schema::TYPE_BOOLEAN:
92
                switch (strtolower($value)) {
93
                    case 't':
94
                    case 'true':
95
                        return true;
96
                    case 'f':
97
                    case 'false':
98
                        return false;
99
                }
100
                return (bool) $value;
101
            case Schema::TYPE_JSON:
102
                return json_decode($value, true);
103
        }
104
105
        return parent::phpTypecast($value);
106
    }
107
108
    /**
109
     * Creates instance of ArrayParser
110
     *
111
     * @return ArrayParser
112
     */
113
    protected function getArrayParser()
114
    {
115
        static $parser = null;
116
117
        if ($parser === null) {
118
            $parser = new ArrayParser();
119
        }
120
121
        return $parser;
122
    }
123
}
124