Passed
Push — master ( b7f728...dee1f4 )
by Edgard
18:46
created

ColumnSchema::typecast()   C

Complexity

Conditions 20
Paths 15

Size

Total Lines 56
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 21.015

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 56
ccs 19
cts 22
cp 0.8636
rs 6.5217
cc 20
eloc 37
nc 15
nop 1
crap 21.015

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\Expression;
11
12
/**
13
 *
14
 * @author Edgard Lorraine Messias <[email protected]>
15
 * @since 2.0
16
 */
17
class ColumnSchema extends \yii\db\ColumnSchema
18
{
19
20
    /**
21
     * Converts the input value according to [[phpType]] after retrieval from the database.
22
     * If the value is null or an [[Expression]], it will not be converted.
23
     * @param mixed $value input value
24
     * @return mixed converted value
25
     * @since 2.0.3
26
     */
27 98
    protected function typecast($value)
28
    {
29
        if ($value === ''
30 98
            && !in_array(
31 1
                $this->type,
32
                [
33 98
                    Schema::TYPE_TEXT,
34 94
                    Schema::TYPE_STRING,
35
                    Schema::TYPE_BINARY,
36
                    Schema::TYPE_CHAR
37 87
                ],
38 87
                true)
39 87
        ) {
40 42
            return null;
41
        }
42
43 42
        if ($value === null
44
            || gettype($value) === $this->phpType
45 2
            || $value instanceof ExpressionInterface
0 ignored issues
show
Bug introduced by
The class edgardmessias\db\firebird\ExpressionInterface does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
46
            || $value instanceof Query
0 ignored issues
show
Bug introduced by
The class edgardmessias\db\firebird\Query does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
47 42
        ) {
48 63
            return $value;
49 63
        }
50 3
51
        if (is_array($value)
52 63
            && count($value) === 2
53 16
            && isset($value[1])
54
            && in_array($value[1], $this->getPdoParamTypes(), true)
55 16
        ) {
56 16
            return new \yii\db\PdoValue($value[0], $value[1]);
57
        }
58
59
        switch ($this->phpType) {
60
            case 'resource':
61
            case 'string':
62
                if (is_resource($value)) {
63
                    return $value;
64
                }
65
                if (is_float($value)) {
66
                    // ensure type cast always has . as decimal separator in all locales
67
                    return str_replace(',', '.', (string) $value);
68
                }
69
                return (string) $value;
70
            case 'integer':
71
                if (is_bool($value)) {
72
                    return ($value) ? 1 : 0;
73
                }
74
                return (int) $value;
75
            case 'boolean':
76
                return (boolean) $value;
77
            case 'double':
78
                return (double) $value;
79
        }
80
81
        return $value;
82
    }
83
84
    /**
85
     * @return int[] array of numbers that represent possible PDO parameter types
86
     */
87
    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...
88
    {
89
        return [\PDO::PARAM_BOOL, \PDO::PARAM_INT, \PDO::PARAM_STR, \PDO::PARAM_LOB, \PDO::PARAM_NULL, \PDO::PARAM_STMT];
90
    }
91
}
92