Skip to content

Commit

Permalink
Merge pull request #532 from kamil-tekiela/types-in-components
Browse files Browse the repository at this point in the history
Add native property types in Statements
  • Loading branch information
MauricioFauth committed Jan 16, 2024
2 parents 2e71e97 + 282db99 commit b05c846
Show file tree
Hide file tree
Showing 31 changed files with 117 additions and 241 deletions.
9 changes: 2 additions & 7 deletions phpstan-baseline.neon
Expand Up @@ -541,12 +541,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 @@ -590,11 +590,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
Expand Up @@ -770,9 +770,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 @@ -811,7 +808,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 @@ -866,9 +862,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
Expand Up @@ -45,7 +45,7 @@ abstract class Statement implements Stringable
* @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 @@ abstract class Statement implements Stringable
* @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 Down
8 changes: 3 additions & 5 deletions src/Statements/AlterStatement.php
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
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
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
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
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
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
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
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

0 comments on commit b05c846

Please sign in to comment.