4
4
5
5
namespace PhpMyAdmin \SqlParser \Tools ;
6
6
7
+ use PhpMyAdmin \SqlParser \Token ;
8
+
7
9
use function array_map ;
8
10
use function array_merge ;
9
11
use function array_slice ;
15
17
use function implode ;
16
18
use function ksort ;
17
19
use function preg_match ;
18
- use function round ;
19
20
use function scandir ;
20
21
use function sort ;
21
22
use function sprintf ;
22
- use function str_repeat ;
23
23
use function str_replace ;
24
24
use function str_split ;
25
25
use function strlen ;
@@ -43,10 +43,10 @@ class ContextGenerator
43
43
* @var array<string, int>
44
44
*/
45
45
public static $ LABELS_FLAGS = [
46
- '(R) ' => 2 , // reserved
47
- '(D) ' => 8 , // data type
48
- '(K) ' => 16 , // keyword
49
- '(F) ' => 32 , // function name
46
+ '(R) ' => Token:: FLAG_KEYWORD_RESERVED ,
47
+ '(D) ' => Token:: FLAG_KEYWORD_DATA_TYPE ,
48
+ '(K) ' => Token:: FLAG_KEYWORD_KEY ,
49
+ '(F) ' => Token:: FLAG_KEYWORD_FUNCTION ,
50
50
];
51
51
52
52
/**
@@ -83,6 +83,20 @@ class ContextGenerator
83
83
'MariaDb110400 ' => 'https://mariadb.com/kb/en/reserved-words/ ' ,
84
84
];
85
85
86
+ /**
87
+ * Reversed const <=> int from {@see Token} class to write the constant name instead of its value.
88
+ *
89
+ * @var array<int, string>
90
+ */
91
+ private static $ typesNumToConst = [
92
+ 1 => 'Token::FLAG_KEYWORD ' ,
93
+ 2 => 'Token::FLAG_KEYWORD_RESERVED ' ,
94
+ 4 => 'Token::FLAG_KEYWORD_COMPOSED ' ,
95
+ 8 => 'Token::FLAG_KEYWORD_DATA_TYPE ' ,
96
+ 16 => 'Token::FLAG_KEYWORD_KEY ' ,
97
+ 32 => 'Token::FLAG_KEYWORD_FUNCTION ' ,
98
+ ];
99
+
86
100
/**
87
101
* The template of a context.
88
102
*
@@ -117,9 +131,7 @@ class %2$s extends Context
117
131
*
118
132
* The value associated to each keyword represents its flags.
119
133
*
120
- * @see Token::FLAG_KEYWORD_RESERVED Token::FLAG_KEYWORD_COMPOSED
121
- * Token::FLAG_KEYWORD_DATA_TYPE Token::FLAG_KEYWORD_KEY
122
- * Token::FLAG_KEYWORD_FUNCTION
134
+ * @see Token
123
135
*
124
136
* @var array<string,int>
125
137
* @phpstan-var non-empty-array<non-empty-string,Token::FLAG_KEYWORD_*|int>
@@ -140,12 +152,9 @@ class %2$s extends Context
140
152
public static function sortWords (array &$ arr )
141
153
{
142
154
ksort ($ arr );
143
- foreach ($ arr as &$ wordsByLen ) {
144
- ksort ($ wordsByLen );
145
- foreach ($ wordsByLen as &$ words ) {
146
- sort ($ words , SORT_STRING );
147
- }
148
- }
155
+ foreach ($ arr as &$ words ) {
156
+ sort ($ words , SORT_STRING );
157
+ } unset($ words );
149
158
150
159
return $ arr ;
151
160
}
@@ -159,17 +168,23 @@ public static function sortWords(array &$arr)
159
168
*/
160
169
public static function readWords (array $ files )
161
170
{
162
- $ words = [];
163
- foreach ($ files as $ file ) {
164
- $ words = array_merge ($ words , file ($ file , FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES ));
165
- }
171
+ $ wordsByFile = array_map (
172
+ static function (string $ file ): array {
173
+ return file ($ file , FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES );
174
+ },
175
+ $ files
176
+ );
177
+ $ words = array_merge (...$ wordsByFile );
166
178
167
179
/** @var array<string, int> $types */
168
180
$ types = [];
169
181
170
182
for ($ i = 0 , $ count = count ($ words ); $ i !== $ count ; ++$ i ) {
171
- $ type = 1 ;
172
183
$ value = trim ($ words [$ i ]);
184
+ if ($ value === '' ) {
185
+ continue ;
186
+ }
187
+ $ type = Token::FLAG_KEYWORD ;
173
188
174
189
// Reserved, data types, keys, functions, etc. keywords.
175
190
foreach (static ::$ LABELS_FLAGS as $ label => $ flags ) {
@@ -183,13 +198,8 @@ public static function readWords(array $files)
183
198
184
199
// Composed keyword.
185
200
if (strstr ($ value , ' ' ) !== false ) {
186
- $ type |= 2 ; // Reserved keyword.
187
- $ type |= 4 ; // Composed keyword.
188
- }
189
-
190
- $ len = strlen ($ words [$ i ]);
191
- if ($ len === 0 ) {
192
- continue ;
201
+ $ type |= Token::FLAG_KEYWORD_RESERVED ;
202
+ $ type |= Token::FLAG_KEYWORD_COMPOSED ;
193
203
}
194
204
195
205
$ value = strtoupper ($ value );
@@ -200,18 +210,10 @@ public static function readWords(array $files)
200
210
}
201
211
}
202
212
213
+ // Prepare an array in a way to sort by type, then by word.
203
214
$ ret = [];
204
215
foreach ($ types as $ word => $ type ) {
205
- $ len = strlen ($ word );
206
- if (! isset ($ ret [$ type ])) {
207
- $ ret [$ type ] = [];
208
- }
209
-
210
- if (! isset ($ ret [$ type ][$ len ])) {
211
- $ ret [$ type ][$ len ] = [];
212
- }
213
-
214
- $ ret [$ type ][$ len ][] = $ word ;
216
+ $ ret [$ type ][] = $ word ;
215
217
}
216
218
217
219
return static ::sortWords ($ ret );
@@ -220,53 +222,34 @@ public static function readWords(array $files)
220
222
/**
221
223
* Prints an array of a words in PHP format.
222
224
*
223
- * @param array<int, array<int, array<int, string>>> $words the list of words to be formatted
224
- * @param int $spaces the number of spaces that starts every line
225
- * @param int $line the length of a line
226
- *
227
- * @return string
225
+ * @param array<int, list<string>> $words the list of words to be formatted
228
226
*/
229
- public static function printWords ($ words , $ spaces = 8 , $ line = 140 )
227
+ public static function printWords (array $ words ): string
230
228
{
231
- $ typesCount = count ($ words );
232
229
$ ret = '' ;
233
- $ j = 0 ;
234
-
235
230
foreach ($ words as $ type => $ wordsByType ) {
236
- foreach ($ wordsByType as $ len => $ wordsByLen ) {
237
- $ count = round (($ line - $ spaces ) / ($ len + 9 )); // strlen("'' => 1, ") = 9
238
- $ i = 0 ;
239
-
240
- foreach ($ wordsByLen as $ word ) {
241
- if ($ i === 0 ) {
242
- $ ret .= str_repeat (' ' , $ spaces );
243
- }
244
-
245
- $ ret .= sprintf ('\'%s \' => %s, ' , $ word , $ type );
246
- if (++$ i !== $ count && ++$ i <= $ count ) {
247
- continue ;
248
- }
249
-
250
- $ ret .= "\n" ;
251
- $ i = 0 ;
252
- }
253
-
254
- if ($ i === 0 ) {
255
- continue ;
256
- }
257
-
258
- $ ret .= "\n" ;
231
+ foreach ($ wordsByType as $ word ) {
232
+ $ ret .= sprintf (" '%s' => %s, \n" , $ word , self ::numTypeToConst ($ type ));
259
233
}
234
+ }
235
+ return $ ret ;
236
+ }
260
237
261
- if (++$ j >= $ typesCount ) {
262
- continue ;
238
+ /**
239
+ * Convert a numeric value representing a set of const to a textual const value.
240
+ *
241
+ * @param int $type The numeric value.
242
+ * @return string The text to write considering the given numeric value.
243
+ */
244
+ private static function numTypeToConst (int $ type ): string
245
+ {
246
+ $ matchingFlags = [];
247
+ foreach (self ::$ typesNumToConst as $ num => $ value ) {
248
+ if ($ type & $ num ) {
249
+ $ matchingFlags [] = $ value ;
263
250
}
264
-
265
- $ ret .= "\n" ;
266
251
}
267
-
268
- // Trim trailing spaces and return.
269
- return str_replace (" \n" , "\n" , $ ret );
252
+ return implode (' | ' , $ matchingFlags );
270
253
}
271
254
272
255
/**
0 commit comments