Skip to content

Commit

Permalink
Merge pull request #505 from kamil-tekiela/Make-constants-const
Browse files Browse the repository at this point in the history
Make constants const
  • Loading branch information
MauricioFauth authored Sep 17, 2023
2 parents 3f50a4d + c3dca8b commit 6b20105
Show file tree
Hide file tree
Showing 23 changed files with 133 additions and 313 deletions.
4 changes: 2 additions & 2 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -461,12 +461,12 @@ parameters:
path: src/Statement.php

-
message: "#^Offset 'class' does not exist on array\\{class\\?\\: class\\-string\\<PhpMyAdmin\\\\SqlParser\\\\Component\\>, field\\?\\: non\\-empty\\-string, options\\?\\: array\\<string, string\\>\\}\\.$#"
message: "#^Offset 'class' does not exist on array\\{\\}\\|array\\{class\\: 'PhpMyAdmin\\\\\\\\SqlParser\\\\\\\\Components\\\\\\\\Array2d', field\\: 'values'\\}\\|array\\{class\\: 'PhpMyAdmin\\\\\\\\SqlParser\\\\\\\\Components\\\\\\\\ArrayObj', field\\: 'partition'\\}\\|array\\{class\\: 'PhpMyAdmin\\\\\\\\SqlParser\\\\\\\\Components\\\\\\\\Condition', field\\: 'having'\\|'where'\\}\\|array\\{class\\: 'PhpMyAdmin\\\\\\\\SqlParser\\\\\\\\Components\\\\\\\\Expression', field\\: 'table', options\\: array\\{parseField\\: 'table'\\}\\}\\|array\\{class\\: 'PhpMyAdmin\\\\\\\\SqlParser\\\\\\\\Components\\\\\\\\ExpressionArray', field\\: 'expr'\\}\\|array\\{class\\: 'PhpMyAdmin\\\\\\\\SqlParser\\\\\\\\Components\\\\\\\\ExpressionArray', field\\: 'fields'\\|'tables', options\\: array\\{parseField\\: 'table'\\}\\}\\|array\\{class\\: 'PhpMyAdmin\\\\\\\\SqlParser\\\\\\\\Components\\\\\\\\ExpressionArray', field\\: 'from', options\\: array\\{field\\: 'table'\\}\\}\\|array\\{class\\: 'PhpMyAdmin\\\\\\\\SqlParser\\\\\\\\Components\\\\\\\\FunctionCall', field\\: 'call'\\|'procedure'\\}\\|array\\{class\\: 'PhpMyAdmin\\\\\\\\SqlParser\\\\\\\\Components\\\\\\\\GroupKeyword', field\\: 'group'\\}\\|array\\{class\\: 'PhpMyAdmin\\\\\\\\SqlParser\\\\\\\\Components\\\\\\\\IndexHint', field\\: 'index_hints'\\}\\|array\\{class\\: 'PhpMyAdmin\\\\\\\\SqlParser\\\\\\\\Components\\\\\\\\IntoKeyword', field\\: 'into'\\}\\|array\\{class\\: 'PhpMyAdmin\\\\\\\\SqlParser\\\\\\\\Components\\\\\\\\JoinKeyword', field\\: 'join'\\}\\|array\\{class\\: 'PhpMyAdmin\\\\\\\\SqlParser\\\\\\\\Components\\\\\\\\Limit', field\\: 'limit'\\}\\|array\\{class\\: 'PhpMyAdmin\\\\\\\\SqlParser\\\\\\\\Components\\\\\\\\OptionsArray', field\\: 'endOptions'\\|'groupOptions'\\|'options'\\}\\|array\\{class\\: 'PhpMyAdmin\\\\\\\\SqlParser\\\\\\\\Components\\\\\\\\OrderKeyword', field\\: 'order'\\}\\|array\\{class\\: 'PhpMyAdmin\\\\\\\\SqlParser\\\\\\\\Components\\\\\\\\RenameOperation', field\\: 'renames'\\}\\|array\\{class\\: 'PhpMyAdmin\\\\\\\\SqlParser\\\\\\\\Components\\\\\\\\SetOperation', field\\: 'set'\\}\\|array\\{class\\: 'PhpMyAdmin\\\\\\\\SqlParser\\\\\\\\Components\\\\\\\\UnionKeyword', field\\: 'union'\\}\\.$#"
count: 1
path: src/Statement.php

