From 44785fadfca997c4e47019c7aa413f3acdb004a3 Mon Sep 17 00:00:00 2001 From: dennis Date: Tue, 13 Sep 2022 11:28:26 +0200 Subject: [PATCH 1/6] Introduce test with code inside namespace --- .../ThrowsBlockAdditionTest.php | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/tests/FileManipulation/ThrowsBlockAdditionTest.php b/tests/FileManipulation/ThrowsBlockAdditionTest.php index ae9eabf415d..cf192ee04e3 100644 --- a/tests/FileManipulation/ThrowsBlockAdditionTest.php +++ b/tests/FileManipulation/ThrowsBlockAdditionTest.php @@ -118,6 +118,30 @@ function foo(string $s): string { ['MissingThrowsDocblock'], true, ], + 'addThrowsAnnotationToFunctionInNamespace' => [ + ' Date: Tue, 13 Sep 2022 11:31:20 +0200 Subject: [PATCH 2/6] Ensure @throws annotations reference valid Exception classes --- .../Internal/FileManipulation/FunctionDocblockManipulator.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Psalm/Internal/FileManipulation/FunctionDocblockManipulator.php b/src/Psalm/Internal/FileManipulation/FunctionDocblockManipulator.php index ca652333ea8..6cc3c594681 100644 --- a/src/Psalm/Internal/FileManipulation/FunctionDocblockManipulator.php +++ b/src/Psalm/Internal/FileManipulation/FunctionDocblockManipulator.php @@ -405,6 +405,7 @@ private function getDocblock(): string $inferredThrowsClause = array_reduce( $this->throwsExceptions, function (string $throwsClause, string $exception) { + $exception = '\\' . $exception; return $throwsClause === '' ? $exception : $throwsClause.'|'.$exception; }, '' From b88d2890e29155d56727d9bdb7c3d551da774440 Mon Sep 17 00:00:00 2001 From: dennis Date: Tue, 13 Sep 2022 11:32:19 +0200 Subject: [PATCH 3/6] Fix existing tests --- tests/FileManipulation/ThrowsBlockAdditionTest.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/FileManipulation/ThrowsBlockAdditionTest.php b/tests/FileManipulation/ThrowsBlockAdditionTest.php index cf192ee04e3..af4876ecd6f 100644 --- a/tests/FileManipulation/ThrowsBlockAdditionTest.php +++ b/tests/FileManipulation/ThrowsBlockAdditionTest.php @@ -20,7 +20,7 @@ function foo(string $s): string { }', ' Date: Sat, 17 Sep 2022 09:58:49 +0200 Subject: [PATCH 4/6] Add test to document cross namespace behaviour --- .../ThrowsBlockAdditionTest.php | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/tests/FileManipulation/ThrowsBlockAdditionTest.php b/tests/FileManipulation/ThrowsBlockAdditionTest.php index af4876ecd6f..02cb974e86e 100644 --- a/tests/FileManipulation/ThrowsBlockAdditionTest.php +++ b/tests/FileManipulation/ThrowsBlockAdditionTest.php @@ -142,6 +142,44 @@ function foo(string $s): string { ['MissingThrowsDocblock'], true, ], + 'addThrowsAnnotationToFunctionFromFunctionFromOtherNamespace' => [ + ' Date: Sat, 17 Sep 2022 10:55:19 +0200 Subject: [PATCH 5/6] Improved class name generation for @throws annotation --- src/Psalm/Internal/Analyzer/FunctionLikeAnalyzer.php | 9 ++++++++- .../FileManipulation/FunctionDocblockManipulator.php | 1 - tests/FileManipulation/ThrowsBlockAdditionTest.php | 8 ++++---- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/src/Psalm/Internal/Analyzer/FunctionLikeAnalyzer.php b/src/Psalm/Internal/Analyzer/FunctionLikeAnalyzer.php index 385a954c946..c9c63238b8e 100644 --- a/src/Psalm/Internal/Analyzer/FunctionLikeAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/FunctionLikeAnalyzer.php @@ -720,7 +720,14 @@ public function analyze( } if (!$is_expected) { - $missingThrowsDocblockErrors[] = $possibly_thrown_exception; + $missing_docblock_exception = new TNamedObject($possibly_thrown_exception); + $missingThrowsDocblockErrors[] = $missing_docblock_exception->toNamespacedString( + $this->source->getNamespace(), + $this->source->getAliasedClassesFlipped(), + $this->source->getFQCLN(), + true + ); + foreach ($codelocations as $codelocation) { // issues are suppressed in ThrowAnalyzer, CallAnalyzer, etc. IssueBuffer::maybeAdd( diff --git a/src/Psalm/Internal/FileManipulation/FunctionDocblockManipulator.php b/src/Psalm/Internal/FileManipulation/FunctionDocblockManipulator.php index 6cc3c594681..ca652333ea8 100644 --- a/src/Psalm/Internal/FileManipulation/FunctionDocblockManipulator.php +++ b/src/Psalm/Internal/FileManipulation/FunctionDocblockManipulator.php @@ -405,7 +405,6 @@ private function getDocblock(): string $inferredThrowsClause = array_reduce( $this->throwsExceptions, function (string $throwsClause, string $exception) { - $exception = '\\' . $exception; return $throwsClause === '' ? $exception : $throwsClause.'|'.$exception; }, '' diff --git a/tests/FileManipulation/ThrowsBlockAdditionTest.php b/tests/FileManipulation/ThrowsBlockAdditionTest.php index 02cb974e86e..b5a57c1924d 100644 --- a/tests/FileManipulation/ThrowsBlockAdditionTest.php +++ b/tests/FileManipulation/ThrowsBlockAdditionTest.php @@ -20,7 +20,7 @@ function foo(string $s): string { }', ' Date: Sat, 17 Sep 2022 10:56:29 +0200 Subject: [PATCH 6/6] Test to describe use statements being applied --- .../ThrowsBlockAdditionTest.php | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/tests/FileManipulation/ThrowsBlockAdditionTest.php b/tests/FileManipulation/ThrowsBlockAdditionTest.php index b5a57c1924d..41f4e4ae2df 100644 --- a/tests/FileManipulation/ThrowsBlockAdditionTest.php +++ b/tests/FileManipulation/ThrowsBlockAdditionTest.php @@ -180,6 +180,46 @@ function bar(): void { ['MissingThrowsDocblock'], true, ], + 'addThrowsAnnotationAccountsForUseStatements' => [ + '