Skip to content

Commit

Permalink
Refactor OptionsArray
Browse files Browse the repository at this point in the history
Signed-off-by: Kamil Tekiela <tekiela246@gmail.com>
  • Loading branch information
kamil-tekiela committed Sep 24, 2024
1 parent 98b704c commit e204155
Show file tree
Hide file tree
Showing 9 changed files with 111 additions and 126 deletions.
55 changes: 30 additions & 25 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,6 @@ parameters:
count: 1
path: src/Components/DataType.php

-
message: "#^Parameter \\#2 \\$string2 of function strcasecmp expects string, mixed given\\.$#"
count: 2
path: src/Components/OptionsArray.php

-
message: "#^Parameter \\#1 \\$str of static method PhpMyAdmin\\\\SqlParser\\\\Context\\:\\:escape\\(\\) expects string, string\\|null given\\.$#"
count: 1
Expand Down Expand Up @@ -256,48 +251,58 @@ parameters:
path: src/Parsers/Limits.php

-
message: "#^Cannot access offset 'equals' on mixed\\.$#"
count: 1
message: "#^Cannot access offset 1 on mixed\\.$#"
count: 2
path: src/Parsers/OptionsArrays.php

-
message: "#^Cannot access offset 'expr' on mixed\\.$#"
count: 2
message: "#^Cannot access offset 2 on mixed\\.$#"
count: 1
path: src/Parsers/OptionsArrays.php

-
message: "#^Cannot access offset 'name' on mixed\\.$#"
message: "#^Cannot assign offset 'equals' to array\\<string, bool\\|PhpMyAdmin\\\\SqlParser\\\\Components\\\\Expression\\|string\\|null\\>\\|string\\.$#"
count: 1
path: src/Parsers/OptionsArrays.php

-
message: "#^Cannot access offset 'value' on mixed\\.$#"
message: "#^Cannot assign offset 'expr' to array\\<string, bool\\|PhpMyAdmin\\\\SqlParser\\\\Components\\\\Expression\\|string\\|null\\>\\|string\\.$#"
count: 2
path: src/Parsers/OptionsArrays.php

-
message: "#^Cannot access offset 1 on mixed\\.$#"
count: 2
message: "#^Offset 'expr' does not exist on array\\{name\\: string, equals\\: bool, expr\\: PhpMyAdmin\\\\SqlParser\\\\Components\\\\Expression\\|string, value\\: string\\|null\\}\\|string\\.$#"
count: 1
path: src/Parsers/OptionsArrays.php

-
message: "#^Cannot access offset 2 on mixed\\.$#"
message: "#^Offset 'name' does not exist on array\\{name\\: string, equals\\: bool, expr\\: PhpMyAdmin\\\\SqlParser\\\\Components\\\\Expression\\|string, value\\: string\\|null\\}\\|string\\.$#"
count: 1
path: src/Parsers/OptionsArrays.php

-
message: "#^Parameter \\#2 \\.\\.\\.\\$values of function sprintf expects bool\\|float\\|int\\|string\\|null, mixed given\\.$#"
message: "#^Parameter \\#3 \\$options of static method PhpMyAdmin\\\\SqlParser\\\\Parsers\\\\Expressions\\:\\:parse\\(\\) expects array\\<string, mixed\\>, mixed given\\.$#"
count: 1
path: src/Parsers/OptionsArrays.php

-
message: "#^Parameter \\#3 \\$options of static method PhpMyAdmin\\\\SqlParser\\\\Parsers\\\\Expressions\\:\\:parse\\(\\) expects array\\<string, mixed\\>, mixed given\\.$#"
message: "#^Property PhpMyAdmin\\\\SqlParser\\\\Components\\\\OptionsArray\\:\\:\\$options \\(array\\<int, array\\{name\\: string, equals\\: bool, expr\\: PhpMyAdmin\\\\SqlParser\\\\Components\\\\Expression\\|string, value\\: string\\|null\\}\\|string\\>\\) does not accept non\\-empty\\-array\\<int\\|string, array\\{name\\: bool\\|float\\|int\\|string, equals\\: bool, expr\\: PhpMyAdmin\\\\SqlParser\\\\Components\\\\Expression\\|string, value\\: string\\|null\\}\\|string\\>\\.$#"
count: 2
path: src/Parsers/OptionsArrays.php

