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

Limit::build()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
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\Parser;
9
use PhpMyAdmin\SqlParser\Token;
10
use PhpMyAdmin\SqlParser\TokensList;
11
12
/**
13
 * `LIMIT` keyword parser.
14
 */
15
final class Limit implements Component
16
{
17
    /**
18
     * The number of rows skipped.
19
     *
20
     * @var int
21
     */
22
    public $offset;
23
24
    /**
25
     * The number of rows to be returned.
26
     *
27
     * @var int
28
     */
29
    public $rowCount;
30
31
    /**
32
     * @param int $rowCount the row count
33
     * @param int $offset   the offset
34
     */
35 64
    public function __construct($rowCount = 0, $offset = 0)
36
    {
37 64
        $this->rowCount = $rowCount;
38 64
        $this->offset = $offset;
39
    }
40
41
    /**
42
     * @param Parser               $parser  the parser that serves as context
43
     * @param TokensList           $list    the list of tokens that are being parsed
44
     * @param array<string, mixed> $options parameters for parsing
45
     *
46
     * @return Limit
47
     */
48 58
    public static function parse(Parser $parser, TokensList $list, array $options = [])
49
    {
50 58
        $ret = new static();
51
52 58
        $offset = false;
53
54 58
        for (; $list->idx < $list->count; ++$list->idx) {
55
            /**
56
             * Token parsed at this moment.
57
             */
58 58
            $token = $list->tokens[$list->idx];
59
60
            // End of statement.
61 58
            if ($token->type === Token::TYPE_DELIMITER) {
62 48
                break;
63
            }
64
65
            // Skipping whitespaces and comments.
66 58
            if (($token->type === Token::TYPE_WHITESPACE) || ($token->type === Token::TYPE_COMMENT)) {
67 58
                continue;
68
            }
69
70 58
            if (($token->type === Token::TYPE_KEYWORD) && ($token->flags & Token::FLAG_KEYWORD_RESERVED)) {
71 6
                break;
72
            }
73
74 58
            if ($token->type === Token::TYPE_KEYWORD && $token->keyword === 'OFFSET') {
75 8
                if ($offset) {
76 2
                    $parser->error('An offset was expected.', $token);
77
                }
78
79 8
                $offset = true;
80 8
                continue;
81
            }
82
83 58
            if (($token->type === Token::TYPE_OPERATOR) && ($token->value === ',')) {
84 46
                $ret->offset = $ret->rowCount;
85 46
                $ret->rowCount = 0;
86 46
                continue;
87
            }
88
89
            // Skip if not a number
90 58
            if (($token->type !== Token::TYPE_NUMBER)) {
91 6
                break;
92
            }
93
94 58
            if ($offset) {
95 6
                $ret->offset = $token->value;
96 6
                $offset = false;
97
            } else {
98 58
                $ret->rowCount = $token->value;
99
            }
100
        }
101
102 58
        if ($offset) {
0 ignored issues
show
introduced by
The condition $offset is always false.
Loading history...
103 2
            $parser->error('An offset was expected.', $list->tokens[$list->idx - 1]);
104
        }
105
106 58
        --$list->idx;
107
108 58
        return $ret;
109
    }
110
111
    /**
112
     * @param Limit                $component the component to be built
113
     * @param array<string, mixed> $options   parameters for building
114
     */
115 28
    public static function build($component, array $options = []): string
116
    {
117 28
        return $component->offset . ', ' . $component->rowCount;
118
    }
119
120 2
    public function __toString(): string
121
    {
122 2
        return static::build($this);
123
    }
124
}
125