Skip to content

Commit

Permalink
Add 'safe' methods to prevent overriding issueHandlers already define…
Browse files Browse the repository at this point in the history
…d in the configuration
  • Loading branch information
gmessier committed Sep 13, 2023
1 parent 7428e49 commit 0f9f962
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/Psalm/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -1461,12 +1461,26 @@ 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

0 comments on commit 0f9f962

Please sign in to comment.