Passed
Pull Request — master (#19319)
by Paweł
09:59
created

ColumnSchema::typecast()   D

Complexity

Conditions 27
Paths 19

Size

Total Lines 68
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 35
CRAP Score 27.7873

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 27
eloc 41
c 1
b 1
f 0
nc 19
nop 1
dl 0
loc 68
ccs 35
cts 39
cp 0.8974
crap 27.7873
rs 4.1666

How to fix   Long Method    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
 * @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;
9
10
use yii\base\BaseObject;
11
use yii\helpers\StringHelper;
12
13
/**
14
 * ColumnSchema class describes the metadata of a column in a database table.
15
 *
16
 * @author Qiang Xue <[email protected]>
17
 * @since 2.0
18
 */
19
class ColumnSchema extends BaseObject
20
{
21
    /**
22
     * @var string name of this column (without quotes).
23
     */
24
    public $name;
25
    /**
26
     * @var bool whether this column can be null.
27
     */
28
    public $allowNull;
29
    /**
30
     * @var string abstract type of this column. Possible abstract types include:
31
     * char, string, text, boolean, smallint, integer, bigint, float, decimal, datetime,
32
     * timestamp, time, date, binary, and money.
33
     */
34
    public $type;
35
    /**
36
     * @var string the PHP type of this column. Possible PHP types include:
37
     * `string`, `boolean`, `integer`, `double`, `array`.
38
     */
39
    public $phpType;
40
    /**
41
     * @var string the DB type of this column. Possible DB types vary according to the type of DBMS.
42
     */
43
    public $dbType;
44
    /**
45
     * @var mixed default value of this column
46
     */
47
    public $defaultValue;
48
    /**
49
     * @var array enumerable values. This is set only if the column is declared to be an enumerable type.
50
     */
51
    public $enumValues;
52
    /**
53
     * @var int display size of the column.
54
     */
55
    public $size;
56
    /**
57
     * @var int precision of the column data, if it is numeric.
58
     */
59
    public $precision;
60
    /**
61
     * @var int scale of the column data, if it is numeric.
62
     */
63
    public $scale;
64
    /**
65
     * @var bool whether this column is a primary key
66
     */
67
    public $isPrimaryKey;
68
    /**
69
     * @var bool whether this column is auto-incremental
70
     */
71
    public $autoIncrement = false;
72
    /**
73
     * @var bool whether this column is unsigned. This is only meaningful
74
     * when [[type]] is `smallint`, `integer` or `bigint`.
75
     */
76
    public $unsigned;
77
    /**
78
     * @var string comment of this column. Not all DBMS support this.
79
     */
80
    public $comment;
81
82
83
    /**
84
     * Converts the input value according to [[phpType]] after retrieval from the database.
85
     * If the value is null or an [[Expression]], it will not be converted.
86
     * @param mixed $value input value
87
     * @return mixed converted value
88
     */
89 617
    public function phpTypecast($value)
90
    {
91 617
        return $this->typecast($value);
92
    }
93
94
    /**
95
     * Converts the input value according to [[type]] and [[dbType]] for use in a db query.
96
     * If the value is null or an [[Expression]], it will not be converted.
97
     * @param mixed $value input value
98
     * @return mixed converted value. This may also be an array containing the value as the first element
99
     * and the PDO type as the second element.
100
     */
101 172
    public function dbTypecast($value)
102
    {
103
        // the default implementation does the same as casting for PHP, but it should be possible
104
        // to override this with annotation of explicit PDO type.
105 172
        return $this->typecast($value);
106
    }
107
108
    /**
109
     * Converts the input value according to [[phpType]] after retrieval from the database.
110
     * If the value is null or an [[Expression]], it will not be converted.
111
     * @param mixed $value input value
112
     * @return mixed converted value
113
     * @since 2.0.3
114
     */
115 1017
    protected function typecast($value)
116
    {
117 1017
        if ($value === ''
118 46
            && !in_array(
119 46
                $this->type,
120
                [
121 46
                    Schema::TYPE_TEXT,
122
                    Schema::TYPE_STRING,
123
                    Schema::TYPE_BINARY,
124
                    Schema::TYPE_CHAR
125
                ],
126 1017
                true)
127
        ) {
128 6
            return null;
129
        }
130
131 1017
        if ($value === null
132 1017
            || gettype($value) === $this->phpType
133 675
            || $value instanceof ExpressionInterface
134 1017
            || $value instanceof Query
135
        ) {
136 931
            return $value;
137
        }
138
139 668
        if (is_array($value)
140 668
            && count($value) === 2
141 668
            && isset($value[1])
142 668
            && in_array($value[1], $this->getPdoParamTypes(), true)
143
        ) {
144
            return new PdoValue($value[0], $value[1]);
145
        }
146
147 668
        switch ($this->phpType) {
148 668
            case 'resource':
149 611
            case 'string':
150 182
                if (is_resource($value)) {
151
                    return $value;
152
                }
153 182
                if (is_float($value)) {
154
                    // ensure type cast always has . as decimal separator in all locales
155 6
                    return StringHelper::floatToString($value);
156
                }
157 176
                if (is_numeric($value)
158 176
                    && ColumnSchemaBuilder::CATEGORY_NUMERIC === ColumnSchemaBuilder::$typeCategoryMap[$this->type]
159
                ) {
160
                    // https://github.com/yiisoft/yii2/issues/14663
161 19
                    return $value;
162
                }
163
                
164 157
                if (PHP_VERSION_ID >= 80100 && is_object($value) && $value instanceof \BackedEnum) {
0 ignored issues
show
Bug introduced by
The type BackedEnum 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...
165
                    return (string) $value->value;
166
                }
167
168 157
                return (string) $value;
169 506
            case 'integer':
170 502
                if (PHP_VERSION_ID >= 80100 && is_object($value) && $value instanceof \BackedEnum) {
171
                    return (int) $value->value;
172
                }
173 502
                return (int) $value;
174 95
            case 'boolean':
175
                // treating a 0 bit value as false too
176
                // https://github.com/yiisoft/yii2/issues/9006
177 24
                return (bool) $value && $value !== "\0";
178 92
            case 'double':
179 89
                return (float) $value;
180
        }
181
182 3
        return $value;
183
    }
184
185
    /**
186
     * @return int[] array of numbers that represent possible PDO parameter types
187
     */
188
    private function getPdoParamTypes()
189
    {
190
        return [\PDO::PARAM_BOOL, \PDO::PARAM_INT, \PDO::PARAM_STR, \PDO::PARAM_LOB, \PDO::PARAM_NULL, \PDO::PARAM_STMT];
191
    }
192
}
193