-
message: "#^Offset 'field' does not exist on array\\{class\\?\\: class\\-string\\<PhpMyAdmin\\\\SqlParser\\\\Component\\>, field\\?\\: non\\-empty\\-string, options\\?\\: array\\<string, string\\>\\}\\.$#"
message: "#^Offset 'field' does not exist on array\\{\\}\\|array\\{class\\: 'PhpMyAdmin\\\\\\\\SqlParser\\\\\\\\Components\\\\\\\\Array2d', field\\: 'values'\\}\\|array\\{class\\: 'PhpMyAdmin\\\\\\\\SqlParser\\\\\\\\Components\\\\\\\\ArrayObj', field\\: 'partition'\\}\\|array\\{class\\: 'PhpMyAdmin\\\\\\\\SqlParser\\\\\\\\Components\\\\\\\\Condition', field\\: 'having'\\|'where'\\}\\|array\\{class\\: 'PhpMyAdmin\\\\\\\\SqlParser\\\\\\\\Components\\\\\\\\Expression', field\\: 'table', options\\: array\\{parseField\\: 'table'\\}\\}\\|array\\{class\\: 'PhpMyAdmin\\\\\\\\SqlParser\\\\\\\\Components\\\\\\\\ExpressionArray', field\\: 'expr'\\}\\|array\\{class\\: 'PhpMyAdmin\\\\\\\\SqlParser\\\\\\\\Components\\\\\\\\ExpressionArray', field\\: 'fields'\\|'tables', options\\: array\\{parseField\\: 'table'\\}\\}\\|array\\{class\\: 'PhpMyAdmin\\\\\\\\SqlParser\\\\\\\\Components\\\\\\\\ExpressionArray', field\\: 'from', options\\: array\\{field\\: 'table'\\}\\}\\|array\\{class\\: 'PhpMyAdmin\\\\\\\\SqlParser\\\\\\\\Components\\\\\\\\FunctionCall', field\\: 'call'\\|'procedure'\\}\\|array\\{class\\: 'PhpMyAdmin\\\\\\\\SqlParser\\\\\\\\Components\\\\\\\\GroupKeyword', field\\: 'group'\\}\\|array\\{class\\: 'PhpMyAdmin\\\\\\\\SqlParser\\\\\\\\Components\\\\\\\\IndexHint', field\\: 'index_hints'\\}\\|array\\{class\\: 'PhpMyAdmin\\\\\\\\SqlParser\\\\\\\\Components\\\\\\\\IntoKeyword', field\\: 'into'\\}\\|array\\{class\\: 'PhpMyAdmin\\\\\\\\SqlParser\\\\\\\\Components\\\\\\\\JoinKeyword', field\\: 'join'\\}\\|array\\{class\\: 'PhpMyAdmin\\\\\\\\SqlParser\\\\\\\\Components\\\\\\\\Limit', field\\: 'limit'\\}\\|array\\{class\\: 'PhpMyAdmin\\\\\\\\SqlParser\\\\\\\\Components\\\\\\\\OptionsArray', field\\: 'endOptions'\\|'groupOptions'\\|'options'\\}\\|array\\{class\\: 'PhpMyAdmin\\\\\\\\SqlParser\\\\\\\\Components\\\\\\\\OrderKeyword', field\\: 'order'\\}\\|array\\{class\\: 'PhpMyAdmin\\\\\\\\SqlParser\\\\\\\\Components\\\\\\\\RenameOperation', field\\: 'renames'\\}\\|array\\{class\\: 'PhpMyAdmin\\\\\\\\SqlParser\\\\\\\\Components\\\\\\\\SetOperation', field\\: 'set'\\}\\|array\\{class\\: 'PhpMyAdmin\\\\\\\\SqlParser\\\\\\\\Components\\\\\\\\UnionKeyword', field\\: 'union'\\}\\.$#"
count: 1
path: src/Statement.php