-
message: "#^Property PhpMyAdmin\\\\SqlParser\\\\Components\\\\OptionsArray\\:\\:\\$options \\(array\\<int, array\\{name\\: string, equals\\: bool, expr\\: PhpMyAdmin\\\\SqlParser\\\\Components\\\\Expression\\|string, value\\: string\\|null\\}\\|string\\>\\) does not accept non\\-empty\\-array\\<int\\|string, array\\{name\\: string, equals\\: bool, expr\\: PhpMyAdmin\\\\SqlParser\\\\Components\\\\Expression\\|string, value\\: string\\|null\\}\\|bool\\|float\\|int\\|string\\>\\.$#"
count: 1
path: src/Parsers/OptionsArrays.php

-
message: "#^Property PhpMyAdmin\\\\SqlParser\\\\Components\\\\OptionsArray\\:\\:\\$options \\(array\\<int, mixed\\>\\) does not accept array\\<int\\|string, mixed\\>\\.$#"
count: 8
message: "#^Property PhpMyAdmin\\\\SqlParser\\\\Components\\\\OptionsArray\\:\\:\\$options \\(array\\<int, array\\{name\\: string, equals\\: bool, expr\\: PhpMyAdmin\\\\SqlParser\\\\Components\\\\Expression\\|string, value\\: string\\|null\\}\\|string\\>\\) does not accept non\\-empty\\-array\\<int\\|string, array\\{name\\: string, equals\\: bool, expr\\: PhpMyAdmin\\\\SqlParser\\\\Components\\\\Expression\\|string, value\\: string\\|null\\}\\|string\\>\\.$#"
count: 3
path: src/Parsers/OptionsArrays.php

-
message: "#^Property PhpMyAdmin\\\\SqlParser\\\\Components\\\\OptionsArray\\:\\:\\$options \\(array\\<int, array\\{name\\: string, equals\\: bool, expr\\: PhpMyAdmin\\\\SqlParser\\\\Components\\\\Expression\\|string, value\\: string\\|null\\}\\|string\\>\\) does not accept non\\-empty\\-array\\<int\\|string, mixed\\>\\.$#"
count: 3
path: src/Parsers/OptionsArrays.php

-
Expand Down Expand Up @@ -506,18 +511,18 @@ parameters:
path: src/Statements/CreateStatement.php

-
message: "#^Cannot call method has\\(\\) on PhpMyAdmin\\\\SqlParser\\\\Components\\\\OptionsArray\\|null\\.$#"
count: 11
message: "#^Cannot call method get\\(\\) on PhpMyAdmin\\\\SqlParser\\\\Components\\\\OptionsArray\\|null\\.$#"
count: 2
path: src/Statements/CreateStatement.php

-
message: "#^Parameter \\#1 \\$component of static method PhpMyAdmin\\\\SqlParser\\\\Parsers\\\\ParameterDefinitions\\:\\:buildAll\\(\\) expects array\\<PhpMyAdmin\\\\SqlParser\\\\Components\\\\ParameterDefinition\\>, array\\<PhpMyAdmin\\\\SqlParser\\\\Components\\\\ParameterDefinition\\>\\|null given\\.$#"
count: 1
message: "#^Cannot call method has\\(\\) on PhpMyAdmin\\\\SqlParser\\\\Components\\\\OptionsArray\\|null\\.$#"
count: 9
path: src/Statements/CreateStatement.php

-
message: "#^Parameter \\#3 \\$subject of function str_replace expects array\\|string, mixed given\\.$#"
count: 2
message: "#^Parameter \\#1 \\$component of static method PhpMyAdmin\\\\SqlParser\\\\Parsers\\\\ParameterDefinitions\\:\\:buildAll\\(\\) expects array\\<PhpMyAdmin\\\\SqlParser\\\\Components\\\\ParameterDefinition\\>, array\\<PhpMyAdmin\\\\SqlParser\\\\Components\\\\ParameterDefinition\\>\\|null given\\.$#"
count: 1
path: src/Statements/CreateStatement.php

-
Expand Down Expand Up @@ -896,7 +901,7 @@ parameters:
path: src/Utils/Table.php

