Skip to content

Commit

Permalink
Merge pull request #7324 from vstm/fix-xinclude-missing-files
Browse files Browse the repository at this point in the history
  • Loading branch information
weirdan committed Jan 7, 2022
2 parents 760badd + 7f2f0d9 commit 4080b03
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/Psalm/Config.php
Expand Up @@ -104,6 +104,7 @@
use const LIBXML_ERR_ERROR;
use const LIBXML_ERR_FATAL;
use const LIBXML_NONET;
use const LIBXML_NOWARNING;
use const PHP_EOL;
use const PHP_VERSION_ID;
use const PSALM_VERSION;
Expand Down Expand Up @@ -691,7 +692,7 @@ private static function loadDomDocument(string $base_dir, string $file_contents)
chdir($base_dir);

$dom_document->loadXML($file_contents, LIBXML_NONET);
$dom_document->xinclude(LIBXML_NONET);
$dom_document->xinclude(LIBXML_NOWARNING | LIBXML_NONET);

chdir($oldpwd);
return $dom_document;
Expand Down
63 changes: 63 additions & 0 deletions tests/Config/ConfigTest.php
Expand Up @@ -11,6 +11,7 @@
use Psalm\Exception\ConfigException;
use Psalm\Internal\Analyzer\FileAnalyzer;
use Psalm\Internal\Analyzer\ProjectAnalyzer;
use Psalm\Internal\ErrorHandler;
use Psalm\Internal\Provider\FakeFileProvider;
use Psalm\Internal\Provider\Providers;
use Psalm\Internal\RuntimeCaches;
Expand All @@ -32,6 +33,7 @@
use function is_array;
use function preg_match;
use function realpath;
use function set_error_handler;
use function sprintf;
use function symlink;
use function uniqid;
Expand All @@ -47,6 +49,9 @@ class ConfigTest extends TestCase
/** @var ProjectAnalyzer */
protected $project_analyzer;

/** @var callable(int, string, string=, int=, array=):bool|null */
protected $original_error_handler = null;

public static function setUpBeforeClass(): void
{
self::$config = new TestConfig();
Expand All @@ -64,6 +69,8 @@ public function setUp(): void
{
RuntimeCaches::clearAll();
$this->file_provider = new FakeFileProvider();
$this->original_error_handler = set_error_handler(null);
set_error_handler($this->original_error_handler);
}

private function getProjectAnalyzerWithConfig(Config $config): ProjectAnalyzer
Expand Down Expand Up @@ -1098,6 +1105,8 @@ public function testModularConfig(): void

public function tearDown(): void
{
set_error_handler($this->original_error_handler);

parent::tearDown();

if ($this->getName() === 'testTemplatedFiles') {
Expand Down Expand Up @@ -1549,4 +1558,58 @@ public function testStrictTypesForFileReporting(): void
$this->assertFalse($config->useStrictTypesForFile(realpath('src/Psalm/Report') . DIRECTORY_SEPARATOR));
$this->assertFalse($config->useStrictTypesForFile(realpath('src/Psalm/SourceControl') . DIRECTORY_SEPARATOR));
}

public function testConfigFileWithXIncludeWithoutFallbackShouldThrowException(): void
{
$this->expectException(ConfigException::class);
$this->expectExceptionMessageMatches('/and no fallback was found/');
ErrorHandler::install();
Config::loadFromXML(
dirname(__DIR__, 2),
'<?xml version="1.0"?>
<psalm xmlns:xi="http://www.w3.org/2001/XInclude">
<projectFiles>
<directory name="src" />
<directory name="tests" />
</projectFiles>
<issueHandlers>
<xi:include href="zz.xml" />
</issueHandlers>
</psalm>'
);
}

public function testConfigFileWithXIncludeWithFallback(): void
{
ErrorHandler::install();
$this->project_analyzer = $this->getProjectAnalyzerWithConfig(
Config::loadFromXML(
dirname(__DIR__, 2),
'<?xml version="1.0"?>
<psalm xmlns:xi="http://www.w3.org/2001/XInclude">
<projectFiles>
<directory name="src" />
<directory name="tests" />
</projectFiles>
<issueHandlers>
<xi:include href="zz.xml">
<xi:fallback>
<MixedAssignment>
<errorLevel type="suppress">
<file name="src/Psalm/Type.php" />
</errorLevel>
</MixedAssignment>
</xi:fallback>
</xi:include>
</issueHandlers>
</psalm>'
)
);

$config = $this->project_analyzer->getConfig();

$this->assertFalse($config->reportIssueInFile('MixedAssignment', realpath('src/Psalm/Type.php')));
}
}

0 comments on commit 4080b03

Please sign in to comment.