Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce the TokenType enum #507

Merged
merged 1 commit into from
Sep 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
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,18 +322,18 @@
$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.
$ret->unknown[] = $token;
Expand All @@ -346,8 +347,8 @@
// 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) {

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 @@ -386,29 +387,29 @@

$state = 2;
} elseif ($state === 2) {
if (is_string($token->value) || is_int($token->value)) {

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

View workflow job for this annotation

GitHub Actions / Mutation tests with PHP 8.1

Escaped Mutant for Mutator "LogicalOr": --- Original +++ New @@ @@ } $state = 2; } elseif ($state === 2) { - if (is_string($token->value) || is_int($token->value)) { + if (is_string($token->value) && is_int($token->value)) { $arrayKey = $token->value; } else { $arrayKey = $token->token;

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

View workflow job for this annotation

GitHub Actions / Mutation tests with PHP 8.1

Escaped Mutant for Mutator "LogicalOrAllSubExprNegation": --- Original +++ New @@ @@ } $state = 2; } elseif ($state === 2) { - if (is_string($token->value) || is_int($token->value)) { + if (!is_string($token->value) || !is_int($token->value)) { $arrayKey = $token->value; } else { $arrayKey = $token->token;

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

View workflow job for this annotation

GitHub Actions / Mutation tests with PHP 8.1

Escaped Mutant for Mutator "LogicalOrNegation": --- Original +++ New @@ @@ } $state = 2; } elseif ($state === 2) { - if (is_string($token->value) || is_int($token->value)) { + if (!(is_string($token->value) || is_int($token->value))) { $arrayKey = $token->value; } else { $arrayKey = $token->token;

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

View workflow job for this annotation

GitHub Actions / Mutation tests with PHP 8.1

Escaped Mutant for Mutator "LogicalOrSingleSubExprNegation": --- Original +++ New @@ @@ } $state = 2; } elseif ($state === 2) { - if (is_string($token->value) || is_int($token->value)) { + if (!is_string($token->value) || is_int($token->value)) { $arrayKey = $token->value; } else { $arrayKey = $token->token;

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

View workflow job for this annotation

GitHub Actions / Mutation tests with PHP 8.1

Escaped Mutant for Mutator "LogicalOrSingleSubExprNegation": --- Original +++ New @@ @@ } $state = 2; } elseif ($state === 2) { - if (is_string($token->value) || is_int($token->value)) { + if (is_string($token->value) || !is_int($token->value)) { $arrayKey = $token->value; } else { $arrayKey = $token->token;
$arrayKey = $token->value;
} else {
$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;
} else {
Expand Down Expand Up @@ -438,12 +439,12 @@
$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 @@
}

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,11 +553,11 @@
*/
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
{
return $this->build();

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

View check run for this annotation

Codecov / codecov/patch

src/Components/AlterOperation.php#L561

Added line #L561 was not covered by tests
}
}
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 @@
$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 Expand Up @@ -113,13 +114,13 @@
return $ret;
}

public function build(): string

Check warning on line 117 in src/Components/Array2d.php

View check run for this annotation

Codecov / codecov/patch

src/Components/Array2d.php#L117

Added line #L117 was not covered by tests
{
throw new RuntimeException(Translator::gettext('Not implemented yet.'));

Check warning on line 119 in src/Components/Array2d.php

View check run for this annotation

Codecov / codecov/patch

src/Components/Array2d.php#L119

Added line #L119 was not covered by tests
}

public function __toString(): string

Check warning on line 122 in src/Components/Array2d.php

View check run for this annotation

Codecov / codecov/patch

src/Components/Array2d.php#L122

Added line #L122 was not covered by tests
{
return $this->build();

Check warning on line 124 in src/Components/Array2d.php

View check run for this annotation

Codecov / codecov/patch

src/Components/Array2d.php#L124

Added line #L124 was not covered by tests
}
}
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 @@
$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 @@
}
} 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 @@
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 @@
$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 @@

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 @@

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 Expand Up @@ -285,8 +286,8 @@
return $ret;
}

public function __toString(): string

Check warning on line 289 in src/Components/CaseExpression.php

View check run for this annotation

Codecov / codecov/patch

src/Components/CaseExpression.php#L289

Added line #L289 was not covered by tests
{
return $this->build();

Check warning on line 291 in src/Components/CaseExpression.php

View check run for this annotation

Codecov / codecov/patch

src/Components/CaseExpression.php#L291

Added line #L291 was not covered by tests
}
}
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
Loading