From 2e36bcdcd946b1e204e106d2c965edd9743d0451 Mon Sep 17 00:00:00 2001 From: Guillaume Sainthillier Date: Fri, 25 Oct 2024 08:12:37 +0200 Subject: [PATCH] PHPStan improvements (#65) - Set max level in PHPStan config - Use PHPStan in tests dir - Use PHPStan config in CI --- .github/workflows/phpstan.yaml | 2 +- phpstan.neon | 4 +++- src/zugferd2/Reader.php | 1 + tests/zugferd2/Tests/Legacy/BuilderTest.php | 4 ++-- tests/zugferd2/Tests/Legacy/ReaderAndBuildTest.php | 7 +++++-- tests/zugferd2/Tests/Legacy/ValidatorTest.php | 8 ++++---- tests/zugferd2/Tests/ProfileXRechnungTest.php | 4 ++-- tests/zugferd2/Tests/Traits/AssertXmlOutputTrait.php | 2 +- tests/zugferd2/Tests/Traits/ReformatXmlTrait.php | 4 ++-- 9 files changed, 21 insertions(+), 15 deletions(-) diff --git a/.github/workflows/phpstan.yaml b/.github/workflows/phpstan.yaml index 9fe7b688..e873fe94 100644 --- a/.github/workflows/phpstan.yaml +++ b/.github/workflows/phpstan.yaml @@ -33,4 +33,4 @@ jobs: - name: Analyze code for issues run: | - php vendor/bin/phpstan analyse src --level 8 + php vendor/bin/phpstan analyse diff --git a/phpstan.neon b/phpstan.neon index 9fd12a11..166988ef 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -1,6 +1,8 @@ parameters: - level: 8 + level: max paths: - 'src' + - 'tests' excludePaths: - 'src/zugferd10' + - 'tests/zugferd10' diff --git a/src/zugferd2/Reader.php b/src/zugferd2/Reader.php index f4e4208c..066d8b4e 100644 --- a/src/zugferd2/Reader.php +++ b/src/zugferd2/Reader.php @@ -16,6 +16,7 @@ public function __construct(private readonly SerializerInterface $serializer) public function transform(string $xml): CrossIndustryInvoice { + // @phpstan-ignore-next-line return $this->serializer->deserialize($xml, CrossIndustryInvoice::class, 'xml'); } diff --git a/tests/zugferd2/Tests/Legacy/BuilderTest.php b/tests/zugferd2/Tests/Legacy/BuilderTest.php index 9f214bab..851f037b 100644 --- a/tests/zugferd2/Tests/Legacy/BuilderTest.php +++ b/tests/zugferd2/Tests/Legacy/BuilderTest.php @@ -198,7 +198,7 @@ public function testBuildXRechnungExample(): void $xml = Builder::create()->transform($invoice); self::assertNotEmpty($xml); - $referenceFile = file_get_contents(__DIR__ . '/official_example_xml/zugferd_2p1_XRECHNUNG_Einfach.xml'); + $referenceFile = (string)file_get_contents(__DIR__ . '/official_example_xml/zugferd_2p1_XRECHNUNG_Einfach.xml'); $referenceFile = ReaderAndBuildTest::reformatXml($referenceFile); $xml = ReaderAndBuildTest::reformatXml($xml); self::assertEquals($referenceFile, $xml); @@ -389,7 +389,7 @@ public function testBuildXRechnungExtendedExample(): void $xml = Builder::create()->transform($invoice); self::assertNotEmpty($xml); - $referenceFile = file_get_contents(__DIR__ . '/official_example_xml/zugferd_2p1_XRECHNUNG_Extended.xml'); + $referenceFile = (string)file_get_contents(__DIR__ . '/official_example_xml/zugferd_2p1_XRECHNUNG_Extended.xml'); $referenceFile = ReaderAndBuildTest::reformatXml($referenceFile); $xml = ReaderAndBuildTest::reformatXml($xml); self::assertEquals($referenceFile, $xml); diff --git a/tests/zugferd2/Tests/Legacy/ReaderAndBuildTest.php b/tests/zugferd2/Tests/Legacy/ReaderAndBuildTest.php index 8f31fa2f..ef8dbd15 100644 --- a/tests/zugferd2/Tests/Legacy/ReaderAndBuildTest.php +++ b/tests/zugferd2/Tests/Legacy/ReaderAndBuildTest.php @@ -24,7 +24,7 @@ public function setupAnnotationRegistry(): void /** @dataProvider dataProvider */ public function testGetDocument(string $filename): void { - $xml = file_get_contents(__DIR__ . '/official_example_xml/' . $filename); + $xml = (string)file_get_contents(__DIR__ . '/official_example_xml/' . $filename); $obj = Reader::create()->transform($xml); $str = Builder::create()->transform($obj); @@ -36,7 +36,10 @@ public function testGetDocument(string $filename): void self::assertTrue(true); } - public function dataProvider() + /** + * @return string[][] + */ + public function dataProvider(): array { return [ ['zugferd_2p1_BASIC-WL_Einfach.xml'], diff --git a/tests/zugferd2/Tests/Legacy/ValidatorTest.php b/tests/zugferd2/Tests/Legacy/ValidatorTest.php index 094fa047..b10e7160 100644 --- a/tests/zugferd2/Tests/Legacy/ValidatorTest.php +++ b/tests/zugferd2/Tests/Legacy/ValidatorTest.php @@ -9,18 +9,18 @@ class ValidatorTest extends TestCase { - public function testXsdSuccess() + public function testXsdSuccess(): void { $validator = new Validator(); - $xml = file_get_contents(__DIR__ . '/official_example_xml/zugferd_2p1_EN16931_Einfach.xml'); + $xml = (string)file_get_contents(__DIR__ . '/official_example_xml/zugferd_2p1_EN16931_Einfach.xml'); $errors = $validator->validateAgainstXsd($xml, Validator::SCHEMA_EN16931); self::assertNull($errors, $errors ?? ''); } - public function testXsdFail() + public function testXsdFail(): void { $validator = new Validator(); - $xml = file_get_contents(__DIR__ . '/references/broken_example.xml'); + $xml = (string)file_get_contents(__DIR__ . '/references/broken_example.xml'); $errors = $validator->validateAgainstXsd($xml, Validator::SCHEMA_EN16931); self::assertNotNull($errors, 'Validator says broken xml is valid.'); } diff --git a/tests/zugferd2/Tests/ProfileXRechnungTest.php b/tests/zugferd2/Tests/ProfileXRechnungTest.php index cf626fe0..0c31efa8 100644 --- a/tests/zugferd2/Tests/ProfileXRechnungTest.php +++ b/tests/zugferd2/Tests/ProfileXRechnungTest.php @@ -362,7 +362,7 @@ public function testBuildXRechnungReisekostenabrechnung(): void $additionalReferencedDocument->attachmentBinaryObject = $additionalReferencedDocumentBinaryObject = new BinaryObject(); $additionalReferencedDocumentBinaryObject->filename = 'Hotelrechnung-Immo.pdf'; $additionalReferencedDocumentBinaryObject->mimeCode = 'application/pdf'; - $additionalReferencedDocumentBinaryObject->value = base64_encode(file_get_contents(__DIR__ . '/Examples/XRECHNUNG/Attachments/EN16931_Betriebskostenabrechnung_Abrechnung 2010.pdf')); + $additionalReferencedDocumentBinaryObject->value = base64_encode((string)file_get_contents(__DIR__ . '/Examples/XRECHNUNG/Attachments/EN16931_Betriebskostenabrechnung_Abrechnung 2010.pdf')); $invoice->supplyChainTradeTransaction->applicableHeaderTradeSettlement = new HeaderTradeSettlement(); $invoice->supplyChainTradeTransaction->applicableHeaderTradeSettlement->currency = 'EUR'; @@ -507,7 +507,7 @@ public function testBuildXRechnungBetrieskostenabrechnung(): void $additionalReferencedDocument->attachmentBinaryObject = $additionalReferencedDocumentBinaryObject = new BinaryObject(); $additionalReferencedDocumentBinaryObject->filename = 'Betriebskostenabrechnung.pdf'; $additionalReferencedDocumentBinaryObject->mimeCode = 'application/pdf'; - $additionalReferencedDocumentBinaryObject->value = base64_encode(file_get_contents(__DIR__ . '/Examples/XRECHNUNG/Attachments/EN16931_Betriebskostenabrechnung_Abrechnung 2010.pdf')); + $additionalReferencedDocumentBinaryObject->value = base64_encode((string)file_get_contents(__DIR__ . '/Examples/XRECHNUNG/Attachments/EN16931_Betriebskostenabrechnung_Abrechnung 2010.pdf')); $invoice->supplyChainTradeTransaction->applicableHeaderTradeSettlement = new HeaderTradeSettlement(); $invoice->supplyChainTradeTransaction->applicableHeaderTradeSettlement->currency = 'EUR'; diff --git a/tests/zugferd2/Tests/Traits/AssertXmlOutputTrait.php b/tests/zugferd2/Tests/Traits/AssertXmlOutputTrait.php index 8c9443f2..926e0d94 100644 --- a/tests/zugferd2/Tests/Traits/AssertXmlOutputTrait.php +++ b/tests/zugferd2/Tests/Traits/AssertXmlOutputTrait.php @@ -21,7 +21,7 @@ private function buildAndAssertXmlFromCII(CrossIndustryInvoice $invoice, string $result = (new Validator())->validateAgainstXsd($xml, $validatorSchema); self::assertNull($result, $result ?? ''); - $referenceFile = file_get_contents($referenceFilePath); + $referenceFile = (string)file_get_contents($referenceFilePath); $referenceFile = ReaderAndBuildTest::reformatXml($referenceFile); self::assertEquals($referenceFile, $xml); } diff --git a/tests/zugferd2/Tests/Traits/ReformatXmlTrait.php b/tests/zugferd2/Tests/Traits/ReformatXmlTrait.php index 77bc1d94..1f47fa40 100644 --- a/tests/zugferd2/Tests/Traits/ReformatXmlTrait.php +++ b/tests/zugferd2/Tests/Traits/ReformatXmlTrait.php @@ -8,12 +8,12 @@ trait ReformatXmlTrait { public static function reformatXml(string $xml): string { - $xml = preg_replace('//', '', $xml); + $xml = (string)preg_replace('//', '', $xml); $doc = new \DOMDocument('1.0', 'UTF-8'); $doc->preserveWhiteSpace = false; $doc->formatOutput = true; $doc->loadXML($xml); - return $doc->saveXML(); + return (string)$doc->saveXML(); } }