Issues (119)

src/Parsers/Limits.php (1 issue)

Severity
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpMyAdmin\SqlParser\Parsers;
6
7
use PhpMyAdmin\SqlParser\Components\Limit;
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
14
/**
15
 * `LIMIT` keyword parser.
16
 */
17
final class Limits implements Parseable
18
{
19
    /**
20
     * @param Parser               $parser  the parser that serves as context
21
     * @param TokensList           $list    the list of tokens that are being parsed
22
     * @param array<string, mixed> $options parameters for parsing
23
     */
24 60
    public static function parse(Parser $parser, TokensList $list, array $options = []): Limit
25
    {
26 60
        $ret = new Limit();
27
28 60
        $offset = false;
29
30 60
        for (; $list->idx < $list->count; ++$list->idx) {
31
            /**
32
             * Token parsed at this moment.
33
             */
34 60
            $token = $list->tokens[$list->idx];
35
36
            // End of statement.
37 60
            if ($token->type === TokenType::Delimiter) {
38 50
                break;
39
            }
40
41
            // Skipping whitespaces and comments.
42 60
            if (($token->type === TokenType::Whitespace) || ($token->type === TokenType::Comment)) {
43 60
                continue;
44
            }
45
46 60
            if (($token->type === TokenType::Keyword) && ($token->flags & Token::FLAG_KEYWORD_RESERVED)) {
47 6
                break;
48
            }
49
50 60
            if ($token->type === TokenType::Keyword && $token->keyword === 'OFFSET') {
51 10
                if ($offset) {
52 2
                    $parser->error('An offset was expected.', $token);
53
                }
54
55 10
                $offset = true;
56 10
                continue;
57
            }
58
59 60
            if (($token->type === TokenType::Operator) && ($token->value === ',')) {
60 46
                $ret->offset = $ret->rowCount;
61 46
                $ret->rowCount = 0;
62 46
                continue;
63
            }
64
65
            // Skip if not a number or a bind parameter (?)
66
            if (
67 60
                ! ($token->type === TokenType::Number
68 60
                    || ($token->type === TokenType::Symbol && ($token->flags & Token::FLAG_SYMBOL_PARAMETER)))
69
            ) {
70 6
                break;
71
            }
72
73 60
            if ($offset) {
74 8
                $ret->offset = $token->value;
75 8
                $offset = false;
76
            } else {
77 60
                $ret->rowCount = $token->value;
78
            }
79
        }
80
81 60
        if ($offset) {
0 ignored issues
show
The condition $offset is always false.
Loading history...
82 2
            $parser->error('An offset was expected.', $list->tokens[$list->idx - 1]);
83
        }
84
85 60
        --$list->idx;
86
87 60
        return $ret;
88
    }
89
}
90