Skip to content
Open
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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@
')' => 16,
'.' => 16,
',' => 16,
'->' => 16,
'->>' => 16,
';' => 16,
];

Expand Down Expand Up @@ -386,7 +388,7 @@
*/
public static function isComment(string $string, bool $end = false): int|null
{
return match (true) {

Check warning on line 391 in src/Context.php

View workflow job for this annotation

GitHub Actions / Mutation tests with PHP 8.2

Escaped Mutant for Mutator "MatchArmRemoval": @@ @@ { return match (true) { str_starts_with($string, '#') => Token::FLAG_COMMENT_BASH, - str_starts_with($string, '/*!') => Token::FLAG_COMMENT_MYSQL_CMD, // If comment is opening C style (/*) or is closing C style (*/), warning, it could conflict // with wildcard and a real opening C style. // It would look like the following valid SQL statement: "SELECT */* comment */ FROM...".
str_starts_with($string, '#') => Token::FLAG_COMMENT_BASH,
str_starts_with($string, '/*!') => Token::FLAG_COMMENT_MYSQL_CMD,
// If comment is opening C style (/*) or is closing C style (*/), warning, it could conflict
Expand Down Expand Up @@ -463,7 +465,7 @@
{
// NOTES: Only non-alphanumeric ASCII characters may be separators.
// `~` is the last printable ASCII character.
return $string <= '~'

Check warning on line 468 in src/Context.php

View workflow job for this annotation

GitHub Actions / Mutation tests with PHP 8.2

Escaped Mutant for Mutator "LessThanOrEqualTo": @@ @@ { // NOTES: Only non-alphanumeric ASCII characters may be separators. // `~` is the last printable ASCII character. - return $string <= '~' + return $string < '~' && $string !== '_' && $string !== '$' && ($string < '0' || $string > '9')
&& $string !== '_'
&& $string !== '$'
&& ($string < '0' || $string > '9')
Expand All @@ -490,7 +492,7 @@
if (! class_exists($contextClass)) {
$contextClass = self::CONTEXT_PREFIX . $context;
if (! class_exists($contextClass)) {
$contextClass = '\\' . $context;

Check warning on line 495 in src/Context.php

View workflow job for this annotation

GitHub Actions / Mutation tests with PHP 8.2

Escaped Mutant for Mutator "ConcatOperandRemoval": @@ @@ if (! class_exists($contextClass)) { $contextClass = self::CONTEXT_PREFIX . $context; if (! class_exists($contextClass)) { - $contextClass = '\\' . $context; + $contextClass = $context; if (! class_exists($contextClass)) { return false; }

Check warning on line 495 in src/Context.php

View workflow job for this annotation

GitHub Actions / Mutation tests with PHP 8.2

Escaped Mutant for Mutator "Concat": @@ @@ if (! class_exists($contextClass)) { $contextClass = self::CONTEXT_PREFIX . $context; if (! class_exists($contextClass)) { - $contextClass = '\\' . $context; + $contextClass = $context . '\\'; if (! class_exists($contextClass)) { return false; }
if (! class_exists($contextClass)) {
return false;
}
Expand Down Expand Up @@ -518,19 +520,19 @@
public static function loadClosest(string $context = ''): string|null
{
$length = strlen($context);
for ($i = $length; $i > 0;) {

Check warning on line 523 in src/Context.php

View workflow job for this annotation

GitHub Actions / Mutation tests with PHP 8.2

Escaped Mutant for Mutator "GreaterThan": @@ @@ public static function loadClosest(string $context = ''): string|null { $length = strlen($context); - for ($i = $length; $i > 0;) { + for ($i = $length; $i >= 0;) { /* Trying to load the new context */ if (static::load($context)) { return $context;
/* Trying to load the new context */
if (static::load($context)) {
return $context;
}

/* Replace last two non zero digits by zeroes */
do {

Check warning on line 530 in src/Context.php

View workflow job for this annotation

GitHub Actions / Mutation tests with PHP 8.2

Escaped Mutant for Mutator "DoWhile": @@ @@ if (! is_numeric($part)) { break 2; } - } while (intval($part) === 0 && $i > 0); + } while (false); $context = substr($context, 0, $i) . '00' . substr($context, $i + 2); }
$i -= 2;

Check warning on line 531 in src/Context.php

View workflow job for this annotation

GitHub Actions / Mutation tests with PHP 8.2

Escaped Mutant for Mutator "DecrementInteger": @@ @@ /* Replace last two non zero digits by zeroes */ do { - $i -= 2; + $i -= 1; $part = substr($context, $i, 2); /* No more numeric parts to strip */ if (! is_numeric($part)) {
$part = substr($context, $i, 2);

Check warning on line 532 in src/Context.php

View workflow job for this annotation

GitHub Actions / Mutation tests with PHP 8.2

Escaped Mutant for Mutator "IncrementInteger": @@ @@ /* Replace last two non zero digits by zeroes */ do { $i -= 2; - $part = substr($context, $i, 2); + $part = substr($context, $i, 3); /* No more numeric parts to strip */ if (! is_numeric($part)) { break 2;

Check warning on line 532 in src/Context.php

View workflow job for this annotation

GitHub Actions / Mutation tests with PHP 8.2

Escaped Mutant for Mutator "DecrementInteger": @@ @@ /* Replace last two non zero digits by zeroes */ do { $i -= 2; - $part = substr($context, $i, 2); + $part = substr($context, $i, 1); /* No more numeric parts to strip */ if (! is_numeric($part)) { break 2;
/* No more numeric parts to strip */
if (! is_numeric($part)) {
break 2;

Check warning on line 535 in src/Context.php

View workflow job for this annotation

GitHub Actions / Mutation tests with PHP 8.2

Escaped Mutant for Mutator "Break_": @@ @@ $part = substr($context, $i, 2); /* No more numeric parts to strip */ if (! is_numeric($part)) { - break 2; + continue; } } while (intval($part) === 0 && $i > 0);
}
} while (intval($part) === 0 && $i > 0);

Expand Down
11 changes: 7 additions & 4 deletions src/Utils/Formatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -518,12 +518,15 @@
} elseif (
$prev->keyword === 'DELIMITER'
|| ! (
($prev->type === TokenType::Operator && ($prev->value === '.' || $prev->value === '('))
// No space after . (
($prev->type === TokenType::Operator
&& ($prev->value === '.' || $prev->value === '('
|| $prev->value === '->' || $prev->value === '->>'))
// No space after punctuation and JSON operators.
|| ($curr->type === TokenType::Operator
&& ($curr->value === '.' || $curr->value === ','
|| $curr->value === '(' || $curr->value === ')'))
// No space before . , ( )
|| $curr->value === '(' || $curr->value === ')'
|| $curr->value === '->' || $curr->value === '->>'))
// No space before punctuation and JSON operators.
|| $curr->type === TokenType::Delimiter && mb_strlen((string) $curr->value, 'UTF-8') < 2
)
) {
Expand Down Expand Up @@ -746,7 +749,7 @@
public static function isClause(Token $token): int|false
{
if (
($token->type === TokenType::Keyword && isset(Parser::STATEMENT_PARSERS[$token->keyword]))

Check failure on line 752 in src/Utils/Formatter.php

View workflow job for this annotation

GitHub Actions / analyse-php (8.2)

PossiblyNullArrayOffset

src/Utils/Formatter.php:752:59: PossiblyNullArrayOffset: Cannot access value on variable PhpMyAdmin\SqlParser\Parser::STATEMENT_PARSERS using possibly null offset null|string (see https://psalm.dev/125)
|| ($token->type === TokenType::None && strtoupper($token->token) === 'DELIMITER')
) {
return 2;
Expand All @@ -754,7 +757,7 @@

if (
$token->type === TokenType::Keyword && (
in_array($token->keyword, self::FORMATTERS, true) || isset(Parser::KEYWORD_PARSERS[$token->keyword])

Check failure on line 760 in src/Utils/Formatter.php

View workflow job for this annotation

GitHub Actions / analyse-php (8.2)

PossiblyNullArrayOffset

src/Utils/Formatter.php:760:72: PossiblyNullArrayOffset: Cannot access value on variable PhpMyAdmin\SqlParser\Parser::KEYWORD_PARSERS using possibly null offset null|string (see https://psalm.dev/125)
)
) {
return 1;
Expand Down
15 changes: 15 additions & 0 deletions tests/Utils/FormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,21 @@ public static function formatQueriesProviders(): array
'<span class="sql-reserved">WHERE</span><br/>' .
'&nbsp;&nbsp;&nbsp;&nbsp;<span class="sql-number">1</span>',
],
'json operator' => [
'query' => 'SELECT details->\'$."first_name"\' FROM users',
'text' => 'SELECT' . "\n" .
' details->\'$."first_name"\'' . "\n" .
'FROM' . "\n" .
' users',
'cli' => "\x1b[35mSELECT\n" .
" \x1b[39mdetails->\x1b[91m'$.\"first_name\"'\n" .
"\x1b[35mFROM\n" .
" \x1b[39musers\x1b[0m",
'html' => '<span class="sql-reserved">SELECT</span><br/>' .
'&nbsp;&nbsp;&nbsp;&nbsp;details-&gt;<span class="sql-string">\'$."first_name"\'</span><br/>' .
'<span class="sql-reserved">FROM</span><br/>' .
'&nbsp;&nbsp;&nbsp;&nbsp;users',
],
'typical' => [
'query' => 'SELECT id, if(id=1,"Si","No") from `tbl` where id = 0 or ' .
'id = 1 group by id order by id desc limit 1 offset 0',
Expand Down
Loading