Completed
Push — 2.1 ( 8a46b3...8ecc36 )
by Dmitry
13:22
created

ColumnSchema   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 92.31%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 18
c 2
b 0
f 0
lcom 1
cbo 4
dl 0
loc 93
ccs 36
cts 39
cp 0.9231
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A dbTypecast() 0 15 4
A phpTypecast() 0 17 4
C phpTypecastValue() 0 23 8
A getArrayParser() 0 10 2
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\pgsql;
9
10
use yii\db\ArrayExpression;
11
use yii\db\ExpressionInterface;
12
use yii\db\JsonExpression;
13
14
/**
15
 * Class ColumnSchema for PostgreSQL database.
16
 *
17
 * @author Dmytro Naumenko <[email protected]>
18
 */
19
class ColumnSchema extends \yii\db\ColumnSchema
20
{
21
    /**
22
     * @var int the dimension of array. Defaults to 0, means this column is not an array.
23
     */
24
    public $dimension = 0;
25
26
    /**
27
     * {@inheritdoc}
28
     */
29 198
    public function dbTypecast($value)
30
    {
31 198
        if ($value instanceof ExpressionInterface) {
32 83
            return $value;
33
        }
34
35 196
        if ($this->dimension > 0) {
36
            return new ArrayExpression($value, $this->dbType, $this->dimension);
37
        }
38 196
        if (in_array($this->dbType, [Schema::TYPE_JSON, Schema::TYPE_JSONB], true)) {
39 3
            return new JsonExpression($value, $this->type);
40
        }
41
42 193
        return $this->typecast($value);
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48 143
    public function phpTypecast($value)
49
    {
50 143
        if ($this->dimension > 0) {
51 11
            if (!is_array($value)) {
52 11
                $value = $this->getArrayParser()->parse($value);
53
            }
54 11
            if (is_array($value)) {
55 2
                array_walk_recursive($value, function (&$val, $key) {
0 ignored issues
show
Unused Code introduced by
The parameter $key is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
56 2
                    $val = $this->phpTypecastValue($val);
57 2
                });
58
            }
59
60 11
            return new ArrayExpression($value, $this->dbType, $this->dimension);
61
        }
62
63 143
        return $this->phpTypecastValue($value);
64
    }
65
66
    /**
67
     * Casts $value after retrieving from the DBMS to PHP representation.
68
     *
69
     * @param string|null $value
70
     * @return bool|mixed|null
71
     */
72 143
    protected function phpTypecastValue($value)
73
    {
74 143
        if ($value === null) {
75 41
            return null;
76
        }
77
78 143
        switch ($this->type) {
79 143
            case Schema::TYPE_BOOLEAN:
80 56
                switch (strtolower($value)) {
81 56
                    case 't':
82 56
                    case 'true':
83
                        return true;
84 56
                    case 'f':
85 56
                    case 'false':
86
                        return false;
87
                }
88 56
                return (bool) $value;
89 143
            case Schema::TYPE_JSON:
90 33
                return json_decode($value, true);
91
        }
92
93 143
        return parent::phpTypecast($value);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (phpTypecast() instead of phpTypecastValue()). Are you sure this is correct? If so, you might want to change this to $this->phpTypecast().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
94
    }
95
96
    /**
97
     * Creates instance of ArrayParser
98
     *
99
     * @return ArrayParser
100
     */
101 11
    protected function getArrayParser()
102
    {
103 11
        static $parser = null;
104
105 11
        if ($parser === null) {
106 1
            $parser = new ArrayParser();
107
        }
108
109 11
        return $parser;
110
    }
111
}
112