Skip to content

Add support for @phpstan-throws #292

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

Merged
merged 1 commit into from
Aug 5, 2020
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"ondram/ci-detector": "^3.4.0",
"ondrejmirtes/better-reflection": "4.3.22",
"phpdocumentor/reflection-docblock": "4.3.4",
"phpstan/phpdoc-parser": "^0.4.8",
"phpstan/phpdoc-parser": "^0.4.9",
"react/child-process": "^0.6.1",
"react/event-loop": "^1.1",
"react/socket": "^1.3",
Expand Down
22 changes: 15 additions & 7 deletions src/PhpDoc/PhpDocNodeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
use PHPStan\PhpDocParser\Ast\PhpDoc\MixinTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\TemplateTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\ThrowsTagValueNode;
use PHPStan\Reflection\PassedByReference;
use PHPStan\Type\ErrorType;
use PHPStan\Type\Generic\TemplateTypeVariance;
Expand Down Expand Up @@ -317,15 +316,24 @@ public function resolveReturnTag(PhpDocNode $phpDocNode, NameScope $nameScope):

public function resolveThrowsTags(PhpDocNode $phpDocNode, NameScope $nameScope): ?\PHPStan\PhpDoc\Tag\ThrowsTag
{
$types = array_map(function (ThrowsTagValueNode $throwsTagValue) use ($nameScope): Type {
return $this->typeNodeResolver->resolve($throwsTagValue->type, $nameScope);
}, $phpDocNode->getThrowsTagValues());
foreach (['@phpstan-throws', '@throws'] as $tagName) {
$types = [];

if (count($types) === 0) {
return null;
foreach ($phpDocNode->getThrowsTagValues($tagName) as $tagValue) {
$type = $this->typeNodeResolver->resolve($tagValue->type, $nameScope);
if ($this->shouldSkipType($tagName, $type)) {
continue;
}

$types[] = $type;
}

if (count($types) > 0) {
return new ThrowsTag(TypeCombinator::union(...$types));
}
}

return new ThrowsTag(TypeCombinator::union(...$types));
return null;
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/Rules/PhpDoc/InvalidPHPStanDocTagRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class InvalidPHPStanDocTagRule implements \PHPStan\Rules\Rule
'@phpstan-template',
'@phpstan-template-covariant',
'@phpstan-return',
'@phpstan-throws',
'@phpstan-ignore-next-line',
'@phpstan-ignore-line',
];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ public function dataThrowsAnnotations(): array

],
],
[
\ThrowsAnnotations\PhpstanFoo::class,
[
'withoutThrows' => 'void',
'throwsRuntime' => \RuntimeException::class,
'staticThrowsRuntime' => \RuntimeException::class,

],
],
[
\ThrowsAnnotations\FooInterface::class,
[
Expand Down
34 changes: 34 additions & 0 deletions tests/PHPStan/Reflection/Annotations/data/annotations-throws.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,40 @@ public static function staticThrowsRuntime()

}

class PhpstanFoo
{
/**
* @throws \RuntimeException
*
* @phpstan-throws void
*/
public function withoutThrows()
{

}

/**
* @throws \Exception
*
* @phpstan-throws \RuntimeException
*/
public function throwsRuntime()
{

}

/**
* @throws \Exception
*
* @phpstan-throws \RuntimeException
*/
public static function staticThrowsRuntime()
{

}

}

interface FooInterface
{

Expand Down
8 changes: 8 additions & 0 deletions tests/PHPStan/Rules/PhpDoc/data/invalid-phpstan-doc.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,12 @@ function baz()
/** @phpstan-va */$a = $b; /** @phpstan-ignore-line */
$c = 'foo';
}

/**
* @phpstan-throws void
*/
function any()
{

}
}