Skip to content

Commit

Permalink
Fix linting, phpstan and psalm issues.
Browse files Browse the repository at this point in the history
  • Loading branch information
niconoe- committed Aug 6, 2024
1 parent 0edf645 commit 7674df3
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 56 deletions.
4 changes: 4 additions & 0 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@

<rule ref="PhpMyAdmin"/>

<rule ref="Generic.Files.LineLength.TooLong">
<exclude-pattern>src/Contexts/*</exclude-pattern>
</rule>

<rule ref="SlevomatCodingStandard.ControlStructures.RequireSingleLineCondition"/>
<rule ref="SlevomatCodingStandard.Functions.RequireSingleLineCall"/>
<rule ref="SlevomatCodingStandard.Whitespaces.DuplicateSpaces">
Expand Down
7 changes: 1 addition & 6 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -746,12 +746,7 @@ parameters:
path: src/Tools/ContextGenerator.php

-
message: "#^Offset 'keywords' on array\\{name\\: string, class\\: string, link\\: string, keywords\\: array\\<int, array\\<int, array\\<int, string\\>\\>\\>\\} in isset\\(\\) always exists and is not nullable\\.$#"
count: 1
path: src/Tools/ContextGenerator.php

-
message: "#^Parameter \\#2 \\.\\.\\.\\$arrays of function array_merge expects array, array\\<int, string\\>\\|false given\\.$#"
message: "#^Anonymous function should return array but returns array<int, string>\\|false\\.$#"
count: 1
path: src/Tools/ContextGenerator.php

Expand Down
7 changes: 0 additions & 7 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1057,13 +1057,6 @@
<InvalidArrayOffset>
<code><![CDATA[$version[count($version) - 1]]]></code>
</InvalidArrayOffset>
<PossiblyInvalidArgument>
<code><![CDATA[$options['keywords']]]></code>
</PossiblyInvalidArgument>
<RedundantCondition>
<code><![CDATA[++$i !== $count]]></code>
<code><![CDATA[++$i !== $count]]></code>
</RedundantCondition>
</file>
<file src="src/Tools/CustomJsonSerializer.php">
<MixedAssignment>
Expand Down
40 changes: 22 additions & 18 deletions src/Tools/ContextGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use PhpMyAdmin\SqlParser\Token;

use function array_filter;
use function array_map;
use function array_merge;
use function array_slice;
Expand All @@ -28,6 +29,7 @@
use function substr;
use function trim;

use const ARRAY_FILTER_USE_KEY;
use const FILE_IGNORE_NEW_LINES;
use const FILE_SKIP_EMPTY_LINES;
use const SORT_STRING;
Expand Down Expand Up @@ -147,33 +149,34 @@ class %2$s extends Context
/**
* Sorts an array of words.
*
* @param array<int, array<int, array<int, string>>> $arr
* @param array<int, list<string>> $arr
*
* @return array<int, array<int, array<int, string>>>
* @return array<int, list<string>>
*/
public static function sortWords(array &$arr): array
{
ksort($arr);
foreach ($arr as &$words) {
sort($words, SORT_STRING);
} unset($words);
}

return $arr;
}

/**
* Reads a list of words and sorts it by type, length and keyword.
*
* @param string[] $files
* @param list<string> $files
*
* @return array<int, array<int, array<int, string>>>
* @return array<int, list<string>>
*/
public static function readWords(array $files): array
{
$wordsByFile = array_map(
static fn(string $file): array => file($file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES),
static fn (string $file): array => file($file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES),

Check failure on line 176 in src/Tools/ContextGenerator.php

View workflow job for this annotation

GitHub Actions / analyse-php (8.2)

Anonymous function should return array<int, string> but returns array<int, string>|false.
$files,
);
/** @psalm-var list<string> $words */
$words = array_merge(...$wordsByFile);

/** @var array<string, int> $types */
Expand All @@ -184,6 +187,7 @@ public static function readWords(array $files): array
if ($value === '') {
continue;
}

$type = Token::FLAG_KEYWORD;

// Reserved, data types, keys, functions, etc. keywords.
Expand Down Expand Up @@ -222,7 +226,7 @@ public static function readWords(array $files): array
/**
* Prints an array of a words in PHP format.
*
* @param array<int, list<string>> $words the list of words to be formatted
* @param array<int, list<string>> $words the list of words to be formatted
*/
public static function printWords(array $words): string
{
Expand All @@ -232,42 +236,42 @@ public static function printWords(array $words): string
$ret .= sprintf(" '%s' => %s,\n", $word, self::numTypeToConst($type));
}
}

return $ret;
}

