From ba9b82e957f8bf7f33c1d6ff5fee60384ec5fad5 Mon Sep 17 00:00:00 2001 From: Toon Verwerft Date: Fri, 19 Jan 2024 14:29:55 +0100 Subject: [PATCH] Fix psalm 5.20 --- src/Xml/Dom/Manipulator/Attribute/rename.php | 2 +- src/Xml/Dom/Manipulator/Element/rename.php | 2 +- src/Xml/Dom/Manipulator/Xmlns/rename.php | 3 ++- src/Xml/Dom/Traverser/Visitor/RemoveNamespaces.php | 2 +- src/Xml/Encoding/Internal/Encoder/Builder/element.php | 4 ++-- src/Xml/Reader/Matcher/nested.php | 4 ++-- src/Xml/Reader/Reader.php | 2 ++ src/Xml/Writer/Builder/namespace_attribute.php | 3 ++- src/Xml/Xsd/Schema/Manipulator/overwrite_with_local_files.php | 1 + 9 files changed, 14 insertions(+), 9 deletions(-) diff --git a/src/Xml/Dom/Manipulator/Attribute/rename.php b/src/Xml/Dom/Manipulator/Attribute/rename.php index d28542e..5cc2eb7 100644 --- a/src/Xml/Dom/Manipulator/Attribute/rename.php +++ b/src/Xml/Dom/Manipulator/Attribute/rename.php @@ -21,7 +21,7 @@ function rename(DOMAttr $target, string $newQName, ?string $newNamespaceURI = nu $namespace = $newNamespaceURI ?? $target->namespaceURI; $value = $target->nodeValue ?? ''; - $builder = $namespace + $builder = $namespace !== null ? namespaced_attribute($namespace, $newQName, $value) : attribute($newQName, $value); diff --git a/src/Xml/Dom/Manipulator/Element/rename.php b/src/Xml/Dom/Manipulator/Element/rename.php index b376b40..4b85cfb 100644 --- a/src/Xml/Dom/Manipulator/Element/rename.php +++ b/src/Xml/Dom/Manipulator/Element/rename.php @@ -26,7 +26,7 @@ function rename(DOMElement $target, string $newQName, ?string $newNamespaceURI = $isRootElement = $target->ownerDocument && $target === $target->ownerDocument->documentElement; $parent = $isRootElement ? $target->ownerDocument : parent_element($target); $namespace = $newNamespaceURI ?? $target->namespaceURI; - $builder = $namespace + $builder = $namespace !== null ? namespaced_element($namespace, $newQName) : element($newQName); diff --git a/src/Xml/Dom/Manipulator/Xmlns/rename.php b/src/Xml/Dom/Manipulator/Xmlns/rename.php index d6e8572..21f7b9f 100644 --- a/src/Xml/Dom/Manipulator/Xmlns/rename.php +++ b/src/Xml/Dom/Manipulator/Xmlns/rename.php @@ -28,7 +28,8 @@ function rename(DOMDocument $document, string $namespaceURI, string $newPrefix): void { // Check for prefix collisions - if (($existingUri = $document->lookupNamespaceURI($newPrefix)) && $existingUri !== $namespaceURI) { + $existingUri = $document->lookupNamespaceURI($newPrefix); + if ($existingUri !== null && $existingUri !== $namespaceURI) { throw RuntimeException::withMessage( sprintf( 'Cannot rename the namespace uri %s because the prefix %s is already linked to uri %s', diff --git a/src/Xml/Dom/Traverser/Visitor/RemoveNamespaces.php b/src/Xml/Dom/Traverser/Visitor/RemoveNamespaces.php index 40f5d6b..e9cad27 100644 --- a/src/Xml/Dom/Traverser/Visitor/RemoveNamespaces.php +++ b/src/Xml/Dom/Traverser/Visitor/RemoveNamespaces.php @@ -76,7 +76,7 @@ public function onNodeLeave(DOMNode $node): Action } $namespaces = xmlns_attributes_list($node); - if ($this->filter) { + if ($this->filter !== null) { $namespaces = $namespaces->filter($this->filter); } diff --git a/src/Xml/Encoding/Internal/Encoder/Builder/element.php b/src/Xml/Encoding/Internal/Encoder/Builder/element.php index a9c86e3..e7c6277 100644 --- a/src/Xml/Encoding/Internal/Encoder/Builder/element.php +++ b/src/Xml/Encoding/Internal/Encoder/Builder/element.php @@ -51,7 +51,7 @@ function element(string $name, array $data): Closure /** @var list $children */ $children = filter_nulls([ - $attributes ? attributes($attributes) : null, + $attributes !== null ? attributes($attributes) : null, $namedNamespaces ? xmlns_attributes($namedNamespaces) : null, $cdata !== null ? childrenBuilder(cdata($cdata)) : null, $value !== null ? escaped_value($value) : null, @@ -66,7 +66,7 @@ function element(string $name, array $data): Closure )), ]); - return $currentNamespace + return $currentNamespace !== null ? namespacedElementBuilder($currentNamespace, $name, ...$children) : elementBuilder($name, ...$children); } diff --git a/src/Xml/Reader/Matcher/nested.php b/src/Xml/Reader/Matcher/nested.php index 3e552d2..0ec69a6 100644 --- a/src/Xml/Reader/Matcher/nested.php +++ b/src/Xml/Reader/Matcher/nested.php @@ -31,7 +31,7 @@ function nested(callable ... $matchers): Closure return static function (NodeSequence $sequence) use ($matchers) : bool { $lastMatchedAtIndex = -1; $currentMatcher = array_shift($matchers); - if (!$currentMatcher) { + if ($currentMatcher === null) { return false; } @@ -51,7 +51,7 @@ function nested(callable ... $matchers): Closure // If the list of matchers is empty // The function will return true if the element is the last step in the complete sequence. // Otherwise, the nested match has an even deeper element on which we don't wish to match. - if (!$currentMatcher) { + if ($currentMatcher === null) { $isLastStep = $index === $stepCount - 1; return $isLastStep; diff --git a/src/Xml/Reader/Reader.php b/src/Xml/Reader/Reader.php index 2138be5..2bb3927 100644 --- a/src/Xml/Reader/Reader.php +++ b/src/Xml/Reader/Reader.php @@ -101,6 +101,8 @@ static function () use ($reader): array { $pointer->enterElement($element); $outerXml = $matcher($pointer->getNodeSequence()) ? $reader->readOuterXml() : null; + + /** @psalm-suppress RiskyTruthyFalsyComparison */ $match = $outerXml ? new MatchingNode($outerXml, $pointer->getNodeSequence()) : null; if ($isEmptyElement) { diff --git a/src/Xml/Writer/Builder/namespace_attribute.php b/src/Xml/Writer/Builder/namespace_attribute.php index 4d5ca07..8d3e68f 100644 --- a/src/Xml/Writer/Builder/namespace_attribute.php +++ b/src/Xml/Writer/Builder/namespace_attribute.php @@ -9,6 +9,7 @@ use XMLWriter; /** + * @param non-empty-string|null $prefix * @return Closure(XMLWriter): Generator */ function namespace_attribute(string $namespace, ?string $prefix = null): Closure @@ -18,7 +19,7 @@ function namespace_attribute(string $namespace, ?string $prefix = null): Closure * @return Generator */ static function (XMLWriter $writer) use ($namespace, $prefix): Generator { - if ($prefix) { + if ($prefix !== null) { yield from prefixed_attribute('xmlns', $prefix, $namespace)($writer); return; } diff --git a/src/Xml/Xsd/Schema/Manipulator/overwrite_with_local_files.php b/src/Xml/Xsd/Schema/Manipulator/overwrite_with_local_files.php index 12ebfc7..b340c37 100644 --- a/src/Xml/Xsd/Schema/Manipulator/overwrite_with_local_files.php +++ b/src/Xml/Xsd/Schema/Manipulator/overwrite_with_local_files.php @@ -19,6 +19,7 @@ function overwrite_with_local_files(array $map): Closure new SchemaCollection( ...$schemas->map( static function (Schema $schema) use ($map): Schema { + /** @psalm-suppress RiskyTruthyFalsyComparison */ if (!$namespace = $schema->namespace()) { return $schema; }