Skip to content

Commit

Permalink
Merge pull request #8480 from d-claassen/psalter-throws-namespaced-ex…
Browse files Browse the repository at this point in the history
…ception

Make Psalter add `@throws` annotation with properly namespaced exception
  • Loading branch information
orklah committed Sep 19, 2022
2 parents 3b7e508 + 3a6b709 commit f31f7be
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/Psalm/Internal/Analyzer/FunctionLikeAnalyzer.php
Expand Up @@ -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(
Expand Down
102 changes: 102 additions & 0 deletions tests/FileManipulation/ThrowsBlockAdditionTest.php
Expand Up @@ -118,6 +118,108 @@ function foo(string $s): string {
['MissingThrowsDocblock'],
true,
],
'addThrowsAnnotationToFunctionInNamespace' => [
'<?php
namespace Foo;
function foo(string $s): string {
if("" === $s) {
throw new \InvalidArgumentException();
}
return $s;
}',
'<?php
namespace Foo;
/**
* @throws \InvalidArgumentException
*/
function foo(string $s): string {
if("" === $s) {
throw new \InvalidArgumentException();
}
return $s;
}',
'7.4',
['MissingThrowsDocblock'],
true,
],
'addThrowsAnnotationToFunctionFromFunctionFromOtherNamespace' => [
'<?php
namespace Foo {
function foo(): void {
\Bar\bar();
}
}
namespace Bar {
class BarException extends \DomainException {}
/**
* @throws BarException
*/
function bar(): void {
throw new BarException();
}
}',
'<?php
namespace Foo {
/**
* @throws \Bar\BarException
*/
function foo(): void {
\Bar\bar();
}
}
namespace Bar {
class BarException extends \DomainException {}
/**
* @throws BarException
*/
function bar(): void {
throw new BarException();
}
}',
'7.4',
['MissingThrowsDocblock'],
true,
],
'addThrowsAnnotationAccountsForUseStatements' => [
'<?php
namespace Foo {
use Bar\BarException;
function foo(): void {
bar();
}
/**
* @throws BarException
*/
function bar(): void {
throw new BarException();
}
}
namespace Bar {
class BarException extends \DomainException {}
}',
'<?php
namespace Foo {
use Bar\BarException;
/**
* @throws BarException
*/
function foo(): void {
bar();
}
/**
* @throws BarException
*/
function bar(): void {
throw new BarException();
}
}
namespace Bar {
class BarException extends \DomainException {}
}',
'7.4',
['MissingThrowsDocblock'],
true,
],
];
}
}

0 comments on commit f31f7be

Please sign in to comment.