Skip to content

Commit 3ed9cdd

Browse files
committed
Fix some issue reported by Psalm
Signed-off-by: Maurício Meneghini Fauth <mauricio@fauth.dev>
1 parent e2a84a8 commit 3ed9cdd

7 files changed

Lines changed: 91 additions & 63 deletions

File tree

phpstan-baseline.neon

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1060,11 +1060,6 @@ parameters:
10601060
count: 1
10611061
path: src/Parser.php
10621062

1063-
-
1064-
message: "#^Property PhpMyAdmin\\\\SqlParser\\\\Statements\\\\SelectStatement\\:\\:\\$limit \\(PhpMyAdmin\\\\SqlParser\\\\Components\\\\Limit\\) does not accept null\\.$#"
1065-
count: 1
1066-
path: src/Parser.php
1067-
10681063
-
10691064
message: "#^Cannot access property \\$type on PhpMyAdmin\\\\SqlParser\\\\Token\\|null\\.$#"
10701065
count: 1
@@ -1730,21 +1725,6 @@ parameters:
17301725
count: 1
17311726
path: src/Utils/Query.php
17321727

1733-
-
1734-
message: "#^Method PhpMyAdmin\\\\SqlParser\\\\Utils\\\\Query\\:\\:getFlags\\(\\) return type has no value type specified in iterable type array\\.$#"
1735-
count: 1
1736-
path: src/Utils/Query.php
1737-
1738-
-
1739-
message: "#^Method PhpMyAdmin\\\\SqlParser\\\\Utils\\\\Query\\:\\:getFlagsSelect\\(\\) has parameter \\$flags with no value type specified in iterable type array\\.$#"
1740-
count: 1
1741-
path: src/Utils/Query.php
1742-
1743-
-
1744-
message: "#^Method PhpMyAdmin\\\\SqlParser\\\\Utils\\\\Query\\:\\:getFlagsSelect\\(\\) return type has no value type specified in iterable type array\\.$#"
1745-
count: 1
1746-
path: src/Utils/Query.php
1747-
17481728
-
17491729
message: "#^Method PhpMyAdmin\\\\SqlParser\\\\Utils\\\\Query\\:\\:getTables\\(\\) return type has no value type specified in iterable type array\\.$#"
17501730
count: 1
@@ -1795,11 +1775,6 @@ parameters:
17951775
count: 1
17961776
path: src/Utils/Query.php
17971777

1798-
-
1799-
message: "#^Property PhpMyAdmin\\\\SqlParser\\\\Statements\\\\SelectStatement\\:\\:\\$procedure \\(PhpMyAdmin\\\\SqlParser\\\\Components\\\\FunctionCall\\) in empty\\(\\) is not falsy\\.$#"
1800-
count: 1
1801-
path: src/Utils/Query.php
1802-
18031778
-
18041779
message: "#^Method PhpMyAdmin\\\\SqlParser\\\\Utils\\\\Routine\\:\\:getParameter\\(\\) return type has no value type specified in iterable type array\\.$#"
18051780
count: 1
@@ -2395,19 +2370,14 @@ parameters:
23952370
count: 1
23962371
path: tests/Utils/MiscTest.php
23972372

2398-
-
2399-
message: "#^Method PhpMyAdmin\\\\SqlParser\\\\Tests\\\\Utils\\\\QueryTest\\:\\:getFlagsProvider\\(\\) return type has no value type specified in iterable type array\\.$#"
2400-
count: 1
2401-
path: tests/Utils/QueryTest.php
2402-
24032373
-
24042374
message: "#^Method PhpMyAdmin\\\\SqlParser\\\\Tests\\\\Utils\\\\QueryTest\\:\\:getTablesProvider\\(\\) return type has no value type specified in iterable type array\\.$#"
24052375
count: 1
24062376
path: tests/Utils/QueryTest.php
24072377

24082378
-
24092379
message: "#^Parameter \\#1 \\$list of class PhpMyAdmin\\\\SqlParser\\\\Parser constructor expects PhpMyAdmin\\\\SqlParser\\\\TokensList\\|PhpMyAdmin\\\\SqlParser\\\\UtfString\\|string\\|null, mixed given\\.$#"
2410-
count: 2
2380+
count: 1
24112381
path: tests/Utils/QueryTest.php
24122382

24132383
-

