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

Issue handlers priority #10202

Merged
merged 2 commits into from
Sep 28, 2023
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
17 changes: 17 additions & 0 deletions src/Psalm/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -1481,12 +1481,29 @@ public function setAdvancedErrorLevel(string $issue_key, array $config, ?string
$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
*/
Expand Down
118 changes: 118 additions & 0 deletions tests/Config/ConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -979,6 +979,124 @@ public function testIssueHandlerSetDynamically(): void
);
}

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(
Expand Down