Skip to content

Commit 189e5ad

Browse files
Add RequireExtends and RequireImplements attributes
1 parent 3ffff94 commit 189e5ad

6 files changed

+141
-1
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@ These are the available attributes and their corresponding PHPDoc annotations:
122122
| [Property](https://github.com/php-static-analysis/attributes/blob/main/doc/Property.md) | `@property` `@var` |
123123
| [PropertyRead](https://github.com/php-static-analysis/attributes/blob/main/doc/PropertyRead.md) | `@property-read` |
124124
| [PropertyWrite](https://github.com/php-static-analysis/attributes/blob/main/doc/PropertyWrite.md) | `@property-write` |
125+
| [RequireExtends](https://github.com/php-static-analysis/attributes/blob/main/doc/RequireExtends.md) | `@require-extends` |
126+
| [RequireImplements](https://github.com/php-static-analysis/attributes/blob/main/doc/RequireImplements.md) | `@require-implements` |
125127
| [Returns](https://github.com/php-static-analysis/attributes/blob/main/doc/Returns.md) | `@return` |
126128
| [SelfOutAttributeTest.php](..%2Fphpstan-extension%2Ftests%2FSelfOutAttributeTest.php) | [Template](https://github.com/php-static-analysis/attributes/blob/main/doc/Template.md) | `@template` |
127129
| [TemplateContravariant](https://github.com/php-static-analysis/attributes/blob/main/doc/TemplateContravariant.md) | `@template-contravariant` |

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.13 || dev-main",
31+
"php-static-analysis/attributes": "^0.1.14 || dev-main",
3232
"rector/rector": "^0.19 || ^1.0"
3333
},
3434
"require-dev": {

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
use PhpStaticAnalysis\Attributes\ParamOut;
1010
use PhpStaticAnalysis\Attributes\PropertyRead;
1111
use PhpStaticAnalysis\Attributes\PropertyWrite;
12+
use PhpStaticAnalysis\Attributes\RequireExtends;
13+
use PhpStaticAnalysis\Attributes\RequireImplements;
1214
use PhpStaticAnalysis\Attributes\SelfOut;
1315
use PhpStaticAnalysis\Attributes\TemplateContravariant;
1416
use PhpStaticAnalysis\Attributes\TemplateCovariant;
@@ -41,6 +43,8 @@
4143
new AnnotationToAttribute('property_read', PropertyRead::class),
4244
new AnnotationToAttribute('property_write', PropertyWrite::class),
4345
new AnnotationToAttribute('readonly', IsReadOnly::class),
46+
new AnnotationToAttribute('require_extends', RequireExtends::class),
47+
new AnnotationToAttribute('require_implements', RequireImplements::class),
4448
new AnnotationToAttribute('return', Returns::class),
4549
new AnnotationToAttribute('self_out', SelfOut::class),
4650
new AnnotationToAttribute('template', Template::class),

src/AnnotationsToAttributesRector.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
use PHPStan\PhpDocParser\Ast\PhpDoc\ParamTagValueNode;
2222
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode;
2323
use PHPStan\PhpDocParser\Ast\PhpDoc\PropertyTagValueNode;
24+
use PHPStan\PhpDocParser\Ast\PhpDoc\RequireExtendsTagValueNode;
25+
use PHPStan\PhpDocParser\Ast\PhpDoc\RequireImplementsTagValueNode;
2426
use PHPStan\PhpDocParser\Ast\PhpDoc\ReturnTagValueNode;
2527
use PHPStan\PhpDocParser\Ast\PhpDoc\SelfOutTagValueNode;
2628
use PHPStan\PhpDocParser\Ast\PhpDoc\TemplateTagValueNode;
@@ -287,6 +289,8 @@ private function processAnnotations(PhpDocInfo $phpDocInfo): array
287289
case $tagValueNode instanceof ExtendsTagValueNode:
288290
case $tagValueNode instanceof ImplementsTagValueNode:
289291
case $tagValueNode instanceof MixinTagValueNode:
292+
case $tagValueNode instanceof RequireExtendsTagValueNode:
293+
case $tagValueNode instanceof RequireImplementsTagValueNode:
290294
case $tagValueNode instanceof ReturnTagValueNode:
291295
case $tagValueNode instanceof SelfOutTagValueNode:
292296
case $tagValueNode instanceof UsesTagValueNode:
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
namespace test\PhpStaticAnalysis\RectorRule\Fixture;
4+
5+
use PhpStaticAnalysis\Attributes\Property;
6+
7+
class RequireClass
8+
{
9+
}
10+
11+
/**
12+
* @phpstan-require-extends RequireClass
13+
*/
14+
trait RequireExtendsAttributeTest
15+
{
16+
}
17+
18+
/**
19+
* @codeCoverageIgnore
20+
* @phpstan-require-extends RequireClass this is the class that needs to be extended
21+
*/
22+
#[Property(name:'string')]
23+
trait RequireExtendsAttributeTest2
24+
{
25+
}
26+
27+
/**
28+
* @psalm-require-extends RequireClass
29+
*/
30+
trait RequireExtendsAttributeTest3
31+
{
32+
}
33+
34+
?>
35+
-----
36+
<?php
37+
38+
namespace test\PhpStaticAnalysis\RectorRule\Fixture;
39+
40+
use PhpStaticAnalysis\Attributes\Property;
41+
42+
class RequireClass
43+
{
44+
}
45+
46+
#[\PhpStaticAnalysis\Attributes\RequireExtends('RequireClass')]
47+
trait RequireExtendsAttributeTest
48+
{
49+
}
50+
51+
/**
52+
* @codeCoverageIgnore
53+
*/
54+
#[Property(name:'string')]
55+
#[\PhpStaticAnalysis\Attributes\RequireExtends('RequireClass')] // this is the class that needs to be extended
56+
trait RequireExtendsAttributeTest2
57+
{
58+
}
59+
60+
#[\PhpStaticAnalysis\Attributes\RequireExtends('RequireClass')]
61+
trait RequireExtendsAttributeTest3
62+
{
63+
}
64+
65+
?>
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
namespace test\PhpStaticAnalysis\RectorRule\Fixture;
4+
5+
use PhpStaticAnalysis\Attributes\Property;
6+
7+
interface RequireInterface
8+
{
9+
}
10+
11+
/**
12+
* @phpstan-require-implements RequireInterface
13+
*/
14+
trait RequireImplementsAttributeTest
15+
{
16+
}
17+
18+
/**
19+
* @codeCoverageIgnore
20+
* @phpstan-require-implements RequireInterface this is the interface that needs to be implemented
21+
*/
22+
#[Property(name:'string')]
23+
trait RequireImplementsAttributeTest2
24+
{
25+
}
26+
27+
/**
28+
* @psalm-require-implements RequireInterface
29+
*/
30+
trait RequireImplementsAttributeTest3
31+
{
32+
}
33+
34+
?>
35+
-----
36+
<?php
37+
38+
namespace test\PhpStaticAnalysis\RectorRule\Fixture;
39+
40+
use PhpStaticAnalysis\Attributes\Property;
41+
42+
interface RequireInterface
43+
{
44+
}
45+
46+
#[\PhpStaticAnalysis\Attributes\RequireImplements('RequireInterface')]
47+
trait RequireImplementsAttributeTest
48+
{
49+
}
50+
51+
/**
52+
* @codeCoverageIgnore
53+
*/
54+
#[Property(name:'string')]
55+
#[\PhpStaticAnalysis\Attributes\RequireImplements('RequireInterface')] // this is the interface that needs to be implemented
56+
trait RequireImplementsAttributeTest2
57+
{
58+
}
59+
60+
#[\PhpStaticAnalysis\Attributes\RequireImplements('RequireInterface')]
61+
trait RequireImplementsAttributeTest3
62+
{
63+
}
64+
65+
?>

0 commit comments

Comments
 (0)