psalm-baseline.xml

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -905,9 +905,6 @@
905905
<ParamNameMismatch occurrences="1">
906906
<code>$msg</code>
907907
</ParamNameMismatch>
908-
<PossiblyNullPropertyAssignmentValue occurrences="1">
909-
<code>null</code>
910-
</PossiblyNullPropertyAssignmentValue>
911908
<PropertyNotSetInConstructor occurrences="1">
912909
<code>$list</code>
913910
</PropertyNotSetInConstructor>
@@ -1083,23 +1080,21 @@
10831080
<PossiblyNullPropertyAssignmentValue occurrences="1">
10841081
<code>null</code>
10851082
</PossiblyNullPropertyAssignmentValue>
1086-
<PropertyNotSetInConstructor occurrences="10">
1083+
<PropertyNotSetInConstructor occurrences="9">
10871084
<code>$columns</code>
10881085
<code>$from</code>
10891086
<code>$join</code>
1090-
<code>$limit</code>
10911087
<code>$order</code>
10921088
<code>$partition</code>
10931089
<code>$using</code>
10941090
<code>$where</code>
10951091
<code>DeleteStatement</code>
10961092
<code>DeleteStatement</code>
10971093
</PropertyNotSetInConstructor>
1098-
<RedundantConditionGivenDocblockType occurrences="7">
1094+
<RedundantConditionGivenDocblockType occurrences="6">
10991095
<code>$this-&gt;columns !== null</code>
11001096
<code>$this-&gt;from !== null</code>
11011097
<code>$this-&gt;join !== null</code>
1102-
<code>$this-&gt;limit !== null</code>
11031098
<code>$this-&gt;order !== null</code>
11041099
<code>$this-&gt;using !== null</code>
11051100
<code>$this-&gt;where !== null</code>
@@ -1284,17 +1279,15 @@
12841279
<NonInvariantDocblockPropertyType occurrences="1">
12851280
<code>$END_OPTIONS</code>
12861281
</NonInvariantDocblockPropertyType>
1287-
<PropertyNotSetInConstructor occurrences="13">
1282+
<PropertyNotSetInConstructor occurrences="11">
12881283
<code>$end_options</code>
12891284
<code>$group</code>
12901285
<code>$having</code>
12911286
<code>$index_hints</code>
12921287
<code>$into</code>
12931288
<code>$join</code>
1294-
<code>$limit</code>
12951289
<code>$order</code>
12961290
<code>$partition</code>
1297-
<code>$procedure</code>
12981291
<code>$where</code>
12991292
<code>SelectStatement</code>
13001293
<code>SelectStatement</code>
@@ -1338,8 +1331,7 @@
13381331
</PropertyNotSetInConstructor>
13391332
</file>
13401333
<file src="src/Statements/UpdateStatement.php">
1341-
<PropertyNotSetInConstructor occurrences="7">
1342-
<code>$limit</code>
1334+
<PropertyNotSetInConstructor occurrences="6">
13431335
<code>$order</code>
13441336
<code>$set</code>
13451337
<code>$tables</code>
@@ -2161,7 +2153,7 @@
21612153
</PossiblyNullArgument>
21622154
</file>
21632155
<file src="tests/Utils/QueryTest.php">
2164-
<MixedArgument occurrences="10">
2156+
<MixedArgument occurrences="9">
21652157
<code>$delimiter</code>
21662158
<code>$delimiter</code>
21672159
<code>$delimiter</code>
@@ -2171,10 +2163,8 @@
21712163
<code>$query</code>
21722164
<code>$query</code>
21732165
<code>$query</code>
2174-
<code>$query</code>
21752166
</MixedArgument>
2176-
<MixedInferredReturnType occurrences="2">
2177-
<code>array</code>
2167+
<MixedInferredReturnType occurrences="1">
21782168
<code>array</code>
21792169
</MixedInferredReturnType>
21802170
<UnusedVariable occurrences="3">

src/Statements/DeleteStatement.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ class DeleteStatement extends Statement
155155
/**
156156
* Conditions used for limiting the size of the result set.
157157
*
158-
* @var Limit
158+
* @var Limit|null
159159
*/
160160
public $limit;
161161

src/Statements/SelectStatement.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -279,14 +279,14 @@ class SelectStatement extends Statement
279279
/**
280280
* Conditions used for limiting the size of the result set.
281281
*
282-
* @var Limit
282+
* @var Limit|null
283283
*/
284284
public $limit;
285285

286286
/**
287287
* Procedure that should process the data in the result set.
288288
*
289-
* @var FunctionCall
289+
* @var FunctionCall|null
290290
*/
291291
public $procedure;
292292

src/Statements/UpdateStatement.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ class UpdateStatement extends Statement
112112
/**
113113
* Conditions used for limiting the size of the result set.
114114
*
115-
* @var Limit
115+
* @var Limit|null
116116
*/
117117
public $limit;
118118
}

src/Utils/Query.php

Lines changed: 69 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,36 @@
4343