/**
* Convert a numeric value representing a set of const to a textual const value.
*
* @param int $type The numeric value.
*
* @return string The text to write considering the given numeric value.
*/
private static function numTypeToConst(int $type): string
{
$matchingFlags = [];
foreach (self::$typesNumToConst as $num => $value) {
if ($type & $num) {
$matchingFlags[] = $value;
}
}
$matchingFlags = array_filter(
self::$typesNumToConst,
static fn (int $num): bool => ($type & $num) === 1,

Check failure on line 254 in src/Tools/ContextGenerator.php

View workflow job for this annotation

GitHub Actions / analyse-php (8.2)

MixedArgumentTypeCoercion

src/Tools/ContextGenerator.php:254:13: MixedArgumentTypeCoercion: Argument 2 of array_filter expects callable(array-key):mixed, but parent type pure-Closure(int):bool provided (see https://psalm.dev/194)
ARRAY_FILTER_USE_KEY,
);

return implode(' | ', $matchingFlags);
}

/**
* Generates a context's class.
*
* @param array<string, string|array<int, array<int, array<int, string>>>> $options the options for this context
* @param array<string, string|array<int, list<string>>> $options the options for this context
* @psalm-param array{
* name: string,
* class: string,
* link: string,
* keywords: array<int, array<int, array<int, string>>>
* keywords: array<int, list<string>>
* } $options
*/
public static function generate(array $options): string
{
if (isset($options['keywords'])) {
$options['keywords'] = static::printWords($options['keywords']);
}
$options['keywords'] = static::printWords($options['keywords']);

return sprintf(self::TEMPLATE, $options['name'], $options['class'], $options['link'], $options['keywords']);
}
Expand Down
23 changes: 13 additions & 10 deletions tests/Tools/ContextGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ public function testFormatName(): void

public function testSortWords(): void
{
$wordsArray = ['41' => [['GEOMETRYCOLLECTION', 'DATE']], '35' => [['SCHEMA', 'REPEAT', 'VALUES']]];
$wordsArray = ['41' => ['GEOMETRYCOLLECTION', 'DATE'], '35' => ['SCHEMA', 'REPEAT', 'VALUES']];
ContextGenerator::sortWords($wordsArray);
$this->assertEquals([
'41' => ['0' => ['DATE', 'GEOMETRYCOLLECTION']],
'35' => ['0' => ['REPEAT', 'SCHEMA', 'VALUES']],
'41' => ['DATE', 'GEOMETRYCOLLECTION'],
'35' => ['REPEAT', 'SCHEMA', 'VALUES'],
], $wordsArray);
}

Expand All @@ -42,14 +42,17 @@ public function testReadWords(): void
$readWords = ContextGenerator::readWords($testFiles);
$this->assertEquals([
TokenType::Keyword->value | Token::FLAG_KEYWORD_RESERVED => [
8 => ['RESERVED'],
9 => ['RESERVED2','RESERVED3','RESERVED4','RESERVED5'],
'RESERVED',
'RESERVED2',
'RESERVED3',
'RESERVED4',
'RESERVED5',
],
TokenType::Keyword->value | Token::FLAG_KEYWORD_FUNCTION => [8 => ['FUNCTION']],
TokenType::Keyword->value | Token::FLAG_KEYWORD_DATA_TYPE => [8 => ['DATATYPE']],
TokenType::Keyword->value | Token::FLAG_KEYWORD_KEY => [7 => ['KEYWORD']],
TokenType::Keyword->value => [7 => ['NO_FLAG']],
TokenType::Keyword->value | Token::FLAG_KEYWORD_RESERVED | 4 => [16 => ['COMPOSED KEYWORD']],
TokenType::Keyword->value | Token::FLAG_KEYWORD_FUNCTION => ['FUNCTION'],
TokenType::Keyword->value | Token::FLAG_KEYWORD_DATA_TYPE => ['DATATYPE'],
TokenType::Keyword->value | Token::FLAG_KEYWORD_KEY => ['KEYWORD'],
TokenType::Keyword->value => ['NO_FLAG'],
TokenType::Keyword->value | Token::FLAG_KEYWORD_RESERVED | 4 => ['COMPOSED KEYWORD'],
], $readWords);
}

Expand Down
26 changes: 11 additions & 15 deletions tests/Tools/templates/TestContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,22 @@ class TestContext extends Context
*
* The value associated to each keyword represents its flags.
*
* @see Token::FLAG_KEYWORD_RESERVED Token::FLAG_KEYWORD_COMPOSED
* Token::FLAG_KEYWORD_DATA_TYPE Token::FLAG_KEYWORD_KEY
* Token::FLAG_KEYWORD_FUNCTION
* @see Token
*
* @var array<string,int>
* @psalm-var non-empty-array<string,Token::FLAG_KEYWORD_*|int>
* @phpstan-var non-empty-array<non-empty-string,Token::FLAG_KEYWORD_*|int>
*/
public static array $keywords = [
'NO_FLAG' => 1,

'RESERVED' => 3,
'RESERVED2' => 3, 'RESERVED3' => 3, 'RESERVED4' => 3, 'RESERVED5' => 3,

'COMPOSED KEYWORD' => 7,

'DATATYPE' => 9,

'KEYWORD' => 17,

'FUNCTION' => 33,
'NO_FLAG' => Token::FLAG_KEYWORD,
'RESERVED' => Token::FLAG_KEYWORD | Token::FLAG_KEYWORD_RESERVED,
'RESERVED2' => Token::FLAG_KEYWORD | Token::FLAG_KEYWORD_RESERVED,
'RESERVED3' => Token::FLAG_KEYWORD | Token::FLAG_KEYWORD_RESERVED,
'RESERVED4' => Token::FLAG_KEYWORD | Token::FLAG_KEYWORD_RESERVED,
'RESERVED5' => Token::FLAG_KEYWORD | Token::FLAG_KEYWORD_RESERVED,
'COMPOSED KEYWORD' => Token::FLAG_KEYWORD | Token::FLAG_KEYWORD_RESERVED | Token::FLAG_KEYWORD_COMPOSED,
'DATATYPE' => Token::FLAG_KEYWORD | Token::FLAG_KEYWORD_DATA_TYPE,
'KEYWORD' => Token::FLAG_KEYWORD | Token::FLAG_KEYWORD_KEY,
'FUNCTION' => Token::FLAG_KEYWORD | Token::FLAG_KEYWORD_FUNCTION,
];
}

0 comments on commit 7674df3

Please sign in to comment.