Passed
Push — master ( 851a65...b0ffb0 )
by Alexander
08:45 queued 07:03
created

ColumnSchema::phpTypecast()   A

Complexity

Conditions 5
Paths 7

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 10
nc 7
nop 1
dl 0
loc 18
rs 9.6111
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Pgsql;
6
7
use Yiisoft\Db\Expressions\ArrayExpression;
8
use Yiisoft\Db\Expressions\ExpressionInterface;
9
use Yiisoft\Db\Expressions\JsonExpression;
10
11
/**
12
 * Class ColumnSchema for Postgres SQL database.
13
 */
14
class ColumnSchema extends \Yiisoft\Db\Schemas\ColumnSchema
15
{
16
    /**
17
     * @var int the dimension of array. Defaults to 0, means this column is not an array.
18
     */
19
    public int $dimension = 0;
20
21
    /**
22
     * @var string name of associated sequence if column is auto-incremental.
23
     */
24
    public ?string $sequenceName = null;
25
26
    /**
27
     * {@inheritdoc}
28
     */
29
    public function dbTypecast($value)
30
    {
31
        if ($value === null) {
32
            return $value;
33
        }
34
35
        if ($value instanceof ExpressionInterface) {
36
            return $value;
37
        }
38
39
        if ($this->dimension > 0) {
40
            return $this->disableArraySupport
0 ignored issues
show
Bug Best Practice introduced by
The property disableArraySupport does not exist on Yiisoft\Db\Pgsql\ColumnSchema. Did you maybe forget to declare it?
Loading history...
41
                ? (string) $value
42
                : new ArrayExpression($value, $this->dbType, $this->dimension);
43
        }
44
45
        if (\in_array($this->dbType, [Schema::TYPE_JSON, Schema::TYPE_JSONB], true)) {
0 ignored issues
show
Bug introduced by
The type Yiisoft\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...
46
            return new JsonExpression($value, $this->dbType);
47
        }
48
49
        return $this->typecast($value);
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55
    public function phpTypecast($value)
56
    {
57
        if ($this->dimension > 0) {
58
            if (!\is_array($value)) {
59
                $value = $this->getArrayParser()->parse($value);
60
            }
61
            if (\is_array($value)) {
62
                array_walk_recursive($value, function (&$val, $key) {
63
                    $val = $this->phpTypecastValue($val);
64
                });
65
            } elseif ($value === null) {
0 ignored issues
show
introduced by
The condition $value === null is always true.
Loading history...
66
                return null;
67
            }
68
69
            return $value;
70
        }
71
72
        return $this->phpTypecastValue($value);
73
    }
74
75
    /**
76
     * Casts $value after retrieving from the DBMS to PHP representation.
77
     *
78
     * @param string|null $value
79
     *
80
     * @return bool|mixed|null
81
     */
82
    protected function phpTypecastValue($value)
83
    {
84
        if ($value === null) {
85
            return null;
86
        }
87
88
        switch ($this->type) {
89
            case Schema::TYPE_BOOLEAN:
90
                $value = \is_string($value) ? strtolower($value) : $value;
0 ignored issues
show
introduced by
The condition is_string($value) is always true.
Loading history...
91
92
                switch ($value) {
93
                    case 't':
94
                    case 'true':
95
                        return true;
96
                    case 'f':
97
                    case 'false':
98
                        return false;
99
                }
100
101
                return (bool) $value;
102
            case Schema::TYPE_JSON:
103
                return \json_decode($value, true);
104
        }
105
106
        return parent::phpTypecast($value);
107
    }
108
109
    /**
110
     * Creates instance of ArrayParser
111
     *
112
     * @return ArrayParser
113
     */
114
    protected function getArrayParser(): ArrayParser
115
    {
116
        static $parser = null;
117
118
        if ($parser === null) {
119
            $parser = new ArrayParser();
120
        }
121
122
        return $parser;
123
    }
124
}
125