1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-22 13:51:54 +01:00

Merge pull request #10202 from gmessier/issue-handlers-priority

Issue handlers priority
This commit is contained in:
orklah 2023-09-28 09:09:33 +02:00 committed by GitHub
commit a8ef5a213d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 135 additions and 0 deletions

View File

@ -1481,12 +1481,29 @@ 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(