Passed
Pull Request — master (#85)
by
unknown
02:46
created

ColumnSchema::phpTypecastValue()   B

Complexity

Conditions 10
Paths 14

Size

Total Lines 29
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 10.0203

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 10
eloc 18
c 1
b 0
f 0
nc 14
nop 1
dl 0
loc 29
ccs 16
cts 17
cp 0.9412
crap 10.0203
rs 7.6666

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Pgsql;
6
7
use JsonException;
8
use Yiisoft\Db\Expression\ArrayExpression;
9
use Yiisoft\Db\Expression\ExpressionInterface;
10
use Yiisoft\Db\Expression\JsonExpression;
11
use Yiisoft\Db\Schema\ColumnSchema as AbstractColumnSchema;
12
13
use function array_walk_recursive;
14
use function in_array;
15
use function is_array;
16
use function is_string;
17
use function json_decode;
18
use function strtolower;
19
20
/**
21
 * The class ColumnSchema for PostgreSQL database.
22
 */
23
final class ColumnSchema extends AbstractColumnSchema
24
{
25
    /**
26
     * @var int the dimension of array. Defaults to 0, means this column is not an array.
27
     */
28
    private int $dimension = 0;
29
30
    /**
31
     * @var string|null name of associated sequence if column is auto-incremental.
32
     */
33
    private ?string $sequenceName = null;
34
35
    /**
36
     * Converts the input value according to {@see type} and {@see dbType} for use in a db query.
37
     *
38
     * If the value is null or an {@see Expression}, it will not be converted.
39
     *
40
     * @param mixed $value input value
41
     *
42
     * @return mixed converted value. This may also be an array containing the value as the first element and the PDO
43
     * type as the second element.
44
     */
45 51
    public function dbTypecast($value)
46
    {
47 51
        if ($value === null) {
48 6
            return null;
49
        }
50
51 51
        if ($value instanceof ExpressionInterface) {
52 11
            return $value;
53
        }
54
55 46
        if ($this->dimension > 0) {
56 1
            return new ArrayExpression($value, $this->getDbType(), $this->dimension);
57
        }
58
59 46
        if (in_array($this->getDbType(), [Schema::TYPE_JSON, Schema::TYPE_JSONB], true)) {
60 2
            return new JsonExpression($value, $this->getDbType());
61
        }
62
63 45
        return $this->typecast($value);
64
    }
65
66
    /**
67
     * Converts the input value according to {@see phpType} after retrieval from the database.
68
     *
69
     * If the value is null or an {@see Expression}, it will not be converted.
70
     *
71
     * @param mixed $value input value
72
     *
73
     * @throws JsonException
74
     *
75
     * @return mixed converted value
76
     */
77 32
    public function phpTypecast($value)
78
    {
79 32
        if ($this->dimension > 0) {
80 1
            if (!is_array($value)) {
81 1
                $value = $this->getArrayParser()->parse($value);
82
            }
83
84 1
            if (is_array($value)) {
85 1
                array_walk_recursive($value, function (?string &$val) {
86 1
                    $val = $this->phpTypecastValue($val);
87 1
                });
88
            } else {
89 1
                return null;
90
            }
91
92 1
            return $value;
93
        }
94
95 32
        return $this->phpTypecastValue($value);
96
    }
97
98
    /**
99
     * Casts $value after retrieving from the DBMS to PHP representation.
100
     *
101
     * @param int|string|null $value
102
     *
103
     * @throws JsonException
104
     *
105
     * @return mixed
106
     */
107 32
    protected function phpTypecastValue($value)
108
    {
109 32
        if ($value === null) {
110 1
            return null;
111
        }
112
113 32
        if (RangeParser::isAllowedType($this->getType())) {
114
            return $this->getRangeParser()->parse($value);
115
        }
116
117 32
        switch ($this->getType()) {
118
            case Schema::TYPE_BOOLEAN:
119 2
                $value = is_string($value) ? strtolower($value) : $value;
120
121 2
                switch ($value) {
122 2
                    case 't':
123 2
                    case 'true':
124 1
                        return true;
125 2
                    case 'f':
126 2
                    case 'false':
127 1
                        return false;
128
                }
129
130 1
                return (bool) $value;
131
            case Schema::TYPE_JSON:
132 28
                return json_decode((string) $value, true, 512, JSON_THROW_ON_ERROR);
133
        }
134
135 31
        return parent::phpTypecast($value);
136
    }
137
138
    /**
139
     * Creates instance of ArrayParser.
140
     *
141
     * @return ArrayParser
142
     */
143 1
    protected function getArrayParser(): ArrayParser
144
    {
145 1
        return new ArrayParser();
146
    }
147
148
    /**
149
     * Creates instance of RangeParser.
150
     *
151
     * @return RangeParser
152
     */
153
    protected function getRangeParser(): RangeParser
154
    {
155
        return new RangerParser($this->getType());
0 ignored issues
show
Bug introduced by
The type Yiisoft\Db\Pgsql\RangerParser 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...
156
    }
157
158
    /**
159
     * @return int Get the dimension of array. Defaults to 0, means this column is not an array.
160
     */
161 1
    public function getDimension(): int
162
    {
163 1
        return $this->dimension;
164
    }
165
166
    /**
167
     * @return string name of associated sequence if column is auto-incremental.
168
     */
169 66
    public function getSequenceName(): ?string
170
    {
171 66
        return $this->sequenceName;
172
    }
173
174
    /**
175
     * Set dimension of array. Defaults to 0, means this column is not an array.
176
     */
177 98
    public function dimension(int $dimension): void
178
    {
179 98
        $this->dimension = $dimension;
180 98
    }
181
182
    /**
183
     * Set name of associated sequence if column is auto-incremental.
184
     */
185 62
    public function sequenceName(?string $sequenceName): void
186
    {
187 62
        $this->sequenceName = $sequenceName;
188 62
    }
189
}
190