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

Make Psalter add @throws annotation with properly namespaced exception #8480

Merged
merged 6 commits into from Sep 19, 2022
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
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,
],
];
}
}