From e5353595b1d42193b497ed2229d216ad81195d41 Mon Sep 17 00:00:00 2001 From: Nicolas Giraud Date: Tue, 6 Aug 2024 14:30:45 +0200 Subject: [PATCH] Fix Psalm, Phpstan and unit tests. --- phpstan-baseline.neon | 2 +- psalm-baseline.xml | 6 +++--- src/Tools/ContextGenerator.php | 18 +++++++----------- tests/Tools/ContextGeneratorTest.php | 6 ++++++ tests/Tools/contexts/testContext.txt | 12 +++++++----- 5 files changed, 24 insertions(+), 20 deletions(-) diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 17f1b323d..24ccdbb94 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -746,7 +746,7 @@ parameters: path: src/Tools/ContextGenerator.php - - message: "#^Anonymous function should return array but returns array\\|false\\.$#" + message: "#^Parameter \\#1 \\.\\.\\.\\$arrays of function array_merge expects array, array\\\\|false given\\.$#" count: 1 path: src/Tools/ContextGenerator.php diff --git a/psalm-baseline.xml b/psalm-baseline.xml index d94fa3a16..86603c0b6 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -1054,12 +1054,12 @@ - - - ($type & $num) !== 0]]> + + + diff --git a/src/Tools/ContextGenerator.php b/src/Tools/ContextGenerator.php index c5410fb3f..5c16c8f5a 100644 --- a/src/Tools/ContextGenerator.php +++ b/src/Tools/ContextGenerator.php @@ -13,9 +13,11 @@ use function basename; use function count; use function dirname; +use function end; use function file; use function file_put_contents; use function implode; +use function intval; use function ksort; use function preg_match; use function scandir; @@ -30,8 +32,6 @@ 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; /** @@ -172,12 +172,8 @@ public static function sortWords(array &$arr): array */ 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), - $files, - ); /** @psalm-var list $words */ - $words = array_merge(...$wordsByFile); + $words = array_merge(...array_map(file(...), $files)); /** @var array $types */ $types = []; @@ -285,7 +281,7 @@ public static function formatName(string $name): string { /* Split name and version */ $parts = []; - if (preg_match('/([^[0-9]*)([0-9]*)/', $name, $parts) === false) { + if (preg_match('/^(\D+)(\d+)$/', $name, $parts) === 0) { return $name; } @@ -303,10 +299,10 @@ public static function formatName(string $name): string $versionString = '0' . $versionString; } - $version = array_map('intval', str_split($versionString, 2)); + $version = array_map(intval(...), str_split($versionString, 2)); /* Remove trailing zero */ - if ($version[count($version) - 1] === 0) { - $version = array_slice($version, 0, count($version) - 1); + if (end($version) === 0) { + $version = array_slice($version, 0, -1); } /* Create name */ diff --git a/tests/Tools/ContextGeneratorTest.php b/tests/Tools/ContextGeneratorTest.php index df2f3d98c..92f08a7a7 100644 --- a/tests/Tools/ContextGeneratorTest.php +++ b/tests/Tools/ContextGeneratorTest.php @@ -15,6 +15,9 @@ class ContextGeneratorTest extends TestCase { public function testFormatName(): void { + $name = ContextGenerator::formatName('00InvalidFormat00'); + $this->assertEquals('00InvalidFormat00', $name); + $name = ContextGenerator::formatName('MySql80000'); $this->assertEquals('MySQL 8.0', $name); @@ -23,6 +26,9 @@ public function testFormatName(): void $name = ContextGenerator::formatName('MariaDb100000'); $this->assertEquals('MariaDB 10.0', $name); + + $name = ContextGenerator::formatName('FutureDBMS45784012500'); + $this->assertEquals('FutureDBMS 4.57.84.1.25', $name); } public function testSortWords(): void diff --git a/tests/Tools/contexts/testContext.txt b/tests/Tools/contexts/testContext.txt index 9707168c2..e7b107b80 100644 --- a/tests/Tools/contexts/testContext.txt +++ b/tests/Tools/contexts/testContext.txt @@ -1,14 +1,16 @@ RESERVED (R) -RESERVED2 (R) -RESERVED3 (R) -RESERVED4 (R) -RESERVED5 (R) + RESERVED2 (R) + RESERVED3 (R) + RESERVED4 (R) +reserved5 (R) FUNCTION (F) DATATYPE (D) KEYWORD (K) + + NO_FLAG COMPOSED KEYWORD -FUNCTION \ No newline at end of file +FUNCTION