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

Add native property types in Statements #532

Merged
merged 2 commits into from
Jan 16, 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
9 changes: 2 additions & 7 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -556,12 +556,12 @@ parameters:
path: src/Statements/ExplainStatement.php

-
message: "#^Property PhpMyAdmin\\\\SqlParser\\\\Statements\\\\ExplainStatement\\:\\:\\$connectionId \\(int\\|null\\) does not accept mixed\\.$#"
message: "#^Cannot cast mixed to string\\.$#"
count: 1
path: src/Statements/ExplainStatement.php

-
message: "#^Property PhpMyAdmin\\\\SqlParser\\\\Statements\\\\ExplainStatement\\:\\:\\$explainedColumn \\(string\\|null\\) does not accept mixed\\.$#"
message: "#^Property PhpMyAdmin\\\\SqlParser\\\\Statements\\\\ExplainStatement\\:\\:\\$connectionId \\(int\\|null\\) does not accept mixed\\.$#"
count: 1
path: src/Statements/ExplainStatement.php

Expand Down Expand Up @@ -605,11 +605,6 @@ parameters:
count: 1
path: src/Statements/MaintenanceStatement.php

-
message: "#^Property PhpMyAdmin\\\\SqlParser\\\\Statements\\\\PurgeStatement\\:\\:\\$endExpr \\(string\\|null\\) does not accept PhpMyAdmin\\\\SqlParser\\\\Components\\\\Expression\\|null\\.$#"
count: 1
path: src/Statements/PurgeStatement.php

-
message: "#^Property PhpMyAdmin\\\\SqlParser\\\\Statements\\\\PurgeStatement\\:\\:\\$endOption \\(string\\|null\\) does not accept mixed\\.$#"
count: 1
Expand Down
7 changes: 0 additions & 7 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -784,9 +784,6 @@
)]]></code>
<code>ArrayObj::parse($parser, $list)</code>
</PropertyTypeCoercion>
<RedundantConditionGivenDocblockType>
<code><![CDATA[$this->fields instanceof ArrayObj]]></code>
</RedundantConditionGivenDocblockType>
</file>
<file src="src/Statements/DeleteStatement.php">
<MixedArgument>
Expand Down Expand Up @@ -825,7 +822,6 @@
<file src="src/Statements/ExplainStatement.php">
<MixedAssignment>
<code><![CDATA[$this->connectionId]]></code>
<code><![CDATA[$this->explainedColumn]]></code>
<code><![CDATA[$this->explainedDatabase]]></code>
<code><![CDATA[$this->explainedTable]]></code>
</MixedAssignment>
Expand Down Expand Up @@ -880,9 +876,6 @@
</PossiblyUnusedProperty>
</file>
<file src="src/Statements/PurgeStatement.php">
<ImplicitToStringCast>
<code>Expression::parse($parser, $list, [])</code>
</ImplicitToStringCast>
<MixedAssignment>
<code><![CDATA[$this->endOption]]></code>
<code><![CDATA[$this->logType]]></code>
Expand Down
16 changes: 5 additions & 11 deletions src/Statement.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
* @var array<string, int|array<int, int|string>>
* @psalm-var array<string, (positive-int|array{positive-int, ('var'|'var='|'expr'|'expr=')})>
*/
public static $statementOptions = [];
public static array $statementOptions = [];

/**
* The clauses of this statement, in order.
Expand All @@ -60,30 +60,24 @@
* @var array<string, array<int, int|string>>
* @psalm-var array<string, array{non-empty-string, (1|2|3)}>
*/
public static $clauses = [];
public static array $clauses = [];

/**
* The options of this query.
*
* @see Statement::$statementOptions
*
* @var OptionsArray|null
*/
public $options;
public OptionsArray|null $options = null;

/**
* The index of the first token used in this statement.
*
* @var int|null
*/
public $first;
public int|null $first = null;

