ColumnSchema::typecast()   C
last analyzed

Complexity

Conditions 20
Paths 15

Size

Total Lines 56
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 21.1672

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 56
ccs 18
cts 21
cp 0.8571
rs 6.5217
c 1
b 0
f 0
cc 20
eloc 37
nc 15
nop 1
crap 21.1672

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 edgardmessias\db\firebird;
9
10
use yii\db\ExpressionInterface;
11
use yii\db\PdoValue;
12
use yii\db\Query;
13
14
/**
15
 *
16
 * @author Edgard Lorraine Messias <[email protected]>
17
 * @since 2.0
18
 */
19
class ColumnSchema extends \yii\db\ColumnSchema
20
{
21
22
    /**
23
     * Converts the input value according to [[phpType]] after retrieval from the database.
24
     * If the value is null or an [[Expression]], it will not be converted.
25
     * @param mixed $value input value
26
     * @return mixed converted value
27 98
     * @since 2.0.3
28
     */
29
    protected function typecast($value)
30 98
    {
31 1
        if ($value === ''
32
            && !in_array(
33 98
                $this->type,
34 94
                [
35
                    Schema::TYPE_TEXT,
36
                    Schema::TYPE_STRING,
37 87
                    Schema::TYPE_BINARY,
38 87
                    Schema::TYPE_CHAR
39 87
                ],
40 42
                true)
41
        ) {
42
            return null;
43 42
        }
44
45 2
        if ($value === null
46
            || gettype($value) === $this->phpType
47 42
            || $value instanceof ExpressionInterface
48 63
            || $value instanceof Query
49 63
        ) {
50 3
            return $value;
51
        }
52 63
53 16
        if (is_array($value)
54
            && count($value) === 2
55 16
            && isset($value[1])
56 16
            && in_array($value[1], $this->getPdoParamTypes(), true)
57
        ) {
58
            return new PdoValue($value[0], $value[1]);
59
        }
60
61
        switch ($this->phpType) {
62
            case 'resource':
63
            case 'string':
64
                if (is_resource($value)) {
65
                    return $value;
66
                }
67
                if (is_float($value)) {
68
                    // ensure type cast always has . as decimal separator in all locales
69
                    return str_replace(',', '.', (string) $value);
70
                }
71
                return (string) $value;
72
            case 'integer':
73
                if (is_bool($value)) {
74
                    return ($value) ? 1 : 0;
75
                }
76
                return (int) $value;
77
            case 'boolean':
78
                return (boolean) $value;
79
            case 'double':
80
                return (double) $value;
81
        }
82
83
        return $value;
84
    }
85
86
    /**
87
     * @return int[] array of numbers that represent possible PDO parameter types
88
     */
89
    private function getPdoParamTypes()
0 ignored issues
show
Bug introduced by
Consider using a different method name as you override a private method of the parent class.

Overwriting private methods is generally fine as long as you also use private visibility. It might still be preferable for understandability to use a different method name.

Loading history...
90
    {
91
        return [\PDO::PARAM_BOOL, \PDO::PARAM_INT, \PDO::PARAM_STR, \PDO::PARAM_LOB, \PDO::PARAM_NULL, \PDO::PARAM_STMT];
92
    }
93
}
94