Skip to content

Commit 0ff9969

Browse files
authoredOct 25, 2024
Merge pull request #586 from kamil-tekiela/Refactor-OptionsArray
Refactor OptionsArray
2 parents 98b704c + e204155 commit 0ff9969

File tree

9 files changed

+111
-126
lines changed

9 files changed

+111
-126
lines changed
 

‎phpstan-baseline.neon

+30-25
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,6 @@ parameters:
2020
count: 1
2121
path: src/Components/DataType.php
2222

23-
-
24-
message: "#^Parameter \\#2 \\$string2 of function strcasecmp expects string, mixed given\\.$#"
25-
count: 2
26-
path: src/Components/OptionsArray.php
27-
2823
-
2924
message: "#^Parameter \\#1 \\$str of static method PhpMyAdmin\\\\SqlParser\\\\Context\\:\\:escape\\(\\) expects string, string\\|null given\\.$#"
3025
count: 1
@@ -256,48 +251,58 @@ parameters:
256251
path: src/Parsers/Limits.php
257252

258253
-
259-
message: "#^Cannot access offset 'equals' on mixed\\.$#"
260-
count: 1
254+
message: "#^Cannot access offset 1 on mixed\\.$#"
255+
count: 2
261256
path: src/Parsers/OptionsArrays.php
262257

263258
-
264-
message: "#^Cannot access offset 'expr' on mixed\\.$#"
265-
count: 2
259+
message: "#^Cannot access offset 2 on mixed\\.$#"
260+
count: 1
266261
path: src/Parsers/OptionsArrays.php
267262

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

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

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

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

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

293288
-
294-
message: "#^Parameter \\#3 \\$options of static method PhpMyAdmin\\\\SqlParser\\\\Parsers\\\\Expressions\\:\\:parse\\(\\) expects array\\<string, mixed\\>, mixed given\\.$#"
289+
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\\>\\.$#"
290+
count: 2
291+
path: src/Parsers/OptionsArrays.php
292+
293+
-
294+
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\\>\\.$#"
295295
count: 1
296296
path: src/Parsers/OptionsArrays.php
297297

298298
-
299-
message: "#^Property PhpMyAdmin\\\\SqlParser\\\\Components\\\\OptionsArray\\:\\:\\$options \\(array\\<int, mixed\\>\\) does not accept array\\<int\\|string, mixed\\>\\.$#"
300-
count: 8
299+
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\\>\\.$#"
300+
count: 3
301+
path: src/Parsers/OptionsArrays.php
302+
303+
-
304+
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\\>\\.$#"
305+
count: 3
301306
path: src/Parsers/OptionsArrays.php
302307

303308
-
@@ -506,18 +511,18 @@ parameters:
506511
path: src/Statements/CreateStatement.php
507512

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

513518
-
514-
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\\.$#"
515-
count: 1
519+
message: "#^Cannot call method has\\(\\) on PhpMyAdmin\\\\SqlParser\\\\Components\\\\OptionsArray\\|null\\.$#"
520+
count: 9
516521
path: src/Statements/CreateStatement.php
517522

518523
-
519-
message: "#^Parameter \\#3 \\$subject of function str_replace expects array\\|string, mixed given\\.$#"
520-
count: 2
524+
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\\.$#"
525+
count: 1
521526
path: src/Statements/CreateStatement.php
522527

523528
-
@@ -896,7 +901,7 @@ parameters:
896901
path: src/Utils/Table.php
897902

898903
-
899-
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\\}\\>\\.$#"
904+
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\\}\\>\\.$#"
900905
count: 1
901906
path: src/Utils/Table.php
902907

‎psalm-baseline.xml

