6
6
7
7
use PhpMyAdmin \SqlParser \Token ;
8
8
9
+ use function array_filter ;
9
10
use function array_map ;
10
11
use function array_merge ;
11
12
use function array_slice ;
28
29
use function substr ;
29
30
use function trim ;
30
31
31
- use const FILE_IGNORE_NEW_LINES ;
32
- use const FILE_SKIP_EMPTY_LINES ;
32
+ use const ARRAY_FILTER_USE_KEY ;
33
33
use const SORT_STRING ;
34
34
35
35
/**
@@ -146,35 +146,36 @@ class %2$s extends Context
146
146
/**
147
147
* Sorts an array of words.
148
148
*
149
- * @param array<int, array<int, array<int, string> >> $arr
149
+ * @param array<int, list< string>> $arr
150
150
*
151
- * @return array<int, array<int, array<int, string> >>
151
+ * @return array<int, list< string>>
152
152
*/
153
153
public static function sortWords (array &$ arr )
154
154
{
155
155
ksort ($ arr );
156
156
foreach ($ arr as &$ words ) {
157
157
sort ($ words , SORT_STRING );
158
- } unset( $ words );
158
+ }
159
159
160
160
return $ arr ;
161
161
}
162
162
163
163
/**
164
164
* Reads a list of words and sorts it by type, length and keyword.
165
165
*
166
- * @param string[] $files
166
+ * @param list< string> $files
167
167
*
168
- * @return array<int, array<int, array<int, string> >>
168
+ * @return array<int, list< string>>
169
169
*/
170
170
public static function readWords (array $ files )
171
171
{
172
172
$ wordsByFile = array_map (
173
173
static function (string $ file ): array {
174
- return file ($ file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES );
174
+ return file ($ file );
175
175
},
176
176
$ files
177
177
);
178
+ /** @psalm-var list<string> $words */
178
179
$ words = array_merge (...$ wordsByFile );
179
180
180
181
/** @var array<string, int> $types */
@@ -185,6 +186,7 @@ static function (string $file): array {
185
186
if ($ value === '' ) {
186
187
continue ;
187
188
}
189
+
188
190
$ type = Token::FLAG_KEYWORD ;
189
191
190
192
// Reserved, data types, keys, functions, etc. keywords.
@@ -223,7 +225,7 @@ static function (string $file): array {
223
225
/**
224
226
* Prints an array of a words in PHP format.
225
227
*
226
- * @param array<int, list<string>> $words the list of words to be formatted
228
+ * @param array<int, list<string>> $words the list of words to be formatted
227
229
*/
228
230
public static function printWords (array $ words ): string
229
231
{
@@ -233,44 +235,46 @@ public static function printWords(array $words): string
233
235
$ ret .= sprintf (" '%s' => %s, \n" , $ word , self ::numTypeToConst ($ type ));
234
236
}
235
237
}
238
+
236
239
return $ ret ;
237
240
}
238
241
239
242
/**
240
243
* Convert a numeric value representing a set of const to a textual const value.
241
244
*
242
245
* @param int $type The numeric value.
246
+ *
243
247
* @return string The text to write considering the given numeric value.
244
248
*/
245
249
private static function numTypeToConst (int $ type ): string
246
250
{
247
- $ matchingFlags = [];
248
- foreach (self ::$ typesNumToConst as $ num => $ value ) {
249
- if ($ type & $ num ) {
250
- $ matchingFlags [] = $ value ;
251
- }
252
- }
251
+ $ matchingFlags = array_filter (
252
+ self ::$ typesNumToConst ,
253
+ static function (int $ num ) use ($ type ): bool {
254
+ return ($ type & $ num ) !== 1 ;
255
+ },
256
+ ARRAY_FILTER_USE_KEY
257
+ );
258
+
253
259
return implode (' | ' , $ matchingFlags );
254
260
}
255
261
256
262
/**
257
263
* Generates a context's class.
258
264
*
259
- * @param array<string, string|array<int, array<int, array<int, string> >>> $options the options for this context
265
+ * @param array<string, string|array<int, list< string>>> $options the options for this context
260
266
* @psalm-param array{
261
267
* name: string,
262
268
* class: string,
263
269
* link: string,
264
- * keywords: array<int, array<int, array<int, string> >>
270
+ * keywords: array<int, list< string>>
265
271
* } $options
266
272
*
267
273
* @return string
268
274
*/
269
275
public static function generate ($ options )
270
276
{
271
- if (isset ($ options ['keywords ' ])) {
272
- $ options ['keywords ' ] = static ::printWords ($ options ['keywords ' ]);
273
- }
277
+ $ options ['keywords ' ] = static ::printWords ($ options ['keywords ' ]);
274
278
275
279
return sprintf (self ::TEMPLATE , $ options ['name ' ], $ options ['class ' ], $ options ['link ' ], $ options ['keywords ' ]);
276
280
}
0 commit comments