Expand Down
104 changes: 23 additions & 81 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
@@ -1,46 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="5.15.0@5c774aca4746caf3d239d9c8cadb9f882ca29352">
<file src="src/Components/AlterOperation.php">
<InvalidPropertyAssignmentValue>
<code><![CDATA[[
'ON SCHEDULE' => 1,
'EVERY' => [
2,
'expr',
],
'AT' => [
2,
'expr',
],
'STARTS' => [
3,
'expr',
],
'ENDS' => [
4,
'expr',
],
'ON COMPLETION PRESERVE' => 5,
'ON COMPLETION NOT PRESERVE' => 5,
'RENAME' => 6,
'TO' => [
7,
'expr',
['parseField' => 'table'],
],
'ENABLE' => 8,
'DISABLE' => 8,
'DISABLE ON SLAVE' => 8,
'COMMENT' => [
9,
'var',
],
'DO' => 10,
]]]></code>
</InvalidPropertyAssignmentValue>
<PossiblyInvalidArgument>
<code>$arrayKey</code>
</PossiblyInvalidArgument>
<PossiblyNullPropertyAssignmentValue>
<code>$options</code>
</PossiblyNullPropertyAssignmentValue>
Expand Down Expand Up @@ -104,7 +64,7 @@
</file>
<file src="src/Components/Condition.php">
<MixedArrayOffset>
<code><![CDATA[static::$allowedKeywords[$token->value]]]></code>
<code><![CDATA[self::ALLOWED_KEYWORDS[$token->value]]]></code>
</MixedArrayOffset>
<PossiblyUnusedProperty>
<code>$isOperator</code>
Expand Down Expand Up @@ -137,13 +97,9 @@
<code><![CDATA[$options['breakOnParentheses']]]></code>
</InvalidArgument>
<MixedArrayOffset>
<code><![CDATA[Parser::$statementParsers[$token->keyword]]]></code>
<code><![CDATA[self::$allowedKeywords[$token->keyword]]]></code>
<code><![CDATA[Parser::STATEMENT_PARSERS[$token->keyword]]]></code>
<code><![CDATA[self::ALLOWED_KEYWORDS[$token->keyword]]]></code>
</MixedArrayOffset>
<MixedArrayTypeCoercion>
<code><![CDATA[Parser::$statementParsers[$token->keyword]]]></code>
<code><![CDATA[self::$allowedKeywords[$token->keyword]]]></code>
</MixedArrayTypeCoercion>
<MixedAssignment>
<code>$field</code>
<code><![CDATA[$options['field']]]></code>
Expand Down Expand Up @@ -241,15 +197,11 @@
<code><![CDATA['']]></code>
</DocblockTypeContradiction>
<MixedArrayOffset>
<code><![CDATA[static::$joins[$token->keyword]]]></code>
<code><![CDATA[static::$joins[$token->keyword]]]></code>
<code><![CDATA[self::JOINS[$token->keyword]]]></code>
<code><![CDATA[self::JOINS[$token->keyword]]]></code>
</MixedArrayOffset>
<MixedArrayTypeCoercion>
<code><![CDATA[static::$joins[$token->keyword]]]></code>
<code><![CDATA[static::$joins[$token->keyword]]]></code>
</MixedArrayTypeCoercion>
<PossiblyFalseOperand>
<code><![CDATA[array_search($c->type, static::$joins)]]></code>
<code><![CDATA[array_search($c->type, self::JOINS)]]></code>
</PossiblyFalseOperand>
<PossiblyInvalidPropertyAssignmentValue>
<code>ArrayObj::parse($parser, $list)</code>
Expand Down Expand Up @@ -712,13 +664,9 @@
<code><![CDATA[new $class($this, $this->list)]]></code>
</InvalidStringClass>
<MixedArrayOffset>
<code><![CDATA[static::$statementParsers[$statementName ?? $token->keyword]]]></code>
<code><![CDATA[static::$statementParsers[$token->keyword]]]></code>
<code><![CDATA[self::STATEMENT_PARSERS[$statementName ?? $token->keyword]]]></code>
<code><![CDATA[self::STATEMENT_PARSERS[$token->keyword]]]></code>
</MixedArrayOffset>
<MixedArrayTypeCoercion>
<code><![CDATA[static::$statementParsers[$statementName ?? $token->keyword]]]></code>
<code><![CDATA[static::$statementParsers[$token->keyword]]]></code>
</MixedArrayTypeCoercion>
<ParamNameMismatch>
<code>$msg</code>
</ParamNameMismatch>
Expand All @@ -733,7 +681,7 @@
</PossiblyNullArrayAccess>
<PossiblyNullArrayOffset>
<code><![CDATA[$list->tokens]]></code>
<code>static::$statementParsers</code>
<code>self::STATEMENT_PARSERS</code>
</PossiblyNullArrayOffset>
<PossiblyNullOperand>
<code><![CDATA[$list->idx]]></code>
Expand Down Expand Up @@ -773,17 +721,13 @@
<MixedArrayOffset>
<code><![CDATA[$parsedClauses[$token->value]]]></code>
<code><![CDATA[$parsedClauses[$token->value]]]></code>
<code><![CDATA[Parser::$keywordParsers[$token->value]]]></code>
<code>Parser::$keywordParsers[$tokenValue]</code>
<code><![CDATA[Parser::$statementParsers[$token->keyword]]]></code>
<code><![CDATA[Parser::$statementParsers[$token->value]]]></code>
<code><![CDATA[Parser::KEYWORD_PARSERS[$token->value]]]></code>
<code>Parser::KEYWORD_PARSERS[$tokenValue]</code>
<code><![CDATA[Parser::STATEMENT_PARSERS[$token->keyword]]]></code>
<code><![CDATA[Parser::STATEMENT_PARSERS[$token->value]]]></code>
<code><![CDATA[static::$clauses[$token->value]]]></code>
<code><![CDATA[static::$statementOptions[$token->value]]]></code>
</MixedArrayOffset>
<MixedArrayTypeCoercion>
<code>Parser::$keywordParsers[$tokenValue]</code>
<code><![CDATA[Parser::$statementParsers[$token->keyword]]]></code>
</MixedArrayTypeCoercion>
<MixedAssignment>
<code>$tokenValue</code>
</MixedAssignment>
Expand All @@ -794,10 +738,10 @@
<code><![CDATA[$this->$field->build()]]></code>
</MixedOperand>
<PossiblyUndefinedArrayOffset>
<code><![CDATA[Parser::$keywordParsers[$name]['class']]]></code>
<code><![CDATA[Parser::$keywordParsers[$name]['field']]]></code>
<code><![CDATA[Parser::$keywordParsers[$tokenValue]['class']]]></code>
<code><![CDATA[Parser::$keywordParsers[$tokenValue]['field']]]></code>
<code><![CDATA[Parser::KEYWORD_PARSERS[$name]['class']]]></code>
<code><![CDATA[Parser::KEYWORD_PARSERS[$name]['field']]]></code>
<code><![CDATA[Parser::KEYWORD_PARSERS[$tokenValue]['class']]]></code>
<code><![CDATA[Parser::KEYWORD_PARSERS[$tokenValue]['field']]]></code>
</PossiblyUndefinedArrayOffset>
<PossiblyUnusedReturnValue>
<code>bool</code>
Expand Down Expand Up @@ -1179,19 +1123,17 @@
<code><![CDATA[$format['type']]]></code>
</MixedArrayAccess>
<MixedArrayOffset>
<code><![CDATA[JoinKeyword::$joins[$list->tokens[$list->idx - 2]->value]]]></code>
<code><![CDATA[JoinKeyword::$joins[$list->tokens[$list->idx - 4]->value]]]></code>
<code><![CDATA[JoinKeyword::$joins[$list->tokens[$list->idx - 6]->value]]]></code>
<code><![CDATA[JoinKeyword::$joins[$prev->value]]]></code>
<code><![CDATA[Parser::$keywordParsers[$token->keyword]]]></code>
<code><![CDATA[Parser::$statementParsers[$token->keyword]]]></code>
<code><![CDATA[JoinKeyword::JOINS[$list->tokens[$list->idx - 2]->value]]]></code>
<code><![CDATA[JoinKeyword::JOINS[$list->tokens[$list->idx - 4]->value]]]></code>
<code><![CDATA[JoinKeyword::JOINS[$list->tokens[$list->idx - 6]->value]]]></code>
<code><![CDATA[JoinKeyword::JOINS[$prev->value]]]></code>
<code><![CDATA[Parser::KEYWORD_PARSERS[$token->keyword]]]></code>
<code><![CDATA[Parser::STATEMENT_PARSERS[$token->keyword]]]></code>
<code>self::$inlineClauses[$lastClause]</code>
<code>self::$inlineClauses[$lastClause]</code>
<code>self::$shortClauses[$lastClause]</code>
</MixedArrayOffset>
<MixedArrayTypeCoercion>
<code><![CDATA[Parser::$keywordParsers[$token->keyword]]]></code>
<code><![CDATA[Parser::$statementParsers[$token->keyword]]]></code>
<code>self::$inlineClauses[$lastClause]</code>
<code>self::$inlineClauses[$lastClause]</code>
<code>self::$shortClauses[$lastClause]</code>
Expand Down
31 changes: 8 additions & 23 deletions src/Components/AlterOperation.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,8 @@ final class AlterOperation implements Component
{
/**
* All database options.
*
* @var array<string, int|array<int, int|string>>
* @psalm-var array<string, (positive-int|array{positive-int, ('var'|'var='|'expr'|'expr=')})>
*/
public static $databaseOptions = [
public const DATABASE_OPTIONS = [
'CHARACTER SET' => [
1,
'var',
Expand Down Expand Up @@ -59,11 +56,8 @@ final class AlterOperation implements Component

/**
* All table options.
*
* @var array<string, int|array<int, int|string>>
* @psalm-var array<string, (positive-int|array{positive-int, ('var'|'var='|'expr'|'expr=')})>
*/
public static $tableOptions = [
public const TABLE_OPTIONS = [
'ENGINE' => [
1,
'var=',
Expand Down Expand Up @@ -151,11 +145,8 @@ final class AlterOperation implements Component

/**
* All user options.
*
* @var array<string, int|array<int, int|string>>
* @psalm-var array<string, (positive-int|array{positive-int, ('var'|'var='|'expr'|'expr=')})>
*/
public static $userOptions = [
public const USER_OPTIONS = [
'ATTRIBUTE' => [
1,
'var',
Expand Down Expand Up @@ -202,19 +193,13 @@ final class AlterOperation implements Component

/**
* All view options.
*
* @var array<string, int|array<int, int|string>>
* @psalm-var array<string, (positive-int|array{positive-int, ('var'|'var='|'expr'|'expr=')})>
*/
public static $viewOptions = ['AS' => 1];
public const VIEW_OPTIONS = ['AS' => 1];

/**
* All event options.
*
* @var array<string, int|array<int, int|string>>
* @psalm-var array<string, (positive-int|array{positive-int, ('var'|'var='|'expr'|'expr=')})>
*/
public static $eventOptions = [
public const EVENT_OPTIONS = [
'ON SCHEDULE' => 1,
'EVERY' => [
2,
Expand Down Expand Up @@ -441,7 +426,7 @@ public static function parse(Parser $parser, TokensList $list, array $options =
);
break;
}
} elseif (! empty(Parser::$statementParsers[$arrayKey])) {
} elseif (! empty(Parser::STATEMENT_PARSERS[$arrayKey])) {
// We have reached the end of ALTER operation and suddenly found
// a start to new statement, but have not found a delimiter between them
$parser->error(
Expand All @@ -450,8 +435,8 @@ public static function parse(Parser $parser, TokensList $list, array $options =
);
break;
} elseif (
(array_key_exists($arrayKey, self::$databaseOptions)
|| array_key_exists($arrayKey, self::$tableOptions))
(array_key_exists($arrayKey, self::DATABASE_OPTIONS)
|| array_key_exists($arrayKey, self::TABLE_OPTIONS))
&& ! self::checkIfColumnDefinitionKeyword($arrayKey)
) {
// This alter operation has finished, which means a comma
Expand Down
12 changes: 4 additions & 8 deletions src/Components/Condition.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,8 @@ final class Condition implements Component
{
/**
* Logical operators that can be used to delimit expressions.
*
* @var string[]
*/
public static $delimiters = [
private const DELIMITERS = [
'&&',
'||',
'AND',
Expand All @@ -33,10 +31,8 @@ final class Condition implements Component

/**
* List of allowed reserved keywords in conditions.
*
* @var array<string, int>
*/
public static $allowedKeywords = [
private const ALLOWED_KEYWORDS = [
'ALL' => 1,
'AND' => 1,
'BETWEEN' => 1,
Expand Down Expand Up @@ -143,7 +139,7 @@ public static function parse(Parser $parser, TokensList $list, array $options =
}

// Conditions are delimited by logical operators.
if (in_array($token->value, static::$delimiters, true)) {
if (in_array($token->value, self::DELIMITERS, true)) {
if ($betweenBefore && ($token->value === 'AND')) {
// The syntax of keyword `BETWEEN` is hard-coded.
$betweenBefore = false;
Expand Down Expand Up @@ -174,7 +170,7 @@ public static function parse(Parser $parser, TokensList $list, array $options =
$betweenBefore = true;
}

if (($brackets === 0) && empty(static::$allowedKeywords[$token->value])) {
if (($brackets === 0) && empty(self::ALLOWED_KEYWORDS[$token->value])) {
break;
}
}
Expand Down
11 changes: 2 additions & 9 deletions src/Components/CreateDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,8 @@ final class CreateDefinition implements Component
{
/**
* All field options.
*
* @var array<string, bool|int|array<int, int|string|array<string, bool>>>
* @psalm-var array<string, (bool|positive-int|array{
* 0: positive-int,
* 1: ('var'|'var='|'expr'|'expr='),
* 2?: array<string, bool>
* })>
*/
public static $fieldOptions = [
private const FIELD_OPTIONS = [
// Tells the `OptionsArray` to not sort the options.
// See the note below.
'_UNSORTED' => true,
Expand Down Expand Up @@ -271,7 +264,7 @@ public static function parse(Parser $parser, TokensList $list, array $options =
$expr->type = DataType::parse($parser, $list);
$state = 3;
} elseif ($state === 3) {
$expr->options = OptionsArray::parse($parser, $list, static::$fieldOptions);
$expr->options = OptionsArray::parse($parser, $list, self::FIELD_OPTIONS);
$state = 4;
} elseif ($state === 4) {
if ($token->type === Token::TYPE_KEYWORD && $token->keyword === 'REFERENCES') {
Expand Down
7 changes: 2 additions & 5 deletions src/Components/DataType.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,8 @@ final class DataType implements Component
{
/**
* All data type options.
*
* @var array<string, int|array<int, int|string>>
* @psalm-var array<string, (positive-int|array{positive-int, ('var'|'var='|'expr'|'expr=')})>
*/
public static $dataTypeOptions = [
private const DATA_TYPE_OPTIONS = [
'BINARY' => 1,
'CHARACTER SET' => [
2,
Expand Down Expand Up @@ -137,7 +134,7 @@ public static function parse(Parser $parser, TokensList $list, array $options =
$parameters->raw : $parameters->values;
}

$ret->options = OptionsArray::parse($parser, $list, static::$dataTypeOptions);
$ret->options = OptionsArray::parse($parser, $list, self::DATA_TYPE_OPTIONS);
++$list->idx;
break;
}
Expand Down
Loading

0 comments on commit 6b20105

Please sign in to comment.