Skip to content

Commit 629f2f4

Browse files
Add TemplateCovariant and TemplateContravariant attributes
1 parent 33ceb3e commit 629f2f4

File tree

5 files changed

+87
-1
lines changed

5 files changed

+87
-1
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,8 @@ These are the available attributes and their corresponding PHPDoc annotations:
119119
| [PropertyWrite](https://github.com/php-static-analysis/attributes/blob/main/doc/PropertyWrite.md) | `@property-write` |
120120
| [Returns](https://github.com/php-static-analysis/attributes/blob/main/doc/Returns.md) | `@return` |
121121
| [Template](https://github.com/php-static-analysis/attributes/blob/main/doc/Template.md) | `@template` |
122+
| [TemplateContravariant](https://github.com/php-static-analysis/attributes/blob/main/doc/TemplateContravariant.md) | `@template-contravariant` |
123+
| [TemplateCovariant](https://github.com/php-static-analysis/attributes/blob/main/doc/TemplateCovariant.md) | `@template-covariant` |
122124
| [Type](https://github.com/php-static-analysis/attributes/blob/main/doc/Type.md) | `@var` `@return` |
123125

124126
### Location of Param attributes

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
"prefer-stable": true,
2525
"require": {
2626
"php": ">=8.0",
27-
"php-static-analysis/attributes": "^0.1.4 || dev-main",
27+
"php-static-analysis/attributes": "^0.1.5 || dev-main",
2828
"rector/rector": "^0.19 || ^1.0"
2929
},
3030
"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
@@ -4,6 +4,8 @@
44

55
use PhpStaticAnalysis\Attributes\PropertyRead;
66
use PhpStaticAnalysis\Attributes\PropertyWrite;
7+
use PhpStaticAnalysis\Attributes\TemplateContravariant;
8+
use PhpStaticAnalysis\Attributes\TemplateCovariant;
79
use Rector\Config\RectorConfig;
810
use Rector\Php80\ValueObject\AnnotationToAttribute;
911
use PhpStaticAnalysis\Attributes\IsReadOnly;
@@ -25,6 +27,8 @@
2527
new AnnotationToAttribute('readonly', IsReadOnly::class),
2628
new AnnotationToAttribute('return', Returns::class),
2729
new AnnotationToAttribute('template', Template::class),
30+
new AnnotationToAttribute('template_contravariant', TemplateContravariant::class),
31+
new AnnotationToAttribute('template_covariant', TemplateCovariant::class),
2832
new AnnotationToAttribute('var', Type::class),
2933
'addParamAttributeOnParameters' => false,
3034
'useTypeAttributeForReturnAnnotation' => false,
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace test\PhpStaticAnalysis\RectorRule\Fixture;
4+
5+
use Exception;
6+
use PhpStaticAnalysis\Attributes\Property;
7+
8+
/**
9+
* @deprecated
10+
* @template-contravariant T1
11+
* @template-contravariant T2 of Exception
12+
* @phpstan-template-contravariant T3
13+
*/
14+
#[Property(name:'string')]
15+
class TemplateContravariantAttributeTest
16+
{
17+
}
18+
19+
?>
20+
-----
21+
<?php
22+
23+
namespace test\PhpStaticAnalysis\RectorRule\Fixture;
24+
25+
use Exception;
26+
use PhpStaticAnalysis\Attributes\Property;
27+
28+
/**
29+
* @deprecated
30+
*/
31+
#[Property(name:'string')]
32+
#[\PhpStaticAnalysis\Attributes\TemplateContravariant('T1')]
33+
#[\PhpStaticAnalysis\Attributes\TemplateContravariant('T2', 'Exception')]
34+
#[\PhpStaticAnalysis\Attributes\TemplateContravariant('T3')]
35+
class TemplateContravariantAttributeTest
36+
{
37+
}
38+
39+
?>
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace test\PhpStaticAnalysis\RectorRule\Fixture;
4+
5+
use Exception;
6+
use PhpStaticAnalysis\Attributes\Property;
7+
8+
/**
9+
* @deprecated
10+
* @template-covariant T1
11+
* @template-covariant T2 of Exception
12+
* @psalm-template-covariant T3
13+
* @phpstan-template-covariant T4
14+
*/
15+
#[Property(name:'string')]
16+
class TemplateCovariantAttributeTest
17+
{
18+
}
19+
20+
?>
21+
-----
22+
<?php
23+
24+
namespace test\PhpStaticAnalysis\RectorRule\Fixture;
25+
26+
use Exception;
27+
use PhpStaticAnalysis\Attributes\Property;
28+
29+
/**
30+
* @deprecated
31+
*/
32+
#[Property(name:'string')]
33+
#[\PhpStaticAnalysis\Attributes\TemplateCovariant('T1')]
34+
#[\PhpStaticAnalysis\Attributes\TemplateCovariant('T2', 'Exception')]
35+
#[\PhpStaticAnalysis\Attributes\TemplateCovariant('T3')]
36+
#[\PhpStaticAnalysis\Attributes\TemplateCovariant('T4')]
37+
class TemplateCovariantAttributeTest
38+
{
39+
}
40+
41+
?>

0 commit comments

Comments
 (0)