diff --git a/src/Psalm/Config.php b/src/Psalm/Config.php index d99f30d3db3..fe2208d0d80 100644 --- a/src/Psalm/Config.php +++ b/src/Psalm/Config.php @@ -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'); @@ -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(); } } diff --git a/tests/Config/ConfigTest.php b/tests/Config/ConfigTest.php index 465f4087192..9d5b5a97754 100644 --- a/tests/Config/ConfigTest.php +++ b/tests/Config/ConfigTest.php @@ -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(