@@ -24,6 +24,17 @@ foreach ($gherkinLanguages as $lang => $keywords) {
2424 $ words = [$ words ];
2525 }
2626
27+ assert (array_find ($ words , fn ($ word ) => !is_string ($ word )) === null , 'Expected $words to be an array of strings ' );
28+ /**
29+ * Here we force phpstan to recognise that we have narrowed the type of $words to a list of strings with the
30+ * assertion above. Otherwise it will report an error from the later `implode()` call that $words must be a
31+ * list<string>.
32+ *
33+ * There does not currently seem to be a way to force phpstan to detect this type narrowing at runtime
34+ * https://github.com/phpstan/phpstan/issues/14360
35+ *
36+ * @var array<string> $words
37+ */
2738 if ($ type === 'scenarioOutline ' ) {
2839 $ type = 'scenario_outline ' ;
2940 }
@@ -32,7 +43,6 @@ foreach ($gherkinLanguages as $lang => $keywords) {
3243 $ formattedKeywords = [];
3344
3445 foreach ($ words as $ word ) {
35- assert (is_string ($ word ));
3646 $ formattedWord = trim ($ word );
3747
3848 if ($ formattedWord === $ word ) {
@@ -45,12 +55,14 @@ foreach ($gherkinLanguages as $lang => $keywords) {
4555 $ words = $ formattedKeywords ;
4656 }
4757
48- usort ($ words, static function ( $ type1 , $ type2 ) {
49- assert (is_string ($ type1 ));
50- assert ( is_string ( $ type2 ));
58+ foreach ($ words as $ word ) {
59+ assert (is_string ($ word ));
60+ }
5161
52- return [mb_strlen ($ type2 , 'utf8 ' ), $ type1 ] <=> [mb_strlen ($ type1 , 'utf8 ' ), $ type2 ];
53- });
62+ usort (
63+ $ words ,
64+ static fn (string $ type1 , string $ type2 ) => [mb_strlen ($ type2 , 'utf8 ' ), $ type1 ] <=> [mb_strlen ($ type1 , 'utf8 ' ), $ type2 ]
65+ );
5466
5567 $ langMessages [$ type ] = implode ('| ' , $ words );
5668 }
0 commit comments