Skip to content

Commit

Permalink
PHPStan improvements (#65)
Browse files Browse the repository at this point in the history
- Set max level in PHPStan config
- Use PHPStan in tests dir
- Use PHPStan config in CI
  • Loading branch information
guillaume-sainthillier authored Oct 25, 2024
1 parent 8a91257 commit 2e36bcd
Show file tree
Hide file tree
Showing 9 changed files with 21 additions and 15 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/phpstan.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,4 @@ jobs:
- name: Analyze code for issues
run: |
php vendor/bin/phpstan analyse src --level 8
php vendor/bin/phpstan analyse
4 changes: 3 additions & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
parameters:
level: 8
level: max
paths:
- 'src'
- 'tests'
excludePaths:
- 'src/zugferd10'
- 'tests/zugferd10'
1 change: 1 addition & 0 deletions src/zugferd2/Reader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}

Expand Down
4 changes: 2 additions & 2 deletions tests/zugferd2/Tests/Legacy/BuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
7 changes: 5 additions & 2 deletions tests/zugferd2/Tests/Legacy/ReaderAndBuildTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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'],
Expand Down
8 changes: 4 additions & 4 deletions tests/zugferd2/Tests/Legacy/ValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.');
}
Expand Down
4 changes: 2 additions & 2 deletions tests/zugferd2/Tests/ProfileXRechnungTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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';
Expand Down
2 changes: 1 addition & 1 deletion tests/zugferd2/Tests/Traits/AssertXmlOutputTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
4 changes: 2 additions & 2 deletions tests/zugferd2/Tests/Traits/ReformatXmlTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ trait ReformatXmlTrait
{
public static function reformatXml(string $xml): string
{
$xml = preg_replace('/<!--(.|\s)*?-->/', '', $xml);
$xml = (string)preg_replace('/<!--(.|\s)*?-->/', '', $xml);

$doc = new \DOMDocument('1.0', 'UTF-8');
$doc->preserveWhiteSpace = false;
$doc->formatOutput = true;
$doc->loadXML($xml);
return $doc->saveXML();
return (string)$doc->saveXML();
}
}

0 comments on commit 2e36bcd

Please sign in to comment.