@@ -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 ();
@@ -286,7 +260,7 @@ public function parse(Parser $parser, TokensList $list): void
286260 $ options = [];
287261
288262 // Looking for duplicated clauses.
289- if (! empty (Parser::KEYWORD_PARSERS [$ token ->value ]) || ! empty (Parser::STATEMENT_PARSERS [$ token ->value ])) {
263+ if (isset (Parser::KEYWORD_PARSERS [$ token ->value ]) || ! empty (Parser::STATEMENT_PARSERS [$ token ->value ])) {
290264 if (! empty ($ parsedClauses [$ token ->value ])) {
291265 $ parser ->error ('This type of clause was previously parsed. ' , $ token );
292266 break ;
@@ -300,7 +274,7 @@ public function parse(Parser $parser, TokensList $list): void
300274 // but it might be the beginning of a statement of truncate,
301275 // so let the value use the keyword field for truncate type.
302276 $ tokenValue = in_array ($ token ->keyword , ['TRUNCATE ' ]) ? $ token ->keyword : $ token ->value ;
303- if (! empty (Parser::KEYWORD_PARSERS [$ tokenValue ]) && $ list ->idx < $ list ->count ) {
277+ if (isset (Parser::KEYWORD_PARSERS [$ tokenValue ]) && $ list ->idx < $ list ->count ) {
304278 $ class = Parser::KEYWORD_PARSERS [$ tokenValue ]['class ' ];
305279 $ field = Parser::KEYWORD_PARSERS [$ tokenValue ]['field ' ];
306280 if (! empty (Parser::KEYWORD_PARSERS [$ tokenValue ]['options ' ])) {
@@ -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 {
0 commit comments