From c0185b7ea6d036ba53e301b688bca8ac4f10a6c0 Mon Sep 17 00:00:00 2001 From: Sebastian Bergmann Date: Fri, 20 Dec 2024 15:03:05 +0100 Subject: [PATCH] Refactor --- src/Util/Xml/Loader.php | 37 ++++++++++++++----------------------- 1 file changed, 14 insertions(+), 23 deletions(-) diff --git a/src/Util/Xml/Loader.php b/src/Util/Xml/Loader.php index 1e65ec3ade3..1289c253fc6 100644 --- a/src/Util/Xml/Loader.php +++ b/src/Util/Xml/Loader.php @@ -14,6 +14,7 @@ use function libxml_get_errors; use function libxml_use_internal_errors; use function sprintf; +use function trim; use DOMDocument; /** @@ -42,25 +43,25 @@ public function loadFile(string $filename): DOMDocument ); } - return $this->load($contents, $filename); + if (trim($contents) === '') { + throw new XmlException( + sprintf( + 'Could not parse XML from empty file "%s"', + $filename, + ), + ); + } + + return $this->load($contents); } /** * @throws XmlException */ - public function load(string $actual, ?string $filename = null): DOMDocument + public function load(string $actual): DOMDocument { if ($actual === '') { - if ($filename === null) { - throw new XmlException('Could not parse XML from empty string'); - } - - throw new XmlException( - sprintf( - 'Could not parse XML from empty file "%s"', - $filename, - ), - ); + throw new XmlException('Could not parse XML from empty string'); } $document = new DOMDocument; @@ -78,17 +79,7 @@ public function load(string $actual, ?string $filename = null): DOMDocument libxml_use_internal_errors($internal); error_reporting($reporting); - if ($loaded === false || $message !== '') { - if ($filename !== null) { - throw new XmlException( - sprintf( - 'Could not load "%s"%s', - $filename, - $message !== '' ? ":\n" . $message : '', - ), - ); - } - + if ($loaded === false) { if ($message === '') { $message = 'Could not load XML for unknown reason'; }