From ae427fd60e6a1de4f8b2b9a0461d3ba6a3a1f0a0 Mon Sep 17 00:00:00 2001 From: Guillaume Messier Date: Wed, 13 Sep 2023 16:11:56 -0400 Subject: [PATCH 1/2] Add 'safe' methods to prevent overriding issueHandlers already defined in the configuration --- src/Psalm/Config.php | 14 +++++ tests/Config/ConfigTest.php | 118 ++++++++++++++++++++++++++++++++++++ 2 files changed, 132 insertions(+) diff --git a/src/Psalm/Config.php b/src/Psalm/Config.php index 1439c9bb3..107e6bbaf 100644 --- a/src/Psalm/Config.php +++ b/src/Psalm/Config.php @@ -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 */ diff --git a/tests/Config/ConfigTest.php b/tests/Config/ConfigTest.php index c4b682967..17d6a2e5c 100644 --- a/tests/Config/ConfigTest.php +++ b/tests/Config/ConfigTest.php @@ -979,6 +979,124 @@ class ConfigTest extends TestCase ); } + public function testIssueHandlerOverride(): void + { + $this->project_analyzer = $this->getProjectAnalyzerWithConfig( + Config::loadFromXML( + dirname(__DIR__, 2), + ' + + + + + + + + + + + + + + + + + ', + ), + ); + + $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), + ' + + + + + + + + + + + + + + + + + ', + ), + ); + + $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( From bb364a23adfdcf1e965205f2abb660545082b96c Mon Sep 17 00:00:00 2001 From: Guillaume Messier Date: Wed, 13 Sep 2023 16:44:53 -0400 Subject: [PATCH 2/2] Fix Code Style --- src/Psalm/Config.php | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/Psalm/Config.php b/src/Psalm/Config.php index 107e6bbaf..0eaa9f837 100644 --- a/src/Psalm/Config.php +++ b/src/Psalm/Config.php @@ -1481,8 +1481,11 @@ 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 - { + 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); }