From 35810ebc988c6c9d8e9c187146dd29b5fffb6b5d Mon Sep 17 00:00:00 2001 From: Arne Blankerts Date: Sat, 2 Dec 2023 11:38:06 +0100 Subject: [PATCH] Make psalm happy --- src/document/DocumentCollection.php | 5 +++-- src/serializer/HTMLSerializer.php | 25 +++++++++++++++---------- 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/src/document/DocumentCollection.php b/src/document/DocumentCollection.php index a110da1..fb38eae 100644 --- a/src/document/DocumentCollection.php +++ b/src/document/DocumentCollection.php @@ -10,6 +10,7 @@ namespace Templado\Engine; use function array_push; +use function array_values; use function count; use ArrayIterator; use Countable; @@ -21,7 +22,7 @@ final class DocumentCollection implements Countable, IteratorAggregate { private array $documents; public function __construct(Document ...$documents) { - $this->documents = $documents; + $this->documents = array_values($documents); } public function count(): int { @@ -29,7 +30,7 @@ public function count(): int { } public function add(Document ...$documents): void { - array_push($this->documents, ...$documents); + array_push($this->documents, ...array_values($documents)); } /** @return ArrayIterator, Document> */ diff --git a/src/serializer/HTMLSerializer.php b/src/serializer/HTMLSerializer.php index ecc9140..8c5b1ae 100644 --- a/src/serializer/HTMLSerializer.php +++ b/src/serializer/HTMLSerializer.php @@ -29,7 +29,7 @@ class HTMLSerializer implements Serializer { private bool $withDoctypeFlag = true; - private bool $isFirst; + private bool $isFirst = true; /** @psalm-var list */ private array $filters = []; @@ -120,11 +120,12 @@ private function serializeToCleanedString(DOMDocument $document): string { } private function walk(XMLWriter $writer, DOMNode $node, array $knownPrefixes): void { - assert($node->ownerDocument instanceof DOMDocument); + $dom = $node->ownerDocument; + assert($dom instanceof DOMDocument); if (!$node instanceof DOMElement) { $writer->writeRaw( - $node->ownerDocument->saveXML($node) + $dom->saveXML($node) ); return; @@ -149,21 +150,24 @@ private function walk(XMLWriter $writer, DOMNode $node, array $knownPrefixes): v } foreach ($node->attributes as $attribute) { - assert($attribute instanceof DOMAttr); - if ($this->stripRDFaFlag && in_array($attribute->name, ['property', 'resource', 'prefix', 'typeof', 'vocab'], true)) { continue; } - if (empty($attribute->prefix)) { + $prefix = $attribute->prefix; + + if (empty($prefix)) { $writer->writeAttribute($attribute->name, $attribute->value); continue; } - if (!isset($knownPrefixes[$attribute->prefix])) { - $knownPrefixes[$attribute->prefix] = $node->lookupNamespaceURI($attribute->prefix); - $writer->writeAttribute('xmlns:' . $attribute->prefix, $node->lookupNamespaceURI($attribute->prefix)); + if (!isset($knownPrefixes[$prefix])) { + $nsUri = $node->lookupNamespaceURI($prefix); + assert($nsUri !== null); + + $knownPrefixes[$prefix] = $nsUri; + $writer->writeAttribute('xmlns:' . $prefix, $nsUri); } $writer->writeAttribute( @@ -172,7 +176,8 @@ private function walk(XMLWriter $writer, DOMNode $node, array $knownPrefixes): v ); } - foreach ((new DOMXPath($node->ownerDocument))->query('./namespace::*', $node) as $nsNode) { + foreach ((new DOMXPath($dom))->query('./namespace::*', $node) as $nsNode) { + /** @psalm-suppress DocblockTypeContradiction */ assert($nsNode instanceof DOMNameSpaceNode); if (empty($nsNode->prefix) || $nsNode->prefix === 'xml') {