Skip to content

Commit 7ae60b5

Browse files
committed
Improve documentation of getClauses()
Signed-off-by: Kamil Tekiela <[email protected]>
1 parent 208f145 commit 7ae60b5

File tree

9 files changed

+97
-136
lines changed

9 files changed

+97
-136
lines changed

phpstan-baseline.neon

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -450,11 +450,6 @@ parameters:
450450
count: 1
451451
path: src/Parser.php
452452

453-
-
454-
message: "#^Call to an undefined static method PhpMyAdmin\\\\SqlParser\\\\Component\\:\\:buildAll\\(\\)\\.$#"
455-
count: 1
456-
path: src/Statement.php
457-
458453
-
459454
message: "#^Argument of an invalid type array\\<PhpMyAdmin\\\\SqlParser\\\\Components\\\\AlterOperation\\>\\|null supplied for foreach, only iterables are supported\\.$#"
460455
count: 1

psalm-baseline.xml

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -220,9 +220,6 @@
220220
<code>$expr</code>
221221
<code>$expr</code>
222222
</PossiblyNullArgument>
223-
<PossiblyUnusedMethod>
224-
<code>buildAll</code>
225-
</PossiblyUnusedMethod>
226223
</file>
227224
<file src="src/Components/Lists/GroupKeywords.php">
228225
<PossiblyNullPropertyAssignmentValue>
@@ -233,9 +230,6 @@
233230
<MixedAssignment>
234231
<code><![CDATA[$expr->type]]></code>
235232
</MixedAssignment>
236-
<PossiblyUnusedMethod>
237-
<code>buildAll</code>
238-
</PossiblyUnusedMethod>
239233
</file>
240234
<file src="src/Components/Lists/JoinKeywords.php">
241235
<MixedArrayOffset>
@@ -461,11 +455,6 @@
461455
<code>$old</code>
462456
</PossiblyNullPropertyAssignmentValue>
463457
</file>
464-
<file src="src/Components/UnionKeyword.php">
465-
<PossiblyUnusedMethod>
466-
<code>buildAll</code>
467-
</PossiblyUnusedMethod>
468-
</file>
469458
<file src="src/Context.php">
470459
<InvalidPropertyAssignmentValue>
471460
<code>[]</code>
@@ -675,6 +664,19 @@
675664
<code>$built[$field]</code>
676665
<code><![CDATA[$parsedClauses[$token->value]]]></code>
677666
</InvalidArgument>
667+
<MixedArgument>
668+
<code><![CDATA[$this->$field]]></code>
669+
<code><![CDATA[$this->$field]]></code>
670+
<code><![CDATA[$this->$field]]></code>
671+
<code><![CDATA[$this->$field]]></code>
672+
<code><![CDATA[$this->$field]]></code>
673+
<code><![CDATA[$this->$field]]></code>
674+
<code><![CDATA[$this->$field]]></code>
675+
<code><![CDATA[$this->$field]]></code>
676+
<code><![CDATA[$this->$field]]></code>
677+
<code><![CDATA[$this->$field]]></code>
678+
<code><![CDATA[$this->$field]]></code>
679+
</MixedArgument>
678680
<MixedArrayOffset>
679681
<code><![CDATA[$parsedClauses[$token->value]]]></code>
680682
<code><![CDATA[$parsedClauses[$token->value]]]></code>
@@ -699,6 +701,10 @@
699701
</PossiblyUnusedReturnValue>
700702
<UndefinedMethod>
701703
<code><![CDATA[$class::buildAll($this->$field)]]></code>
704+
<code><![CDATA[$class::buildAll($this->$field)]]></code>
705+
<code><![CDATA[$class::buildAll($this->$field)]]></code>
706+
<code><![CDATA[$class::buildAll($this->$field)]]></code>
707+
<code><![CDATA[$class::buildAll($this->$field)]]></code>
702708
</UndefinedMethod>
703709
<UnusedForeachValue>
704710
<code>$index</code>

src/Statement.php

Lines changed: 10 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -47,18 +47,13 @@ abstract class Statement implements Stringable
4747
*/
4848
public static array $statementOptions = [];
4949

50+
protected const ADD_CLAUSE = 1;
51+
protected const ADD_KEYWORD = 2;
52+
5053
/**
5154
* The clauses of this statement, in order.
5255
*
53-
* The value attributed to each clause is used by the builder and it may
54-
* have one of the following values:
55-
*
56-
* - 1 = 01 - add the clause only
57-
* - 2 = 10 - add the keyword
58-
* - 3 = 11 - add both the keyword and the clause
59-
*
60-
* @var array<string, array<int, int|string>>
61-
* @psalm-var array<string, array{non-empty-string, (1|2|3)}>
56+
* @var array<string, array{non-empty-string, int-mask-of<self::ADD_*>}>
6257
*/
6358
public static array $clauses = [];
6459

@@ -116,35 +111,7 @@ public function build(): string
116111
*/
117112
$built = [];
118113

