Skip to content

Commit fbaf831

Browse files
committed
Improve documentation of getClauses()
Signed-off-by: Kamil Tekiela <tekiela246@gmail.com>
1 parent f1dc58d commit fbaf831

7 files changed

Lines changed: 80 additions & 114 deletions

File tree

src/Statement.php

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

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

@@ -115,29 +110,7 @@ public function build(): string
115110
*/
116111
$built = [];
117112

118-
/**
119-
* Statement's clauses.
120-
*/
121-
$clauses = $this->getClauses();
122-
123-
foreach ($clauses as $clause) {
124-
/**
125-
* The name of the clause.
126-
*/
127-
$name = $clause[0];
128-
129-
/**
130-
* The type of the clause.
131-
*
132-
* @see Statement::$clauses
133-
*/
134-
$type = $clause[1];
135-
136-
/**
137-
* The builder (parser) of this clause.
138-
*/
139-
$class = Parser::KEYWORD_PARSERS[$name]['class'];
140-
113+
foreach ($this->getClauses() as [$name, $type]) {
141114
/**
142115
* The name of the field that is used as source for the builder.
143116
* Same field is used to store the result of parsing.
@@ -150,7 +123,7 @@ public function build(): string
150123
}
151124

152125
// Checking if this field was already built.
153-
if ($type & 1) {
126+
if ($type & self::ADD_CLAUSE) {
154127
if (! empty($built[$field])) {
155128
continue;
156129
}
@@ -159,16 +132,17 @@ public function build(): string
159132
}
160133

161134
// Checking if the name of the clause should be added.
162-
if ($type & 2) {
135+
if ($type & self::ADD_KEYWORD) {
163136
$query = trim($query) . ' ' . $name;
164137
}
165138

166139
// Checking if the result of the builder should be added.
167-
if (! ($type & 1)) {
140+
if (! ($type & self::ADD_CLAUSE)) {
168141
continue;
169142
}
170143

171144
if (is_array($this->$field)) {
145+
$class = Parser::KEYWORD_PARSERS[$name]['class'];
172146
$query = trim($query) . ' ' . $class::buildAll($this->$field);
173147
} else {
174148
$query = trim($query) . ' ' . $this->$field->build();
@@ -419,8 +393,7 @@ public function after(Parser $parser, TokensList $list, Token $token): void
419393
/**
420394
* Gets the clauses of this statement.
421395
*
422-
* @return array<string, array<int, int|string>>
423-
* @psalm-return array<string, array{non-empty-string, (1|2|3)}>
396+
* @return array<string, array{non-empty-string, int-mask-of<Statement::ADD_*>}>
424397
*/
425398
public function getClauses(): array
426399
{

src/Statements/DeleteStatement.php

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -62,42 +62,41 @@ class DeleteStatement extends Statement
6262
*
6363
* @see Statement::$clauses
6464
*
65-
* @var array<string, array<int, int|string>>
66-
* @psalm-var array<string, array{non-empty-string, (1|2|3)}>
65+
* @var array<string, array{non-empty-string, int-mask-of<self::ADD_*>}>
6766
*/
6867
public static array $clauses = [
6968
'DELETE' => [
7069
'DELETE',
71-
2,
70+
Statement::ADD_KEYWORD,
7271
],
7372
// Used for options.
7473
'_OPTIONS' => [
7574
'_OPTIONS',
76-
1,
75+
Statement::ADD_CLAUSE,
7776
],
7877
'FROM' => [
7978
'FROM',
80-
3,
79+
Statement::ADD_CLAUSE | Statement::ADD_KEYWORD,
8180
],
8281
'PARTITION' => [
8382
'PARTITION',
84-
3,
83+
Statement::ADD_CLAUSE | Statement::ADD_KEYWORD,
8584
],
8685
'USING' => [
8786
'USING',
88-
3,
87+
Statement::ADD_CLAUSE | Statement::ADD_KEYWORD,
8988
],
9089
'WHERE' => [
9190
'WHERE',
92-
3,
91+
Statement::ADD_CLAUSE | Statement::ADD_KEYWORD,
9392
],
9493
'ORDER BY' => [
9594
'ORDER BY',
96-
3,
95+
Statement::ADD_CLAUSE | Statement::ADD_KEYWORD,
9796
],
9897
'LIMIT' => [
9998
'LIMIT',
100-
3,
99+
Statement::ADD_CLAUSE | Statement::ADD_KEYWORD,
101100
],
102101
];
103102

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

src/Statements/SelectStatement.php

Lines changed: 38 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -83,144 +83,143 @@ class SelectStatement extends Statement
8383
*
8484
* @see Statement::$clauses
8585
*
86-
* @var array<string, array<int, int|string>>
87-
* @psalm-var array<string, array{non-empty-string, (1|2|3)}>
86+
* @var array<string, array{non-empty-string, int-mask-of<self::ADD_*>}>
8887
*/
8988
public static array $clauses = [
9089
'SELECT' => [
9190
'SELECT',
92-
2,
91+
Statement::ADD_KEYWORD,
9392
],
9493
// Used for options.
9594
'_OPTIONS' => [
9695
'_OPTIONS',
97-
1,
96+
Statement::ADD_CLAUSE,
9897
],
9998
// Used for selected expressions.
10099
'_SELECT' => [
101100
'SELECT',
102-
1,
101+
Statement::ADD_CLAUSE,
103102
],
104103
'INTO' => [
105104
'INTO',
106-
3,
105+
Statement::ADD_CLAUSE | Statement::ADD_KEYWORD,
107106
],
108107
'FROM' => [
109108
'FROM',
110-
3,
109+
Statement::ADD_CLAUSE | Statement::ADD_KEYWORD,
111110
],
112111
'FORCE' => [
113112
'FORCE',
114-
1,
113+
Statement::ADD_CLAUSE,
115114
],
116115
'USE' => [
117116
'USE',
118-
1,
117+
Statement::ADD_CLAUSE,
119118
],
120119
'IGNORE' => [
121120
'IGNORE',
122-
3,
121+
Statement::ADD_CLAUSE | Statement::ADD_KEYWORD,
123122
],
124123
'PARTITION' => [
125124
'PARTITION',
126-
3,
125+
Statement::ADD_CLAUSE | Statement::ADD_KEYWORD,
127126
],
128127

129128
'JOIN' => [
130129
'JOIN',
131-
1,
130+
Statement::ADD_CLAUSE,
132131
],
133132
'FULL JOIN' => [
134133
'FULL JOIN',
135-
1,
134+
Statement::ADD_CLAUSE,
136135
],
137136
'INNER JOIN' => [
138137
'INNER JOIN',
139-
1,
138+
Statement::ADD_CLAUSE,
140139
],
141140
'LEFT JOIN' => [
142141
'LEFT JOIN',
143-
1,
142+
Statement::ADD_CLAUSE,
144143
],
145144
'LEFT OUTER JOIN' => [
146145
'LEFT OUTER JOIN',
147-
1,
146+
Statement::ADD_CLAUSE,
148147
],
149148
'RIGHT JOIN' => [
150149
'RIGHT JOIN',
151-
1,
150+
Statement::ADD_CLAUSE,
152151
],
153152
'RIGHT OUTER JOIN' => [
154153
'RIGHT OUTER JOIN',
155-
1,
154+
Statement::ADD_CLAUSE,
156155
],
157156
'NATURAL JOIN' => [
158157
'NATURAL JOIN',
159-
1,
158+
Statement::ADD_CLAUSE,
160159
],
161160
'NATURAL LEFT JOIN' => [
162161
'NATURAL LEFT JOIN',
163-
1,
162+
Statement::ADD_CLAUSE,
164163
],
165164
'NATURAL RIGHT JOIN' => [
166165
'NATURAL RIGHT JOIN',
167-
1,
166+
Statement::ADD_CLAUSE,
168167
],
169168
'NATURAL LEFT OUTER JOIN' => [
170169
'NATURAL LEFT OUTER JOIN',
171-
1,
170+
Statement::ADD_CLAUSE,
172171
],
173172
'NATURAL RIGHT OUTER JOIN' => [
174173
'NATURAL RIGHT JOIN',
175-
1,
174+
Statement::ADD_CLAUSE,
176175
],
177176
'WHERE' => [
178177
'WHERE',
179-
3,
178+
Statement::ADD_CLAUSE | Statement::ADD_KEYWORD,
180179
],
181180
'GROUP BY' => [
182181
'GROUP BY',
183-
3,
182+
Statement::ADD_CLAUSE | Statement::ADD_KEYWORD,
184183
],
185184
'_GROUP_OPTIONS' => [
186185
'_GROUP_OPTIONS',
187-
1,
186+
Statement::ADD_CLAUSE,
188187
],
189188
'HAVING' => [
190189
'HAVING',
191-
3,
190+
Statement::ADD_CLAUSE | Statement::ADD_KEYWORD,
192191
],
193192
'ORDER BY' => [
194193
'ORDER BY',
195-
3,
194+
Statement::ADD_CLAUSE | Statement::ADD_KEYWORD,
196195
],
197196
'LIMIT' => [
198197
'LIMIT',
199-
3,
198+
Statement::ADD_CLAUSE | Statement::ADD_KEYWORD,
200199
],
201200
'PROCEDURE' => [
202201
'PROCEDURE',
203-
3,
202+
Statement::ADD_CLAUSE | Statement::ADD_KEYWORD,
204203
],
205204
'UNION' => [
206205
'UNION',
207-
1,
206+
Statement::ADD_CLAUSE,
208207
],
209208
'EXCEPT' => [
210209
'EXCEPT',
211-
1,
210+
Statement::ADD_CLAUSE,
212211
],
213212
'INTERSECT' => [
214213
'INTERSECT',
215-
1,
214+
Statement::ADD_CLAUSE,
216215
],
217216
'_END_OPTIONS' => [
218217
'_END_OPTIONS',
219-
1,
218+
Statement::ADD_CLAUSE,
220219
],
221220
// These are available only when `UNION` is present.
222-
// 'ORDER BY' => ['ORDER BY', 3],
223-
// 'LIMIT' => ['LIMIT', 3],
221+
// 'ORDER BY' => ['ORDER BY', Statement::ADD_CLAUSE|Statement::ADD_KEYWORD],
222+
// 'LIMIT' => ['LIMIT', Statement::ADD_CLAUSE|Statement::ADD_KEYWORD],
224223
];
225224

226225
/**
@@ -321,8 +320,7 @@ class SelectStatement extends Statement
321320
/**
322321
* Gets the clauses of this statement.
323322
*
324-
* @return array<string, array<int, int|string>>
325-
* @psalm-return array<string, array{non-empty-string, (1|2|3)}>
323+
* @return array<string, array{non-empty-string, int-mask-of<Statement::ADD_*>}>
326324
*/
327325
public function getClauses(): array
328326
{
@@ -334,11 +332,11 @@ public function getClauses(): array
334332
unset($clauses['ORDER BY'], $clauses['LIMIT']);
335333
$clauses['ORDER BY'] = [
336334
'ORDER BY',
337-
3,
335+
Statement::ADD_CLAUSE | Statement::ADD_KEYWORD,
338336
];
339337
$clauses['LIMIT'] = [
340338
'LIMIT',
341-
3,
339+
Statement::ADD_CLAUSE | Statement::ADD_KEYWORD,
342340
];
343341

344342
return $clauses;

0 commit comments

Comments
 (0)