Skip to content

Commit

Permalink
Add RequireExtends and RequireImplements attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
carlos-granados committed Feb 24, 2024
1 parent 3ffff94 commit 189e5ad
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ These are the available attributes and their corresponding PHPDoc annotations:
| [Property](https://github.com/php-static-analysis/attributes/blob/main/doc/Property.md) | `@property` `@var` |
| [PropertyRead](https://github.com/php-static-analysis/attributes/blob/main/doc/PropertyRead.md) | `@property-read` |
| [PropertyWrite](https://github.com/php-static-analysis/attributes/blob/main/doc/PropertyWrite.md) | `@property-write` |
| [RequireExtends](https://github.com/php-static-analysis/attributes/blob/main/doc/RequireExtends.md) | `@require-extends` |
| [RequireImplements](https://github.com/php-static-analysis/attributes/blob/main/doc/RequireImplements.md) | `@require-implements` |
| [Returns](https://github.com/php-static-analysis/attributes/blob/main/doc/Returns.md) | `@return` |
| [SelfOutAttributeTest.php](..%2Fphpstan-extension%2Ftests%2FSelfOutAttributeTest.php) | [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` |
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"require": {
"php": ">=8.0",
"cweagans/composer-patches": "^1.7",
"php-static-analysis/attributes": "^0.1.13 || dev-main",
"php-static-analysis/attributes": "^0.1.14 || 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 @@ -9,6 +9,8 @@
use PhpStaticAnalysis\Attributes\ParamOut;
use PhpStaticAnalysis\Attributes\PropertyRead;
use PhpStaticAnalysis\Attributes\PropertyWrite;
use PhpStaticAnalysis\Attributes\RequireExtends;
use PhpStaticAnalysis\Attributes\RequireImplements;
use PhpStaticAnalysis\Attributes\SelfOut;
use PhpStaticAnalysis\Attributes\TemplateContravariant;
use PhpStaticAnalysis\Attributes\TemplateCovariant;
Expand Down Expand Up @@ -41,6 +43,8 @@
new AnnotationToAttribute('property_read', PropertyRead::class),
new AnnotationToAttribute('property_write', PropertyWrite::class),
new AnnotationToAttribute('readonly', IsReadOnly::class),
new AnnotationToAttribute('require_extends', RequireExtends::class),
new AnnotationToAttribute('require_implements', RequireImplements::class),
new AnnotationToAttribute('return', Returns::class),
new AnnotationToAttribute('self_out', SelfOut::class),
new AnnotationToAttribute('template', Template::class),
Expand Down
4 changes: 4 additions & 0 deletions src/AnnotationsToAttributesRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
use PHPStan\PhpDocParser\Ast\PhpDoc\ParamTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\PropertyTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\RequireExtendsTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\RequireImplementsTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\ReturnTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\SelfOutTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\TemplateTagValueNode;
Expand Down Expand Up @@ -287,6 +289,8 @@ private function processAnnotations(PhpDocInfo $phpDocInfo): array
case $tagValueNode instanceof ExtendsTagValueNode:
case $tagValueNode instanceof ImplementsTagValueNode:
case $tagValueNode instanceof MixinTagValueNode:
case $tagValueNode instanceof RequireExtendsTagValueNode:
case $tagValueNode instanceof RequireImplementsTagValueNode:
case $tagValueNode instanceof ReturnTagValueNode:
case $tagValueNode instanceof SelfOutTagValueNode:
case $tagValueNode instanceof UsesTagValueNode:
Expand Down
65 changes: 65 additions & 0 deletions tests/Fixture/RequireExtendsAttributeTest.php.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

namespace test\PhpStaticAnalysis\RectorRule\Fixture;

use PhpStaticAnalysis\Attributes\Property;

class RequireClass
{
}

/**
* @phpstan-require-extends RequireClass
*/
trait RequireExtendsAttributeTest
{
}

/**
* @codeCoverageIgnore
* @phpstan-require-extends RequireClass this is the class that needs to be extended
*/
#[Property(name:'string')]
trait RequireExtendsAttributeTest2
{
}

/**
* @psalm-require-extends RequireClass
*/
trait RequireExtendsAttributeTest3
{
}

?>
-----
<?php

namespace test\PhpStaticAnalysis\RectorRule\Fixture;

use PhpStaticAnalysis\Attributes\Property;

class RequireClass
{
}

#[\PhpStaticAnalysis\Attributes\RequireExtends('RequireClass')]
trait RequireExtendsAttributeTest
{
}

/**
* @codeCoverageIgnore
*/
#[Property(name:'string')]
#[\PhpStaticAnalysis\Attributes\RequireExtends('RequireClass')] // this is the class that needs to be extended
trait RequireExtendsAttributeTest2
{
}

#[\PhpStaticAnalysis\Attributes\RequireExtends('RequireClass')]
trait RequireExtendsAttributeTest3
{
}

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

namespace test\PhpStaticAnalysis\RectorRule\Fixture;

use PhpStaticAnalysis\Attributes\Property;

interface RequireInterface
{
}

/**
* @phpstan-require-implements RequireInterface
*/
trait RequireImplementsAttributeTest
{
}

/**
* @codeCoverageIgnore
* @phpstan-require-implements RequireInterface this is the interface that needs to be implemented
*/
#[Property(name:'string')]
trait RequireImplementsAttributeTest2
{
}

/**
* @psalm-require-implements RequireInterface
*/
trait RequireImplementsAttributeTest3
{
}

?>
-----
<?php

namespace test\PhpStaticAnalysis\RectorRule\Fixture;

use PhpStaticAnalysis\Attributes\Property;

interface RequireInterface
{
}

#[\PhpStaticAnalysis\Attributes\RequireImplements('RequireInterface')]
trait RequireImplementsAttributeTest
{
}

/**
* @codeCoverageIgnore
*/
#[Property(name:'string')]
#[\PhpStaticAnalysis\Attributes\RequireImplements('RequireInterface')] // this is the interface that needs to be implemented
trait RequireImplementsAttributeTest2
{
}

#[\PhpStaticAnalysis\Attributes\RequireImplements('RequireInterface')]
trait RequireImplementsAttributeTest3
{
}

?>

0 comments on commit 189e5ad

Please sign in to comment.