Skip to content

Commit 01cbe49

Browse files
Add support for @phpstan-throws
1 parent cdbed86 commit 01cbe49

File tree

6 files changed

+72
-8
lines changed

6 files changed

+72
-8
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"ondram/ci-detector": "^3.4.0",
2424
"ondrejmirtes/better-reflection": "4.3.22",
2525
"phpdocumentor/reflection-docblock": "4.3.4",
26-
"phpstan/phpdoc-parser": "^0.4.8",
26+
"phpstan/phpdoc-parser": "^0.4.9",
2727
"react/child-process": "^0.6.1",
2828
"react/event-loop": "^1.1",
2929
"react/socket": "^1.3",

src/PhpDoc/PhpDocNodeResolver.php

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
use PHPStan\PhpDocParser\Ast\PhpDoc\MixinTagValueNode;
2121
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocNode;
2222
use PHPStan\PhpDocParser\Ast\PhpDoc\TemplateTagValueNode;
23-
use PHPStan\PhpDocParser\Ast\PhpDoc\ThrowsTagValueNode;
2423
use PHPStan\Reflection\PassedByReference;
2524
use PHPStan\Type\ErrorType;
2625
use PHPStan\Type\Generic\TemplateTypeVariance;
@@ -317,15 +316,28 @@ public function resolveReturnTag(PhpDocNode $phpDocNode, NameScope $nameScope):
317316

318317
public function resolveThrowsTags(PhpDocNode $phpDocNode, NameScope $nameScope): ?\PHPStan\PhpDoc\Tag\ThrowsTag
319318
{
320-
$types = array_map(function (ThrowsTagValueNode $throwsTagValue) use ($nameScope): Type {
321-
return $this->typeNodeResolver->resolve($throwsTagValue->type, $nameScope);
322-
}, $phpDocNode->getThrowsTagValues());
319+
$resolved = null;
320+
321+
foreach (['@phpstan-throws', '@throws'] as $tagName) {
322+
$types = [];
323+
324+
foreach ($phpDocNode->getThrowsTagValues($tagName) as $tagValue) {
325+
$type = $this->typeNodeResolver->resolve($tagValue->type, $nameScope);
326+
if ($this->shouldSkipType($tagName, $type)) {
327+
continue;
328+
}
323329

324-
if (count($types) === 0) {
325-
return null;
330+
$types[] = $type;
331+
}
332+
333+
if (count($types) === 0) {
334+
continue;
335+
}
336+
337+
$resolved = new ThrowsTag(TypeCombinator::union(...$types));
326338
}
327339

328-
return new ThrowsTag(TypeCombinator::union(...$types));
340+
return $resolved;
329341
}
330342

331343
/**

src/Rules/PhpDoc/InvalidPHPStanDocTagRule.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class InvalidPHPStanDocTagRule implements \PHPStan\Rules\Rule
2525
'@phpstan-template',
2626
'@phpstan-template-covariant',
2727
'@phpstan-return',
28+
'@phpstan-throws',
2829
'@phpstan-ignore-next-line',
2930
'@phpstan-ignore-line',
3031
];

tests/PHPStan/Reflection/Annotations/ThrowsAnnotationsTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,15 @@ public function dataThrowsAnnotations(): array
2222

2323
],
2424
],
25+
[
26+
\ThrowsAnnotations\PhpstanFoo::class,
27+
[
28+
'withoutThrows' => 'void',
29+
'throwsRuntime' => \RuntimeException::class,
30+
'staticThrowsRuntime' => \RuntimeException::class,
31+
32+
],
33+
],
2534
[
2635
\ThrowsAnnotations\FooInterface::class,
2736
[

tests/PHPStan/Reflection/Annotations/data/annotations-throws.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,40 @@ public static function staticThrowsRuntime()
4141

4242
}
4343

44+
class PhpstanFoo
45+
{
46+
/**
47+
* @throws \RuntimeException
48+
*
49+
* @phpstan-throws void
50+
*/
51+
public function withoutThrows()
52+
{
53+
54+
}
55+
56+
/**
57+
* @throws \Exception
58+
*
59+
* @phpstan-throws \RuntimeException
60+
*/
61+
public function throwsRuntime()
62+
{
63+
64+
}
65+
66+
/**
67+
* @throws \Exception
68+
*
69+
* @phpstan-throws \RuntimeException
70+
*/
71+
public static function staticThrowsRuntime()
72+
{
73+
74+
}
75+
76+
}
77+
4478
interface FooInterface
4579
{
4680

tests/PHPStan/Rules/PhpDoc/data/invalid-phpstan-doc.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,12 @@ function baz()
2727
/** @phpstan-va */$a = $b; /** @phpstan-ignore-line */
2828
$c = 'foo';
2929
}
30+
31+
/**
32+
* @phpstan-throws void
33+
*/
34+
function any()
35+
{
36+
37+
}
3038
}

0 commit comments

Comments
 (0)