/**
* The index of the last token used in this statement.
*
* @var int|null
*/
public $last;
public int|null $last = null;

/**
* @param Parser|null $parser the instance that requests parsing
Expand All @@ -91,7 +85,7 @@
*/
public function __construct(Parser|null $parser = null, TokensList|null $list = null)
{
if (($parser === null) || ($list === null)) {

Check warning on line 88 in src/Statement.php

View workflow job for this annotation

GitHub Actions / Mutation tests with PHP 8.1

Escaped Mutant for Mutator "LogicalOr": --- Original +++ New @@ @@ */ public function __construct(Parser|null $parser = null, TokensList|null $list = null) { - if ($parser === null || $list === null) { + if ($parser === null && $list === null) { return; } $this->parse($parser, $list);
return;
}

Expand Down Expand Up @@ -175,7 +169,7 @@

// Checking if the name of the clause should be added.
if ($type & 2) {
$query = trim($query) . ' ' . $name;

Check warning on line 172 in src/Statement.php

View workflow job for this annotation

GitHub Actions / Mutation tests with PHP 8.1

Escaped Mutant for Mutator "UnwrapTrim": --- Original +++ New @@ @@ } // Checking if the name of the clause should be added. if ($type & 2) { - $query = trim($query) . ' ' . $name; + $query = $query . ' ' . $name; } // Checking if the result of the builder should be added. if (!($type & 1)) {
}

// Checking if the result of the builder should be added.
Expand All @@ -186,7 +180,7 @@
if (is_array($this->$field)) {
$query = trim($query) . ' ' . $class::buildAll($this->$field);
} else {
$query = trim($query) . ' ' . $this->$field->build();

Check warning on line 183 in src/Statement.php

View workflow job for this annotation

GitHub Actions / Mutation tests with PHP 8.1

Escaped Mutant for Mutator "UnwrapTrim": --- Original +++ New @@ @@ if (is_array($this->{$field})) { $query = trim($query) . ' ' . $class::buildAll($this->{$field}); } else { - $query = trim($query) . ' ' . $this->{$field}->build(); + $query = $query . ' ' . $this->{$field}->build(); } } return $query;
}
}

Expand All @@ -201,7 +195,7 @@
*
* @throws Exceptions\ParserException
*/
public function parse(Parser $parser, TokensList $list): void

Check warning on line 198 in src/Statement.php

View workflow job for this annotation

GitHub Actions / Mutation tests with PHP 8.1

