1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-30 04:39:00 +01:00

Add 'safe' methods to prevent overriding issueHandlers already defined in the configuration

This commit is contained in:
Guillaume Messier 2023-09-13 16:11:56 -04:00
parent 13ea0c241d
commit ae427fd60e
2 changed files with 132 additions and 0 deletions

View File

@ -1481,12 +1481,26 @@ class Config
$this->issue_handlers[$issue_key]->setCustomLevels($config, $this->base_dir);
}
public function safeSetAdvancedErrorLevel(string $issue_key, array $config, ?string $default_error_level = null): void
{
if (!isset($this->issue_handlers[$issue_key])) {
$this->setAdvancedErrorLevel($issue_key, $config, $default_error_level);
}
}
public function setCustomErrorLevel(string $issue_key, string $error_level): void
{
$this->issue_handlers[$issue_key] = new IssueHandler();
$this->issue_handlers[$issue_key]->setErrorLevel($error_level);
}
public function safeSetCustomErrorLevel(string $issue_key, string $error_level): void
{
if (!isset($this->issue_handlers[$issue_key])) {
$this->setCustomErrorLevel($issue_key, $error_level);
}
}
/**
* @throws ConfigException if a Config file could not be found
*/

View File

@ -979,6 +979,124 @@ class ConfigTest extends TestCase
);
}
public function testIssueHandlerOverride(): void
{
$this->project_analyzer = $this->getProjectAnalyzerWithConfig(
Config::loadFromXML(
dirname(__DIR__, 2),
'<?xml version="1.0"?>
<psalm>
<projectFiles>
<directory name="src" />
<directory name="tests" />
</projectFiles>
<issueHandlers>
<MissingReturnType errorLevel="error">
<errorLevel type="info">
<directory name="tests" />
</errorLevel>
<errorLevel type="info">
<directory name="src/Psalm/Internal/Analyzer" />
</errorLevel>
</MissingReturnType>
<UndefinedClass errorLevel="error"></UndefinedClass>
</issueHandlers>
</psalm>',
),
);
$config = $this->project_analyzer->getConfig();
$config->setAdvancedErrorLevel('MissingReturnType', [
[
'type' => 'error',
'directory' => [['name' => 'src/Psalm/Internal/Analyzer']],
],
], 'info');
$config->setCustomErrorLevel('UndefinedClass', 'suppress');
$this->assertSame(
'info',
$config->getReportingLevelForFile(
'MissingReturnType',
realpath('src/Psalm/Type.php'),
),
);
$this->assertSame(
'error',
$config->getReportingLevelForFile(
'MissingReturnType',
realpath('src/Psalm/Internal/Analyzer/FileAnalyzer.php'),
),
);
$this->assertSame(
'suppress',
$config->getReportingLevelForFile(
'UndefinedClass',
realpath('src/Psalm/Internal/Analyzer/FileAnalyzer.php'),
),
);
}
public function testIssueHandlerSafeOverride(): void
{
$this->project_analyzer = $this->getProjectAnalyzerWithConfig(
Config::loadFromXML(
dirname(__DIR__, 2),
'<?xml version="1.0"?>
<psalm>
<projectFiles>
<directory name="src" />
<directory name="tests" />
</projectFiles>
<issueHandlers>
<MissingReturnType errorLevel="error">
<errorLevel type="info">
<directory name="tests" />
</errorLevel>
<errorLevel type="info">
<directory name="src/Psalm/Internal/Analyzer" />
</errorLevel>
</MissingReturnType>
<UndefinedClass errorLevel="info"></UndefinedClass>
</issueHandlers>
</psalm>',
),
);
$config = $this->project_analyzer->getConfig();
$config->safeSetAdvancedErrorLevel('MissingReturnType', [
[
'type' => 'error',
'directory' => [['name' => 'src/Psalm/Internal/Analyzer']],
],
], 'info');
$config->safeSetCustomErrorLevel('UndefinedClass', 'suppress');
$this->assertSame(
'error',
$config->getReportingLevelForFile(
'MissingReturnType',
realpath('src/Psalm/Type.php'),
),
);
$this->assertSame(
'info',
$config->getReportingLevelForFile(
'MissingReturnType',
realpath('src/Psalm/Internal/Analyzer/FileAnalyzer.php'),
),
);
$this->assertSame(
'info',
$config->getReportingLevelForFile(
'UndefinedClass',
realpath('src/Psalm/Internal/Analyzer/FileAnalyzer.php'),
),
);
}
public function testAllPossibleIssues(): void
{
$all_possible_handlers = implode(