-
message: "#^Method PhpMyAdmin\\\\SqlParser\\\\Utils\\\\Table\\:\\:getFields\\(\\) should return array\\<string, array\\{type\\: string, timestamp_not_null\\: bool, default_value\\?\\: mixed, default_current_timestamp\\?\\: true, on_update_current_timestamp\\?\\: true, expr\\?\\: mixed\\}\\> but returns array\\<string, array\\{default_current_timestamp\\?\\: true, default_value\\?\\: mixed, expr\\?\\: mixed, on_update_current_timestamp\\?\\: true, timestamp_not_null\\: bool, type\\: string\\|null\\}\\>\\.$#"
message: "#^Method PhpMyAdmin\\\\SqlParser\\\\Utils\\\\Table\\:\\:getFields\\(\\) should return array\\<string, array\\{type\\: string, timestamp_not_null\\: bool, default_value\\?\\: mixed, default_current_timestamp\\?\\: true, on_update_current_timestamp\\?\\: true, expr\\?\\: mixed\\}\\> but returns array\\<string, array\\{default_current_timestamp\\?\\: true, default_value\\?\\: PhpMyAdmin\\\\SqlParser\\\\Components\\\\Expression\\|non\\-empty\\-string, expr\\?\\: PhpMyAdmin\\\\SqlParser\\\\Components\\\\Expression\\|non\\-empty\\-string, on_update_current_timestamp\\?\\: true, timestamp_not_null\\: bool, type\\: string\\|null\\}\\>\\.$#"
count: 1
path: src/Utils/Table.php