Escaped Mutant for Mutator "PublicVisibility": --- Original +++ New @@ @@ * * @throws Exceptions\ParserException */ - public function parse(Parser $parser, TokensList $list) : void + protected function parse(Parser $parser, TokensList $list) : void { /** * Array containing all list of clauses parsed.
{
/**
* Array containing all list of clauses parsed.
Expand Down Expand Up @@ -264,7 +258,7 @@
// ON DUPLICATE KEY UPDATE ...
// has to be parsed in parent statement (INSERT or REPLACE)
// so look for it and break
if ($this instanceof Statements\SelectStatement && $token->value === 'ON') {

Check warning on line 261 in src/Statement.php

View workflow job for this annotation

GitHub Actions / Mutation tests with PHP 8.1

Escaped Mutant for Mutator "InstanceOf_": --- Original +++ New @@ @@ // ON DUPLICATE KEY UPDATE ... // has to be parsed in parent statement (INSERT or REPLACE) // so look for it and break - if ($this instanceof Statements\SelectStatement && $token->value === 'ON') { + if (true && $token->value === 'ON') { ++$list->idx; // Skip ON // look for ON DUPLICATE KEY UPDATE

Check warning on line 261 in src/Statement.php

View workflow job for this annotation

GitHub Actions / Mutation tests with PHP 8.1

Escaped Mutant for Mutator "LogicalAnd": --- Original +++ New @@ @@ // ON DUPLICATE KEY UPDATE ... // has to be parsed in parent statement (INSERT or REPLACE) // so look for it and break - if ($this instanceof Statements\SelectStatement && $token->value === 'ON') { + if ($this instanceof Statements\SelectStatement || $token->value === 'ON') { ++$list->idx; // Skip ON // look for ON DUPLICATE KEY UPDATE
++$list->idx; // Skip ON

// look for ON DUPLICATE KEY UPDATE
Expand All @@ -273,7 +267,7 @@
$third = $list->getNextOfType(TokenType::Keyword);

if (
$first && $second && $third

Check warning on line 270 in src/Statement.php

View workflow job for this annotation

GitHub Actions / Mutation tests with PHP 8.1

Escaped Mutant for Mutator "LogicalAnd": --- Original +++ New @@ @@ $first = $list->getNextOfType(TokenType::Keyword); $second = $list->getNextOfType(TokenType::Keyword); $third = $list->getNextOfType(TokenType::Keyword); - if ($first && $second && $third && $first->value === 'DUPLICATE' && $second->value === 'KEY' && $third->value === 'UPDATE') { + if (($first || $second) && $third && $first->value === 'DUPLICATE' && $second->value === 'KEY' && $third->value === 'UPDATE') { $list->idx = $lastIdx; break; }

Check warning on line 270 in src/Statement.php

View workflow job for this annotation

GitHub Actions / Mutation tests with PHP 8.1

Escaped Mutant for Mutator "LogicalAnd": --- Original +++ New @@ @@ $first = $list->getNextOfType(TokenType::Keyword); $second = $list->getNextOfType(TokenType::Keyword); $third = $list->getNextOfType(TokenType::Keyword); - if ($first && $second && $third && $first->value === 'DUPLICATE' && $second->value === 'KEY' && $third->value === 'UPDATE') { + if (($first && $second || $third) && $first->value === 'DUPLICATE' && $second->value === 'KEY' && $third->value === 'UPDATE') { $list->idx = $lastIdx; break; }

Check warning on line 270 in src/Statement.php

View workflow job for this annotation

GitHub Actions / Mutation tests with PHP 8.1

Escaped Mutant for Mutator "LogicalAnd": --- Original +++ New @@ @@ $first = $list->getNextOfType(TokenType::Keyword); $second = $list->getNextOfType(TokenType::Keyword); $third = $list->getNextOfType(TokenType::Keyword); - if ($first && $second && $third && $first->value === 'DUPLICATE' && $second->value === 'KEY' && $third->value === 'UPDATE') { + if (($first && $second && $third || $first->value === 'DUPLICATE') && $second->value === 'KEY' && $third->value === 'UPDATE') { $list->idx = $lastIdx; break; }

Check warning on line 270 in src/Statement.php

View workflow job for this annotation

GitHub Actions / Mutation tests with PHP 8.1

Escaped Mutant for Mutator "LogicalAnd": --- Original +++ New @@ @@ $first = $list->getNextOfType(TokenType::Keyword); $second = $list->getNextOfType(TokenType::Keyword); $third = $list->getNextOfType(TokenType::Keyword); - if ($first && $second && $third && $first->value === 'DUPLICATE' && $second->value === 'KEY' && $third->value === 'UPDATE') { + if (($first && $second && $third && $first->value === 'DUPLICATE' || $second->value === 'KEY') && $third->value === 'UPDATE') { $list->idx = $lastIdx; break; }
&& $first->value === 'DUPLICATE'
&& $second->value === 'KEY'
&& $third->value === 'UPDATE'
Expand Down
8 changes: 3 additions & 5 deletions src/Statements/AlterStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,23 @@ class AlterStatement extends Statement
{
/**
* Table affected.
*
* @var Expression|null
*/
public $table;
public Expression|null $table = null;

/**
* Column affected by this statement.
*
* @var AlterOperation[]|null
*/
public $altered = [];
public array|null $altered = [];

/**
* Options of this statement.
*
* @var array<string, int|array<int, int|string>>
* @psalm-var array<string, (positive-int|array{positive-int, ('var'|'var='|'expr'|'expr=')})>
*/
public static $statementOptions = [
public static array $statementOptions = [
'ONLINE' => 1,
'OFFLINE' => 1,
'IGNORE' => 2,
Expand Down
4 changes: 2 additions & 2 deletions src/Statements/AnalyzeStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class AnalyzeStatement extends Statement
* @var array<string, int|array<int, int|string>>
* @psalm-var array<string, (positive-int|array{positive-int, ('var'|'var='|'expr'|'expr=')})>
*/
public static $statementOptions = [
public static array $statementOptions = [
'TABLE' => 1,

'NO_WRITE_TO_BINLOG' => 2,
Expand All @@ -33,5 +33,5 @@ class AnalyzeStatement extends Statement
*
* @var Expression[]|null
*/
public $tables;
public array|null $tables = null;
}
2 changes: 1 addition & 1 deletion src/Statements/BackupStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class BackupStatement extends MaintenanceStatement
* @var array<string, int|array<int, int|string>>
* @psalm-var array<string, (positive-int|array{positive-int, ('var'|'var='|'expr'|'expr=')})>
*/
public static $statementOptions = [
public static array $statementOptions = [
'TABLE' => 1,

'NO_WRITE_TO_BINLOG' => 2,
Expand Down
4 changes: 1 addition & 3 deletions src/Statements/CallStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,8 @@ class CallStatement extends Statement
{
/**
* The name of the function and its parameters.
*
* @var FunctionCall|null
*/
public $call;
public FunctionCall|null $call = null;

/**
* Build statement for CALL.
Expand Down
2 changes: 1 addition & 1 deletion src/Statements/CheckStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class CheckStatement extends MaintenanceStatement
* @var array<string, int|array<int, int|string>>
* @psalm-var array<string, (positive-int|array{positive-int, ('var'|'var='|'expr'|'expr=')})>
*/
public static $statementOptions = [
public static array $statementOptions = [
'TABLE' => 1,

'FOR UPGRADE' => 2,
Expand Down
2 changes: 1 addition & 1 deletion src/Statements/ChecksumStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class ChecksumStatement extends MaintenanceStatement
* @var array<string, int|array<int, int|string>>
* @psalm-var array<string, (positive-int|array{positive-int, ('var'|'var='|'expr'|'expr=')})>
*/
public static $statementOptions = [
public static array $statementOptions = [
'TABLE' => 1,

'QUICK' => 2,
Expand Down
54 changes: 17 additions & 37 deletions src/Statements/CreateStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class CreateStatement extends Statement
* @var array<string, int|array<int, int|string>>
* @psalm-var array<string, (positive-int|array{positive-int, ('var'|'var='|'expr'|'expr=')})>
*/
public static $statementOptions = [
public static array $statementOptions = [
// CREATE TABLE
'TEMPORARY' => 1,

Expand Down Expand Up @@ -279,10 +279,8 @@ class CreateStatement extends Statement
* @see CreateStatement::TABLE_OPTIONS
* @see CreateStatement::FUNCTION_OPTIONS
* @see CreateStatement::TRIGGER_OPTIONS
*
* @var OptionsArray|null
*/
public $entityOptions;
public OptionsArray|null $entityOptions = null;

/**
* If `CREATE TABLE`, a list of columns and keys.
Expand All @@ -292,90 +290,72 @@ class CreateStatement extends Statement
*
* @var CreateDefinition[]|ArrayObj|null
*/
public $fields;
public array|ArrayObj|null $fields = null;

/**
* If `CREATE TABLE WITH`.
* If `CREATE TABLE AS WITH`.
* If `CREATE VIEW AS WITH`.
*
* Used by `CREATE TABLE`, `CREATE VIEW`
*
* @var WithStatement|null
*/
public $with;
public WithStatement|null $with = null;

/**
* If `CREATE TABLE ... SELECT`.
* If `CREATE VIEW AS ` ... SELECT`.
*
* Used by `CREATE TABLE`, `CREATE VIEW`
*
* @var SelectStatement|null
*/
public $select;
public SelectStatement|null $select = null;

/**
* If `CREATE TABLE ... LIKE`.
*
* Used by `CREATE TABLE`
*
* @var Expression|null
*/
public $like;
public Expression|null $like = null;

/**
* Expression used for partitioning.
*
* @var string|null
*/
public $partitionBy;
public string|null $partitionBy = null;

/**
* The number of partitions.
*
* @var int|null
*/
public $partitionsNum;
public int|null $partitionsNum = null;

/**
* Expression used for subpartitioning.
*
* @var string|null
*/
public $subpartitionBy;
public string|null $subpartitionBy = null;

/**
* The number of subpartitions.
*
* @var int|null
*/
public $subpartitionsNum;
public int|null $subpartitionsNum = null;

/**
* The partition of the new table.
*
* @var PartitionDefinition[]|null
*/
public $partitions;
public array|null $partitions = null;

/**
* If `CREATE TRIGGER` the name of the table.
*
* Used by `CREATE TRIGGER`.
*
* @var Expression|null
*/
public $table;
public Expression|null $table = null;

/**
* The return data type of this routine.
*
* Used by `CREATE FUNCTION`.
*
* @var DataType|null
*/
public $return;
public DataType|null $return = null;

/**
* The parameters of this routine.
Expand All @@ -384,7 +364,7 @@ class CreateStatement extends Statement
*
* @var ParameterDefinition[]|null
*/
public $parameters;
public array|null $parameters = null;

/**
* The body of this function or procedure.
Expand All @@ -398,10 +378,10 @@ class CreateStatement extends Statement
public function build(): string
{
$fields = '';
if (! empty($this->fields)) {
if ($this->fields !== null && $this->fields !== []) {
if (is_array($this->fields)) {
$fields = CreateDefinition::buildAll($this->fields) . ' ';
} elseif ($this->fields instanceof ArrayObj) {
} else {
$fields = $this->fields->build();
}
}
Expand Down Expand Up @@ -589,7 +569,7 @@ public function parse(Parser $parser, TokensList $list): void
}
} else {
$this->fields = CreateDefinition::parse($parser, $list);
if (empty($this->fields)) {
if ($this->fields === []) {
$parser->error('At least one column definition was expected.', $list->tokens[$list->idx]);
}

Expand Down
12 changes: 4 additions & 8 deletions src/Statements/DeleteStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class DeleteStatement extends Statement
* @var array<string, int|array<int, int|string>>
* @psalm-var array<string, (positive-int|array{positive-int, ('var'|'var='|'expr'|'expr=')})>
*/
public static $statementOptions = [
public static array $statementOptions = [
'LOW_PRIORITY' => 1,
'QUICK' => 2,
'IGNORE' => 3,
Expand All @@ -65,7 +65,7 @@ class DeleteStatement extends Statement
* @var array<string, array<int, int|string>>
* @psalm-var array<string, array{non-empty-string, (1|2|3)}>
*/
public static $clauses = [
public static array $clauses = [
'DELETE' => [
'DELETE',
2,
Expand Down Expand Up @@ -131,10 +131,8 @@ class DeleteStatement extends Statement

/**
* Partitions used as source for this statement.
*
* @var ArrayObj|null
*/
public $partition;
public ArrayObj|null $partition = null;

/**
* Conditions used for filtering each row of the result set.
Expand All @@ -152,10 +150,8 @@ class DeleteStatement extends Statement

/**
* Conditions used for limiting the size of the result set.
*
* @var Limit|null
*/
public $limit;
public Limit|null $limit = null;

public function build(): string
{
Expand Down
Loading
Loading