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

Remove nullability from IndexHint #616

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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: 1 addition & 1 deletion phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -745,7 +745,7 @@ parameters:
path: src/Parsers/GroupKeywords.php

-
message: '#^Property PhpMyAdmin\\SqlParser\\Components\\IndexHint\:\:\$type \(string\|null\) does not accept mixed\.$#'
message: '#^Property PhpMyAdmin\\SqlParser\\Components\\IndexHint\:\:\$type \(string\) does not accept mixed\.$#'
identifier: assign.propertyType
count: 1
path: src/Parsers/IndexHints.php
Expand Down
8 changes: 1 addition & 7 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="6.0.0@b8e96bb617bf59382113b1b56cef751f648a7dc9">
<files psalm-version="6.1.0@827971f8bc7a28bb4f842f34bf8901521de1cea3">
<file src="src/Components/AlterOperation.php">
<PossiblyNullReference>
<code><![CDATA[has]]></code>
Expand Down Expand Up @@ -45,12 +45,6 @@
<code><![CDATA[$type]]></code>
</PossiblyUnusedProperty>
</file>
<file src="src/Components/IndexHint.php">
<PossiblyNullOperand>
<code><![CDATA[$this->indexOrKey]]></code>
<code><![CDATA[$this->type]]></code>
</PossiblyNullOperand>
</file>
<file src="src/Components/IntoKeyword.php">
<PossiblyNullOperand>
<code><![CDATA[$this->dest]]></code>
Expand Down
38 changes: 6 additions & 32 deletions src/Components/IndexHint.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,43 +13,17 @@
final class IndexHint implements Component
{
/**
* The type of hint (USE/FORCE/IGNORE)
*/
public string|null $type;

/**
* What the hint is for (INDEX/KEY)
*/
public string|null $indexOrKey;

/**
* The clause for which this hint is (JOIN/ORDER BY/GROUP BY)
*/
public string|null $for;

/**
* List of indexes in this hint
*
* @var Expression[]
*/
public array $indexes = [];

/**
* @param string $type the type of hint (USE/FORCE/IGNORE)
* @param string $type The type of hint (USE/FORCE/IGNORE)
* @param string $indexOrKey What the hint is for (INDEX/KEY)
* @param string $for the clause for which this hint is (JOIN/ORDER BY/GROUP BY)
* @param string|null $for The clause for which this hint is (JOIN/ORDER BY/GROUP BY)
* @param Expression[] $indexes List of indexes in this hint
*/
public function __construct(
string|null $type = null,
string|null $indexOrKey = null,
string|null $for = null,
array $indexes = [],
public string $type = '',
public string $indexOrKey = '',
public string|null $for = null,
public array $indexes = [],
) {
$this->type = $type;
$this->indexOrKey = $indexOrKey;
$this->for = $for;
$this->indexes = $indexes;
}

public function build(): string
Expand Down
2 changes: 1 addition & 1 deletion src/Parsers/IndexHints.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
{
$ret = [];
$expr = new IndexHint();
$expr->type = $options['type'] ?? null;
$expr->type = $options['type'] ?? '';

Check warning on line 31 in src/Parsers/IndexHints.php

View workflow job for this annotation

GitHub Actions / Mutation tests with PHP 8.2

Escaped Mutant for Mutator "Coalesce": --- Original +++ New @@ @@ { $ret = []; $expr = new IndexHint(); - $expr->type = $options['type'] ?? ''; + $expr->type = '' ?? $options['type']; /** * The state of the parser. *
/**
* The state of the parser.
*
Expand All @@ -44,11 +44,11 @@

// By design, the parser will parse first token after the keyword. So, the keyword
// must be analyzed too, in order to determine the type of this index hint.
if ($list->idx > 0) {

Check warning on line 47 in src/Parsers/IndexHints.php

View workflow job for this annotation

GitHub Actions / Mutation tests with PHP 8.2

Escaped Mutant for Mutator "GreaterThan": --- Original +++ New @@ @@ $state = 0; // By design, the parser will parse first token after the keyword. So, the keyword // must be analyzed too, in order to determine the type of this index hint. - if ($list->idx > 0) { + if ($list->idx >= 0) { --$list->idx; } for (; $list->idx < $list->count; ++$list->idx) {
--$list->idx;
}

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

Check warning on line 51 in src/Parsers/IndexHints.php

View workflow job for this annotation

GitHub Actions / Mutation tests with PHP 8.2

Escaped Mutant for Mutator "LessThan": --- Original +++ New @@ @@ if ($list->idx > 0) { --$list->idx; } - 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 Down Expand Up @@ -92,7 +92,7 @@

break;
case 2:
if ($token->type === TokenType::Keyword && $token->keyword === 'FOR') {

Check warning on line 95 in src/Parsers/IndexHints.php

View workflow job for this annotation

GitHub Actions / Mutation tests with PHP 8.2

Escaped Mutant for Mutator "LogicalAnd": --- Original +++ New @@ @@ } break; case 2: - if ($token->type === TokenType::Keyword && $token->keyword === 'FOR') { + if ($token->type === TokenType::Keyword || $token->keyword === 'FOR') { $state = 3; } else { $expr->indexes = ExpressionArray::parse($parser, $list);
$state = 3;
} else {
$expr->indexes = ExpressionArray::parse($parser, $list);
Expand Down
2 changes: 1 addition & 1 deletion tests/data/parser/parseSelectIndexHintErr1.out
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@
{
"@type": "PhpMyAdmin\\SqlParser\\Components\\IndexHint",
"type": "FORCE",
"indexOrKey": null,
"indexOrKey": "",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess that's a bit less intuitive for developers

Copy link
Contributor Author

@kamil-tekiela kamil-tekiela Feb 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How so? Either way, it's an invalid value. The developer should check the parser errors. The only valid values for this are INDEX or KEY.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh okay, it's empty but the parser would have errors

"for": null,
"indexes": [
{
Expand Down
Loading