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

OrderSortKeyword enum #596

Merged
merged 2 commits into from
Nov 10, 2024
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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions src/Components/GroupKeyword.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
*/
final class GroupKeyword implements Component
{
/** @var 'ASC'|'DESC'|null */
public string|null $type = null;
public OrderSortKeyword|null $type = null;

/**
* The expression that is used for grouping.
Expand Down
10 changes: 5 additions & 5 deletions src/Components/OrderKeyword.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,21 @@ final class OrderKeyword implements Component
/**
* The order type.
*/
public string $type;
public OrderSortKeyword $type;

/**
* @param Expression|null $expr the expression that we are sorting by
* @param string $type the sorting type
* @param Expression|null $expr the expression that we are sorting by
* @param OrderSortKeyword $type the sorting type
*/
public function __construct(Expression|null $expr = null, string $type = 'ASC')
public function __construct(Expression|null $expr = null, OrderSortKeyword $type = OrderSortKeyword::Asc)
{
$this->expr = $expr;
$this->type = $type;
}

public function build(): string
{
return $this->expr . ' ' . $this->type;
return $this->expr . ' ' . $this->type->value;
}

public function __toString(): string
Expand Down
11 changes: 11 additions & 0 deletions src/Components/OrderSortKeyword.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace PhpMyAdmin\SqlParser\Components;

enum OrderSortKeyword: string
{
case Asc = 'ASC';
case Desc = 'DESC';
}
3 changes: 2 additions & 1 deletion src/Parsers/GroupKeywords.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace PhpMyAdmin\SqlParser\Parsers;

use PhpMyAdmin\SqlParser\Components\GroupKeyword;
use PhpMyAdmin\SqlParser\Components\OrderSortKeyword;
use PhpMyAdmin\SqlParser\Parseable;
use PhpMyAdmin\SqlParser\Parser;
use PhpMyAdmin\SqlParser\TokensList;
Expand Down Expand Up @@ -42,7 +43,7 @@
*/
$state = 0;

for (; $list->idx < $list->count; ++$list->idx) {

Check warning on line 46 in src/Parsers/GroupKeywords.php

View workflow job for this annotation

GitHub Actions / Mutation tests with PHP 8.2

Escaped Mutant for Mutator "LessThan": --- Original +++ New @@ @@ * 1 -------------------[ ASC / DESC ]--------------------> 1 */ $state = 0; - for (; $list->idx < $list->count; ++$list->idx) { + for (; $list->idx <= $list->count; ++$list->idx) { /** * Token parsed at this moment. */
/**
* Token parsed at this moment.
*/
Expand All @@ -54,20 +55,20 @@
}

// Skipping whitespaces and comments.
if (($token->type === TokenType::Whitespace) || ($token->type === TokenType::Comment)) {

Check warning on line 58 in src/Parsers/GroupKeywords.php

View workflow job for this annotation

GitHub Actions / Mutation tests with PHP 8.2

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

if ($state === 0) {
$expr->expr = Expressions::parse($parser, $list);
$state = 1;

Check warning on line 64 in src/Parsers/GroupKeywords.php

View workflow job for this annotation

GitHub Actions / Mutation tests with PHP 8.2

Escaped Mutant for Mutator "IncrementInteger": --- Original +++ New @@ @@ } if ($state === 0) { $expr->expr = Expressions::parse($parser, $list); - $state = 1; + $state = 2; } else { if ($token->type === TokenType::Keyword && ($token->keyword === 'ASC' || $token->keyword === 'DESC')) { $expr->type = OrderSortKeyword::from($token->keyword);
} else {
if (
($token->type === TokenType::Keyword)
&& (($token->keyword === 'ASC') || ($token->keyword === 'DESC'))
) {
$expr->type = $token->keyword;
$expr->type = OrderSortKeyword::from($token->keyword);
} elseif (($token->type === TokenType::Operator) && ($token->value === ',')) {

Check warning on line 71 in src/Parsers/GroupKeywords.php

View workflow job for this annotation

GitHub Actions / Mutation tests with PHP 8.2

Escaped Mutant for Mutator "LogicalAnd": --- Original +++ New @@ @@ } else { if ($token->type === TokenType::Keyword && ($token->keyword === 'ASC' || $token->keyword === 'DESC')) { $expr->type = OrderSortKeyword::from($token->keyword); - } elseif ($token->type === TokenType::Operator && $token->value === ',') { + } elseif ($token->type === TokenType::Operator || $token->value === ',') { if (!empty($expr->expr)) { $ret[] = $expr; }
if (! empty($expr->expr)) {
$ret[] = $expr;
}
Expand Down
3 changes: 2 additions & 1 deletion src/Parsers/OrderKeywords.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace PhpMyAdmin\SqlParser\Parsers;

use PhpMyAdmin\SqlParser\Components\OrderKeyword;
use PhpMyAdmin\SqlParser\Components\OrderSortKeyword;
use PhpMyAdmin\SqlParser\Parseable;
use PhpMyAdmin\SqlParser\Parser;
use PhpMyAdmin\SqlParser\TokensList;
Expand Down Expand Up @@ -42,7 +43,7 @@
*/
$state = 0;

for (; $list->idx < $list->count; ++$list->idx) {

Check warning on line 46 in src/Parsers/OrderKeywords.php

View workflow job for this annotation

GitHub Actions / Mutation tests with PHP 8.2

Escaped Mutant for Mutator "LessThan": --- Original +++ New @@ @@ * 1 -------------------[ ASC / DESC ]--------------------> 1 */ $state = 0; - for (; $list->idx < $list->count; ++$list->idx) { + for (; $list->idx <= $list->count; ++$list->idx) { /** * Token parsed at this moment. */
/**
* Token parsed at this moment.
*/
Expand All @@ -54,20 +55,20 @@
}

// Skipping whitespaces and comments.
if (($token->type === TokenType::Whitespace) || ($token->type === TokenType::Comment)) {

Check warning on line 58 in src/Parsers/OrderKeywords.php

View workflow job for this annotation

GitHub Actions / Mutation tests with PHP 8.2

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

if ($state === 0) {
$expr->expr = Expressions::parse($parser, $list);
$state = 1;

Check warning on line 64 in src/Parsers/OrderKeywords.php

View workflow job for this annotation

GitHub Actions / Mutation tests with PHP 8.2

Escaped Mutant for Mutator "IncrementInteger": --- Original +++ New @@ @@ } if ($state === 0) { $expr->expr = Expressions::parse($parser, $list); - $state = 1; + $state = 2; } else { if ($token->type === TokenType::Keyword && ($token->keyword === 'ASC' || $token->keyword === 'DESC')) { $expr->type = OrderSortKeyword::from($token->keyword);
} else {
if (
($token->type === TokenType::Keyword)
&& (($token->keyword === 'ASC') || ($token->keyword === 'DESC'))
) {
$expr->type = $token->keyword;
$expr->type = OrderSortKeyword::from($token->keyword);
} elseif (($token->type === TokenType::Operator) && ($token->value === ',')) {

Check warning on line 71 in src/Parsers/OrderKeywords.php

View workflow job for this annotation

GitHub Actions / Mutation tests with PHP 8.2

Escaped Mutant for Mutator "LogicalAnd": --- Original +++ New @@ @@ } else { if ($token->type === TokenType::Keyword && ($token->keyword === 'ASC' || $token->keyword === 'DESC')) { $expr->type = OrderSortKeyword::from($token->keyword); - } elseif ($token->type === TokenType::Operator && $token->value === ',') { + } elseif ($token->type === TokenType::Operator || $token->value === ',') { if (!empty($expr->expr)) { $ret[] = $expr; }
if (! empty($expr->expr)) {
$ret[] = $expr;
}
Expand Down
5 changes: 3 additions & 2 deletions tests/Components/OrderKeywordTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use PhpMyAdmin\SqlParser\Components\Expression;
use PhpMyAdmin\SqlParser\Components\OrderKeyword;
use PhpMyAdmin\SqlParser\Components\OrderSortKeyword;
use PhpMyAdmin\SqlParser\Parsers\OrderKeywords;
use PhpMyAdmin\SqlParser\Tests\TestCase;

Expand All @@ -17,8 +18,8 @@ public function testBuildAll(): void
'a ASC, b DESC',
OrderKeywords::buildAll(
[
new OrderKeyword(new Expression('a'), 'ASC'),
new OrderKeyword(new Expression('b'), 'DESC'),
new OrderKeyword(new Expression('a'), OrderSortKeyword::Asc),
new OrderKeyword(new Expression('b'), OrderSortKeyword::Desc),
],
),
);
Expand Down
6 changes: 5 additions & 1 deletion tests/data/parser/parseDelete.out
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,11 @@
"function": null,
"subquery": null
},
"type": "ASC"
"type": {
"@type": "PhpMyAdmin\\SqlParser\\Components\\OrderSortKeyword",
"name": "Asc",
"value": "ASC"
}
}
],
"limit": null,
Expand Down
6 changes: 5 additions & 1 deletion tests/data/parser/parseDelete4.out
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,11 @@
"function": null,
"subquery": null
},
"type": "ASC"
"type": {
"@type": "PhpMyAdmin\\SqlParser\\Components\\OrderSortKeyword",
"name": "Asc",
"value": "ASC"
}
}
],
"limit": null,
Expand Down
6 changes: 5 additions & 1 deletion tests/data/parser/parseDelete5.out
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,11 @@
"function": null,
"subquery": null
},
"type": "ASC"
"type": {
"@type": "PhpMyAdmin\\SqlParser\\Components\\OrderSortKeyword",
"name": "Asc",
"value": "ASC"
}
}
],
"limit": {
Expand Down
6 changes: 5 additions & 1 deletion tests/data/parser/parseDelete6.out
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,11 @@
"function": null,
"subquery": null
},
"type": "ASC"
"type": {
"@type": "PhpMyAdmin\\SqlParser\\Components\\OrderSortKeyword",
"name": "Asc",
"value": "ASC"
}
}
],
"limit": null,
Expand Down
6 changes: 5 additions & 1 deletion tests/data/parser/parseDelete7.out
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,11 @@
"function": null,
"subquery": null
},
"type": "ASC"
"type": {
"@type": "PhpMyAdmin\\SqlParser\\Components\\OrderSortKeyword",
"name": "Asc",
"value": "ASC"
}
}
],
"limit": {
Expand Down
6 changes: 5 additions & 1 deletion tests/data/parser/parseDeleteErr5.out
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,11 @@
"function": null,
"subquery": null
},
"type": "ASC"
"type": {
"@type": "PhpMyAdmin\\SqlParser\\Components\\OrderSortKeyword",
"name": "Asc",
"value": "ASC"
}
}
],
"limit": null,
Expand Down
6 changes: 5 additions & 1 deletion tests/data/parser/parseDeleteErr7.out
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,11 @@
"function": null,
"subquery": null
},
"type": "ASC"
"type": {
"@type": "PhpMyAdmin\\SqlParser\\Components\\OrderSortKeyword",
"name": "Asc",
"value": "ASC"
}
}
],
"limit": null,
Expand Down
12 changes: 10 additions & 2 deletions tests/data/parser/parseSelect.out
Original file line number Diff line number Diff line change
Expand Up @@ -1037,7 +1037,11 @@
"function": null,
"subquery": null
},
"type": "DESC"
"type": {
"@type": "PhpMyAdmin\\SqlParser\\Components\\OrderSortKeyword",
"name": "Desc",
"value": "DESC"
}
},
{
"@type": "PhpMyAdmin\\SqlParser\\Components\\OrderKeyword",
Expand All @@ -1051,7 +1055,11 @@
"function": null,
"subquery": null
},
"type": "ASC"
"type": {
"@type": "PhpMyAdmin\\SqlParser\\Components\\OrderSortKeyword",
"name": "Asc",
"value": "ASC"
}
}
],
"limit": {
Expand Down
6 changes: 5 additions & 1 deletion tests/data/parser/parseSelect13.out
Original file line number Diff line number Diff line change
Expand Up @@ -11992,7 +11992,11 @@
"function": null,
"subquery": null
},
"type": "ASC"
"type": {
"@type": "PhpMyAdmin\\SqlParser\\Components\\OrderSortKeyword",
"name": "Asc",
"value": "ASC"
}
}
],
"limit": null,
Expand Down
6 changes: 5 additions & 1 deletion tests/data/parser/parseSelect9.out
Original file line number Diff line number Diff line change
Expand Up @@ -949,7 +949,11 @@
"function": null,
"subquery": null
},
"type": "ASC"
"type": {
"@type": "PhpMyAdmin\\SqlParser\\Components\\OrderSortKeyword",
"name": "Asc",
"value": "ASC"
}
}
],
"limit": null,
Expand Down
12 changes: 10 additions & 2 deletions tests/data/parser/parseSelectErr1.out
Original file line number Diff line number Diff line change
Expand Up @@ -983,7 +983,11 @@
"function": null,
"subquery": null
},
"type": "DESC"
"type": {
"@type": "PhpMyAdmin\\SqlParser\\Components\\OrderSortKeyword",
"name": "Desc",
"value": "DESC"
}
},
{
"@type": "PhpMyAdmin\\SqlParser\\Components\\OrderKeyword",
Expand All @@ -997,7 +1001,11 @@
"function": null,
"subquery": null
},
"type": "ASC"
"type": {
"@type": "PhpMyAdmin\\SqlParser\\Components\\OrderSortKeyword",
"name": "Asc",
"value": "ASC"
}
}
],
"limit": {
Expand Down
6 changes: 5 additions & 1 deletion tests/data/parser/parseSelectGroupBy.out
Original file line number Diff line number Diff line change
Expand Up @@ -1636,7 +1636,11 @@
"function": null,
"subquery": null
},
"type": "ASC"
"type": {
"@type": "PhpMyAdmin\\SqlParser\\Components\\OrderSortKeyword",
"name": "Asc",
"value": "ASC"
}
}
],
"limit": {
Expand Down
6 changes: 5 additions & 1 deletion tests/data/parser/parseSelectOrderByComment.out
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,11 @@
"function": null,
"subquery": null
},
"type": "ASC"
"type": {
"@type": "PhpMyAdmin\\SqlParser\\Components\\OrderSortKeyword",
"name": "Asc",
"value": "ASC"
}
}
],
"limit": null,
Expand Down
6 changes: 5 additions & 1 deletion tests/data/parser/parseSelectOrderByIsNull.out
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,11 @@
"function": null,
"subquery": null
},
"type": "ASC"
"type": {
"@type": "PhpMyAdmin\\SqlParser\\Components\\OrderSortKeyword",
"name": "Asc",
"value": "ASC"
}
}
],
"limit": null,
Expand Down
12 changes: 10 additions & 2 deletions tests/data/parser/parseSelectUnion2.out
Original file line number Diff line number Diff line change
Expand Up @@ -1289,7 +1289,11 @@
"function": null,
"subquery": null
},
"type": "ASC"
"type": {
"@type": "PhpMyAdmin\\SqlParser\\Components\\OrderSortKeyword",
"name": "Asc",
"value": "ASC"
}
},
{
"@type": "PhpMyAdmin\\SqlParser\\Components\\OrderKeyword",
Expand All @@ -1303,7 +1307,11 @@
"function": null,
"subquery": null
},
"type": "ASC"
"type": {
"@type": "PhpMyAdmin\\SqlParser\\Components\\OrderSortKeyword",
"name": "Asc",
"value": "ASC"
}
}
],
"limit": null,
Expand Down
Loading