Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix side effect when loading config #7506

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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