Expand Down
75 changes: 18 additions & 57 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -86,27 +86,6 @@
<code><![CDATA[$this->type]]></code>
</PossiblyNullOperand>
</file>
<file src="src/Components/OptionsArray.php">
<MixedArgument>
<code><![CDATA[$option]]></code>
<code><![CDATA[$option]]></code>
<code><![CDATA[$option['name']]]></code>
<code><![CDATA[$option['name']]]></code>
</MixedArgument>
<MixedArgumentTypeCoercion>
<code><![CDATA[$options]]></code>
</MixedArgumentTypeCoercion>
<MixedAssignment>
<code><![CDATA[$option]]></code>
<code><![CDATA[$option]]></code>
<code><![CDATA[$option]]></code>
<code><![CDATA[$options[]]]></code>
</MixedAssignment>
<MixedOperand>
<code><![CDATA[! empty($option['expr']) ? $option['expr'] : $option['value']]]></code>
<code><![CDATA[$option['name']]]></code>
</MixedOperand>
</file>
<file src="src/Components/OrderKeyword.php">
<PossiblyNullOperand>
<code><![CDATA[$this->expr]]></code>
Expand Down Expand Up @@ -498,25 +477,12 @@
<MixedArgument>
<code><![CDATA[$ret->options[$lastOptionId]['name']]]></code>
<code><![CDATA[empty($lastOption[2]) ? [] : $lastOption[2]]]></code>
<code><![CDATA[is_array($ret->options[$lastOptionId])
? $ret->options[$lastOptionId]['name']
: $ret->options[$lastOptionId]]]></code>
</MixedArgument>
<MixedArrayAccess>
<code><![CDATA[$lastOption[1]]]></code>
<code><![CDATA[$lastOption[1]]]></code>
<code><![CDATA[$lastOption[2]]]></code>
<code><![CDATA[$ret->options[$lastOptionId]['expr']]]></code>
<code><![CDATA[$ret->options[$lastOptionId]['name']]]></code>
<code><![CDATA[$ret->options[$lastOptionId]['value']]]></code>
</MixedArrayAccess>
<MixedArrayAssignment>
<code><![CDATA[$ret->options[$lastOptionId]['equals']]]></code>
<code><![CDATA[$ret->options[$lastOptionId]['expr']]]></code>
<code><![CDATA[$ret->options[$lastOptionId]['expr']]]></code>
<code><![CDATA[$ret->options[$lastOptionId]['value']]]></code>
<code><![CDATA[$ret->options[$lastOptionId]['value']]]></code>
</MixedArrayAssignment>
<MixedArrayOffset>
<code><![CDATA[$ret->options[$lastOptionId]]]></code>
<code><![CDATA[$ret->options[$lastOptionId]]]></code>
Expand All @@ -529,6 +495,7 @@
<code><![CDATA[$ret->options[$lastOptionId]]]></code>
<code><![CDATA[$ret->options[$lastOptionId]]]></code>
<code><![CDATA[$ret->options[$lastOptionId]]]></code>
<code><![CDATA[$ret->options[$lastOptionId]]]></code>
</MixedArrayOffset>
<MixedArrayTypeCoercion>
<code><![CDATA[$ret->options[$lastOptionId]]]></code>
Expand All @@ -549,19 +516,30 @@
<code><![CDATA[$ret->options]]></code>
<code><![CDATA[$ret->options]]></code>
<code><![CDATA[$ret->options]]></code>
<code><![CDATA[$ret->options]]></code>
<code><![CDATA[$ret->options]]></code>
</MixedPropertyTypeCoercion>
<PossiblyInvalidArrayOffset>
<code><![CDATA[$ret->options[$lastOptionId]['equals']]]></code>
<code><![CDATA[$ret->options[$lastOptionId]['expr']]]></code>
<code><![CDATA[$ret->options[$lastOptionId]['expr']]]></code>
<code><![CDATA[$ret->options[$lastOptionId]['expr']]]></code>
<code><![CDATA[$ret->options[$lastOptionId]['expr']]]></code>
<code><![CDATA[$ret->options[$lastOptionId]['name']]]></code>
<code><![CDATA[$ret->options[$lastOptionId]['value']]]></code>
<code><![CDATA[$ret->options[$lastOptionId]['value']]]></code>
<code><![CDATA[$ret->options[$lastOptionId]['value']]]></code>
</PossiblyInvalidArrayOffset>
<PossiblyNullArgument>
<code><![CDATA[empty($lastOption[2]) ? [] : $lastOption[2]]]></code>
</PossiblyNullArgument>
<PossiblyNullArrayAccess>
<code><![CDATA[$lastOption[1]]]></code>
<code><![CDATA[$lastOption[2]]]></code>
</PossiblyNullArrayAccess>
<PossiblyNullPropertyFetch>
<code><![CDATA[$ret->options[$lastOptionId]['expr']->expr]]></code>
</PossiblyNullPropertyFetch>
<PropertyTypeCoercion>
<code><![CDATA[$ret->options]]></code>
<code><![CDATA[$ret->options]]></code>
<code><![CDATA[$ret->options]]></code>
</PropertyTypeCoercion>
<RiskyTruthyFalsyComparison>
<code><![CDATA[$lastOption]]></code>
<code><![CDATA[empty($lastOption[2])]]></code>
Expand Down Expand Up @@ -679,25 +657,15 @@
</file>
<file src="src/Statements/CreateStatement.php">
<MixedArgument>
<code><![CDATA[$opt]]></code>
<code><![CDATA[$opt]]></code>
<code><![CDATA[$this->$field]]></code>
</MixedArgument>
<MixedAssignment>
<code><![CDATA[$opt]]></code>
<code><![CDATA[$opt]]></code>
</MixedAssignment>
<MixedOperand>
<code><![CDATA[$this->$field]]></code>
</MixedOperand>
<PossiblyFalseOperand>
<code><![CDATA[$brackets]]></code>
<code><![CDATA[$brackets]]></code>
</PossiblyFalseOperand>
<PossiblyInvalidPropertyAssignmentValue>
<code><![CDATA[str_replace(' ', '_', $opt)]]></code>
<code><![CDATA[str_replace(' ', '_', $opt)]]></code>
</PossiblyInvalidPropertyAssignmentValue>
<PossiblyNullArgument>
<code><![CDATA[$this->parameters]]></code>
</PossiblyNullArgument>
Expand All @@ -723,7 +691,7 @@
<code><![CDATA[build]]></code>
<code><![CDATA[build]]></code>
<code><![CDATA[build]]></code>
<code><![CDATA[has]]></code>
<code><![CDATA[get]]></code>
<code><![CDATA[has]]></code>
<code><![CDATA[has]]></code>
<code><![CDATA[has]]></code>
Expand Down Expand Up @@ -1191,13 +1159,6 @@
<LessSpecificReturnStatement>
<code><![CDATA[$ret]]></code>
</LessSpecificReturnStatement>
<MixedAssignment>
<code><![CDATA[$option]]></code>
<code><![CDATA[$option]]></code>
<code><![CDATA[$option]]></code>
<code><![CDATA[$ret[$field->name]['default_value']]]></code>
<code><![CDATA[$ret[$field->name]['expr']]]></code>
</MixedAssignment>
<MoreSpecificReturnType>
<code><![CDATA[array<string, array{
* type: string,
Expand Down
55 changes: 36 additions & 19 deletions src/Components/OptionsArray.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,25 @@
use function is_array;
use function strcasecmp;

/**
* Parses a list of options.
*/
final class OptionsArray implements Component
{
/**
* @param array<int, mixed> $options The array of options. Options that have a value
* must be an array with at least two keys `name` and
* `expr` or `value`.
* @param array<int, string|array<string, string|bool|null>> $options $options The array of options.
* Options that have a value must be an array with at least two keys `name` and `expr` or `value`.
* @psalm-param array<int, string|array{
* name: string,
* equals: bool,
* expr: string|Expression,
* value: string|null
* }> $options
*/
public function __construct(public array $options = [])
{
}

public function build(): string
{
if (empty($this->options)) {
if ($this->options === []) {
return '';
}

Expand All @@ -37,34 +39,49 @@ public function build(): string
$options[] = $option;
} else {
$options[] = $option['name']
. (! empty($option['equals']) ? '=' : ' ')
. (! empty($option['expr']) ? $option['expr'] : $option['value']);
. ($option['equals'] ? '=' : ' ')
. ($option['expr'] !== '' ? $option['expr'] : ($option['value'] ?? ''));
}
}

return implode(' ', $options);
}

public function has(string $key): bool
{
foreach ($this->options as $option) {
if (is_array($option)) {
if (strcasecmp($key, $option['name']) === 0) {
return ($option['value'] ?? '') !== '';
}
} elseif (strcasecmp($key, $option) === 0) {
return true;
}
}

return false;
}

/**
* Checks if it has the specified option and returns it value or true.
* Checks if it has the specified option and returns its value.
*
* @param string $key the key to be checked
* @param bool $getExpr Gets the expression instead of the value.
* The value is the processed form of the expression.
*/
public function has(string $key, bool $getExpr = false): mixed
public function get(string $key, bool $getExpr = false): string|Expression
{
foreach ($this->options as $option) {
if (is_array($option)) {
if (! strcasecmp($key, $option['name'])) {
return $getExpr ? $option['expr'] : $option['value'];
if (strcasecmp($key, $option['name']) === 0) {
return $getExpr ? $option['expr'] : ($option['value'] ?? '');
}
} elseif (! strcasecmp($key, $option)) {
return true;
} elseif (strcasecmp($key, $option) === 0) {

Check warning on line 79 in src/Components/OptionsArray.php

View workflow job for this annotation

GitHub Actions / Mutation tests with PHP 8.2

Escaped Mutant for Mutator "DecrementInteger": --- Original +++ New @@ @@ if (strcasecmp($key, $option['name']) === 0) { return $getExpr ? $option['expr'] : $option['value'] ?? ''; } - } elseif (strcasecmp($key, $option) === 0) { + } elseif (strcasecmp($key, $option) === -1) { return $option; } }
return $option;
}
}

return false;
return '';
}

/**
Expand All @@ -78,12 +95,12 @@ public function remove(string $key): bool
{
foreach ($this->options as $idx => $option) {
if (is_array($option)) {
if (! strcasecmp($key, $option['name'])) {
if (strcasecmp($key, $option['name']) === 0) {
unset($this->options[$idx]);

return true;

Check warning on line 101 in src/Components/OptionsArray.php

View workflow job for this annotation

GitHub Actions / Mutation tests with PHP 8.2

Escaped Mutant for Mutator "TrueValue": --- Original +++ New @@ @@ if (is_array($option)) { if (strcasecmp($key, $option['name']) === 0) { unset($this->options[$idx]); - return true; + return false; } } elseif (strcasecmp($key, $option) === 0) { unset($this->options[$idx]);
}
} elseif (! strcasecmp($key, $option)) {
} elseif (strcasecmp($key, $option) === 0) {
unset($this->options[$idx]);

return true;
Expand All @@ -107,7 +124,7 @@ public function merge(OptionsArray $options): void
*/
public function isEmpty(): bool
{
return empty($this->options);
return $this->options === [];
}

public function __toString(): string
Expand Down
Loading

0 comments on commit e204155

Please sign in to comment.