Skip to content

Commit

Permalink
Add TemplateCovariant and TemplateContravariant attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
carlos-granados committed Feb 19, 2024
1 parent 33ceb3e commit 629f2f4
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ These are the available attributes and their corresponding PHPDoc annotations:
| [PropertyWrite](https://github.com/php-static-analysis/attributes/blob/main/doc/PropertyWrite.md) | `@property-write` |
| [Returns](https://github.com/php-static-analysis/attributes/blob/main/doc/Returns.md) | `@return` |
| [Template](https://github.com/php-static-analysis/attributes/blob/main/doc/Template.md) | `@template` |
| [TemplateContravariant](https://github.com/php-static-analysis/attributes/blob/main/doc/TemplateContravariant.md) | `@template-contravariant` |
| [TemplateCovariant](https://github.com/php-static-analysis/attributes/blob/main/doc/TemplateCovariant.md) | `@template-covariant` |
| [Type](https://github.com/php-static-analysis/attributes/blob/main/doc/Type.md) | `@var` `@return` |

### Location of Param attributes
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"prefer-stable": true,
"require": {
"php": ">=8.0",
"php-static-analysis/attributes": "^0.1.4 || dev-main",
"php-static-analysis/attributes": "^0.1.5 || dev-main",
"rector/rector": "^0.19 || ^1.0"
},
"require-dev": {
Expand Down
4 changes: 4 additions & 0 deletions config/sets/php-static-analysis-annotations-to-attributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use PhpStaticAnalysis\Attributes\PropertyRead;
use PhpStaticAnalysis\Attributes\PropertyWrite;
use PhpStaticAnalysis\Attributes\TemplateContravariant;
use PhpStaticAnalysis\Attributes\TemplateCovariant;
use Rector\Config\RectorConfig;
use Rector\Php80\ValueObject\AnnotationToAttribute;
use PhpStaticAnalysis\Attributes\IsReadOnly;
Expand All @@ -25,6 +27,8 @@
new AnnotationToAttribute('readonly', IsReadOnly::class),
new AnnotationToAttribute('return', Returns::class),
new AnnotationToAttribute('template', Template::class),
new AnnotationToAttribute('template_contravariant', TemplateContravariant::class),
new AnnotationToAttribute('template_covariant', TemplateCovariant::class),
new AnnotationToAttribute('var', Type::class),
'addParamAttributeOnParameters' => false,
'useTypeAttributeForReturnAnnotation' => false,
Expand Down
39 changes: 39 additions & 0 deletions tests/Fixture/TemplateContravariantAttributeTest.php.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace test\PhpStaticAnalysis\RectorRule\Fixture;

use Exception;
use PhpStaticAnalysis\Attributes\Property;

/**
* @deprecated
* @template-contravariant T1
* @template-contravariant T2 of Exception
* @phpstan-template-contravariant T3
*/
#[Property(name:'string')]
class TemplateContravariantAttributeTest
{
}

?>
-----
<?php

namespace test\PhpStaticAnalysis\RectorRule\Fixture;

use Exception;
use PhpStaticAnalysis\Attributes\Property;

/**
* @deprecated
*/
#[Property(name:'string')]
#[\PhpStaticAnalysis\Attributes\TemplateContravariant('T1')]
#[\PhpStaticAnalysis\Attributes\TemplateContravariant('T2', 'Exception')]
#[\PhpStaticAnalysis\Attributes\TemplateContravariant('T3')]
class TemplateContravariantAttributeTest
{
}

?>
41 changes: 41 additions & 0 deletions tests/Fixture/TemplateCovariantAttributeTest.php.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace test\PhpStaticAnalysis\RectorRule\Fixture;

use Exception;
use PhpStaticAnalysis\Attributes\Property;

/**
* @deprecated
* @template-covariant T1
* @template-covariant T2 of Exception
* @psalm-template-covariant T3
* @phpstan-template-covariant T4
*/
#[Property(name:'string')]
class TemplateCovariantAttributeTest
{
}

?>
-----
<?php

namespace test\PhpStaticAnalysis\RectorRule\Fixture;

use Exception;
use PhpStaticAnalysis\Attributes\Property;

/**
* @deprecated
*/
#[Property(name:'string')]
#[\PhpStaticAnalysis\Attributes\TemplateCovariant('T1')]
#[\PhpStaticAnalysis\Attributes\TemplateCovariant('T2', 'Exception')]
#[\PhpStaticAnalysis\Attributes\TemplateCovariant('T3')]
#[\PhpStaticAnalysis\Attributes\TemplateCovariant('T4')]
class TemplateCovariantAttributeTest
{
}

?>

0 comments on commit 629f2f4

Please sign in to comment.