From 91cc0012187ff1c438bfaab2fca5ca56cd3ff702 Mon Sep 17 00:00:00 2001 From: Tim van Dijen Date: Sat, 20 Jan 2024 23:46:04 +0100 Subject: [PATCH] Replace Psalm with PHPStan --- .gitattributes | 4 +- .github/workflows/php.yml | 26 +++-------- composer.json | 2 +- phpstan-dev.neon | 4 ++ phpstan.neon | 4 ++ psalm-dev.xml | 27 ----------- psalm.xml | 38 --------------- src/AbstractElement.php | 9 ++-- src/ArrayizableElementInterface.php | 4 +- src/Attribute.php | 9 ++-- src/Chunk.php | 1 - src/DOMDocumentFactory.php | 4 +- src/SerializableElementTrait.php | 7 +-- src/Utils.php | 7 ++- tests/Utils/ExtendableAttributesElement.php | 1 - tests/Utils/ExtendableElement.php | 2 +- tests/Utils/XMLDumper.php | 5 +- tests/XML/AbstractElementTest.php | 17 +++++-- tests/XML/AttributeTest.php | 17 +++++-- tests/XML/Base64ElementTraitTest.php | 13 ++++-- tests/XML/ChunkTest.php | 52 +++++++++++---------- tests/XML/DOMDocumentFactoryTest.php | 8 ++-- tests/XML/ExtendableAttributesTraitTest.php | 14 ++++++ tests/XML/ExtendableElementTest.php | 12 ++++- tests/XML/ExtendableElementTraitTest.php | 45 ++++++++++++++---- tests/XML/QNameElementTraitTest.php | 8 +++- 26 files changed, 175 insertions(+), 165 deletions(-) create mode 100644 phpstan-dev.neon create mode 100644 phpstan.neon delete mode 100644 psalm-dev.xml delete mode 100644 psalm.xml diff --git a/.gitattributes b/.gitattributes index a222650..fbf1510 100644 --- a/.gitattributes +++ b/.gitattributes @@ -9,8 +9,8 @@ codecov.yml export-ignore .editorconfig export-ignore .gitattributes export-ignore .gitignore export-ignore -psalm.xml export-ignore -psalm-dev.xml export-ignore +phpstan.neon export-ignore +phpstan-dev.neon export-ignore phpcs.xml export-ignore phpunit.xml export-ignore .php_cs.dist export-ignore diff --git a/.github/workflows/php.yml b/.github/workflows/php.yml index b65c784..b48eb8b 100644 --- a/.github/workflows/php.yml +++ b/.github/workflows/php.yml @@ -48,9 +48,8 @@ jobs: with: # Should be the higest supported version, so we can use the newest tools php-version: '8.3' - tools: composer, composer-require-checker, composer-unused, phpcs, psalm - # optional performance gain for psalm: opcache - extensions: ctype, date, dom, filter, libxml, opcache, pcre, spl, xml + tools: composer, composer-require-checker, composer-unused, phpcs, phpstan + extensions: ctype, date, dom, filter, libxml, pcre, spl, xml coverage: none - name: Setup problem matchers for PHP @@ -83,26 +82,13 @@ jobs: - name: PHP Code Sniffer run: phpcs - - name: Psalm - run: | - psalm -c psalm.xml \ - --show-info=true \ - --shepherd \ - --php-version=${{ steps.setup-php.outputs.php-version }} - - - name: Psalm (testsuite) + - name: PHPStan run: | - psalm -c psalm-dev.xml \ - --show-info=true \ - --shepherd \ - --php-version=${{ steps.setup-php.outputs.php-version }} + phpstan analyze -c phpstan.neon - - name: Psalter + - name: PHPStan (testsuite) run: | - psalm --alter \ - --issues=UnnecessaryVarAnnotation \ - --dry-run \ - --php-version=${{ steps.setup-php.outputs.php-version }} + phpstan analyze -c phpstan-dev.neon security: name: Security checks diff --git a/composer.json b/composer.json index 519ca8c..172013d 100644 --- a/composer.json +++ b/composer.json @@ -34,7 +34,7 @@ "ext-spl": "*", "ext-xmlreader": "*", - "simplesamlphp/assert": "^1.0" + "simplesamlphp/assert": "^1.1" }, "require-dev": { "simplesamlphp/simplesamlphp-test-framework": "^1.5" diff --git a/phpstan-dev.neon b/phpstan-dev.neon new file mode 100644 index 0000000..4d29b8b --- /dev/null +++ b/phpstan-dev.neon @@ -0,0 +1,4 @@ +parameters: + level: 9 + paths: + - tests diff --git a/phpstan.neon b/phpstan.neon new file mode 100644 index 0000000..db37782 --- /dev/null +++ b/phpstan.neon @@ -0,0 +1,4 @@ +parameters: + level: 6 + paths: + - src diff --git a/psalm-dev.xml b/psalm-dev.xml deleted file mode 100644 index 6116331..0000000 --- a/psalm-dev.xml +++ /dev/null @@ -1,27 +0,0 @@ - - - - - - - - - - - - - - - - - - - - diff --git a/psalm.xml b/psalm.xml deleted file mode 100644 index 4840d16..0000000 --- a/psalm.xml +++ /dev/null @@ -1,38 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/src/AbstractElement.php b/src/AbstractElement.php index 4bbce47..b104131 100644 --- a/src/AbstractElement.php +++ b/src/AbstractElement.php @@ -252,9 +252,11 @@ public static function getNamespaceURI(): ?string . '::NS constant must be defined and set to the namespace for the XML-class it represents.', RuntimeException::class, ); - Assert::nullOrValidURI(static::NS, SchemaViolationException::class); + // @phpstan-ignore classConstant.notFound + Assert::nullOrValidURI(static::NS, SchemaViolationException::class); // @phpstan-ignore-line - return static::NS; + // @phpstan-ignore classConstant.notFound + return static::NS; // @phpstan-ignore-line } @@ -272,7 +274,8 @@ public static function getNamespacePrefix(): string RuntimeException::class, ); - return strval(static::NS_PREFIX); + // @phpstan-ignore classConstant.notFound + return strval(static::NS_PREFIX); // @phpstan-ignore-line } diff --git a/src/ArrayizableElementInterface.php b/src/ArrayizableElementInterface.php index 27a995a..e107a2e 100644 --- a/src/ArrayizableElementInterface.php +++ b/src/ArrayizableElementInterface.php @@ -14,7 +14,7 @@ interface ArrayizableElementInterface /** * Create a class from an array * - * @param array $data + * @param array $data * @return static */ public static function fromArray(array $data): static; @@ -23,7 +23,7 @@ public static function fromArray(array $data): static; /** * Create an array from this class * - * @return array + * @return array */ public function toArray(): array; } diff --git a/src/Attribute.php b/src/Attribute.php index 32db8c6..a686050 100644 --- a/src/Attribute.php +++ b/src/Attribute.php @@ -89,7 +89,7 @@ public function getAttrValue(): string /** * Create a class from XML * - * @param \DOMAttr $xml + * @param \DOMAttr $attr * @return static */ public static function fromXML(DOMAttr $attr): static @@ -120,7 +120,7 @@ public function toXML(DOMElement $parent): DOMElement /** * Create a class from an array * - * @param array $data + * @param array{namespaceURI: string, namespacePrefix: string|null, attrName: string, attrValue: mixed} $data * @return static */ public static function fromArray(array $data): static @@ -139,13 +139,14 @@ public static function fromArray(array $data): static /** * Validates an array representation of this object and returns the same array with rationalized keys * - * @param array $data - * @return array + * @param array{namespaceURI: string, namespacePrefix: string|null, attrName: string, attrValue: mixed} $data + * @return array{namespaceURI: string, namespacePrefix: string|null, attrName: string, attrValue: mixed} */ private static function processArrayContents(array $data): array { $data = array_change_key_case($data, CASE_LOWER); + /** @var array{namespaceuri: string, namespaceprefix: string|null, attrname: string, attrvalue: mixed} $data */ Assert::allOneOf( array_keys($data), ['namespaceuri', 'namespaceprefix', 'attrname', 'attrvalue'], diff --git a/src/Chunk.php b/src/Chunk.php index 97926ff..6b72753 100644 --- a/src/Chunk.php +++ b/src/Chunk.php @@ -237,7 +237,6 @@ public static function getOptionalBooleanAttribute(DOMElement $xml, string $name * * @param \DOMElement $xml The element where we should search for the attribute. * @param string $name The name of the attribute. - * @param int $default The default to return in case the attribute does not exist and it is optional. * @return int * * @throws \SimpleSAML\XML\Exception\MissingAttributeException if the attribute is missing from the element diff --git a/src/DOMDocumentFactory.php b/src/DOMDocumentFactory.php index 25998ac..ddca1d5 100644 --- a/src/DOMDocumentFactory.php +++ b/src/DOMDocumentFactory.php @@ -24,7 +24,7 @@ final class DOMDocumentFactory { /** * @param string $xml - * @psalm-param non-empty-string $xml + * @param non-empty-string $xml * * @return \DOMDocument */ @@ -76,7 +76,6 @@ public static function fromFile(string $file): DOMDocument error_clear_last(); $xml = @file_get_contents($file); if ($xml === false) { - /** @psalm-var array $e */ $e = error_get_last(); $error = $e['message'] ?: "Check that the file exists and can be read."; @@ -84,7 +83,6 @@ public static function fromFile(string $file): DOMDocument } Assert::notWhitespaceOnly($xml, sprintf('File "%s" does not have content', $file), RuntimeException::class); - /** @psalm-var non-empty-string $xml */ return static::fromString($xml); } diff --git a/src/SerializableElementTrait.php b/src/SerializableElementTrait.php index a97e1f4..28d34e7 100644 --- a/src/SerializableElementTrait.php +++ b/src/SerializableElementTrait.php @@ -36,10 +36,8 @@ public function __toString(): string { $xml = $this->toXML(); - /** @psalm-var \DOMDocument $xml->ownerDocument */ $xmlString = $xml->ownerDocument->saveXML(); - /** @psalm-var non-empty-string $xmlString */ $doc = DOMDocumentFactory::fromString($xmlString); $doc->formatOutput = $this->formatOutput; @@ -52,12 +50,11 @@ public function __toString(): string * * This method will be invoked by any calls to serialize(). * - * @return array The serialized representation of this XML object. + * @return array{0: string} The serialized representation of this XML object. */ public function __serialize(): array { $xml = $this->toXML(); - /** @psalm-var \DOMDocument $xml->ownerDocument */ return [$xml->ownerDocument->saveXML($xml)]; } @@ -68,7 +65,7 @@ public function __serialize(): array * This method will be invoked by any calls to unserialize(), allowing us to restore any data that might not * be serializable in its original form (e.g.: DOM objects). * - * @param array $serialized The XML object that we want to restore. + * @param array{0: string} $serialized The XML object that we want to restore. */ public function __unserialize(array $serialized): void { diff --git a/src/Utils.php b/src/Utils.php index ba04688..7db2ab7 100644 --- a/src/Utils.php +++ b/src/Utils.php @@ -25,7 +25,7 @@ class Utils * @param \DOMElement $parent The element that contains the localized strings. * @param string $namespaceURI The namespace URI the localized strings should have. * @param string $localName The localName of the localized strings. - * @return array Localized strings. + * @return array Localized strings. */ public static function extractLocalizedStrings(DOMElement $parent, string $namespaceURI, string $localName): array { @@ -57,7 +57,7 @@ public static function extractLocalizedStrings(DOMElement $parent, string $names * @param \DOMElement $parent The element that contains the localized strings. * @param string $namespaceURI The namespace URI the string elements should have. * @param string $localName The localName of the string elements. - * @return array The string values of the various nodes. + * @return string[] The string values of the various nodes. */ public static function extractStrings(DOMElement $parent, string $namespaceURI, string $localName): array { @@ -111,7 +111,7 @@ public static function addString( * @param string $namespace The namespace of the created elements * @param string $name The name of the created elements * @param bool $localized Whether the strings are localized, and should include the xml:lang attribute. - * @param array $values The values we should create the elements from. + * @param string[] $values The values we should create the elements from. */ public static function addStrings( DOMElement $parent, @@ -122,7 +122,6 @@ public static function addStrings( ): void { $doc = $parent->ownerDocument; Assert::notNull($doc); - /** @psalm-var \DOMDocument $doc */ foreach ($values as $index => $value) { $n = $doc->createElementNS($namespace, $name); diff --git a/tests/Utils/ExtendableAttributesElement.php b/tests/Utils/ExtendableAttributesElement.php index 17bb59d..35eb48e 100644 --- a/tests/Utils/ExtendableAttributesElement.php +++ b/tests/Utils/ExtendableAttributesElement.php @@ -90,7 +90,6 @@ public static function fromXML(DOMElement $xml): static */ public function toXML(DOMElement $parent = null): DOMElement { - /** @psalm-var \DOMDocument $e->ownerDocument */ $e = $this->instantiateParentElement($parent); foreach ($this->getAttributesNS() as $attr) { diff --git a/tests/Utils/ExtendableElement.php b/tests/Utils/ExtendableElement.php index 9469aa2..d4ecbdd 100644 --- a/tests/Utils/ExtendableElement.php +++ b/tests/Utils/ExtendableElement.php @@ -31,7 +31,7 @@ class ExtendableElement extends AbstractElement /** @var string */ public const LOCALNAME = 'ExtendableElement'; - /** @var \SimpleSAML\XML\XsNamespace|array */ + /** @var \SimpleSAML\XML\XsNamespace|array */ public const XS_ANY_ELT_NAMESPACE = NS::ANY; diff --git a/tests/Utils/XMLDumper.php b/tests/Utils/XMLDumper.php index 589c53b..f3e4dca 100644 --- a/tests/Utils/XMLDumper.php +++ b/tests/Utils/XMLDumper.php @@ -17,7 +17,10 @@ final class XMLDumper { public static function dumpDOMDocumentXMLWithBase64Content(DOMDocument $document): string { + /** @var string $dump */ $dump = $document->saveXML($document->documentElement); - return preg_replace('/ *[\\r\\n] */', '', $dump); + /** @var string $result */ + $result = preg_replace('/ *[\\r\\n] */', '', $dump); + return $result; } } diff --git a/tests/XML/AbstractElementTest.php b/tests/XML/AbstractElementTest.php index 2412ddf..8f91d9b 100644 --- a/tests/XML/AbstractElementTest.php +++ b/tests/XML/AbstractElementTest.php @@ -55,7 +55,9 @@ public function testMarshalling(): void */ public function testUnmarshalling(): void { - $element = Element::fromXML(self::$xmlRepresentation->documentElement); + /** @var \DOMElement $elt */ + $elt = self::$xmlRepresentation->documentElement; + $element = Element::fromXML($elt); $this->assertEquals(2, $element->getInteger()); $this->assertEquals(false, $element->getBoolean()); @@ -67,6 +69,7 @@ public function testUnmarshalling(): void */ public function testGetAttribute(): void { + /** @var \DOMElement $xml */ $xml = self::$xmlRepresentation->documentElement; // Get mandatory attributes @@ -101,7 +104,9 @@ public function testGetAttribute(): void */ public function testGetAttributeThrowsExceptionOnMissingAttribute(): void { - $xml = clone self::$xmlRepresentation->documentElement; + /** @var \DOMElement $xml */ + $xml = self::$xmlRepresentation->documentElement; + $xml = clone $xml; $xml->removeAttribute('text'); $this->expectException(MissingAttributeException::class); @@ -113,7 +118,9 @@ public function testGetAttributeThrowsExceptionOnMissingAttribute(): void */ public function testGetBooleanAttributeThrowsExceptionOnMissingAttribute(): void { - $xml = clone self::$xmlRepresentation->documentElement; + /** @var \DOMElement $xml */ + $xml = self::$xmlRepresentation->documentElement; + $xml = clone $xml; $xml->removeAttribute('boolean'); $this->expectException(MissingAttributeException::class); @@ -125,7 +132,9 @@ public function testGetBooleanAttributeThrowsExceptionOnMissingAttribute(): void */ public function testGetIntegerAttributeThrowsExceptionOnMissingAttribute(): void { - $xml = clone self::$xmlRepresentation->documentElement; + /** @var \DOMElement $xml */ + $xml = self::$xmlRepresentation->documentElement; + $xml = clone $xml; $xml->removeAttribute('integer'); $this->expectException(MissingAttributeException::class); diff --git a/tests/XML/AttributeTest.php b/tests/XML/AttributeTest.php index 92dce9e..549f137 100644 --- a/tests/XML/AttributeTest.php +++ b/tests/XML/AttributeTest.php @@ -67,7 +67,9 @@ public function testMarshallingArray(): void */ public function testUnmarshalling(): void { - $extendableAttribute = Attribute::fromArray(self::$arrayRepresentation); + /** @var array{namespaceURI: string, namespacePrefix: string|null, attrName: string, attrValue: mixed} $arrayRepresentation */ + $arrayRepresentation = self::$arrayRepresentation; + $extendableAttribute = Attribute::fromArray($arrayRepresentation); $this->assertEquals( self::$arrayRepresentation, $extendableAttribute->toArray(), @@ -87,11 +89,14 @@ public function testMarshallingXML(): void ); $doc = DOMDocumentFactory::fromString(''); - $elt = $extendableAttribute->toXML($doc->documentElement); + /** @var \DOMElement $docElement */ + $docElement = $doc->documentElement; + + $elt = $extendableAttribute->toXML($docElement); $this->assertStringContainsString( - self::$xmlRepresentation->saveXML(), - $elt->ownerDocument->saveXML(), + strval(self::$xmlRepresentation->saveXML()), + strval($elt->ownerDocument?->saveXML()), ); } @@ -100,7 +105,9 @@ public function testMarshallingXML(): void */ public function testUnmarshallingXML(): void { - $attr = self::$xmlRepresentation->documentElement->getAttributeNodeNS('urn:x-simplesamlphp:phpunit', 'test1'); + /** @var \DOMElement $elt */ + $elt = self::$xmlRepresentation->documentElement; + $attr = $elt->getAttributeNodeNS('urn:x-simplesamlphp:phpunit', 'test1'); $extendableAttribute = Attribute::fromXML($attr); $this->assertEquals($extendableAttribute->toArray(), self::$arrayRepresentation); diff --git a/tests/XML/Base64ElementTraitTest.php b/tests/XML/Base64ElementTraitTest.php index 29d3389..8765f81 100644 --- a/tests/XML/Base64ElementTraitTest.php +++ b/tests/XML/Base64ElementTraitTest.php @@ -55,7 +55,9 @@ public function testMarshalling(): void */ public function testUnmarshalling(): void { - $base64Element = Base64Element::fromXML(self::$xmlRepresentation->documentElement); + /** @var \DOMElement $xml */ + $xml = self::$xmlRepresentation->documentElement; + $base64Element = Base64Element::fromXML($xml); $this->assertEquals('/CTj03d1DB5e2t7CTo9BEzCf5S9NRzwnBgZRlm32REI=', $base64Element->getContent()); } @@ -68,12 +70,17 @@ public function testUnmarshalling(): void public function testBase64Cases(string $xml): void { $xmlRepresentation = DOMDocumentFactory::fromString($xml); + /** @var \DOMElement $xmlElement */ + $xmlElement = $xmlRepresentation->documentElement; - $xmlElement = Base64Element::fromXML($xmlRepresentation->documentElement); + $base64 = Base64Element::fromXML($xmlElement); - $this->assertStringContainsString($xmlElement->getRawContent(), $xml); + $this->assertStringContainsString($base64->getRawContent(), $xml); } + /** + * @return array + */ public static function provideBase64Cases(): array { return [ diff --git a/tests/XML/ChunkTest.php b/tests/XML/ChunkTest.php index 97fd7f6..c413adc 100644 --- a/tests/XML/ChunkTest.php +++ b/tests/XML/ChunkTest.php @@ -42,11 +42,13 @@ public static function setUpBeforeClass(): void */ public function testMarshalling(): void { - $element = new Chunk(self::$xmlRepresentation->documentElement); + /** @var \DOMElement $xml */ + $xml = self::$xmlRepresentation->documentElement; + $chunk = new Chunk($xml); $this->assertEquals( self::$xmlRepresentation->saveXML(self::$xmlRepresentation->documentElement), - strval($element), + strval($chunk), ); } @@ -55,40 +57,40 @@ public function testMarshalling(): void */ public function testUnmarshalling(): void { - $element = Chunk::fromXML(self::$xmlRepresentation->documentElement); - - $this->assertEquals($element->getLocalName(), 'Element'); - $this->assertEquals($element->getNamespaceURI(), 'urn:x-simplesamlphp:namespace'); - $this->assertEquals($element->getprefix(), 'ssp'); - $this->assertEquals($element->getQualifiedName(), 'ssp:Element'); - $this->assertFalse($element->isEmptyElement()); - + /** @var \DOMElement $xml */ $xml = self::$xmlRepresentation->documentElement; + $chunk = new Chunk($xml); + + $this->assertEquals($chunk->getLocalName(), 'Element'); + $this->assertEquals($chunk->getNamespaceURI(), 'urn:x-simplesamlphp:namespace'); + $this->assertEquals($chunk->getprefix(), 'ssp'); + $this->assertEquals($chunk->getQualifiedName(), 'ssp:Element'); + $this->assertFalse($chunk->isEmptyElement()); // Get mandatory attributes - $this->assertEquals(2, $element::getIntegerAttribute($xml, 'integer')); - $this->assertEquals(false, $element::getBooleanAttribute($xml, 'boolean')); - $this->assertEquals('text', $element::getAttribute($xml, 'text')); + $this->assertEquals(2, $chunk::getIntegerAttribute($xml, 'integer')); + $this->assertEquals(false, $chunk::getBooleanAttribute($xml, 'boolean')); + $this->assertEquals('text', $chunk::getAttribute($xml, 'text')); // Get optional attributes - $this->assertEquals('text', $element::getOptionalAttribute($xml, 'text')); - $this->assertFalse($element::getOptionalBooleanAttribute($xml, 'boolean')); - $this->assertEquals(2, $element::getOptionalIntegerAttribute($xml, 'integer')); + $this->assertEquals('text', $chunk::getOptionalAttribute($xml, 'text')); + $this->assertFalse($chunk::getOptionalBooleanAttribute($xml, 'boolean')); + $this->assertEquals(2, $chunk::getOptionalIntegerAttribute($xml, 'integer')); // Get optional non-existing attributes - $this->assertNull($element::getOptionalAttribute($xml, 'non-existing')); - $this->assertNull($element::getOptionalBooleanAttribute($xml, 'non-existing')); - $this->assertNull($element::getOptionalIntegerAttribute($xml, 'non-existing')); + $this->assertNull($chunk::getOptionalAttribute($xml, 'non-existing')); + $this->assertNull($chunk::getOptionalBooleanAttribute($xml, 'non-existing')); + $this->assertNull($chunk::getOptionalIntegerAttribute($xml, 'non-existing')); // Get optional non-existing attributes with default - $this->assertEquals('other text', $element::getOptionalAttribute($xml, 'non-existing', 'other text')); - $this->assertTrue($element::getOptionalBooleanAttribute($xml, 'non-existing', true)); - $this->assertEquals(3, $element::getOptionalIntegerAttribute($xml, 'non-existing', 3)); + $this->assertEquals('other text', $chunk::getOptionalAttribute($xml, 'non-existing', 'other text')); + $this->assertTrue($chunk::getOptionalBooleanAttribute($xml, 'non-existing', true)); + $this->assertEquals(3, $chunk::getOptionalIntegerAttribute($xml, 'non-existing', 3)); // Get mandatory non-existing attributes $this->expectException(MissingAttributeException::class); - $element::getAttribute($xml, 'non-existing'); - $element::getBooleanAttribute($xml, 'non-existing'); - $element::getIntegerAttribute($xml, 'non-existing'); + $chunk::getAttribute($xml, 'non-existing'); + $chunk::getBooleanAttribute($xml, 'non-existing'); + $chunk::getIntegerAttribute($xml, 'non-existing'); } } diff --git a/tests/XML/DOMDocumentFactoryTest.php b/tests/XML/DOMDocumentFactoryTest.php index c15d173..9c01396 100644 --- a/tests/XML/DOMDocumentFactoryTest.php +++ b/tests/XML/DOMDocumentFactoryTest.php @@ -10,6 +10,8 @@ use SimpleSAML\XML\DOMDocumentFactory; use SimpleSAML\XML\Exception\UnparseableXMLException; +use function strval; + /** * @covers \SimpleSAML\XML\DOMDocumentFactory * @package simplesamlphp\xml-common @@ -35,7 +37,7 @@ public function testXmlStringIsCorrectlyLoaded(): void $document = DOMDocumentFactory::fromString($xml); - $this->assertXmlStringEqualsXmlString($xml, $document->saveXML()); + $this->assertXmlStringEqualsXmlString($xml, strval($document->saveXML())); } @@ -67,7 +69,7 @@ public function testFileWithValidXMLCanBeLoaded(): void $file = 'resources/xml/domdocument_valid_xml.xml'; $document = DOMDocumentFactory::fromFile($file); - $this->assertXmlStringEqualsXmlFile($file, $document->saveXML()); + $this->assertXmlStringEqualsXmlFile($file, strval($document->saveXML())); } @@ -120,7 +122,7 @@ public function testEmptyStringIsNotValid(): void $this->expectExceptionMessage( 'Expected a non-whitespace string. Got: ""', ); - /** @psalm-suppress InvalidArgument */ + /** @phpstan-ignore-next-line */ DOMDocumentFactory::fromString(''); } } diff --git a/tests/XML/ExtendableAttributesTraitTest.php b/tests/XML/ExtendableAttributesTraitTest.php index 307c72d..a6178b6 100644 --- a/tests/XML/ExtendableAttributesTraitTest.php +++ b/tests/XML/ExtendableAttributesTraitTest.php @@ -71,6 +71,7 @@ public function testIllegalNamespaceComboThrowsAnException(): void { $this->expectException(AssertionFailedException::class); new class ([self::$target]) extends ExtendableAttributesElement { + /** @return array|NS */ public function getAttributeNamespace(): array|NS { return [NS::OTHER, NS::ANY]; @@ -83,6 +84,7 @@ public function testEmptyNamespaceArrayThrowsAnException(): void { $this->expectException(AssertionFailedException::class); new class ([]) extends ExtendableAttributesElement { + /** @return array|NS */ public function getAttributeNamespace(): array|NS { return []; @@ -96,6 +98,7 @@ public function getAttributeNamespace(): array|NS public function testOtherNamespacePassingOtherSucceeds(): void { $c = new class ([self::$other]) extends ExtendableAttributesElement { + /** @return array|NS */ public function getAttributeNamespace(): array|NS { return NS::OTHER; @@ -112,6 +115,7 @@ public function testOtherNamespacePassingLocalThrowsAnException(): void { $this->expectException(AssertionFailedException::class); new class ([self::$local]) extends ExtendableAttributesElement { + /** @return array|NS */ public function getAttributeNamespace(): array|NS { return NS::OTHER; @@ -125,6 +129,7 @@ public function getAttributeNamespace(): array|NS public function testTargetNamespacePassingTargetSucceeds(): void { $c = new class ([self::$target]) extends ExtendableAttributesElement { + /** @return array|NS */ public function getAttributeNamespace(): array|NS { return NS::TARGET; @@ -140,6 +145,7 @@ public function getAttributeNamespace(): array|NS public function testTargetNamespacePassingTargetArraySucceeds(): void { $c = new class ([self::$target]) extends ExtendableAttributesElement { + /** @return array|NS */ public function getAttributeNamespace(): array|NS { return [NS::TARGET]; @@ -155,6 +161,7 @@ public function getAttributeNamespace(): array|NS public function testTargetNamespacePassingTargetArraySucceedsWithLocal(): void { $c = new class ([self::$target]) extends ExtendableAttributesElement { + /** @return array|NS */ public function getAttributeNamespace(): array|NS { return [NS::TARGET, NS::LOCAL]; @@ -171,6 +178,7 @@ public function testTargetNamespacePassingOtherThrowsAnException(): void { $this->expectException(AssertionFailedException::class); new class ([self::$other]) extends ExtendableAttributesElement { + /** @return array|NS */ public function getAttributeNamespace(): array|NS { return NS::TARGET; @@ -184,6 +192,7 @@ public function getAttributeNamespace(): array|NS public function testLocalNamespacePassingLocalSucceeds(): void { $c = new class ([self::$local]) extends ExtendableAttributesElement { + /** @return array|NS */ public function getAttributeNamespace(): array|NS { return NS::LOCAL; @@ -199,6 +208,7 @@ public function getAttributeNamespace(): array|NS public function testLocalNamespacePassingLocalArraySucceeds(): void { $c = new class ([self::$local]) extends ExtendableAttributesElement { + /** @return array|NS */ public function getAttributeNamespace(): array|NS { return [NS::LOCAL]; @@ -215,6 +225,7 @@ public function testLocalNamespacePassingTargetThrowsAnException(): void { $this->expectException(AssertionFailedException::class); new class ([self::$target]) extends ExtendableAttributesElement { + /** @return array|NS */ public function getAtributeNamespace(): array|NS { return NS::LOCAL; @@ -230,6 +241,7 @@ public function testLocalNamespacePassingOtherThrowsAnException(): void { $this->expectException(AssertionFailedException::class); new class ([self::$other]) extends ExtendableAttributesElement { + /** @return array|NS */ public function getAttributeNamespace(): array|NS { return NS::LOCAL; @@ -243,6 +255,7 @@ public function getAttributeNamespace(): array|NS public function testAnyNamespacePassingTargetSucceeds(): void { $c = new class ([self::$target]) extends ExtendableAttributesElement { + /** @return array|NS */ public function getAttributeNamespace(): array|NS { return NS::ANY; @@ -258,6 +271,7 @@ public function getAttributeNamespace(): array|NS public function testAnyNamespacePassingOtherSucceeds(): void { $c = new class ([self::$other]) extends ExtendableAttributesElement { + /** @return array|NS */ public function getAttributeNamespace(): array|NS { return NS::ANY; diff --git a/tests/XML/ExtendableElementTest.php b/tests/XML/ExtendableElementTest.php index d0ae1fc..5cf1073 100644 --- a/tests/XML/ExtendableElementTest.php +++ b/tests/XML/ExtendableElementTest.php @@ -45,10 +45,18 @@ public static function setUpBeforeClass(): void */ public function testMarshalling(): void { + $dummyDocument1 = DOMDocumentFactory::fromString('some'); + $dummyDocument2 = DOMDocumentFactory::fromString('some'); + + /** @var \DOMElement $dummyElement1 */ + $dummyElement1 = $dummyDocument1->documentElement; + /** @var \DOMElement $dummyElement2 */ + $dummyElement2 = $dummyDocument2->documentElement; + $extendableElement = new ExtendableElement( [ - new Chunk(DOMDocumentFactory::fromString('some')->documentElement), - new Chunk(DOMDocumentFactory::fromString('some')->documentElement), + new Chunk($dummyElement1), + new Chunk($dummyElement2), ], ); diff --git a/tests/XML/ExtendableElementTraitTest.php b/tests/XML/ExtendableElementTraitTest.php index 744da6f..1ed927a 100644 --- a/tests/XML/ExtendableElementTraitTest.php +++ b/tests/XML/ExtendableElementTraitTest.php @@ -40,25 +40,39 @@ final class ExtendableElementTraitTest extends TestCase */ public static function setUpBeforeClass(): void { - self::$empty = new Chunk(DOMDocumentFactory::fromString(<< XML - )->documentElement); + ); - self::$local = new Chunk(DOMDocumentFactory::fromString(<<some XML - )->documentElement); + ); - self::$target = new Chunk(DOMDocumentFactory::fromString(<<some XML - )->documentElement); + ); - self::$other = new Chunk(DOMDocumentFactory::fromString(<<some XML - )->documentElement); + ); + + /** @var \DOMElement $emptyElement */ + $emptyElement = $emptyDocument->documentElement; + /** @var \DOMElement $localElement */ + $localElement = $localDocument->documentElement; + /** @var \DOMElement $targetElement */ + $targetElement = $targetDocument->documentElement; + /** @var \DOMElement $otherElement */ + $otherElement = $otherDocument->documentElement; + + self::$empty = new Chunk($emptyElement); + self::$local = new Chunk($localElement); + self::$target = new Chunk($targetElement); + self::$other = new Chunk($otherElement); } @@ -68,6 +82,7 @@ public function testIllegalNamespaceComboThrowsAnException(): void { $this->expectException(AssertionFailedException::class); new class ([]) extends ExtendableElement { + /** @return array|NS */ public function getElementNamespace(): array|NS { return [NS::OTHER, NS::ANY]; @@ -82,6 +97,7 @@ public function testEmptyNamespaceArrayThrowsAnException(): void { $this->expectException(AssertionFailedException::class); new class ([]) extends ExtendableElement { + /** @return array|NS */ public function getElementNamespace(): array|NS { return []; @@ -95,6 +111,7 @@ public function getElementNamespace(): array|NS public function testOtherNamespacePassingOtherSucceeds(): void { $c = new class ([self::$other]) extends ExtendableElement { + /** @return array|NS */ public function getElementNamespace(): array|NS { return NS::OTHER; @@ -111,6 +128,7 @@ public function testOtherNamespacePassingLocalThrowsAnException(): void { $this->expectException(AssertionFailedException::class); new class ([self::$local]) extends ExtendableElement { + /** @return array|NS */ public function getElementNamespace(): array|NS { return NS::OTHER; @@ -124,6 +142,7 @@ public function getElementNamespace(): array|NS public function testTargetNamespacePassingTargetSucceeds(): void { $c = new class ([self::$target]) extends ExtendableElement { + /** @return array|NS */ public function getElementNamespace(): array|NS { return NS::TARGET; @@ -139,6 +158,7 @@ public function getElementNamespace(): array|NS public function testTargetNamespacePassingTargetArraySucceeds(): void { $c = new class ([self::$target]) extends ExtendableElement { + /** @return array|NS */ public function getElementNamespace(): array|NS { return [NS::TARGET]; @@ -154,6 +174,7 @@ public function getElementNamespace(): array|NS public function testTargetNamespacePassingTargetArraySucceedsWithLocal(): void { $c = new class ([self::$target]) extends ExtendableElement { + /** @return array|NS */ public function getElementNamespace(): array|NS { return [NS::TARGET, NS::LOCAL]; @@ -170,6 +191,7 @@ public function testTargetNamespacePassingOtherThrowsAnException(): void { $this->expectException(AssertionFailedException::class); new class ([self::$other]) extends ExtendableElement { + /** @return array|NS */ public function getElementNamespace(): array|NS { return NS::TARGET; @@ -183,6 +205,7 @@ public function getElementNamespace(): array|NS public function testLocalNamespacePassingLocalSucceeds(): void { $c = new class ([self::$local]) extends ExtendableElement { + /** @return array|NS */ public function getElementNamespace(): array|NS { return NS::LOCAL; @@ -198,6 +221,7 @@ public function getElementNamespace(): array|NS public function testLocalNamespacePassingLocalArraySucceeds(): void { $c = new class ([self::$local]) extends ExtendableElement { + /** @return array|NS */ public function getElementNamespace(): array|NS { return [NS::LOCAL]; @@ -214,6 +238,7 @@ public function testLocalNamespacePassingTargetThrowsAnException(): void { $this->expectException(AssertionFailedException::class); new class ([self::$target]) extends ExtendableElement { + /** @return array|NS */ public function getElementNamespace(): array|NS { return NS::LOCAL; @@ -228,6 +253,7 @@ public function testLocalNamespacePassingOtherThrowsAnException(): void { $this->expectException(AssertionFailedException::class); new class ([self::$other]) extends ExtendableElement { + /** @return array|NS */ public function getElementNamespace(): array|NS { return NS::LOCAL; @@ -241,6 +267,7 @@ public function getElementNamespace(): array|NS public function testAnyNamespacePassingLocalSucceeds(): void { $c = new class ([self::$local]) extends ExtendableElement { + /** @return array|NS */ public function getElementNamespace(): array|NS { return NS::ANY; @@ -256,6 +283,7 @@ public function getElementNamespace(): array|NS public function testAnyNamespacePassingTargetSucceeds(): void { $c = new class ([self::$target]) extends ExtendableElement { + /** @return array|NS */ public function getElementNamespace(): array|NS { return NS::ANY; @@ -271,6 +299,7 @@ public function getElementNamespace(): array|NS public function testAnyNamespacePassingOtherSucceeds(): void { $c = new class ([self::$other]) extends ExtendableElement { + /** @return array|NS */ public function getElementNamespace(): array|NS { return NS::ANY; diff --git a/tests/XML/QNameElementTraitTest.php b/tests/XML/QNameElementTraitTest.php index 100a64b..f0300f6 100644 --- a/tests/XML/QNameElementTraitTest.php +++ b/tests/XML/QNameElementTraitTest.php @@ -77,9 +77,13 @@ public function testMarshallingInvalidQualifiedNameThrowsException(): void */ public function testUnmarshallingNonNamepacedQualifiedName(): void { - $qnameElement = QNameElement::fromXML(DOMDocumentFactory::fromString( + $doc = DOMDocumentFactory::fromString( 'Sender' - )->documentElement); + ); + + /** @var \DOMElement $element */ + $element = $doc->documentElement; + $qnameElement = QNameElement::fromXML($element); $this->assertEquals('Sender', $qnameElement->getContent()); $this->assertNull($qnameElement->getContentNamespaceUri());