Skip to content

Commit

Permalink
Merge pull request #7506 from AndrolGenhald/bugfix/fix-side-effect-wh…
Browse files Browse the repository at this point in the history
…en-loading-config
  • Loading branch information
weirdan committed Jan 28, 2022
2 parents b9d9a49 + b473d81 commit 8914793
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
24 changes: 13 additions & 11 deletions src/Psalm/Config.php
Expand Up @@ -721,6 +721,10 @@ private static function validateXmlConfig(string $base_dir, string $file_content
throw new ConfigException('Cannot locate config schema');
}

// Enable user error handling
$prev_xml_internal_errors = libxml_use_internal_errors(true);
libxml_clear_errors();

$dom_document = self::loadDomDocument($base_dir, $file_contents);

$psalm_nodes = $dom_document->getElementsByTagName('psalm');
Expand All @@ -743,19 +747,17 @@ private static function validateXmlConfig(string $base_dir, string $file_content
$dom_document = self::loadDomDocument($base_dir, $old_file_contents);
}

// Enable user error handling
libxml_use_internal_errors(true);
$dom_document->schemaValidate($schema_path); // If it returns false it will generate errors handled below

if (!$dom_document->schemaValidate($schema_path)) {
$errors = libxml_get_errors();
foreach ($errors as $error) {
if ($error->level === LIBXML_ERR_FATAL || $error->level === LIBXML_ERR_ERROR) {
throw new ConfigException(
'Error on line ' . $error->line . ":\n" . ' ' . $error->message
);
}
$errors = libxml_get_errors();
libxml_clear_errors();
libxml_use_internal_errors($prev_xml_internal_errors);
foreach ($errors as $error) {
if ($error->level === LIBXML_ERR_FATAL || $error->level === LIBXML_ERR_ERROR) {
throw new ConfigException(
'Error on line ' . $error->line . ":\n" . ' ' . $error->message
);
}
libxml_clear_errors();
}
}

Expand Down
1 change: 1 addition & 0 deletions tests/Config/ConfigTest.php
Expand Up @@ -1531,6 +1531,7 @@ public function testStrictTypesForFileReporting(): void

public function testConfigFileWithXIncludeWithoutFallbackShouldThrowException(): void
{
$this->expectException(ConfigException::class);
$this->expectExceptionMessageMatches('/and no fallback was found/');
ErrorHandler::install();
Config::loadFromXML(
Expand Down

0 comments on commit 8914793

Please sign in to comment.