Skip to content

Commit

Permalink
Fix Psalm, Phpstan and unit tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
niconoe- committed Aug 6, 2024
1 parent 2b95c81 commit e535359
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 20 deletions.
2 changes: 1 addition & 1 deletion phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -746,7 +746,7 @@ parameters:
path: src/Tools/ContextGenerator.php

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

Expand Down
6 changes: 3 additions & 3 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1054,12 +1054,12 @@
</RiskyTruthyFalsyComparison>
</file>
<file src="src/Tools/ContextGenerator.php">
<InvalidArrayOffset>
<code><![CDATA[$version[count($version) - 1]]]></code>
</InvalidArrayOffset>
<MixedArgumentTypeCoercion>
<code><![CDATA[static fn (int $num): bool => ($type & $num) !== 0]]></code>
</MixedArgumentTypeCoercion>
<PossiblyFalseArgument>
<code><![CDATA[array_map(file(...), $files)]]></code>
</PossiblyFalseArgument>
</file>
<file src="src/Tools/CustomJsonSerializer.php">
<MixedAssignment>
Expand Down
18 changes: 7 additions & 11 deletions src/Tools/ContextGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

/**
Expand Down Expand Up @@ -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<string> $words */
$words = array_merge(...$wordsByFile);
$words = array_merge(...array_map(file(...), $files));

/** @var array<string, int> $types */
$types = [];
Expand Down Expand Up @@ -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;
}

Expand All @@ -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 */
Expand Down
6 changes: 6 additions & 0 deletions tests/Tools/ContextGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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
Expand Down
12 changes: 7 additions & 5 deletions tests/Tools/contexts/testContext.txt
Original file line number Diff line number Diff line change
@@ -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
FUNCTION

0 comments on commit e535359

Please sign in to comment.