119-
/**
120-
* Statement's clauses.
121-
*/
122-
$clauses = $this->getClauses();
123-
124-
foreach ($clauses as $clause) {
125-
/**
126-
* The name of the clause.
127-
*
128-
* @var string
129-
*/
130-
$name = $clause[0];
131-
132-
/**
133-
* The type of the clause.
134-
*
135-
* @see Statement::$clauses
136-
*
137-
* @var int
138-
*/
139-
$type = $clause[1];
140-
141-
/**
142-
* The builder (parser) of this clause.
143-
*
144-
* @var Component
145-
*/
146-
$class = Parser::KEYWORD_PARSERS[$name]['class'];
147-
114+
foreach ($this->getClauses() as [$name, $type]) {
148115
/**
149116
* The name of the field that is used as source for the builder.
150117
* Same field is used to store the result of parsing.
@@ -159,7 +126,7 @@ public function build(): string
159126
}
160127

161128
// Checking if this field was already built.
162-
if ($type & 1) {
129+
if ($type & self::ADD_CLAUSE) {
163130
if (! empty($built[$field])) {
164131
continue;
165132
}
@@ -168,16 +135,17 @@ public function build(): string
168135
}
169136

170137
// Checking if the name of the clause should be added.
171-
if ($type & 2) {
138+
if ($type & self::ADD_KEYWORD) {
172139
$query = trim($query) . ' ' . $name;
173140
}
174141

175142
// Checking if the result of the builder should be added.
176-
if (! ($type & 1)) {
143+
if (! ($type & self::ADD_CLAUSE)) {
177144
continue;
178145
}
179146

180147
if (is_array($this->$field)) {
148+
$class = Parser::KEYWORD_PARSERS[$name]['class'];
181149
$query = trim($query) . ' ' . $class::buildAll($this->$field);
182150
} else {
183151
$query = trim($query) . ' ' . $this->$field->build();
@@ -430,8 +398,7 @@ public function after(Parser $parser, TokensList $list, Token $token): void
430398
/**
431399
* Gets the clauses of this statement.
432400
*
433-
* @return array<string, array<int, int|string>>
434-
* @psalm-return array<string, array{non-empty-string, (1|2|3)}>
401+
* @return array<string, array{non-empty-string, int-mask-of<Statement::ADD_*>}>
435402
*/
436403
public function getClauses(): array
437404
{

src/Statements/DeleteStatement.php

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -65,42 +65,41 @@ class DeleteStatement extends Statement
6565
*
6666
* @see Statement::$clauses
6767
*
68-
* @var array<string, array<int, int|string>>
69-
* @psalm-var array<string, array{non-empty-string, (1|2|3)}>
68+
* @var array<string, array{non-empty-string, int-mask-of<self::ADD_*>}>
7069
*/
7170
public static array $clauses = [
7271
'DELETE' => [
7372
'DELETE',
74-
2,
73+
Statement::ADD_KEYWORD,
7574
],
7675
// Used for options.
7776
'_OPTIONS' => [
7877
'_OPTIONS',
79-
1,
78+
Statement::ADD_CLAUSE,
8079
],
8180
'FROM' => [
8281
'FROM',
83-
3,
82+
Statement::ADD_CLAUSE | Statement::ADD_KEYWORD,
8483
],
8584
'PARTITION' => [
8685
'PARTITION',
87-
3,
86+
Statement::ADD_CLAUSE | Statement::ADD_KEYWORD,
8887
],
8988
'USING' => [
9089
'USING',
91-
3,
90+
Statement::ADD_CLAUSE | Statement::ADD_KEYWORD,
9291
],
9392
'WHERE' => [
9493
'WHERE',
95-
3,
94+
Statement::ADD_CLAUSE | Statement::ADD_KEYWORD,
9695
],
9796
'ORDER BY' => [
9897
'ORDER BY',
99-
3,
98+
Statement::ADD_CLAUSE | Statement::ADD_KEYWORD,
10099
],
101100
'LIMIT' => [
102101
'LIMIT',
103-
3,
102+
Statement::ADD_CLAUSE | Statement::ADD_KEYWORD,
104103
],
105104
];
106105

src/Statements/DropStatement.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,27 +42,26 @@ class DropStatement extends Statement
4242
*
4343
* @see Statement::$clauses
4444
*
45-
* @var array<string, array<int, int|string>>
46-
* @psalm-var array<string, array{non-empty-string, (1|2|3)}>
45+
* @var array<string, array{non-empty-string, int-mask-of<self::ADD_*>}>
4746
*/
4847
public static array $clauses = [
4948
'DROP' => [
5049
'DROP',
51-
2,
50+
Statement::ADD_KEYWORD,
5251
],
5352
// Used for options.
5453
'_OPTIONS' => [
5554
'_OPTIONS',
56-
1,
55+
Statement::ADD_CLAUSE,
5756
],
5857
// Used for select expressions.
5958
'DROP_' => [
6059
'DROP',
61-
1,
60+
Statement::ADD_CLAUSE,
6261
],
6362
'ON' => [
6463
'ON',
65-
3,
64+
Statement::ADD_CLAUSE | Statement::ADD_KEYWORD,
6665
],
6766
];
6867

0 commit comments

Comments
 (0)