Passed
Pull Request — master (#535)
by
unknown
02:55
created

Array2d   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 35
c 0
b 0
f 0
dl 0
loc 94
rs 10
ccs 38
cts 38
cp 1
wmc 14

1 Method

Rating   Name   Duplication   Size   Complexity  
C parse() 0 85 14
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpMyAdmin\SqlParser\Components\Parsers;
6
7
use PhpMyAdmin\SqlParser\Components\ArrayObj;
8
use PhpMyAdmin\SqlParser\Parseable;
9
use PhpMyAdmin\SqlParser\Parser;
10
use PhpMyAdmin\SqlParser\Token;
11
use PhpMyAdmin\SqlParser\TokensList;
12
use PhpMyAdmin\SqlParser\TokenType;
13
use PhpMyAdmin\SqlParser\Translator;
14
15
use function count;
16
use function sprintf;
17
18
/**
19
 * `VALUES` keyword parser.
20
 */
21
final class Array2d implements Parseable
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<string, mixed> $options parameters for parsing
27
     *
28
     * @return ArrayObj[]
29
     */
30 94
    public static function parse(Parser $parser, TokensList $list, array $options = []): array
31
    {
32 94
        $ret = [];
33
34
        /**
35
         * The number of values in each set.
36
         *
37
         * @var int
38
         */
39 94
        $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 94
        $state = 0;
54
55 94
        for (; $list->idx < $list->count; ++$list->idx) {
56
            /**
57
             * Token parsed at this moment.
58
             */
59 94
            $token = $list->tokens[$list->idx];
60
61
            // End of statement.
62 94
            if ($token->type === TokenType::Delimiter) {
63 44
                break;
64
            }
65
66
            // Skipping whitespaces and comments.
67 94
            if (($token->type === TokenType::Whitespace) || ($token->type === TokenType::Comment)) {
68 84
                continue;
69
            }
70
71
            // No keyword is expected.
72 94
            if (($token->type === TokenType::Keyword) && ($token->flags & Token::FLAG_KEYWORD_RESERVED)) {
73 42
                break;
74
            }
75
76 92
            if ($state === 0) {
77 92
                if ($token->value !== '(') {
78 2
                    break;
79
                }
80
81
                /** @var ArrayObj $arr */
82 90
                $arr = ArrayObj::parse($parser, $list, $options);
83 90
                $arrCount = count($arr->values);
84 90
                if ($count === -1) {
85 90
                    $count = $arrCount;
86 10
                } elseif ($arrCount !== $count) {
87 2
                    $parser->error(
88 2
                        sprintf(
89 2
                            Translator::gettext('%1$d values were expected, but found %2$d.'),
90 2
                            $count,
91 2
                            $arrCount,
92 2
                        ),
93 2
                        $token,
94 2
                    );
95
                }
96
97 90
                $ret[] = $arr;
98 90
                $state = 1;
99 18
            } elseif ($state === 1) {
100 18
                if ($token->value !== ',') {
101 6
                    break;
102
                }
103
104 12
                $state = 0;
105
            }
106
        }
107
108 94
        if ($state === 0) {
0 ignored issues
show
introduced by
The condition $state === 0 is always true.
Loading history...
109 6
            $parser->error('An opening bracket followed by a set of values was expected.', $list->tokens[$list->idx]);
110
        }
111
112 94
        --$list->idx;
113
114 94
        return $ret;
115
    }
116
}
117