4444
/**
4545
* Statement utilities.
46+
*
47+
* @psalm-type QueryFlagsType = array{
48+
* distinct?: bool,
49+
* drop_database?: bool,
50+
* group?: bool,
51+
* having?: bool,
52+
* is_affected?: bool,
53+
* is_analyse?: bool,
54+
* is_count?: bool,
55+
* is_delete?: bool,
56+
* is_explain?: bool,
57+
* is_export?: bool,
58+
* is_func?: bool,
59+
* is_group?: bool,
60+
* is_insert?: bool,
61+
* is_maint?: bool,
62+
* is_procedure?: bool,
63+
* is_replace?: bool,
64+
* is_select?: bool,
65+
* is_show?: bool,
66+
* is_subquery?: bool,
67+
* join?: bool,
68+
* limit?: bool,
69+
* offset?: bool,
70+
* order?: bool,
71+
* querytype: ('ALTER'|'ANALYZE'|'CALL'|'CHECK'|'CHECKSUM'|'CREATE'|'DELETE'|'DROP'|'EXPLAIN'|'INSERT'|'LOAD'|'OPTIMIZE'|'REPAIR'|'REPLACE'|'SELECT'|'SET'|'SHOW'|'UPDATE'|false),
72+
* reload?: bool,
73+
* select_from?: bool,
74+
* union?: bool
75+
* }
4676
*/
4777
class Query
4878
{
@@ -62,7 +92,38 @@ class Query
6292
'BIT_AND',
6393
];
6494

65-
/** @var array<string,false> */
95+
/**
96+
* @var array<string, false>
97+
* @psalm-var array{
98+
* distinct: false,
99+
* drop_database: false,
100+
* group: false,
101+
* having: false,
102+
* is_affected: false,
103+
* is_analyse: false,
104+
* is_count: false,
105+
* is_delete: false,
106+
* is_explain: false,
107+
* is_export: false,
108+
* is_func: false,
109+
* is_group: false,
110+
* is_insert: false,
111+
* is_maint: false,
112+
* is_procedure: false,
113+
* is_replace: false,
114+
* is_select: false,
115+
* is_show: false,
116+
* is_subquery: false,
117+
* join: false,
118+
* limit: false,
119+
* offset: false,
120+
* order: false,
121+
* querytype: false,
122+
* reload: false,
123+
* select_from: false,
124+
* union: false
125+
* }
126+
*/
66127
public static $ALLFLAGS = [
67128
/*
68129
* select ... DISTINCT ...
@@ -222,10 +283,12 @@ class Query
222283
/**
223284
* Gets an array with flags select statement has.
224285
*
225-
* @param SelectStatement $statement the statement to be processed
226-
* @param array $flags flags set so far
286+
* @param SelectStatement $statement the statement to be processed
287+
* @param array<string, bool|string> $flags flags set so far
288+
* @psalm-param QueryFlagsType $flags
227289
*
228-
* @return array
290+
* @return array<string, bool|string>
291+
* @psalm-return QueryFlagsType
229292
*/
230293
private static function getFlagsSelect($statement, $flags)
231294
{
@@ -300,7 +363,8 @@ private static function getFlagsSelect($statement, $flags)
300363
* @param Statement|null $statement the statement to be processed
301364
* @param bool $all if `false`, false values will not be included
302365
*
303-
* @return array
366+
* @return array<string, bool|string>
367+
* @psalm-return QueryFlagsType
304368
*/
305369
public static function getFlags($statement, $all = false)
306370
{

tests/Utils/QueryTest.php

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,27 @@
1010

1111
use function array_merge;
1212

13+
/**
14+
* @psalm-import-type QueryFlagsType from Query
15+
*/
1316
class QueryTest extends TestCase
1417
{
1518
/**
16-
* @param mixed $query
17-
* @param mixed $expected
19+
* @param array<string, bool|string> $expected
20+
* @psalm-param QueryFlagsType $expected
1821
*
1922
* @dataProvider getFlagsProvider
2023
*/
21-
public function testGetFlags($query, $expected): void
24+
public function testGetFlags(string $query, array $expected): void
2225
{
2326
$parser = new Parser($query);
24-
$this->assertEquals(
25-
$expected,
26-
Query::getFlags($parser->statements[0])
27-
);
27+
$this->assertEquals($expected, Query::getFlags($parser->statements[0]));
2828
}
2929

30+
/**
31+
* @return array<int, array<int, string|array<string, bool|string>>>
32+
* @psalm-return list<array{non-empty-string, QueryFlagsType}>
33+
*/
3034
public function getFlagsProvider(): array
3135
{
3236
return [

0 commit comments

Comments
 (0)