66
77use PhpMyAdmin \SqlParser \Token ;
88
9+ use function array_filter ;
910use function array_map ;
1011use function array_merge ;
1112use function array_slice ;
2829use function substr ;
2930use function trim ;
3031
31- use const FILE_IGNORE_NEW_LINES ;
32- use const FILE_SKIP_EMPTY_LINES ;
32+ use const ARRAY_FILTER_USE_KEY ;
3333use const SORT_STRING ;
3434
3535/**
@@ -146,35 +146,36 @@ class %2$s extends Context
146146 /**
147147 * Sorts an array of words.
148148 *
149- * @param array<int, array<int, array<int, string> >> $arr
149+ * @param array<int, list< string>> $arr
150150 *
151- * @return array<int, array<int, array<int, string> >>
151+ * @return array<int, list< string>>
152152 */
153153 public static function sortWords (array &$ arr )
154154 {
155155 ksort ($ arr );
156156 foreach ($ arr as &$ words ) {
157157 sort ($ words , SORT_STRING );
158- } unset( $ words );
158+ }
159159
160160 return $ arr ;
161161 }
162162
163163 /**
164164 * Reads a list of words and sorts it by type, length and keyword.
165165 *
166- * @param string[] $files
166+ * @param list< string> $files
167167 *
168- * @return array<int, array<int, array<int, string> >>
168+ * @return array<int, list< string>>
169169 */
170170 public static function readWords (array $ files )
171171 {
172172 $ wordsByFile = array_map (
173173 static function (string $ file ): array {
174- return file ($ file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES );
174+ return file ($ file );
175175 },
176176 $ files
177177 );
178+ /** @psalm-var list<string> $words */
178179 $ words = array_merge (...$ wordsByFile );
179180
180181 /** @var array<string, int> $types */
@@ -185,6 +186,7 @@ static function (string $file): array {
185186 if ($ value === '' ) {
186187 continue ;
187188 }
189+
188190 $ type = Token::FLAG_KEYWORD ;
189191
190192 // Reserved, data types, keys, functions, etc. keywords.
@@ -223,7 +225,7 @@ static function (string $file): array {
223225 /**
224226 * Prints an array of a words in PHP format.
225227 *
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
227229 */
228230 public static function printWords (array $ words ): string
229231 {
@@ -233,44 +235,46 @@ public static function printWords(array $words): string
233235 $ ret .= sprintf (" '%s' => %s, \n" , $ word , self ::numTypeToConst ($ type ));
234236 }
235237 }
238+
236239 return $ ret ;
237240 }
238241
239242 /**
240243 * Convert a numeric value representing a set of const to a textual const value.
241244 *
242245 * @param int $type The numeric value.
246+ *
243247 * @return string The text to write considering the given numeric value.
244248 */
245249 private static function numTypeToConst (int $ type ): string
246250 {
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+
253259 return implode (' | ' , $ matchingFlags );
254260 }
255261
256262 /**
257263 * Generates a context's class.
258264 *
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
260266 * @psalm-param array{
261267 * name: string,
262268 * class: string,
263269 * link: string,
264- * keywords: array<int, array<int, array<int, string> >>
270+ * keywords: array<int, list< string>>
265271 * } $options
266272 *
267273 * @return string
268274 */
269275 public static function generate ($ options )
270276 {
271- if (isset ($ options ['keywords ' ])) {
272- $ options ['keywords ' ] = static ::printWords ($ options ['keywords ' ]);
273- }
277+ $ options ['keywords ' ] = static ::printWords ($ options ['keywords ' ]);
274278
275279 return sprintf (self ::TEMPLATE , $ options ['name ' ], $ options ['class ' ], $ options ['link ' ], $ options ['keywords ' ]);
276280 }
0 commit comments