Skip to content

Commit 03f0f81

Browse files
committed
Fix linting, phpstan and psalm issues.
1 parent 1d61ab0 commit 03f0f81

File tree

4 files changed

+52
-45
lines changed

4 files changed

+52
-45
lines changed

phpcs.xml.dist

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@
1919
<exclude name="Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps"/>
2020
</rule>
2121

22+
<rule ref="Generic.Files.LineLength.TooLong">
23+
<exclude-pattern>src/Contexts/*</exclude-pattern>
24+
</rule>
25+
2226
<rule ref="SlevomatCodingStandard.ControlStructures.RequireSingleLineCondition"/>
2327
<rule ref="SlevomatCodingStandard.Functions.RequireSingleLineCall"/>
2428
<rule ref="SlevomatCodingStandard.Whitespaces.DuplicateSpaces">

src/Tools/ContextGenerator.php

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use PhpMyAdmin\SqlParser\Token;
88

9+
use function array_filter;
910
use function array_map;
1011
use function array_merge;
1112
use function array_slice;
@@ -28,8 +29,7 @@
2829
use function substr;
2930
use function trim;
3031

31-
use const FILE_IGNORE_NEW_LINES;
32-
use const FILE_SKIP_EMPTY_LINES;
32+
use const ARRAY_FILTER_USE_KEY;
3333
use 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
}

tests/Tools/ContextGeneratorTest.php

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ public function testFormatName(): void
2727

2828
public function testSortWords(): void
2929
{
30-
$wordsArray = ['41' => [['GEOMETRYCOLLECTION', 'DATE']], '35' => [['SCHEMA', 'REPEAT', 'VALUES']]];
30+
$wordsArray = ['41' => ['GEOMETRYCOLLECTION', 'DATE'], '35' => ['SCHEMA', 'REPEAT', 'VALUES']];
3131
ContextGenerator::sortWords($wordsArray);
3232
$this->assertEquals([
33-
'41' => ['0' => ['DATE', 'GEOMETRYCOLLECTION']],
34-
'35' => ['0' => ['REPEAT', 'SCHEMA', 'VALUES']],
33+
'41' => ['DATE', 'GEOMETRYCOLLECTION'],
34+
'35' => ['REPEAT', 'SCHEMA', 'VALUES'],
3535
], $wordsArray);
3636
}
3737

@@ -41,14 +41,17 @@ public function testReadWords(): void
4141
$readWords = ContextGenerator::readWords($testFiles);
4242
$this->assertEquals([
4343
Token::TYPE_KEYWORD | Token::FLAG_KEYWORD_RESERVED => [
44-
8 => ['RESERVED'],
45-
9 => ['RESERVED2','RESERVED3','RESERVED4','RESERVED5'],
44+
'RESERVED',
45+
'RESERVED2',
46+
'RESERVED3',
47+
'RESERVED4',
48+
'RESERVED5',
4649
],
47-
Token::TYPE_KEYWORD | Token::FLAG_KEYWORD_FUNCTION => [8 => ['FUNCTION']],
48-
Token::TYPE_KEYWORD | Token::FLAG_KEYWORD_DATA_TYPE => [8 => ['DATATYPE']],
49-
Token::TYPE_KEYWORD | Token::FLAG_KEYWORD_KEY => [7 => ['KEYWORD']],
50-
Token::TYPE_KEYWORD => [7 => ['NO_FLAG']],
51-
Token::TYPE_KEYWORD | Token::FLAG_KEYWORD_RESERVED | 4 => [16 => ['COMPOSED KEYWORD']],
50+
Token::TYPE_KEYWORD | Token::FLAG_KEYWORD_FUNCTION => ['FUNCTION'],
51+
Token::TYPE_KEYWORD | Token::FLAG_KEYWORD_DATA_TYPE => ['DATATYPE'],
52+
Token::TYPE_KEYWORD | Token::FLAG_KEYWORD_KEY => ['KEYWORD'],
53+
Token::TYPE_KEYWORD => ['NO_FLAG'],
54+
Token::TYPE_KEYWORD | Token::FLAG_KEYWORD_RESERVED | Token::FLAG_KEYWORD_COMPOSED => ['COMPOSED KEYWORD'],
5255
], $readWords);
5356
}
5457

tests/Tools/templates/TestContext.php

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,25 +22,21 @@ class TestContext extends Context
2222
*
2323
* The value associated to each keyword represents its flags.
2424
*
25-
* @see Token::FLAG_KEYWORD_RESERVED Token::FLAG_KEYWORD_COMPOSED
26-
* Token::FLAG_KEYWORD_DATA_TYPE Token::FLAG_KEYWORD_KEY
27-
* Token::FLAG_KEYWORD_FUNCTION
25+
* @see Token
2826
*
2927
* @var array<string,int>
3028
* @phpstan-var non-empty-array<non-empty-string,Token::FLAG_KEYWORD_*|int>
3129
*/
3230
public static $KEYWORDS = [
33-
'NO_FLAG' => 1,
34-
35-
'RESERVED' => 3,
36-
'RESERVED2' => 3, 'RESERVED3' => 3, 'RESERVED4' => 3, 'RESERVED5' => 3,
37-
38-
'COMPOSED KEYWORD' => 7,
39-
40-
'DATATYPE' => 9,
41-
42-
'KEYWORD' => 17,
43-
44-
'FUNCTION' => 33,
31+
'NO_FLAG' => Token::FLAG_KEYWORD,
32+
'RESERVED' => Token::FLAG_KEYWORD | Token::FLAG_KEYWORD_RESERVED,
33+
'RESERVED2' => Token::FLAG_KEYWORD | Token::FLAG_KEYWORD_RESERVED,
34+
'RESERVED3' => Token::FLAG_KEYWORD | Token::FLAG_KEYWORD_RESERVED,
35+
'RESERVED4' => Token::FLAG_KEYWORD | Token::FLAG_KEYWORD_RESERVED,
36+
'RESERVED5' => Token::FLAG_KEYWORD | Token::FLAG_KEYWORD_RESERVED,
37+
'COMPOSED KEYWORD' => Token::FLAG_KEYWORD | Token::FLAG_KEYWORD_RESERVED | Token::FLAG_KEYWORD_COMPOSED,
38+
'DATATYPE' => Token::FLAG_KEYWORD | Token::FLAG_KEYWORD_DATA_TYPE,
39+
'KEYWORD' => Token::FLAG_KEYWORD | Token::FLAG_KEYWORD_KEY,
40+
'FUNCTION' => Token::FLAG_KEYWORD | Token::FLAG_KEYWORD_FUNCTION,
4541
];
4642
}

0 commit comments

Comments
 (0)