Skip to content

Commit

Permalink
Make psalm happy
Browse files Browse the repository at this point in the history
  • Loading branch information
theseer committed Dec 2, 2023
1 parent d7ae28d commit 35810eb
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
5 changes: 3 additions & 2 deletions src/document/DocumentCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace Templado\Engine;

use function array_push;
use function array_values;
use function count;
use ArrayIterator;
use Countable;
Expand All @@ -21,15 +22,15 @@ 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 {
return count($this->documents);
}

public function add(Document ...$documents): void {
array_push($this->documents, ...$documents);
array_push($this->documents, ...array_values($documents));
}

/** @return ArrayIterator<int<0,max>, Document> */
Expand Down
25 changes: 15 additions & 10 deletions src/serializer/HTMLSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class HTMLSerializer implements Serializer {

private bool $withDoctypeFlag = true;

private bool $isFirst;
private bool $isFirst = true;

/** @psalm-var list<Filter> */
private array $filters = [];
Expand Down Expand Up @@ -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;
Expand All @@ -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(
Expand All @@ -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') {
Expand Down

0 comments on commit 35810eb

Please sign in to comment.