Completed
Push — master ( 168e32...cd506e )
by Edgard
14:24 queued 40s
created

ColumnSchema::typecast()   D

Complexity

Conditions 17
Paths 14

Size

Total Lines 34
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 18.5198

Importance

Changes 0
Metric Value
dl 0
loc 34
ccs 19
cts 23
cp 0.8261
rs 4.9807
c 0
b 0
f 0
cc 17
eloc 22
nc 14
nop 1
crap 18.5198

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
 * @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 89
    protected function typecast($value)
28
    {
29
30 89
        if ($value === '' && $this->type !== Schema::TYPE_TEXT && $this->type !== Schema::TYPE_STRING && $this->type !== Schema::TYPE_BINARY) {
31 1
            return null;
32
        }
33 89
        if ($value === null || gettype($value) === $this->phpType || $value instanceof Expression) {
34 85
            return $value;
35
        }
36
37 81
        switch ($this->phpType) {
38 81
            case 'resource':
39 81
            case 'string':
40 6
                if (is_resource($value)) {
41
                    return $value;
42
                }
43 6
                if (is_float($value)) {
44
                    // ensure type cast always has . as decimal separator in all locales
45 2
                    return str_replace(',', '.', (string) $value);
46
                }
47 4
                return (string) $value;
48 80
            case 'integer':
49 80
                if (is_bool($value)) {
50 2
                    return ($value) ? 1 : 0;
51
                }
52 80
                return (int) $value;
53 13
            case 'boolean':
54
                return (boolean) $value;
55 13
            case 'double':
56 13
                return (double) $value;
57
        }
58
59
        return $value;
60
    }
61
}
62