Passed
Push — master ( 6c3f09...c54108 )
by William
03:43
created

Expression::parse()   F

Complexity

Conditions 83
Paths 3848

Size

Total Lines 285
Code Lines 149

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 142
CRAP Score 83

Importance

Changes 0
Metric Value
cc 83
eloc 149
nc 3848
nop 3
dl 0
loc 285
rs 0
c 0
b 0
f 0
ccs 142
cts 142
cp 1
crap 83

How to fix   Long Method    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
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\Exceptions\ParserException;
10
use PhpMyAdmin\SqlParser\Parser;
11
use PhpMyAdmin\SqlParser\Token;
12
use PhpMyAdmin\SqlParser\TokensList;
13
14
use function implode;
15
use function is_array;
16
use function rtrim;
17
use function strlen;
18
use function trim;
19
20
/**
21
 * Parses a reference to an expression (column, table or database name, function
22
 * call, mathematical expression, etc.).
23
 */
24
#[\AllowDynamicProperties]
25
final class Expression implements Component
26
{
27
    /**
28
     * List of allowed reserved keywords in expressions.
29
     *
30
     * @var array<string, int>
31
     */
32
    private static $allowedKeywords = [
33
        'AND' => 1,
34
        'AS' => 1,
35
        'BETWEEN' => 1,
36
        'CASE' => 1,
37
        'DUAL' => 1,
38
        'DIV' => 1,
39
        'IS' => 1,
40
        'MOD' => 1,
41
        'NOT' => 1,
42
        'NOT NULL' => 1,
43
        'NULL' => 1,
44
        'OR' => 1,
45
        'OVER' => 1,
46
        'REGEXP' => 1,
47
        'RLIKE' => 1,
48
        'XOR' => 1,
49
    ];
50
51
    /**
52
     * The name of this database.
53
     *
54
     * @var string|null
55
     */
56
    public $database;
57
58
    /**
59
     * The name of this table.
60
     *
61
     * @var string|null
62
     */
63
    public $table;
64
65
    /**
66
     * The name of the column.
67
     *
68
     * @var string|null
69
     */
70
    public $column;
71
72
    /**
73
     * The sub-expression.
74
     *
75
     * @var string|null
76
     */
77
    public $expr = '';
78
79
    /**
80
     * The alias of this expression.
81
     *
82
     * @var string|null
83
     */
84
    public $alias;
85
86
    /**
87
     * The name of the function.
88
     *
89
     * @var mixed
90
     */
91
    public $function;
92
93
    /**
94
     * The type of subquery.
95
     *
96
     * @var string|null
97
     */
98
    public $subquery;
99
100
    /**
101
     * Syntax:
102
     *     new Expression('expr')
103
     *     new Expression('expr', 'alias')
104
     *     new Expression('database', 'table', 'column')
105
     *     new Expression('database', 'table', 'column', 'alias')
106
     *
107
     * If the database, table or column name is not required, pass an empty
108
     * string.
109
     *
110
     * @param string|null $database The name of the database or the expression.
111
     * @param string|null $table    The name of the table or the alias of the expression.
112
     * @param string|null $column   the name of the column
113
     * @param string|null $alias    the name of the alias
114
     */
115 1024
    public function __construct($database = null, $table = null, $column = null, $alias = null)
116
    {
117 1024
        if (($column === null) && ($alias === null)) {
118 1024
            $this->expr = $database; // case 1
119 1024
            $this->alias = $table; // case 2
120
        } else {
121 4
            $this->database = $database; // case 3
122 4
            $this->table = $table; // case 3
123 4
            $this->column = $column; // case 3
124 4
            $this->alias = $alias; // case 4
125
        }
126
    }
127
128
    /**
129
     * Possible options:.
130
     *
131
     *      `field`
132
     *
133
     *          First field to be filled.
134
     *          If this is not specified, it takes the value of `parseField`.
135
     *
136
     *      `parseField`
137
     *
138
     *          Specifies the type of the field parsed. It may be `database`,
139
     *          `table` or `column`. These expressions may not include
140
     *          parentheses.
141
     *
142
     *      `breakOnAlias`
143
     *
144
     *          If not empty, breaks when the alias occurs (it is not included).
145
     *
146
     *      `breakOnParentheses`
147
     *
148
     *          If not empty, breaks when the first parentheses occurs.
149
     *
150
     *      `parenthesesDelimited`
151
     *
152
     *          If not empty, breaks after last parentheses occurred.
153
     *
154
     * @param Parser               $parser  the parser that serves as context
155
     * @param TokensList           $list    the list of tokens that are being parsed
156
     * @param array<string, mixed> $options parameters for parsing
157
     *
158
     * @return Expression|null
159
     *
160
     * @throws ParserException
161
     */
162 1014
    public static function parse(Parser $parser, TokensList $list, array $options = [])
163
    {
164 1014
        $ret = new static();
165
166
        /**
167
         * Whether current tokens make an expression or a table reference.
168
         *
169
         * @var bool
170
         */
171 1014
        $isExpr = false;
172
173
        /**
174
         * Whether a period was previously found.
175
         *
176
         * @var bool
177
         */
178 1014
        $dot = false;
179
180
        /**
181
         * Whether an alias is expected. Is 2 if `AS` keyword was found.
182
         *
183
         * @var bool
184
         */
185 1014
        $alias = false;
186
187
        /**
188
         * Counts brackets.
189
         *
190
         * @var int
191
         */
192 1014
        $brackets = 0;
193
194
        /**
195
         * Keeps track of the last two previous tokens.
196
         *
197
         * @var Token[]
198
         */
199 1014
        $prev = [
200 1014
            null,
201 1014
            null,
202 1014
        ];
203
204
        // When a field is parsed, no parentheses are expected.
205 1014
        if (! empty($options['parseField'])) {
206 564
            $options['breakOnParentheses'] = true;
207 564
            $options['field'] = $options['parseField'];
208
        }
209
210 1014
        for (; $list->idx < $list->count; ++$list->idx) {
211
            /**
212
             * Token parsed at this moment.
213
             */
214 1014
            $token = $list->tokens[$list->idx];
215
216
            // End of statement.
217 1014
            if ($token->type === Token::TYPE_DELIMITER) {
218 414
                break;
219
            }
220
221
            // Skipping whitespaces and comments.
222 1012
            if (($token->type === Token::TYPE_WHITESPACE) || ($token->type === Token::TYPE_COMMENT)) {
223
                // If the token is a closing C comment from a MySQL command, it must be ignored.
224 860
                if ($isExpr && $token->token !== '*/') {
225 394
                    $ret->expr .= $token->token;
226
                }
227
228 860
                continue;
229
            }
230
231 1012
            if ($token->type === Token::TYPE_KEYWORD) {
232 790
                if (($brackets > 0) && empty($ret->subquery) && ! empty(Parser::$statementParsers[$token->keyword])) {
233
                    // A `(` was previously found and this keyword is the
234
                    // beginning of a statement, so this is a subquery.
235 68
                    $ret->subquery = $token->keyword;
236
                } elseif (
237 788
                    ($token->flags & Token::FLAG_KEYWORD_FUNCTION)
238 788
                    && (empty($options['parseField'])
239 788
                    && ! $alias)
240
                ) {
241 100
                    $isExpr = true;
242 764
                } elseif (($token->flags & Token::FLAG_KEYWORD_RESERVED) && ($brackets === 0)) {
243 708
                    if (empty(self::$allowedKeywords[$token->keyword])) {
244
                        // A reserved keyword that is not allowed in the
245
                        // expression was found so the expression must have
246
                        // ended and a new clause is starting.
247 664
                        break;
248
                    }
249
250 196
                    if ($token->keyword === 'AS') {
251 154
                        if (! empty($options['breakOnAlias'])) {
252 30
                            break;
253
                        }
254
255 140
                        if ($alias) {
256 2
                            $parser->error('An alias was expected.', $token);
257 2
                            break;
258
                        }
259
260 140
                        $alias = true;
261 140
                        continue;
262
                    }
263
264 58
                    if ($token->keyword === 'CASE') {
265
                        // For a use of CASE like
266
                        // 'SELECT a = CASE .... END, b=1, `id`, ... FROM ...'
267 10
                        $tempCaseExpr = CaseExpression::parse($parser, $list);
268 10
                        $ret->expr .= CaseExpression::build($tempCaseExpr);
269 10
                        $isExpr = true;
270 10
                        continue;
271
                    }
272
273 48
                    $isExpr = true;
274 218
                } elseif ($brackets === 0 && strlen((string) $ret->expr) > 0 && ! $alias) {
275
                    /* End of expression */
276 150
                    break;
277
                }
278
            }
279
280
            if (
281 1008
                ($token->type === Token::TYPE_NUMBER)
282 990
                || ($token->type === Token::TYPE_BOOL)
283 990
                || (($token->type === Token::TYPE_SYMBOL)
284 990
                && ($token->flags & Token::FLAG_SYMBOL_VARIABLE))
285 982
                || (($token->type === Token::TYPE_SYMBOL)
286 982
                && ($token->flags & Token::FLAG_SYMBOL_PARAMETER))
287 1008
                || (($token->type === Token::TYPE_OPERATOR)
288 1008
                && ($token->value !== '.'))
289
            ) {
290 650
                if (! empty($options['parseField'])) {
291 202
                    break;
292
                }
293
294
                // Numbers, booleans and operators (except dot) are usually part
295
                // of expressions.
296 524
                $isExpr = true;
297
            }
298
299 1008
            if ($token->type === Token::TYPE_OPERATOR) {
300 472
                if (! empty($options['breakOnParentheses']) && (($token->value === '(') || ($token->value === ')'))) {
301
                    // No brackets were expected.
302 4
                    break;
303
                }
304
305 470
                if ($token->value === '(') {
306 178
                    ++$brackets;
307
                    if (
308 178
                        empty($ret->function) && ($prev[1] !== null)
309 178
                        && (($prev[1]->type === Token::TYPE_NONE)
310 178
                        || ($prev[1]->type === Token::TYPE_SYMBOL)
311 178
                        || (($prev[1]->type === Token::TYPE_KEYWORD)
312 178
                        && ($prev[1]->flags & Token::FLAG_KEYWORD_FUNCTION)))
313
                    ) {
314 178
                        $ret->function = $prev[1]->value;
315
                    }
316 468
                } elseif ($token->value === ')') {
317 190
                    if ($brackets === 0) {
318
                        // Not our bracket
319 20
                        break;
320
                    }
321
322 176
                    --$brackets;
323 176
                    if ($brackets === 0) {
324 176
                        if (! empty($options['parenthesesDelimited'])) {
325
                            // The current token is the last bracket, the next
326
                            // one will be outside the expression.
327 50
                            $ret->expr .= $token->token;
328 50
                            ++$list->idx;
329 176
                            break;
330
                        }
331 22
                    } elseif ($brackets < 0) {
332
                        // $parser->error('Unexpected closing bracket.', $token);
333
                        // $brackets = 0;
334 144
                        break;
335
                    }
336 418
                } elseif ($token->value === ',') {
337
                    // Expressions are comma-delimited.
338 268
                    if ($brackets === 0) {
339 234
                        break;
340
                    }
341
                }
342
            }
343
344
            // Saving the previous tokens.
345 1006
            $prev[0] = $prev[1];
346 1006
            $prev[1] = $token;
347
348 1006
            if ($alias) {
349
                // An alias is expected (the keyword `AS` was previously found).
350 138
                if (! empty($ret->alias)) {
351 2
                    $parser->error('An alias was previously found.', $token);
352 2
                    break;
353
                }
354
355 138
                $ret->alias = $token->value;
356 138
                $alias = false;
357 1006
            } elseif ($isExpr) {
358
                // Handling aliases.
359
                if (
360 480
                    $brackets === 0
361 480
                    && ($prev[0] === null
362 480
                        || (($prev[0]->type !== Token::TYPE_OPERATOR || $prev[0]->token === ')')
363 480
                            && ($prev[0]->type !== Token::TYPE_KEYWORD
364 480
                                || ! ($prev[0]->flags & Token::FLAG_KEYWORD_RESERVED))))
365 480
                    && (($prev[1]->type === Token::TYPE_STRING)
366 480
                        || ($prev[1]->type === Token::TYPE_SYMBOL
367 480
                            && ! ($prev[1]->flags & Token::FLAG_SYMBOL_VARIABLE))
368 480
                        || ($prev[1]->type === Token::TYPE_NONE))
369
                ) {
370 14
                    if (! empty($ret->alias)) {
371 4
                        $parser->error('An alias was previously found.', $token);
372 4
                        break;
373
                    }
374
375 12
                    $ret->alias = $prev[1]->value;
376
                } else {
377 480
                    $currIdx = $list->idx;
378 480
                    --$list->idx;
379 480
                    $beforeToken = $list->getPrevious();
380 480
                    $list->idx = $currIdx;
381
                    // columns names tokens are of type NONE, or SYMBOL (`col`), and the columns options
382
                    // would start with a token of type KEYWORD, in that case, we want to have a space
383
                    // between the tokens.
384
                    if (
385 480
                        $ret->expr !== null &&
386
                        $beforeToken &&
387 480
                        ($beforeToken->type === Token::TYPE_NONE ||
388 480
                        $beforeToken->type === Token::TYPE_SYMBOL || $beforeToken->type === Token::TYPE_STRING) &&
389 480
                        $token->type === Token::TYPE_KEYWORD
390
                    ) {
391 76
                        $ret->expr = rtrim($ret->expr, ' ') . ' ';
392
                    }
393
394 480
                    $ret->expr .= $token->token;
395
                }
396 926
            } elseif (! $isExpr) {
397 926
                if (($token->type === Token::TYPE_OPERATOR) && ($token->value === '.')) {
398
                    // Found a `.` which means we expect a column name and
399
                    // the column name we parsed is actually the table name
400
                    // and the table name is actually a database name.
401 110
                    if (! empty($ret->database) || $dot) {
402 4
                        $parser->error('Unexpected dot.', $token);
403
                    }
404
405 110
                    $ret->database = $ret->table;
406 110
                    $ret->table = $ret->column;
407 110
                    $ret->column = null;
408 110
                    $dot = true;
409 110
                    $ret->expr .= $token->token;
410
                } else {
411 926
                    $field = empty($options['field']) ? 'column' : $options['field'];
412 926
                    if (empty($ret->$field)) {
413 926
                        $ret->$field = $token->value;
414 926
                        $ret->expr .= $token->token;
415 926
                        $dot = false;
416
                    } else {
417
                        // No alias is expected.
418 64
                        if (! empty($options['breakOnAlias'])) {
419 22
                            break;
420
                        }
421
422 42
                        if (! empty($ret->alias)) {
423 8
                            $parser->error('An alias was previously found.', $token);
424 8
                            break;
425
                        }
426
427 38
                        $ret->alias = $token->value;
428
                    }
429
                }
430
            }
431
        }
432
433 1014
        if ($alias) {
434 6
            $parser->error('An alias was expected.', $list->tokens[$list->idx - 1]);
435
        }
436
437
        // White-spaces might be added at the end.
438 1014
        $ret->expr = trim((string) $ret->expr);
439
440 1014
        if ($ret->expr === '') {
441 44
            return null;
442
        }
443
444 1006
        --$list->idx;
445
446 1006
        return $ret;
447
    }
448
449
    /**
450
     * @param Expression|Expression[] $component the component to be built
451
     * @param array<string, mixed>    $options   parameters for building
452
     */
453 256
    public static function build($component, array $options = []): string
454
    {
455 256
        if (is_array($component)) {
456 2
            return implode(', ', $component);
457
        }
458
459 256
        if ($component->expr !== '' && $component->expr !== null) {
460 238
            $ret = $component->expr;
461
        } else {
462 22
            $fields = [];
463 22
            if (isset($component->database) && ($component->database !== '')) {
464 2
                $fields[] = $component->database;
465
            }
466
467 22
            if (isset($component->table) && ($component->table !== '')) {
468 22
                $fields[] = $component->table;
469
            }
470
471 22
            if (isset($component->column) && ($component->column !== '')) {
472 2
                $fields[] = $component->column;
473
            }
474
475 22
            $ret = implode('.', Context::escape($fields));
0 ignored issues
show
Bug introduced by
It seems like PhpMyAdmin\SqlParser\Context::escape($fields) can also be of type string; however, parameter $pieces of implode() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

475
            $ret = implode('.', /** @scrutinizer ignore-type */ Context::escape($fields));
Loading history...
476
        }
477
478 256
        if (! empty($component->alias)) {
479 30
            $ret .= ' AS ' . Context::escape($component->alias);
480
        }
481
482 256
        return $ret;
483
    }
484
485 196
    public function __toString(): string
486
    {
487 196
        return static::build($this);
488
    }
489
}
490