+18-57
Original file line numberDiff line numberDiff line change
@@ -86,27 +86,6 @@
8686
<code><![CDATA[$this->type]]></code>
8787
</PossiblyNullOperand>
8888
</file>
89-
<file src="src/Components/OptionsArray.php">
90-
<MixedArgument>
91-
<code><![CDATA[$option]]></code>
92-
<code><![CDATA[$option]]></code>
93-
<code><![CDATA[$option['name']]]></code>
94-
<code><![CDATA[$option['name']]]></code>
95-
</MixedArgument>
96-
<MixedArgumentTypeCoercion>
97-
<code><![CDATA[$options]]></code>
98-
</MixedArgumentTypeCoercion>
99-
<MixedAssignment>
100-
<code><![CDATA[$option]]></code>
101-
<code><![CDATA[$option]]></code>
102-
<code><![CDATA[$option]]></code>
103-
<code><![CDATA[$options[]]]></code>
104-
</MixedAssignment>
105-
<MixedOperand>
106-
<code><![CDATA[! empty($option['expr']) ? $option['expr'] : $option['value']]]></code>
107-
<code><![CDATA[$option['name']]]></code>
108-
</MixedOperand>
109-
</file>
11089
<file src="src/Components/OrderKeyword.php">
11190
<PossiblyNullOperand>
11291
<code><![CDATA[$this->expr]]></code>
@@ -498,25 +477,12 @@
498477
<MixedArgument>
499478
<code><![CDATA[$ret->options[$lastOptionId]['name']]]></code>
500479
<code><![CDATA[empty($lastOption[2]) ? [] : $lastOption[2]]]></code>
501-
<code><![CDATA[is_array($ret->options[$lastOptionId])
502-
? $ret->options[$lastOptionId]['name']
503-
: $ret->options[$lastOptionId]]]></code>
504480
</MixedArgument>
505481
<MixedArrayAccess>
506482
<code><![CDATA[$lastOption[1]]]></code>
507483
<code><![CDATA[$lastOption[1]]]></code>
508484
<code><![CDATA[$lastOption[2]]]></code>
509-
<code><![CDATA[$ret->options[$lastOptionId]['expr']]]></code>
510-
<code><![CDATA[$ret->options[$lastOptionId]['name']]]></code>
511-
<code><![CDATA[$ret->options[$lastOptionId]['value']]]></code>
512485
</MixedArrayAccess>
513-
<MixedArrayAssignment>
514-
<code><![CDATA[$ret->options[$lastOptionId]['equals']]]></code>
515-
<code><![CDATA[$ret->options[$lastOptionId]['expr']]]></code>
516-
<code><![CDATA[$ret->options[$lastOptionId]['expr']]]></code>
517-
<code><![CDATA[$ret->options[$lastOptionId]['value']]]></code>
518-
<code><![CDATA[$ret->options[$lastOptionId]['value']]]></code>
519-
</MixedArrayAssignment>
520486
<MixedArrayOffset>
521487
<code><![CDATA[$ret->options[$lastOptionId]]]></code>
522488
<code><![CDATA[$ret->options[$lastOptionId]]]></code>
@@ -529,6 +495,7 @@
529495
<code><![CDATA[$ret->options[$lastOptionId]]]></code>
530496
<code><![CDATA[$ret->options[$lastOptionId]]]></code>
531497
<code><![CDATA[$ret->options[$lastOptionId]]]></code>
498+
<code><![CDATA[$ret->options[$lastOptionId]]]></code>
532499
</MixedArrayOffset>
533500
<MixedArrayTypeCoercion>
534501
<code><![CDATA[$ret->options[$lastOptionId]]]></code>
@@ -549,19 +516,30 @@
549516
<code><![CDATA[$ret->options]]></code>
550517
<code><![CDATA[$ret->options]]></code>
551518
<code><![CDATA[$ret->options]]></code>
552-
<code><![CDATA[$ret->options]]></code>
553-
<code><![CDATA[$ret->options]]></code>
554519
</MixedPropertyTypeCoercion>
520+
<PossiblyInvalidArrayOffset>
521+
<code><![CDATA[$ret->options[$lastOptionId]['equals']]]></code>
522+
<code><![CDATA[$ret->options[$lastOptionId]['expr']]]></code>
523+
<code><![CDATA[$ret->options[$lastOptionId]['expr']]]></code>
524+
<code><![CDATA[$ret->options[$lastOptionId]['expr']]]></code>
525+
<code><![CDATA[$ret->options[$lastOptionId]['expr']]]></code>
526+
<code><![CDATA[$ret->options[$lastOptionId]['name']]]></code>
527+
<code><![CDATA[$ret->options[$lastOptionId]['value']]]></code>
528+
<code><![CDATA[$ret->options[$lastOptionId]['value']]]></code>
529+
<code><![CDATA[$ret->options[$lastOptionId]['value']]]></code>
530+
</PossiblyInvalidArrayOffset>
555531
<PossiblyNullArgument>
556532
<code><![CDATA[empty($lastOption[2]) ? [] : $lastOption[2]]]></code>
557533
</PossiblyNullArgument>
558534
<PossiblyNullArrayAccess>
559535
<code><![CDATA[$lastOption[1]]]></code>
560536
<code><![CDATA[$lastOption[2]]]></code>
561537
</PossiblyNullArrayAccess>
562-
<PossiblyNullPropertyFetch>
563-
<code><![CDATA[$ret->options[$lastOptionId]['expr']->expr]]></code>
564-
</PossiblyNullPropertyFetch>
538+
<PropertyTypeCoercion>
539+
<code><![CDATA[$ret->options]]></code>
540+
<code><![CDATA[$ret->options]]></code>
541+
<code><![CDATA[$ret->options]]></code>
542+
</PropertyTypeCoercion>
565543
<RiskyTruthyFalsyComparison>
566544
<code><![CDATA[$lastOption]]></code>
567545
<code><![CDATA[empty($lastOption[2])]]></code>
@@ -679,25 +657,15 @@
679657
</file>
680658
<file src="src/Statements/CreateStatement.php">
681659
<MixedArgument>
682-
<code><![CDATA[$opt]]></code>
683-
<code><![CDATA[$opt]]></code>
684660
<code><![CDATA[$this->$field]]></code>
685661
</MixedArgument>
686-
<MixedAssignment>
687-
<code><![CDATA[$opt]]></code>
688-
<code><![CDATA[$opt]]></code>
689-
</MixedAssignment>
690662
<MixedOperand>
691663
<code><![CDATA[$this->$field]]></code>
692664
</MixedOperand>
693665
<PossiblyFalseOperand>
694666
<code><![CDATA[$brackets]]></code>
695667
<code><![CDATA[$brackets]]></code>
696668
</PossiblyFalseOperand>
697-
<PossiblyInvalidPropertyAssignmentValue>
698-
<code><![CDATA[str_replace(' ', '_', $opt)]]></code>
699-
<code><![CDATA[str_replace(' ', '_', $opt)]]></code>
700-
</PossiblyInvalidPropertyAssignmentValue>
701669
<PossiblyNullArgument>
702670
<code><![CDATA[$this->parameters]]></code>
703671
</PossiblyNullArgument>
@@ -723,7 +691,7 @@
723691
<code><![CDATA[build]]></code>
724692
<code><![CDATA[build]]></code>
725693
<code><![CDATA[build]]></code>
726-
<code><![CDATA[has]]></code>
694+
<code><![CDATA[get]]></code>
727695
<code><![CDATA[has]]></code>
728696
<code><![CDATA[has]]></code>
729697
<code><![CDATA[has]]></code>
@@ -1191,13 +1159,6 @@
11911159
<LessSpecificReturnStatement>
11921160
<code><![CDATA[$ret]]></code>
11931161
</LessSpecificReturnStatement>
1194-
<MixedAssignment>
1195-
<code><![CDATA[$option]]></code>
1196-
<code><![CDATA[$option]]></code>
1197-
<code><![CDATA[$option]]></code>
1198-
<code><![CDATA[$ret[$field->name]['default_value']]]></code>
1199-
<code><![CDATA[$ret[$field->name]['expr']]]></code>
1200-
</MixedAssignment>
12011162
<MoreSpecificReturnType>
12021163
<code><![CDATA[array<string, array{
12031164
* type: string,

‎src/Components/OptionsArray.php

+36-19
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,25 @@
1111
use function is_array;
1212
use function strcasecmp;
1313

14-
/**
15-
* Parses a list of options.
16-
*/
1714
final class OptionsArray implements Component
1815
{
1916
/**
20-
* @param array<int, mixed> $options The array of options. Options that have a value
21-
* must be an array with at least two keys `name` and
22-
* `expr` or `value`.
17+
* @param array<int, string|array<string, string|bool|null>> $options $options The array of options.
18+
* Options that have a value must be an array with at least two keys `name` and `expr` or `value`.
19+
* @psalm-param array<int, string|array{
20+
* name: string,
21+
* equals: bool,
22+
* expr: string|Expression,
23+
* value: string|null
24+
* }> $options
2325
*/
2426
public function __construct(public array $options = [])
2527
{
2628
}
2729

2830
public function build(): string
2931
{
30-
if (empty($this->options)) {
32+
if ($this->options === []) {
3133
return '';
3234
}
3335

@@ -37,34 +39,49 @@ public function build(): string
3739
$options[] = $option;
3840
} else {
3941
$options[] = $option['name']
40-
. (! empty($option['equals']) ? '=' : ' ')
41-
. (! empty($option['expr']) ? $option['expr'] : $option['value']);
42+
. ($option['equals'] ? '=' : ' ')
43+
. ($option['expr'] !== '' ? $option['expr'] : ($option['value'] ?? ''));
4244
}
4345
}
4446

4547
return implode(' ', $options);
4648
}
4749

50+
public function has(string $key): bool
51+
{
52+
foreach ($this->options as $option) {
53+
if (is_array($option)) {
54+
if (strcasecmp($key, $option['name']) === 0) {
55+
return ($option['value'] ?? '') !== '';
56+
}
57+
} elseif (strcasecmp($key, $option) === 0) {
58+
return true;
59+
}
60+
}
61+
62+
return false;
63+
}
64+
4865
/**
49-
* Checks if it has the specified option and returns it value or true.
66+
* Checks if it has the specified option and returns its value.
5067
*
5168
* @param string $key the key to be checked
5269
* @param bool $getExpr Gets the expression instead of the value.
5370
* The value is the processed form of the expression.
5471
*/
55-
public function has(string $key, bool $getExpr = false): mixed
72+
public function get(string $key, bool $getExpr = false): string|Expression
5673
{
5774
foreach ($this->options as $option) {
5875
if (is_array($option)) {
59-
if (! strcasecmp($key, $option['name'])) {
60-
return $getExpr ? $option['expr'] : $option['value'];
76+
if (strcasecmp($key, $option['name']) === 0) {
77+
return $getExpr ? $option['expr'] : ($option['value'] ?? '');
6178
}
62-
} elseif (! strcasecmp($key, $option)) {
63-
return true;
79+
} elseif (strcasecmp($key, $option) === 0) {
80+
return $option;
6481
}
6582
}
6683

67-
return false;
84+
return '';
6885
}
6986

7087
/**
@@ -78,12 +95,12 @@ public function remove(string $key): bool
7895
{
7996
foreach ($this->options as $idx => $option) {
8097
if (is_array($option)) {
81-
if (! strcasecmp($key, $option['name'])) {
98+
if (strcasecmp($key, $option['name']) === 0) {
8299
unset($this->options[$idx]);
83100

84101
return true;
85102
}
86-
} elseif (! strcasecmp($key, $option)) {
103+
} elseif (strcasecmp($key, $option) === 0) {
87104
unset($this->options[$idx]);
88105

89106
return true;
@@ -107,7 +124,7 @@ public function merge(OptionsArray $options): void
107124
*/
108125
public function isEmpty(): bool
109126
{
110-
return empty($this->options);
127+
return $this->options === [];
111128
}
112129

113130
public function __toString(): string

0 commit comments

Comments
 (0)