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

Do not throw exception on xinclude with fallbacks #7324

Merged
merged 1 commit into from Jan 7, 2022
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
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')));
}
}