Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianbergmann committed Dec 20, 2024
1 parent 87bbfc7 commit c0185b7
Showing 1 changed file with 14 additions and 23 deletions.
37 changes: 14 additions & 23 deletions src/Util/Xml/Loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use function libxml_get_errors;
use function libxml_use_internal_errors;
use function sprintf;
use function trim;
use DOMDocument;

/**
Expand Down Expand Up @@ -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;
Expand All @@ -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';
}
Expand Down

0 comments on commit c0185b7

Please # to comment.