Passed
Push — master ( 9e99de...aeb52d )
by William
03:32 queued 13s
created

CaseExpression::build()   B

Complexity

Conditions 8
Paths 16

Size

Total Lines 33
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 8

Importance

Changes 0
Metric Value
cc 8
eloc 20
nc 16
nop 2
dl 0
loc 33
ccs 20
cts 20
cp 1
crap 8
rs 8.4444
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpMyAdmin\SqlParser\Components;
6
7
use PhpMyAdmin\SqlParser\Component;
8
use PhpMyAdmin\SqlParser\Context;
9
use PhpMyAdmin\SqlParser\Parser;
10
use PhpMyAdmin\SqlParser\Token;
11
use PhpMyAdmin\SqlParser\TokensList;
12
13
use function count;
14
15
/**
16
 * Parses a reference to a CASE expression.
17
 */
18
final class CaseExpression implements Component
19
{
20
    /**
21
     * The value to be compared.
22
     *
23
     * @var Expression|null
24
     */
25
    public $value;
26
27
    /**
28
     * The conditions in WHEN clauses.
29
     *
30
     * @var Condition[][]
31
     */
32
    public $conditions = [];
33
34
    /**
35
     * The results matching with the WHEN clauses.
36
     *
37
     * @var Expression[]
38
     */
39
    public $results = [];
40
41
    /**
42
     * The values to be compared against.
43
     *
44
     * @var Expression[]
45
     */
46
    public $compareValues = [];
47
48
    /**
49
     * The result in ELSE section of expr.
50
     *
51
     * @var Expression|null
52
     */
53
    public $elseResult;
54
55
    /**
56
     * The alias of this CASE statement.
57
     *
58
     * @var string|null
59
     */
60
    public $alias;
61
62
    /**
63
     * The sub-expression.
64
     *
65
     * @var string
66
     */
67
    public $expr = '';
68
69
    /**
70
     * @param Parser               $parser  the parser that serves as context
71
     * @param TokensList           $list    the list of tokens that are being parsed
72
     * @param array<string, mixed> $options parameters for parsing
73
     *
74
     * @return CaseExpression
75
     */
76 64
    public static function parse(Parser $parser, TokensList $list, array $options = [])
77
    {
78 64
        $ret = new static();
79
80
        /**
81
         * State of parser.
82
         *
83
         * @var int
84
         */
85 64
        $state = 0;
86
87
        /**
88
         * Syntax type (type 0 or type 1).
89
         *
90
         * @var int
91
         */
92 64
        $type = 0;
93
94 64
        ++$list->idx; // Skip 'CASE'
95
96 64
        for (; $list->idx < $list->count; ++$list->idx) {
97
            /**
98
             * Token parsed at this moment.
99
             */
100 64
            $token = $list->tokens[$list->idx];
101
102
            // Skipping whitespaces and comments.
103 64
            if (($token->type === Token::TYPE_WHITESPACE) || ($token->type === Token::TYPE_COMMENT)) {
104 64
                continue;
105
            }
106
107 64
            if ($state === 0) {
108 64
                if ($token->type === Token::TYPE_KEYWORD) {
109 48
                    switch ($token->keyword) {
110 48
                        case 'WHEN':
111 30
                            ++$list->idx; // Skip 'WHEN'
112 30
                            $newCondition = Condition::parse($parser, $list);
113 30
                            $type = 1;
114 30
                            $state = 1;
115 30
                            $ret->conditions[] = $newCondition;
116 30
                            break;
117 46
                        case 'ELSE':
118 16
                            ++$list->idx; // Skip 'ELSE'
119 16
                            $ret->elseResult = Expression::parse($parser, $list);
120 16
                            $state = 0; // last clause of CASE expression
121 16
                            break;
122 46
                        case 'END':
123 44
                            $state = 3; // end of CASE expression
124 44
                            ++$list->idx;
125 44
                            break 2;
126
                        default:
127 2
                            $parser->error('Unexpected keyword.', $token);
128 32
                            break 2;
129
                    }
130
                } else {
131 32
                    $ret->value = Expression::parse($parser, $list);
132 32
                    $type = 0;
133 62
                    $state = 1;
134
                }
135 62
            } elseif ($state === 1) {
136 62
                if ($type === 0) {
137 32
                    if ($token->type === Token::TYPE_KEYWORD) {
138 32
                        switch ($token->keyword) {
139 32
                            case 'WHEN':
140 28
                                ++$list->idx; // Skip 'WHEN'
141 28
                                $newValue = Expression::parse($parser, $list);
142 28
                                $state = 2;
143 28
                                $ret->compareValues[] = $newValue;
144 28
                                break;
145 30
                            case 'ELSE':
146 16
                                ++$list->idx; // Skip 'ELSE'
147 16
                                $ret->elseResult = Expression::parse($parser, $list);
148 16
                                $state = 0; // last clause of CASE expression
149 16
                                break;
150 14
                            case 'END':
151 10
                                $state = 3; // end of CASE expression
152 10
                                ++$list->idx;
153 10
                                break 2;
154
                            default:
155 4
                                $parser->error('Unexpected keyword.', $token);
156 32
                                break 2;
157
                        }
158
                    }
159
                } else {
160 30
                    if ($token->type === Token::TYPE_KEYWORD && $token->keyword === 'THEN') {
161 28
                        ++$list->idx; // Skip 'THEN'
162 28
                        $newResult = Expression::parse($parser, $list);
163 28
                        $state = 0;
164 28
                        $ret->results[] = $newResult;
165 2
                    } elseif ($token->type === Token::TYPE_KEYWORD) {
166 2
                        $parser->error('Unexpected keyword.', $token);
167 58
                        break;
168
                    }
169
                }
170 28
            } elseif ($state === 2) {
171 28
                if ($type === 0) {
172 28
                    if ($token->type === Token::TYPE_KEYWORD && $token->keyword === 'THEN') {
173 28
                        ++$list->idx; // Skip 'THEN'
174 28
                        $newResult = Expression::parse($parser, $list);
175 28
                        $ret->results[] = $newResult;
176 28
                        $state = 1;
177 2
                    } elseif ($token->type === Token::TYPE_KEYWORD) {
178 2
                        $parser->error('Unexpected keyword.', $token);
179 2
                        break;
180
                    }
181
                }
182
            }
183
        }
184
185 64
        if ($state !== 3) {
186 10
            $parser->error('Unexpected end of CASE expression', $list->tokens[$list->idx - 1]);
187
        } else {
188
            // Parse for alias of CASE expression
189 54
            $asFound = false;
190 54
            for (; $list->idx < $list->count; ++$list->idx) {
191 54
                $token = $list->tokens[$list->idx];
192
193
                // End of statement.
194 54
                if ($token->type === Token::TYPE_DELIMITER) {
195 20
                    break;
196
                }
197
198
                // Skipping whitespaces and comments.
199 40
                if (($token->type === Token::TYPE_WHITESPACE) || ($token->type === Token::TYPE_COMMENT)) {
200 40
                    continue;
201
                }
202
203
                // Handle optional AS keyword before alias
204 40
                if ($token->type === Token::TYPE_KEYWORD && $token->keyword === 'AS') {
205 20
                    if ($asFound || ! empty($ret->alias)) {
206 2
                        $parser->error('Potential duplicate alias of CASE expression.', $token);
207 2
                        break;
208
                    }
209
210 20
                    $asFound = true;
211 20
                    continue;
212
                }
213
214
                if (
215 36
                    $asFound
216 36
                    && $token->type === Token::TYPE_KEYWORD
217 36
                    && ($token->flags & Token::FLAG_KEYWORD_RESERVED || $token->flags & Token::FLAG_KEYWORD_FUNCTION)
218
                ) {
219 2
                    $parser->error('An alias expected after AS but got ' . $token->value, $token);
220 2
                    $asFound = false;
221 2
                    break;
222
                }
223
224
                if (
225 34
                    $asFound
226 32
                    || $token->type === Token::TYPE_STRING
227 32
                    || ($token->type === Token::TYPE_SYMBOL && ! $token->flags & Token::FLAG_SYMBOL_VARIABLE)
228 34
                    || $token->type === Token::TYPE_NONE
229
                ) {
230
                    // An alias is expected (the keyword `AS` was previously found).
231 20
                    if (! empty($ret->alias)) {
232 2
                        $parser->error('An alias was previously found.', $token);
233 2
                        break;
234
                    }
235
236 20
                    $ret->alias = $token->value;
237 20
                    $asFound = false;
238
239 20
                    continue;
240
                }
241
242 28
                break;
243
            }
244
245 54
            if ($asFound) {
246 4
                $parser->error('An alias was expected after AS.', $list->tokens[$list->idx - 1]);
247
            }
248
249 54
            $ret->expr = self::build($ret);
250
        }
251
252 64
        --$list->idx;
253
254 64
        return $ret;
255
    }
256
257
    /**
258
     * @param CaseExpression       $component the component to be built
259
     * @param array<string, mixed> $options   parameters for building
260
     */
261 56
    public static function build($component, array $options = []): string
262
    {
263 56
        $ret = 'CASE ';
264 56
        if (isset($component->value)) {
265
            // Syntax type 0
266 26
            $ret .= $component->value . ' ';
267 26
            $valuesCount = count($component->compareValues);
268 26
            $resultsCount = count($component->results);
269 26
            for ($i = 0; $i < $valuesCount && $i < $resultsCount; ++$i) {
270 26
                $ret .= 'WHEN ' . $component->compareValues[$i] . ' ';
271 26
                $ret .= 'THEN ' . $component->results[$i] . ' ';
272
            }
273
        } else {
274
            // Syntax type 1
275 30
            $valuesCount = count($component->conditions);
276 30
            $resultsCount = count($component->results);
277 30
            for ($i = 0; $i < $valuesCount && $i < $resultsCount; ++$i) {
278 28
                $ret .= 'WHEN ' . Condition::build($component->conditions[$i]) . ' ';
279 28
                $ret .= 'THEN ' . $component->results[$i] . ' ';
280
            }
281
        }
282
283 56
        if (isset($component->elseResult)) {
284 32
            $ret .= 'ELSE ' . $component->elseResult . ' ';
285
        }
286
287 56
        $ret .= 'END';
288
289 56
        if ($component->alias) {
290 20
            $ret .= ' AS ' . Context::escape($component->alias);
291
        }
292
293 56
        return $ret;
294
    }
295
296
    public function __toString(): string
297
    {
298
        return static::build($this);
299
    }
300
}
301