Completed
Push — 2.1 ( b44a46...4c2160 )
by
unknown
12:30
created

ArrayParser::parseString()   C

Complexity

Conditions 10
Paths 64

Size

Total Lines 24
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 10

Importance

Changes 0
Metric Value
dl 0
loc 24
ccs 15
cts 15
cp 1
rs 5.2164
c 0
b 0
f 0
cc 10
eloc 15
nc 64
nop 2
crap 10

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 yii\db\pgsql;
9
10
/**
11
 * The class converts PostgreSQL array representation to PHP array
12
 *
13
 * @author Sergei Tigrov <[email protected]>
14
 * @author Dmytro Naumenko <[email protected]>
15
 * @since 2.0.14
16
 */
17
class ArrayParser
18
{
19
    /**
20
     * @var string Character used in array
21
     */
22
    private $delimiter = ',';
23
24
25
    /**
26
     * Convert array from PostgreSQL to PHP
27
     *
28
     * @param string $value string to be converted
29
     * @return array|null
30
     */
31 21
    public function parse($value)
32
    {
33 21
        if ($value === null) {
34 9
            return null;
35
        }
36
37 12
        if ($value === '{}') {
38 1
            return [];
39
        }
40
41 11
        return $this->parseArray($value);
42
    }
43
44
    /**
45
     * Pares PgSQL array encoded in string
46
     *
47
     * @param string $value
48
     * @param int $i parse starting position
49
     * @return array
50
     */
51 11
    private function parseArray($value, &$i = 0)
52
    {
53 11
        $result = [];
54 11
        $len = strlen($value);
55 11
        for (++$i; $i < $len; ++$i) {
56 11
            switch ($value[$i]) {
57 11
                case '{':
58 4
                    $result[] = $this->parseArray($value, $i);
59 4
                    break;
60 11
                case '}':
61 10
                    break 2;
62 11
                case $this->delimiter:
63 11
                    if (empty($result)) { // `{}` case
64 2
                        $result[] = null;
65
                    }
66 11
                    if (in_array($value[$i + 1], [$this->delimiter, '}'], true)) { // `{,}` case
67 5
                        $result[] = null;
68
                    }
69 11
                    break;
70
                default:
71 9
                    $result[] = $this->parseString($value, $i);
72
            }
73
        }
74
75 11
        return $result;
76
    }
77
78
    /**
79
     * Parses PgSQL encoded string
80
     *
81
     * @param string $value
82
     * @param int $i parse starting position
83
     * @return null|string
84
     */
85 9
    private function parseString($value, &$i)
86
    {
87 9
        $isQuoted = $value[$i] === '"';
88 9
        $stringEndChars = $isQuoted ? ['"'] : [$this->delimiter, '}'];
89 9
        $result = '';
90 9
        $len = strlen($value);
91 9
        for ($i += $isQuoted ? 1 : 0; $i < $len; ++$i) {
92 9
            if (in_array($value[$i], ['\\', '"'], true) && in_array($value[$i + 1], [$value[$i], '"'], true)) {
93 4
                ++$i;
94 9
            } elseif (in_array($value[$i], $stringEndChars, true)) {
95 9
                break;
96
            }
97
98 9
            $result .= $value[$i];
99
        }
100
101 9
        $i -= $isQuoted ? 0 : 1;
102
103 9
        if (!$isQuoted && $result === 'NULL') {
104 3
            $result = null;
105
        }
106
107 9
        return $result;
108
    }
109
}
110