Skip to content

Commit c35515d

Browse files
Add Throws attribute
1 parent 8032ba7 commit c35515d

5 files changed

+142
-24
lines changed

README.md

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -112,29 +112,30 @@ These are the available attributes and their corresponding PHPDoc annotations:
112112

113113
| Attribute | PHPDoc Annotations |
114114
|-------------------------------------------------------------------------------------------------------------------|--------------------|
115-
| [Deprecated](https://github.com/php-static-analysis/attributes/blob/main/doc/Deprecated.md) | `@deprecated` |
116-
| [Impure](https://github.com/php-static-analysis/attributes/blob/main/doc/Impure.md) | `@impure` |
117-
| [Internal](https://github.com/php-static-analysis/attributes/blob/main/doc/Internal.md) | `@internal` |
118-
| [IsReadOnly](https://github.com/php-static-analysis/attributes/blob/main/doc/IsReadOnly.md) | `@readonly` |
119-
| [Method](https://github.com/php-static-analysis/attributes/blob/main/doc/Method.md) | `@method` |
120-
| [Mixin](https://github.com/php-static-analysis/attributes/blob/main/doc/Mixin.md) | `@mixin` |
121-
| [Param](https://github.com/php-static-analysis/attributes/blob/main/doc/Param.md) | `@param` |
122-
| [ParamOut](https://github.com/php-static-analysis/attributes/blob/main/doc/ParamOut.md) | `@param-out` |
123-
| [Property](https://github.com/php-static-analysis/attributes/blob/main/doc/Property.md) | `@property` `@var` |
124-
| [PropertyRead](https://github.com/php-static-analysis/attributes/blob/main/doc/PropertyRead.md) | `@property-read` |
125-
| [PropertyWrite](https://github.com/php-static-analysis/attributes/blob/main/doc/PropertyWrite.md) | `@property-write` |
126-
| [Pure](https://github.com/php-static-analysis/attributes/blob/main/doc/Pure.md) | `@pure` |
127-
| [RequireExtends](https://github.com/php-static-analysis/attributes/blob/main/doc/RequireExtends.md) | `@require-extends` |
128-
| [RequireImplements](https://github.com/php-static-analysis/attributes/blob/main/doc/RequireImplements.md) | `@require-implements` |
129-
| [Returns](https://github.com/php-static-analysis/attributes/blob/main/doc/Returns.md) | `@return` |
130-
| [SelfOut](https://github.com/php-static-analysis/attributes/blob/main/doc/SelfOut.md) | `@self-out` `@this-out` |
131-
| [Template](https://github.com/php-static-analysis/attributes/blob/main/doc/Template.md) | `@template` |
132-
| [TemplateContravariant](https://github.com/php-static-analysis/attributes/blob/main/doc/TemplateContravariant.md) | `@template-contravariant` |
133-
| [TemplateCovariant](https://github.com/php-static-analysis/attributes/blob/main/doc/TemplateCovariant.md) | `@template-covariant` |
134-
| [TemplateExtends](https://github.com/php-static-analysis/attributes/blob/main/doc/TemplateExtends.md) | `@extends` `@template-extends` |
135-
| [TemplateImplements](https://github.com/php-static-analysis/attributes/blob/main/doc/TemplateImplements.md) | `@implements` `@template-implements` |
136-
| [TemplateUse](https://github.com/php-static-analysis/attributes/blob/main/doc/TemplateUse.md) | `@use` `@template-use` |
137-
| [Type](https://github.com/php-static-analysis/attributes/blob/main/doc/Type.md) | `@var` `@return` |
115+
| [Deprecated](https://github.com/php-static-analysis/attributes/blob/main/doc/Deprecated.md) | `@deprecated` |
116+
| [Impure](https://github.com/php-static-analysis/attributes/blob/main/doc/Impure.md) | `@impure` |
117+
| [Internal](https://github.com/php-static-analysis/attributes/blob/main/doc/Internal.md) | `@internal` |
118+
| [IsReadOnly](https://github.com/php-static-analysis/attributes/blob/main/doc/IsReadOnly.md) | `@readonly` |
119+
| [Method](https://github.com/php-static-analysis/attributes/blob/main/doc/Method.md) | `@method` |
120+
| [Mixin](https://github.com/php-static-analysis/attributes/blob/main/doc/Mixin.md) | `@mixin` |
121+
| [Param](https://github.com/php-static-analysis/attributes/blob/main/doc/Param.md) | `@param` |
122+
| [ParamOut](https://github.com/php-static-analysis/attributes/blob/main/doc/ParamOut.md) | `@param-out` |
123+
| [Property](https://github.com/php-static-analysis/attributes/blob/main/doc/Property.md) | `@property` `@var` |
124+
| [PropertyRead](https://github.com/php-static-analysis/attributes/blob/main/doc/PropertyRead.md) | `@property-read` |
125+
| [PropertyWrite](https://github.com/php-static-analysis/attributes/blob/main/doc/PropertyWrite.md) | `@property-write` |
126+
| [Pure](https://github.com/php-static-analysis/attributes/blob/main/doc/Pure.md) | `@pure` |
127+
| [RequireExtends](https://github.com/php-static-analysis/attributes/blob/main/doc/RequireExtends.md) | `@require-extends` |
128+
| [RequireImplements](https://github.com/php-static-analysis/attributes/blob/main/doc/RequireImplements.md) | `@require-implements` |
129+
| [Returns](https://github.com/php-static-analysis/attributes/blob/main/doc/Returns.md) | `@return` |
130+
| [SelfOut](https://github.com/php-static-analysis/attributes/blob/main/doc/SelfOut.md) | `@self-out` `@this-out` |
131+
| [Template](https://github.com/php-static-analysis/attributes/blob/main/doc/Template.md) | `@template` |
132+
| [TemplateContravariant](https://github.com/php-static-analysis/attributes/blob/main/doc/TemplateContravariant.md) | `@template-contravariant` |
133+
| [TemplateCovariant](https://github.com/php-static-analysis/attributes/blob/main/doc/TemplateCovariant.md) | `@template-covariant` |
134+
| [TemplateExtends](https://github.com/php-static-analysis/attributes/blob/main/doc/TemplateExtends.md) | `@extends` `@template-extends` |
135+
| [TemplateImplements](https://github.com/php-static-analysis/attributes/blob/main/doc/TemplateImplements.md) | `@implements` `@template-implements` |
136+
| [TemplateUse](https://github.com/php-static-analysis/attributes/blob/main/doc/TemplateUse.md) | `@use` `@template-use` |
137+
| [Throws](https://github.com/php-static-analysis/attributes/blob/main/doc/Throws.md) | `@throws` |
138+
| [Type](https://github.com/php-static-analysis/attributes/blob/main/doc/Type.md) | `@var` `@return` |
138139

139140
### Location of Param and ParamOut attributes
140141

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
"require": {
2929
"php": ">=8.0",
3030
"cweagans/composer-patches": "^1.7",
31-
"php-static-analysis/attributes": "^0.1.15 || dev-main",
31+
"php-static-analysis/attributes": "^0.1.16 || dev-main",
3232
"rector/rector": "^0.19 || ^1.0"
3333
},
3434
"require-dev": {

config/sets/php-static-analysis-annotations-to-attributes.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use PhpStaticAnalysis\Attributes\TemplateExtends;
2020
use PhpStaticAnalysis\Attributes\TemplateImplements;
2121
use PhpStaticAnalysis\Attributes\TemplateUse;
22+
use PhpStaticAnalysis\Attributes\Throws;
2223
use Rector\Config\RectorConfig;
2324
use Rector\Php80\ValueObject\AnnotationToAttribute;
2425
use PhpStaticAnalysis\Attributes\IsReadOnly;
@@ -58,6 +59,7 @@
5859
new AnnotationToAttribute('template_implements', TemplateImplements::class),
5960
new AnnotationToAttribute('template_use', TemplateUse::class),
6061
new AnnotationToAttribute('this_out', SelfOut::class),
62+
new AnnotationToAttribute('throws', Throws::class),
6163
new AnnotationToAttribute('use', TemplateUse::class),
6264
new AnnotationToAttribute('var', Type::class),
6365
'addParamAttributeOnParameters' => false,

src/AnnotationsToAttributesRector.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
use PHPStan\PhpDocParser\Ast\PhpDoc\ReturnTagValueNode;
2727
use PHPStan\PhpDocParser\Ast\PhpDoc\SelfOutTagValueNode;
2828
use PHPStan\PhpDocParser\Ast\PhpDoc\TemplateTagValueNode;
29+
use PHPStan\PhpDocParser\Ast\PhpDoc\ThrowsTagValueNode;
2930
use PHPStan\PhpDocParser\Ast\PhpDoc\UsesTagValueNode;
3031
use PHPStan\PhpDocParser\Ast\PhpDoc\VarTagValueNode;
3132
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
@@ -293,6 +294,7 @@ private function processAnnotations(PhpDocInfo $phpDocInfo): array
293294
case $tagValueNode instanceof RequireImplementsTagValueNode:
294295
case $tagValueNode instanceof ReturnTagValueNode:
295296
case $tagValueNode instanceof SelfOutTagValueNode:
297+
case $tagValueNode instanceof ThrowsTagValueNode:
296298
case $tagValueNode instanceof UsesTagValueNode:
297299
case $tagValueNode instanceof VarTagValueNode:
298300
$args = [
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
<?php
2+
3+
namespace test\PhpStaticAnalysis\RectorRule\Fixture;
4+
5+
use Exception;
6+
use PhpStaticAnalysis\Attributes\Param;
7+
8+
class ThrowsAttributeTest
9+
{
10+
/**
11+
* @throws Exception
12+
*/
13+
public function getName(): void
14+
{
15+
throw new Exception();
16+
}
17+
18+
/**
19+
* @codeCoverageIgnore
20+
* @throws Exception
21+
*/
22+
public function getMoreNames(): void
23+
{
24+
throw new Exception();
25+
}
26+
27+
/**
28+
* @throws Exception
29+
*/
30+
#[Param(name:'string')]
31+
public function getAnotherName($name): void
32+
{
33+
throw new Exception();
34+
}
35+
36+
/**
37+
* @throws Exception this is the exception that is thrown
38+
*/
39+
public function getUserName(): void
40+
{
41+
throw new Exception();
42+
}
43+
44+
/**
45+
* @phpstan-throws Exception
46+
*/
47+
public function getPHPStanName(): void
48+
{
49+
throw new Exception();
50+
}
51+
}
52+
53+
/**
54+
* @throws Exception
55+
*/
56+
function getName(): void
57+
{
58+
throw new Exception();
59+
}
60+
61+
?>
62+
-----
63+
<?php
64+
65+
namespace test\PhpStaticAnalysis\RectorRule\Fixture;
66+
67+
use Exception;
68+
use PhpStaticAnalysis\Attributes\Param;
69+
70+
class ThrowsAttributeTest
71+
{
72+
#[\PhpStaticAnalysis\Attributes\Throws('Exception')]
73+
public function getName(): void
74+
{
75+
throw new Exception();
76+
}
77+
78+
/**
79+
* @codeCoverageIgnore
80+
*/
81+
#[\PhpStaticAnalysis\Attributes\Throws('Exception')]
82+
public function getMoreNames(): void
83+
{
84+
throw new Exception();
85+
}
86+
87+
#[Param(name:'string')]
88+
#[\PhpStaticAnalysis\Attributes\Throws('Exception')]
89+
public function getAnotherName($name): void
90+
{
91+
throw new Exception();
92+
}
93+
94+
#[\PhpStaticAnalysis\Attributes\Throws('Exception')] // this is the exception that is thrown
95+
public function getUserName(): void
96+
{
97+
throw new Exception();
98+
}
99+
100+
#[\PhpStaticAnalysis\Attributes\Throws('Exception')]
101+
public function getPHPStanName(): void
102+
{
103+
throw new Exception();
104+
}
105+
}
106+
107+
#[\PhpStaticAnalysis\Attributes\Throws('Exception')]
108+
function getName(): void
109+
{
110+
throw new Exception();
111+
}
112+
113+
?>

0 commit comments

Comments
 (0)