Skip to content

Commit

Permalink
Introduce the TokenType enum
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
MauricioFauth committed Sep 26, 2023
1 parent 4077a82 commit e9edd4c
Show file tree
Hide file tree
Showing 490 changed files with 66,538 additions and 22,752 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"phpunit/phpunit": "^10.0",
"psalm/plugin-phpunit": "^0.18.4",
"vimeo/psalm": "^5.7",
"zumba/json-serializer": "^3.0"
"zumba/json-serializer": "^3.2"
},
"conflict": {
"phpmyadmin/motranslator": "<5.2"
Expand Down
2 changes: 1 addition & 1 deletion psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1260,7 +1260,7 @@
<code>$i</code>
</PossiblyNullOperand>
<PossiblyNullPropertyFetch>
<code><![CDATA[$lexer->list->getNextOfType(Token::TYPE_KEYWORD)->keyword]]></code>
<code><![CDATA[$lexer->list->getNextOfType(TokenType::Keyword)->keyword]]></code>
<code><![CDATA[$statement->into->dest]]></code>
</PossiblyNullPropertyFetch>
<PossiblyNullReference>
Expand Down
25 changes: 13 additions & 12 deletions src/Components/AlterOperation.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use PhpMyAdmin\SqlParser\Parser;
use PhpMyAdmin\SqlParser\Token;
use PhpMyAdmin\SqlParser\TokensList;
use PhpMyAdmin\SqlParser\TokenType;

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

// End of statement.
if ($token->type === Token::TYPE_DELIMITER) {
if ($token->type === TokenType::Delimiter) {
break;
}

// Skipping comments.
if ($token->type === Token::TYPE_COMMENT) {
if ($token->type === TokenType::Comment) {
continue;

Check warning on line 331 in src/Components/AlterOperation.php

View workflow job for this annotation

GitHub Actions / Mutation tests with PHP 8.1

Escaped Mutant for Mutator "Continue_": --- Original +++ New @@ @@ } // Skipping comments. if ($token->type === TokenType::Comment) { - continue; + break; } // Skipping whitespaces. if ($token->type === TokenType::Whitespace) {
}

// Skipping whitespaces.
if ($token->type === Token::TYPE_WHITESPACE) {
if ($token->type === TokenType::Whitespace) {
if ($state === 2) {

Check warning on line 336 in src/Components/AlterOperation.php

View workflow job for this annotation

GitHub Actions / Mutation tests with PHP 8.1

Escaped Mutant for Mutator "DecrementInteger": --- Original +++ New @@ @@ } // Skipping whitespaces. if ($token->type === TokenType::Whitespace) { - if ($state === 2) { + if ($state === 1) { // When parsing the unknown part, the whitespaces are // included to not break anything. $ret->unknown[] = $token;
// When parsing the unknown part, the whitespaces are
// included to not break anything.
Expand All @@ -347,7 +348,7 @@ public static function parse(Parser $parser, TokensList $list, array $options =
// body in the unknown tokens list, as they define their own statements.
if ($ret->options->has('AS') || $ret->options->has('DO')) {
for (; $list->idx < $list->count; ++$list->idx) {

Check warning on line 350 in src/Components/AlterOperation.php

View workflow job for this annotation

GitHub Actions / Mutation tests with PHP 8.1

Escaped Mutant for Mutator "LessThan": --- Original +++ New @@ @@ // Not only when aliasing but also when parsing the body of an event, we just list the tokens of the // body in the unknown tokens list, as they define their own statements. if ($ret->options->has('AS') || $ret->options->has('DO')) { - for (; $list->idx < $list->count; ++$list->idx) { + for (; $list->idx <= $list->count; ++$list->idx) { if ($list->tokens[$list->idx]->type === TokenType::Delimiter) { break; }
if ($list->tokens[$list->idx]->type === Token::TYPE_DELIMITER) {
if ($list->tokens[$list->idx]->type === TokenType::Delimiter) {
break;
}

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

if ($token->type === Token::TYPE_OPERATOR) {
if ($token->type === TokenType::Operator) {
if ($token->value === '(') {
++$brackets;
} elseif ($token->value === ')') {
--$brackets;
} elseif (($token->value === ',') && ($brackets === 0)) {
break;
}
} elseif (! self::checkIfTokenQuotedSymbol($token) && $token->type !== Token::TYPE_STRING) {
} elseif (! self::checkIfTokenQuotedSymbol($token) && $token->type !== TokenType::String) {
if (isset(Parser::STATEMENT_PARSERS[$arrayKey]) && Parser::STATEMENT_PARSERS[$arrayKey] !== '') {
$list->idx++; // Ignore the current token
$nextToken = $list->getNext();

if ($token->value === 'SET' && $nextToken !== null && $nextToken->value === '(') {

Check warning on line 409 in src/Components/AlterOperation.php

View workflow job for this annotation

GitHub Actions / Mutation tests with PHP 8.1

Escaped Mutant for Mutator "LogicalAnd": --- Original +++ New @@ @@ $list->idx++; // Ignore the current token $nextToken = $list->getNext(); - if ($token->value === 'SET' && $nextToken !== null && $nextToken->value === '(') { + if (($token->value === 'SET' || $nextToken !== null) && $nextToken->value === '(') { // To avoid adding the tokens between the SET() parentheses to the unknown tokens $list->getNextOfTypeAndValue(TokenType::Operator, ')'); } elseif ($token->value === 'SET' && $nextToken !== null && $nextToken->value === 'DEFAULT') {
// To avoid adding the tokens between the SET() parentheses to the unknown tokens
$list->getNextOfTypeAndValue(Token::TYPE_OPERATOR, ')');
$list->getNextOfTypeAndValue(TokenType::Operator, ')');
} elseif ($token->value === 'SET' && $nextToken !== null && $nextToken->value === 'DEFAULT') {

Check warning on line 412 in src/Components/AlterOperation.php

View workflow job for this annotation

GitHub Actions / Mutation tests with PHP 8.1

Escaped Mutant for Mutator "LogicalAnd": --- Original +++ New @@ @@ if ($token->value === 'SET' && $nextToken !== null && $nextToken->value === '(') { // To avoid adding the tokens between the SET() parentheses to the unknown tokens $list->getNextOfTypeAndValue(TokenType::Operator, ')'); - } elseif ($token->value === 'SET' && $nextToken !== null && $nextToken->value === 'DEFAULT') { + } elseif (($token->value === 'SET' || $nextToken !== null) && $nextToken->value === 'DEFAULT') { // to avoid adding the `DEFAULT` token to the unknown tokens. ++$list->idx; } else {
// to avoid adding the `DEFAULT` token to the unknown tokens.
++$list->idx;
Expand Down Expand Up @@ -438,12 +439,12 @@ public static function parse(Parser $parser, TokensList $list, array $options =
$list->idx++; // Ignore the current token
$nextToken = $list->getNext();
if (
($token->type === Token::TYPE_KEYWORD)
($token->type === TokenType::Keyword)
&& (($token->keyword === 'PARTITION BY')
|| ($token->keyword === 'PARTITION' && $nextToken && $nextToken->value !== '('))
) {
$partitionState = 1;
} elseif (($token->type === Token::TYPE_KEYWORD) && ($token->keyword === 'PARTITION')) {
} elseif (($token->type === TokenType::Keyword) && ($token->keyword === 'PARTITION')) {
$partitionState = 2;
}

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

if (
$token->type === Token::TYPE_OPERATOR
$token->type === TokenType::Operator
&& $token->value === '('
&& $nextToken
&& $nextToken->keyword === 'PARTITION'
) {
$partitionState = 2;
--$list->idx; // Current idx is on "(". We need a step back for ArrayObj::parse incoming.
} else {
$ret->field .= $token->type === Token::TYPE_WHITESPACE ? ' ' : $token->token;
$ret->field .= $token->type === TokenType::Whitespace ? ' ' : $token->token;
}
} elseif ($partitionState === 2) {
$ret->partitions = ArrayObj::parse(
Expand Down Expand Up @@ -552,7 +553,7 @@ private static function checkIfColumnDefinitionKeyword($tokenValue): bool
*/
private static function checkIfTokenQuotedSymbol($token): bool
{
return $token->type === Token::TYPE_SYMBOL && $token->flags === Token::FLAG_SYMBOL_BACKTICK;
return $token->type === TokenType::Symbol && $token->flags === Token::FLAG_SYMBOL_BACKTICK;
}

public function __toString(): string

Check warning on line 559 in src/Components/AlterOperation.php

View check run for this annotation

Codecov / codecov/patch

src/Components/AlterOperation.php#L559

Added line #L559 was not covered by tests
Expand Down
7 changes: 4 additions & 3 deletions src/Components/Array2d.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use PhpMyAdmin\SqlParser\Parser;
use PhpMyAdmin\SqlParser\Token;
use PhpMyAdmin\SqlParser\TokensList;
use PhpMyAdmin\SqlParser\TokenType;
use PhpMyAdmin\SqlParser\Translator;
use RuntimeException;

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

// End of statement.
if ($token->type === Token::TYPE_DELIMITER) {
if ($token->type === TokenType::Delimiter) {
break;
}

// Skipping whitespaces and comments.
if (($token->type === Token::TYPE_WHITESPACE) || ($token->type === Token::TYPE_COMMENT)) {
if (($token->type === TokenType::Whitespace) || ($token->type === TokenType::Comment)) {
continue;
}

// No keyword is expected.
if (($token->type === Token::TYPE_KEYWORD) && ($token->flags & Token::FLAG_KEYWORD_RESERVED)) {
if (($token->type === TokenType::Keyword) && ($token->flags & Token::FLAG_KEYWORD_RESERVED)) {
break;
}

Expand Down
10 changes: 5 additions & 5 deletions src/Components/ArrayObj.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

use PhpMyAdmin\SqlParser\Component;
use PhpMyAdmin\SqlParser\Parser;
use PhpMyAdmin\SqlParser\Token;
use PhpMyAdmin\SqlParser\TokensList;
use PhpMyAdmin\SqlParser\TokenType;

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

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

// Skipping whitespaces and comments.
if (($token->type === Token::TYPE_WHITESPACE) || ($token->type === Token::TYPE_COMMENT)) {
if (($token->type === TokenType::Whitespace) || ($token->type === TokenType::Comment)) {
$lastRaw .= $token->token;
$lastValue = trim($lastValue) . ' ';
continue;
}

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

if ($token->type === Token::TYPE_OPERATOR) {
if ($token->type === TokenType::Operator) {
if ($token->value === '(') {
if (++$brackets === 1) { // 1 is the base level.
continue;
Expand Down
29 changes: 15 additions & 14 deletions src/Components/CaseExpression.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use PhpMyAdmin\SqlParser\Parser;
use PhpMyAdmin\SqlParser\Token;
use PhpMyAdmin\SqlParser\TokensList;
use PhpMyAdmin\SqlParser\TokenType;

use function count;

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

// Skipping whitespaces and comments.
if (($token->type === Token::TYPE_WHITESPACE) || ($token->type === Token::TYPE_COMMENT)) {
if (($token->type === TokenType::Whitespace) || ($token->type === TokenType::Comment)) {
continue;
}

if ($state === 0) {
if ($token->type === Token::TYPE_KEYWORD) {
if ($token->type === TokenType::Keyword) {
switch ($token->keyword) {
case 'WHEN':
++$list->idx; // Skip 'WHEN'
Expand Down Expand Up @@ -132,7 +133,7 @@ public static function parse(Parser $parser, TokensList $list, array $options =
}
} elseif ($state === 1) {
if ($type === 0) {
if ($token->type === Token::TYPE_KEYWORD) {
if ($token->type === TokenType::Keyword) {
switch ($token->keyword) {
case 'WHEN':
++$list->idx; // Skip 'WHEN'
Expand All @@ -154,23 +155,23 @@ public static function parse(Parser $parser, TokensList $list, array $options =
break 2;
}
}
} elseif ($token->type === Token::TYPE_KEYWORD && $token->keyword === 'THEN') {
} elseif ($token->type === TokenType::Keyword && $token->keyword === 'THEN') {
++$list->idx; // Skip 'THEN'
$newResult = Expression::parse($parser, $list);
$state = 0;
$ret->results[] = $newResult;
} elseif ($token->type === Token::TYPE_KEYWORD) {
} elseif ($token->type === TokenType::Keyword) {
$parser->error('Unexpected keyword.', $token);
break;
}
} elseif ($state === 2) {
if ($type === 0) {
if ($token->type === Token::TYPE_KEYWORD && $token->keyword === 'THEN') {
if ($token->type === TokenType::Keyword && $token->keyword === 'THEN') {
++$list->idx; // Skip 'THEN'
$newResult = Expression::parse($parser, $list);
$ret->results[] = $newResult;
$state = 1;
} elseif ($token->type === Token::TYPE_KEYWORD) {
} elseif ($token->type === TokenType::Keyword) {
$parser->error('Unexpected keyword.', $token);
break;
}
Expand All @@ -187,17 +188,17 @@ public static function parse(Parser $parser, TokensList $list, array $options =
$token = $list->tokens[$list->idx];

// End of statement.
if ($token->type === Token::TYPE_DELIMITER) {
if ($token->type === TokenType::Delimiter) {
break;
}

// Skipping whitespaces and comments.
if (($token->type === Token::TYPE_WHITESPACE) || ($token->type === Token::TYPE_COMMENT)) {
if (($token->type === TokenType::Whitespace) || ($token->type === TokenType::Comment)) {
continue;
}

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

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

if (
$asFound
|| $token->type === Token::TYPE_STRING
|| ($token->type === Token::TYPE_SYMBOL && ! $token->flags & Token::FLAG_SYMBOL_VARIABLE)
|| $token->type === Token::TYPE_NONE
|| $token->type === TokenType::String
|| ($token->type === TokenType::Symbol && ! $token->flags & Token::FLAG_SYMBOL_VARIABLE)
|| $token->type === TokenType::None
) {
// An alias is expected (the keyword `AS` was previously found).
if (! empty($ret->alias)) {
Expand Down
19 changes: 10 additions & 9 deletions src/Components/Condition.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use PhpMyAdmin\SqlParser\Parser;
use PhpMyAdmin\SqlParser\Token;
use PhpMyAdmin\SqlParser\TokensList;
use PhpMyAdmin\SqlParser\TokenType;

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

// End of statement.
if ($token->type === Token::TYPE_DELIMITER) {
if ($token->type === TokenType::Delimiter) {
break;
}

// Skipping whitespaces and comments.
if ($token->type === Token::TYPE_COMMENT) {
if ($token->type === TokenType::Comment) {
continue;
}

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

if (
($token->type === Token::TYPE_KEYWORD)
($token->type === TokenType::Keyword)
&& ($token->flags & Token::FLAG_KEYWORD_RESERVED)
&& ! ($token->flags & Token::FLAG_KEYWORD_FUNCTION)
) {
Expand All @@ -175,7 +176,7 @@ public static function parse(Parser $parser, TokensList $list, array $options =
}
}

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

$expr->expr .= $token->token;
if (
($token->type !== Token::TYPE_NONE)
&& (($token->type !== Token::TYPE_KEYWORD)
($token->type !== TokenType::None)
&& (($token->type !== TokenType::Keyword)
|| ($token->flags & Token::FLAG_KEYWORD_RESERVED))
&& ($token->type !== Token::TYPE_STRING)
&& ($token->type !== Token::TYPE_SYMBOL)
&& ($token->type !== TokenType::String)
&& ($token->type !== TokenType::Symbol)
) {
continue;
}
Expand Down
Loading

0 comments on commit e9edd4c

Please sign in to comment.