From 91f56adb7220792b476373832d056c75b5b2f6b4 Mon Sep 17 00:00:00 2001 From: Fabio Bonelli Date: Tue, 7 Oct 2025 11:16:00 +0200 Subject: [PATCH] wip: add mago --- mago.toml | 31 ++++++++++++++++++++++++ src/Parser.php | 42 +++++++++++++++------------------ tests/ParserGoldenFilesTest.php | 25 +++++++------------- tests/ParserTest.php | 40 +++++++++++++++---------------- tests/UpstreamFixturesTest.php | 12 ++++++---- 5 files changed, 86 insertions(+), 64 deletions(-) create mode 100644 mago.toml diff --git a/mago.toml b/mago.toml new file mode 100644 index 0000000..27db20c --- /dev/null +++ b/mago.toml @@ -0,0 +1,31 @@ +# Welcome to Mago! +# For full documentation, see https://mago.carthage.software/tools/overview +php-version = "8.0.0" + +[source] +paths = ["src/", "tests/"] +includes = ["vendor"] +excludes = [] + +[formatter] +print-width = 120 +tab-width = 4 +use-tabs = false +null-type-hint = "question" + +[linter] +integrations = ["phpunit"] + +[linter.rules] +ambiguous-function-call = { enabled = false } +literal-named-argument = { enabled = false } +halstead = { effort-threshold = 7000 } +no-ffi = { enabled = false } + +[analyzer] +find-unused-definitions = true +find-unused-expressions = false +analyze-dead-code = false +check-throws = true +allow-possibly-undefined-array-keys = true +perform-heuristic-checks = true diff --git a/src/Parser.php b/src/Parser.php index 42bcf1c..bd2edcb 100644 --- a/src/Parser.php +++ b/src/Parser.php @@ -91,7 +91,7 @@ public function isValid(string $filePath): bool try { $this->parseFile($filePath); return true; - } catch (ParserException | ValidationException $e) { + } catch (ParserException|ValidationException $e) { return false; } } @@ -111,33 +111,29 @@ private function getFFI(): FFI $libraryPath = $this->findLibrary(); $cdef = <<<'CDEF' - typedef struct { - char* Data; - char* Error; - int ErrorCount; - char** Errors; - int WarningCount; - char** Warnings; - } ParseResult; - - typedef uintptr_t ParserHandle; - - ParserHandle NewParser(bool disableNetwork, const char* branch, const char* baseURL); - ParseResult* ParseString(ParserHandle handle, const char* content); - ParseResult* ParseFile(ParserHandle handle, const char* uri); - void FreeResult(ParseResult* result); - void FreeParser(ParserHandle handle); - CDEF; + typedef struct { + char* Data; + char* Error; + int ErrorCount; + char** Errors; + int WarningCount; + char** Warnings; + } ParseResult; + + typedef uintptr_t ParserHandle; + + ParserHandle NewParser(bool disableNetwork, const char* branch, const char* baseURL); + ParseResult* ParseString(ParserHandle handle, const char* content); + ParseResult* ParseFile(ParserHandle handle, const char* uri); + void FreeResult(ParseResult* result); + void FreeParser(ParserHandle handle); + CDEF; try { self::$ffiInstance = FFI::cdef($cdef, $libraryPath); return self::$ffiInstance; } catch (\FFI\Exception $e) { - throw new ParserException( - 'Failed to load publiccode-parser library: ' . $e->getMessage(), - 0, - $e, - ); + throw new ParserException('Failed to load publiccode-parser library: ' . $e->getMessage(), 0, $e); } } diff --git a/tests/ParserGoldenFilesTest.php b/tests/ParserGoldenFilesTest.php index d1faee7..22ded6a 100644 --- a/tests/ParserGoldenFilesTest.php +++ b/tests/ParserGoldenFilesTest.php @@ -41,9 +41,9 @@ protected function setUp(): void /* public function testGenerateGoldenValidWithWarnings(string $yamlPath): void */ /* { */ /* $this->generateGolden($yamlPath); */ - /**/ + /* $pc = $this->parser->parseFile($yamlPath); */ - /**/ + /* $pc->getWarnings() */ /* } */ @@ -62,7 +62,7 @@ public function testGenerateGoldenInvalid(string $yamlPath): void try { $this->parser->parseFile($yamlPath); - $this->fail('Expected ValidationException was not thrown'); + static::fail('Expected ValidationException was not thrown'); } catch (ValidationException $e) { $this->assertErrorsMatchGolden($e, $goldenPath); } @@ -76,7 +76,7 @@ public function testGenerateGoldenInvalidNoNetwork(string $yamlPath): void try { $this->parserNoNetwork->parseFile($yamlPath); - $this->fail('Expected ValidationException was not thrown'); + static::fail('Expected ValidationException was not thrown'); } catch (ValidationException $e) { $this->assertErrorsMatchGolden($e, $goldenPath); } @@ -165,10 +165,8 @@ private function generateGolden(string $yamlPath, bool $noNetwork = false): stri return $outFile; } - private function assertErrorsMatchGolden( - ValidationException $e, - string $goldenPath, - ): void { + private function assertErrorsMatchGolden(ValidationException $e, string $goldenPath): void + { $content = file_get_contents($goldenPath); if ($content === false) { throw new \RuntimeException("Cannot read file: $goldenPath"); @@ -177,8 +175,7 @@ private function assertErrorsMatchGolden( $golden = json_decode($content, true); $goldenMessages = array_map( - fn (array $g) => - sprintf( + fn(array $g) => sprintf( 'publiccode.yml:%d:%d: %s: %s%s', $g['line'], $g['column'], @@ -186,17 +183,13 @@ private function assertErrorsMatchGolden( $g['key'] !== '' ? $g['key'] . ': ' : '', $g['description'], ), - array_filter($golden, fn (array $g) => $g['type'] === 'error'), + array_filter($golden, fn(array $g) => $g['type'] === 'error'), ); $phpErrors = $e->getErrors(); sort($goldenMessages); sort($phpErrors); - $this->assertSame( - $goldenMessages, - $phpErrors, - "Mismatch between golden file and PHP binding for $goldenPath", - ); + $this->assertSame($goldenMessages, $phpErrors, "Mismatch between golden file and PHP binding for $goldenPath"); } } diff --git a/tests/ParserTest.php b/tests/ParserTest.php index 7050f99..43dceb4 100644 --- a/tests/ParserTest.php +++ b/tests/ParserTest.php @@ -30,25 +30,25 @@ public function testPublicCodeAccessors(): void { $publicCode = $this->parser->parse($this->yaml); - $this->assertEquals('0', $publicCode->getPubliccodeYmlVersion()); - $this->assertEquals('Medusa', $publicCode->getName()); - $this->assertEquals('mySuite', $publicCode->getApplicationSuite()); - $this->assertEquals('https://github.com/italia/developers.italia.it.git', $publicCode->getUrl()); - $this->assertEquals(null, $publicCode->getLandingUrl()); - $this->assertEquals('0.10.11', $publicCode->getSoftwareVersion()); - $this->assertEquals(null, $publicCode->getLogo()); - $this->assertEquals('AGPL-3.0-or-later', $publicCode->getLicense()); - $this->assertEquals(['web'], $publicCode->getPlatforms()); - $this->assertEquals(null, $publicCode->getRoadmap()); - - $this->assertNotNull($publicCode->getDescription('en_GB')); - $this->assertNull($publicCode->getDescription('it')); + static::assertSame('0', $publicCode->getPubliccodeYmlVersion()); + static::assertSame('Medusa', $publicCode->getName()); + static::assertSame('mySuite', $publicCode->getApplicationSuite()); + static::assertSame('https://github.com/italia/developers.italia.it.git', $publicCode->getUrl()); + static::assertSame(null, $publicCode->getLandingUrl()); + static::assertSame('0.10.11', $publicCode->getSoftwareVersion()); + static::assertSame(null, $publicCode->getLogo()); + static::assertSame('AGPL-3.0-or-later', $publicCode->getLicense()); + static::assertSame(['web'], $publicCode->getPlatforms()); + static::assertSame(null, $publicCode->getRoadmap()); + + static::assertNotNull($publicCode->getDescription('en_GB')); + static::assertNull($publicCode->getDescription('it')); $maintenance = $publicCode->getMaintenance(); - $this->assertEquals('community', $maintenance['type']); + static::assertSame('community', $maintenance['type']); $categories = $publicCode->getCategories(); - $this->assertContains('cloud-management', $categories); + static::assertContains('cloud-management', $categories); } public function testToArray(): void @@ -56,9 +56,9 @@ public function testToArray(): void $publicCode = $this->parser->parse($this->yaml); $array = $publicCode->toArray(); - $this->assertIsArray($array); - $this->assertArrayHasKey('name', $array); - $this->assertArrayHasKey('url', $array); + static::assertIsArray($array); + static::assertArrayHasKey('name', $array); + static::assertArrayHasKey('url', $array); } public function testToJson(): void @@ -66,9 +66,9 @@ public function testToJson(): void $publicCode = $this->parser->parse($this->yaml); $json = $publicCode->toJson(); - $this->assertJson($json); + static::assertJson($json); $decoded = json_decode($json, true); - $this->assertEquals('Medusa', $decoded['name']); + static::assertSame('Medusa', $decoded['name']); } } diff --git a/tests/UpstreamFixturesTest.php b/tests/UpstreamFixturesTest.php index 512150f..2a7499b 100644 --- a/tests/UpstreamFixturesTest.php +++ b/tests/UpstreamFixturesTest.php @@ -29,9 +29,9 @@ protected function setUp(): void public function testValidFilesParseWithoutError(string $yamlPath): void { $pc = $this->parser->parseFile($yamlPath); - $this->assertNotNull($pc, $yamlPath); + static::assertNotNull($pc, $yamlPath); - $this->assertTrue($this->parser->isValid($yamlPath)); + static::assertTrue($this->parser->isValid($yamlPath)); } /** @@ -40,10 +40,11 @@ public function testValidFilesParseWithoutError(string $yamlPath): void public function testValidFilesNoNetworkParseWithoutError(string $yamlPath): void { $pc = $this->parserNoNetwork->parseFile($yamlPath); - $this->assertNotNull($pc, $yamlPath); + static::assertNotNull($pc, $yamlPath); - $this->assertTrue($this->parserNoNetwork->isValid($yamlPath)); + static::assertTrue($this->parserNoNetwork->isValid($yamlPath)); } + /** * @dataProvider invalidFilesProvider */ @@ -67,7 +68,7 @@ public function testInvalidFilesNoNetworkRaiseValidationException(string $yamlPa */ public function testInvalidFilesIsValid(string $yamlPath): void { - $this->assertFalse($this->parser->isValid($yamlPath)); + static::assertFalse($this->parser->isValid($yamlPath)); } /** @@ -85,6 +86,7 @@ public static function validFilesNoNetworkProvider(): array { return self::scanTestdata(['valid/no-network', 'valid_with_warnings/no-network']); } + /** * @return non-empty-array */