Completed
Push — master ( 65f66e...428edc )
by Michal
04:14
created

Array2d   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 110
ccs 47
cts 47
cp 1
rs 10
c 0
b 0
f 0
wmc 15
lcom 0
cbo 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
D parse() 0 89 14
A build() 0 4 1
1
<?php
2
3
/**
4
 * `VALUES` keyword parser.
5
 */
6
7
namespace PhpMyAdmin\SqlParser\Components;
8
9
use PhpMyAdmin\SqlParser\Component;
10
use PhpMyAdmin\SqlParser\Parser;
11
use PhpMyAdmin\SqlParser\Token;
12
use PhpMyAdmin\SqlParser\TokensList;
13
14
/**
15
 * `VALUES` keyword parser.
16
 *
17
 * @category   Keywords
18
 *
19
 * @license    https://www.gnu.org/licenses/gpl-2.0.txt GPL-2.0+
20
 */
21
class Array2d extends Component
22
{
23
    /**
24
     * @param Parser     $parser  the parser that serves as context
25
     * @param TokensList $list    the list of tokens that are being parsed
26
     * @param array      $options parameters for parsing
27
     *
28
     * @return ArrayObj[]
29
     */
30 19
    public static function parse(Parser $parser, TokensList $list, array $options = array())
31
    {
32 19
        $ret = array();
33
34
        /**
35
         * The number of values in each set.
36
         *
37
         * @var int
38
         */
39 19
        $count = -1;
40
41
        /**
42
         * The state of the parser.
43
         *
44
         * Below are the states of the parser.
45
         *
46
         *      0 ----------------------[ array ]----------------------> 1
47
         *
48
         *      1 ------------------------[ , ]------------------------> 0
49
         *      1 -----------------------[ else ]----------------------> (END)
50
         *
51
         * @var int
52
         */
53 19
        $state = 0;
54
55 19
        for (; $list->idx < $list->count; ++$list->idx) {
56
            /**
57
             * Token parsed at this moment.
58
             *
59
             * @var Token
60
             */
61 19
            $token = $list->tokens[$list->idx];
62
63
            // End of statement.
64 19
            if ($token->type === Token::TYPE_DELIMITER) {
65 12
                break;
66
            }
67
68
            // Skipping whitespaces and comments.
69 19
            if (($token->type === Token::TYPE_WHITESPACE) || ($token->type === Token::TYPE_COMMENT)) {
70 13
                continue;
71
            }
72
73
            // No keyword is expected.
74 19
            if (($token->type === Token::TYPE_KEYWORD) && ($token->flags & Token::FLAG_KEYWORD_RESERVED)) {
75 3
                break;
76
            }
77
78 18
            if ($state === 0) {
79 18
                if ($token->value === '(') {
80 17
                    $arr = ArrayObj::parse($parser, $list, $options);
81 17
                    $arrCount = count($arr->values);
82 17
                    if ($count === -1) {
83 17
                        $count = $arrCount;
84 17
                    } elseif ($arrCount != $count) {
85 1
                        $parser->error(
86 1
                            sprintf(
87 1
                                __('%1$d values were expected, but found %2$d.'),
88 1
                                $count,
89
                                $arrCount
90 1
                            ),
91
                            $token
92 1
                        );
93 1
                    }
94 17
                    $ret[] = $arr;
95 17
                    $state = 1;
96 17
                } else {
97 1
                    break;
98
                }
99 17
            } elseif ($state === 1) {
100 7
                if ($token->value === ',') {
101 6
                    $state = 0;
102 6
                } else {
103 1
                    break;
104
                }
105 6
            }
106 17
        }
107
108 19
        if ($state === 0) {
109 3
            $parser->error(
110 3
                __('An opening bracket followed by a set of values was expected.'),
111 3
                $list->tokens[$list->idx]
112 3
            );
113 3
        }
114
115 19
        --$list->idx;
116
117 19
        return $ret;
118
    }
119
120
    /**
121
     * @param ArrayObj[] $component the component to be built
122
     * @param array      $options   parameters for building
123
     *
124
     * @return string
125
     */
126 3
    public static function build($component, array $options = array())
127
    {
128 3
        return ArrayObj::build($component);
129
    }
130
}
131