Skip to content

Commit e9edd4c

Browse files
committed
Introduce the TokenType enum
Used to replace the Token::TYPE_* constants. The TokenType::cases() method replaces the Token::TYPE_ALL constant. Adds the Token::FLAG_NONE constant with the same value as Token::TYPE_NONE. Signed-off-by: Maurício Meneghini Fauth <[email protected]>
1 parent 4077a82 commit e9edd4c

File tree

490 files changed

+66538
-22752
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

490 files changed

+66538
-22752
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
"phpunit/phpunit": "^10.0",
3636
"psalm/plugin-phpunit": "^0.18.4",
3737
"vimeo/psalm": "^5.7",
38-
"zumba/json-serializer": "^3.0"
38+
"zumba/json-serializer": "^3.2"
3939
},
4040
"conflict": {
4141
"phpmyadmin/motranslator": "<5.2"

psalm-baseline.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1260,7 +1260,7 @@
12601260
<code>$i</code>
12611261
</PossiblyNullOperand>
12621262
<PossiblyNullPropertyFetch>
1263-
<code><![CDATA[$lexer->list->getNextOfType(Token::TYPE_KEYWORD)->keyword]]></code>
1263+
<code><![CDATA[$lexer->list->getNextOfType(TokenType::Keyword)->keyword]]></code>
12641264
<code><![CDATA[$statement->into->dest]]></code>
12651265
</PossiblyNullPropertyFetch>
12661266
<PossiblyNullReference>

src/Components/AlterOperation.php

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use PhpMyAdmin\SqlParser\Parser;
99
use PhpMyAdmin\SqlParser\Token;
1010
use PhpMyAdmin\SqlParser\TokensList;
11+
use PhpMyAdmin\SqlParser\TokenType;
1112

1213
use function array_key_exists;
1314
use function in_array;
@@ -321,17 +322,17 @@ public static function parse(Parser $parser, TokensList $list, array $options =
321322
$token = $list->tokens[$list->idx];
322323

323324
// End of statement.
324-
if ($token->type === Token::TYPE_DELIMITER) {
325+
if ($token->type === TokenType::Delimiter) {
325326
break;
326327
}
327328

328329
// Skipping comments.
329-
if ($token->type === Token::TYPE_COMMENT) {
330+
if ($token->type === TokenType::Comment) {
330331
continue;
331332
}
332333

333334
// Skipping whitespaces.
334-
if ($token->type === Token::TYPE_WHITESPACE) {
335+
if ($token->type === TokenType::Whitespace) {
335336
if ($state === 2) {
336337
// When parsing the unknown part, the whitespaces are
337338
// included to not break anything.
@@ -347,7 +348,7 @@ public static function parse(Parser $parser, TokensList $list, array $options =
347348
// body in the unknown tokens list, as they define their own statements.
348349
if ($ret->options->has('AS') || $ret->options->has('DO')) {
349350
for (; $list->idx < $list->count; ++$list->idx) {
350-
if ($list->tokens[$list->idx]->type === Token::TYPE_DELIMITER) {
351+
if ($list->tokens[$list->idx]->type === TokenType::Delimiter) {
351352
break;
352353
}
353354

@@ -392,22 +393,22 @@ public static function parse(Parser $parser, TokensList $list, array $options =
392393
$arrayKey = $token->token;
393394
}
394395

395-
if ($token->type === Token::TYPE_OPERATOR) {
396+
if ($token->type === TokenType::Operator) {
396397
if ($token->value === '(') {
397398
++$brackets;
398399
} elseif ($token->value === ')') {
399400
--$brackets;
400401
} elseif (($token->value === ',') && ($brackets === 0)) {
401402
break;
402403
}
403-
} elseif (! self::checkIfTokenQuotedSymbol($token) && $token->type !== Token::TYPE_STRING) {
404+
} elseif (! self::checkIfTokenQuotedSymbol($token) && $token->type !== TokenType::String) {
404405
if (isset(Parser::STATEMENT_PARSERS[$arrayKey]) && Parser::STATEMENT_PARSERS[$arrayKey] !== '') {
405406
$list->idx++; // Ignore the current token
406407
$nextToken = $list->getNext();
407408

408409
if ($token->value === 'SET' && $nextToken !== null && $nextToken->value === '(') {
409410
// To avoid adding the tokens between the SET() parentheses to the unknown tokens
410-
$list->getNextOfTypeAndValue(Token::TYPE_OPERATOR, ')');
411+
$list->getNextOfTypeAndValue(TokenType::Operator, ')');
411412
} elseif ($token->value === 'SET' && $nextToken !== null && $nextToken->value === 'DEFAULT') {
412413
// to avoid adding the `DEFAULT` token to the unknown tokens.
413414
++$list->idx;
@@ -438,12 +439,12 @@ public static function parse(Parser $parser, TokensList $list, array $options =
438439
$list->idx++; // Ignore the current token
439440
$nextToken = $list->getNext();
440441
if (
441-
($token->type === Token::TYPE_KEYWORD)
442+
($token->type === TokenType::Keyword)
442443
&& (($token->keyword === 'PARTITION BY')
443444
|| ($token->keyword === 'PARTITION' && $nextToken && $nextToken->value !== '('))
444445
) {
445446
$partitionState = 1;
446-
} elseif (($token->type === Token::TYPE_KEYWORD) && ($token->keyword === 'PARTITION')) {
447+
} elseif (($token->type === TokenType::Keyword) && ($token->keyword === 'PARTITION')) {
447448
$partitionState = 2;
448449
}
449450

@@ -466,15 +467,15 @@ public static function parse(Parser $parser, TokensList $list, array $options =
466467
}
467468

468469
if (
469-
$token->type === Token::TYPE_OPERATOR
470+
$token->type === TokenType::Operator
470471
&& $token->value === '('
471472
&& $nextToken
472473
&& $nextToken->keyword === 'PARTITION'
473474
) {
474475
$partitionState = 2;
475476
--$list->idx; // Current idx is on "(". We need a step back for ArrayObj::parse incoming.
476477
} else {
477-
$ret->field .= $token->type === Token::TYPE_WHITESPACE ? ' ' : $token->token;
478+
$ret->field .= $token->type === TokenType::Whitespace ? ' ' : $token->token;
478479
}
479480
} elseif ($partitionState === 2) {
480481
$ret->partitions = ArrayObj::parse(
@@ -552,7 +553,7 @@ private static function checkIfColumnDefinitionKeyword($tokenValue): bool
552553
*/
553554
private static function checkIfTokenQuotedSymbol($token): bool
554555
{
555-
return $token->type === Token::TYPE_SYMBOL && $token->flags === Token::FLAG_SYMBOL_BACKTICK;
556+
return $token->type === TokenType::Symbol && $token->flags === Token::FLAG_SYMBOL_BACKTICK;
556557
}
557558

558559
public function __toString(): string

src/Components/Array2d.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use PhpMyAdmin\SqlParser\Parser;
99
use PhpMyAdmin\SqlParser\Token;
1010
use PhpMyAdmin\SqlParser\TokensList;
11+
use PhpMyAdmin\SqlParser\TokenType;
1112
use PhpMyAdmin\SqlParser\Translator;
1213
use RuntimeException;
1314

@@ -58,17 +59,17 @@ public static function parse(Parser $parser, TokensList $list, array $options =
5859
$token = $list->tokens[$list->idx];
5960

6061
// End of statement.
61-
if ($token->type === Token::TYPE_DELIMITER) {
62+
if ($token->type === TokenType::Delimiter) {
6263
break;
6364
}
6465

6566
// Skipping whitespaces and comments.
66-
if (($token->type === Token::TYPE_WHITESPACE) || ($token->type === Token::TYPE_COMMENT)) {
67+
if (($token->type === TokenType::Whitespace) || ($token->type === TokenType::Comment)) {
6768
continue;
6869
}
6970

7071
// No keyword is expected.
71-
if (($token->type === Token::TYPE_KEYWORD) && ($token->flags & Token::FLAG_KEYWORD_RESERVED)) {
72+
if (($token->type === TokenType::Keyword) && ($token->flags & Token::FLAG_KEYWORD_RESERVED)) {
7273
break;
7374
}
7475

src/Components/ArrayObj.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66

77
use PhpMyAdmin\SqlParser\Component;
88
use PhpMyAdmin\SqlParser\Parser;
9-
use PhpMyAdmin\SqlParser\Token;
109
use PhpMyAdmin\SqlParser\TokensList;
10+
use PhpMyAdmin\SqlParser\TokenType;
1111

1212
use function implode;
1313
use function strlen;
@@ -88,7 +88,7 @@ public static function parse(Parser $parser, TokensList $list, array $options =
8888
$token = $list->tokens[$list->idx];
8989

9090
// End of statement.
91-
if ($token->type === Token::TYPE_DELIMITER) {
91+
if ($token->type === TokenType::Delimiter) {
9292
if ($brackets > 0) {
9393
$parser->error('A closing bracket was expected.', $token);
9494
}
@@ -97,18 +97,18 @@ public static function parse(Parser $parser, TokensList $list, array $options =
9797
}
9898

9999
// Skipping whitespaces and comments.
100-
if (($token->type === Token::TYPE_WHITESPACE) || ($token->type === Token::TYPE_COMMENT)) {
100+
if (($token->type === TokenType::Whitespace) || ($token->type === TokenType::Comment)) {
101101
$lastRaw .= $token->token;
102102
$lastValue = trim($lastValue) . ' ';
103103
continue;
104104
}
105105

106-
if (($brackets === 0) && (($token->type !== Token::TYPE_OPERATOR) || ($token->value !== '('))) {
106+
if (($brackets === 0) && (($token->type !== TokenType::Operator) || ($token->value !== '('))) {
107107
$parser->error('An opening bracket was expected.', $token);
108108
break;
109109
}
110110

111-
if ($token->type === Token::TYPE_OPERATOR) {
111+
if ($token->type === TokenType::Operator) {
112112
if ($token->value === '(') {
113113
if (++$brackets === 1) { // 1 is the base level.
114114
continue;

src/Components/CaseExpression.php

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use PhpMyAdmin\SqlParser\Parser;
1010
use PhpMyAdmin\SqlParser\Token;
1111
use PhpMyAdmin\SqlParser\TokensList;
12+
use PhpMyAdmin\SqlParser\TokenType;
1213

1314
use function count;
1415

@@ -98,12 +99,12 @@ public static function parse(Parser $parser, TokensList $list, array $options =
9899
$token = $list->tokens[$list->idx];
99100

100101
// Skipping whitespaces and comments.
101-
if (($token->type === Token::TYPE_WHITESPACE) || ($token->type === Token::TYPE_COMMENT)) {
102+
if (($token->type === TokenType::Whitespace) || ($token->type === TokenType::Comment)) {
102103
continue;
103104
}
104105

105106
if ($state === 0) {
106-
if ($token->type === Token::TYPE_KEYWORD) {
107+
if ($token->type === TokenType::Keyword) {
107108
switch ($token->keyword) {
108109
case 'WHEN':
109110
++$list->idx; // Skip 'WHEN'
@@ -132,7 +133,7 @@ public static function parse(Parser $parser, TokensList $list, array $options =
132133
}
133134
} elseif ($state === 1) {
134135
if ($type === 0) {
135-
if ($token->type === Token::TYPE_KEYWORD) {
136+
if ($token->type === TokenType::Keyword) {
136137
switch ($token->keyword) {
137138
case 'WHEN':
138139
++$list->idx; // Skip 'WHEN'
@@ -154,23 +155,23 @@ public static function parse(Parser $parser, TokensList $list, array $options =
154155
break 2;
155156
}
156157
}
157-
} elseif ($token->type === Token::TYPE_KEYWORD && $token->keyword === 'THEN') {
158+
} elseif ($token->type === TokenType::Keyword && $token->keyword === 'THEN') {
158159
++$list->idx; // Skip 'THEN'
159160
$newResult = Expression::parse($parser, $list);
160161
$state = 0;
161162
$ret->results[] = $newResult;
162-
} elseif ($token->type === Token::TYPE_KEYWORD) {
163+
} elseif ($token->type === TokenType::Keyword) {
163164
$parser->error('Unexpected keyword.', $token);
164165
break;
165166
}
166167
} elseif ($state === 2) {
167168
if ($type === 0) {
168-
if ($token->type === Token::TYPE_KEYWORD && $token->keyword === 'THEN') {
169+
if ($token->type === TokenType::Keyword && $token->keyword === 'THEN') {
169170
++$list->idx; // Skip 'THEN'
170171
$newResult = Expression::parse($parser, $list);
171172
$ret->results[] = $newResult;
172173
$state = 1;
173-
} elseif ($token->type === Token::TYPE_KEYWORD) {
174+
} elseif ($token->type === TokenType::Keyword) {
174175
$parser->error('Unexpected keyword.', $token);
175176
break;
176177
}
@@ -187,17 +188,17 @@ public static function parse(Parser $parser, TokensList $list, array $options =
187188
$token = $list->tokens[$list->idx];
188189

189190
// End of statement.
190-
if ($token->type === Token::TYPE_DELIMITER) {
191+
if ($token->type === TokenType::Delimiter) {
191192
break;
192193
}
193194

194195
// Skipping whitespaces and comments.
195-
if (($token->type === Token::TYPE_WHITESPACE) || ($token->type === Token::TYPE_COMMENT)) {
196+
if (($token->type === TokenType::Whitespace) || ($token->type === TokenType::Comment)) {
196197
continue;
197198
}
198199

199200
// Handle optional AS keyword before alias
200-
if ($token->type === Token::TYPE_KEYWORD && $token->keyword === 'AS') {
201+
if ($token->type === TokenType::Keyword && $token->keyword === 'AS') {
201202
if ($asFound || ! empty($ret->alias)) {
202203
$parser->error('Potential duplicate alias of CASE expression.', $token);
203204
break;
@@ -209,7 +210,7 @@ public static function parse(Parser $parser, TokensList $list, array $options =
209210

210211
if (
211212
$asFound
212-
&& $token->type === Token::TYPE_KEYWORD
213+
&& $token->type === TokenType::Keyword
213214
&& ($token->flags & Token::FLAG_KEYWORD_RESERVED || $token->flags & Token::FLAG_KEYWORD_FUNCTION)
214215
) {
215216
$parser->error('An alias expected after AS but got ' . $token->value, $token);
@@ -219,9 +220,9 @@ public static function parse(Parser $parser, TokensList $list, array $options =
219220

220221
if (
221222
$asFound
222-
|| $token->type === Token::TYPE_STRING
223-
|| ($token->type === Token::TYPE_SYMBOL && ! $token->flags & Token::FLAG_SYMBOL_VARIABLE)
224-
|| $token->type === Token::TYPE_NONE
223+
|| $token->type === TokenType::String
224+
|| ($token->type === TokenType::Symbol && ! $token->flags & Token::FLAG_SYMBOL_VARIABLE)
225+
|| $token->type === TokenType::None
225226
) {
226227
// An alias is expected (the keyword `AS` was previously found).
227228
if (! empty($ret->alias)) {

src/Components/Condition.php

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use PhpMyAdmin\SqlParser\Parser;
99
use PhpMyAdmin\SqlParser\Token;
1010
use PhpMyAdmin\SqlParser\TokensList;
11+
use PhpMyAdmin\SqlParser\TokenType;
1112

1213
use function implode;
1314
use function in_array;
@@ -122,18 +123,18 @@ public static function parse(Parser $parser, TokensList $list, array $options =
122123
$token = $list->tokens[$list->idx];
123124

124125
// End of statement.
125-
if ($token->type === Token::TYPE_DELIMITER) {
126+
if ($token->type === TokenType::Delimiter) {
126127
break;
127128
}
128129

129130
// Skipping whitespaces and comments.
130-
if ($token->type === Token::TYPE_COMMENT) {
131+
if ($token->type === TokenType::Comment) {
131132
continue;
132133
}
133134

134135
// Replacing all whitespaces (new lines, tabs, etc.) with a single
135136
// space character.
136-
if ($token->type === Token::TYPE_WHITESPACE) {
137+
if ($token->type === TokenType::Whitespace) {
137138
$expr->expr .= ' ';
138139
continue;
139140
}
@@ -162,7 +163,7 @@ public static function parse(Parser $parser, TokensList $list, array $options =
162163
}
163164

164165
if (
165-
($token->type === Token::TYPE_KEYWORD)
166+
($token->type === TokenType::Keyword)
166167
&& ($token->flags & Token::FLAG_KEYWORD_RESERVED)
167168
&& ! ($token->flags & Token::FLAG_KEYWORD_FUNCTION)
168169
) {
@@ -175,7 +176,7 @@ public static function parse(Parser $parser, TokensList $list, array $options =
175176
}
176177
}
177178

178-
if ($token->type === Token::TYPE_OPERATOR) {
179+
if ($token->type === TokenType::Operator) {
179180
if ($token->value === '(') {
180181
++$brackets;
181182
} elseif ($token->value === ')') {
@@ -189,11 +190,11 @@ public static function parse(Parser $parser, TokensList $list, array $options =
189190

190191
$expr->expr .= $token->token;
191192
if (
192-
($token->type !== Token::TYPE_NONE)
193-
&& (($token->type !== Token::TYPE_KEYWORD)
193+
($token->type !== TokenType::None)
194+
&& (($token->type !== TokenType::Keyword)
194195
|| ($token->flags & Token::FLAG_KEYWORD_RESERVED))
195-
&& ($token->type !== Token::TYPE_STRING)
196-
&& ($token->type !== Token::TYPE_SYMBOL)
196+
&& ($token->type !== TokenType::String)
197+
&& ($token->type !== TokenType::Symbol)
197198
) {
198199
continue;
199200
}

0 commit comments

Comments
 (0)