Skip to content

Commit

Permalink
Add SelfOut attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
carlos-granados committed Feb 24, 2024
1 parent 99c1313 commit 3ffff94
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 13 deletions.
24 changes: 12 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,26 +110,26 @@ return RectorConfig::configure()

These are the available attributes and their corresponding PHPDoc annotations:

| Attribute | PHPDoc Annotations |
|---------------------------------------------------------------------------------------------------|--------------------|
| Attribute | PHPDoc Annotations |
|-------------------------------------------------------------------------------------------------------------------|--------------------|
| [Deprecated](https://github.com/php-static-analysis/attributes/blob/main/doc/Deprecated.md) | `@deprecated` |
| [Internal](https://github.com/php-static-analysis/attributes/blob/main/doc/Internal.md) | `@internal` |
| [IsReadOnly](https://github.com/php-static-analysis/attributes/blob/main/doc/IsReadOnly.md) | `@readonly` |
| [IsReadOnly](https://github.com/php-static-analysis/attributes/blob/main/doc/IsReadOnly.md) | `@readonly` |
| [Method](https://github.com/php-static-analysis/attributes/blob/main/doc/Method.md) | `@method` |
| [Mixin](https://github.com/php-static-analysis/attributes/blob/main/doc/Mixin.md) | `@mixin` |
| [Param](https://github.com/php-static-analysis/attributes/blob/main/doc/Param.md) | `@param` |
| [Param](https://github.com/php-static-analysis/attributes/blob/main/doc/Param.md) | `@param` |
| [ParamOut](https://github.com/php-static-analysis/attributes/blob/main/doc/ParamOut.md) | `@param-out` |
| [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` |
| [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` |
| [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` |
| [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` |
| [TemplateCovariant](https://github.com/php-static-analysis/attributes/blob/main/doc/TemplateCovariant.md) | `@template-covariant` |
| [TemplateExtends](https://github.com/php-static-analysis/attributes/blob/main/doc/TemplateExtends.md) | `@extends` `@template-extends` |
| [TemplateImplements](https://github.com/php-static-analysis/attributes/blob/main/doc/TemplateImplements.md) | `@implements` `@template-implements` |
| [TemplateUse](https://github.com/php-static-analysis/attributes/blob/main/doc/TemplateUse.md) | `@use` `@template-use` |
| [Type](https://github.com/php-static-analysis/attributes/blob/main/doc/Type.md) | `@var` `@return` |
| [TemplateImplements](https://github.com/php-static-analysis/attributes/blob/main/doc/TemplateImplements.md) | `@implements` `@template-implements` |
| [TemplateUse](https://github.com/php-static-analysis/attributes/blob/main/doc/TemplateUse.md) | `@use` `@template-use` |
| [Type](https://github.com/php-static-analysis/attributes/blob/main/doc/Type.md) | `@var` `@return` |

### Location of Param and ParamOut attributes

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.12 || dev-main",
"php-static-analysis/attributes": "^0.1.13 || dev-main",
"rector/rector": "^0.19 || ^1.0"
},
"require-dev": {
Expand Down
3 changes: 3 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,7 @@
use PhpStaticAnalysis\Attributes\ParamOut;
use PhpStaticAnalysis\Attributes\PropertyRead;
use PhpStaticAnalysis\Attributes\PropertyWrite;
use PhpStaticAnalysis\Attributes\SelfOut;
use PhpStaticAnalysis\Attributes\TemplateContravariant;
use PhpStaticAnalysis\Attributes\TemplateCovariant;
use PhpStaticAnalysis\Attributes\TemplateExtends;
Expand Down Expand Up @@ -41,12 +42,14 @@
new AnnotationToAttribute('property_write', PropertyWrite::class),
new AnnotationToAttribute('readonly', IsReadOnly::class),
new AnnotationToAttribute('return', Returns::class),
new AnnotationToAttribute('self_out', SelfOut::class),
new AnnotationToAttribute('template', Template::class),
new AnnotationToAttribute('template_contravariant', TemplateContravariant::class),
new AnnotationToAttribute('template_covariant', TemplateCovariant::class),
new AnnotationToAttribute('template_extends', TemplateExtends::class),
new AnnotationToAttribute('template_implements', TemplateImplements::class),
new AnnotationToAttribute('template_use', TemplateUse::class),
new AnnotationToAttribute('this_out', SelfOut::class),
new AnnotationToAttribute('use', TemplateUse::class),
new AnnotationToAttribute('var', Type::class),
'addParamAttributeOnParameters' => false,
Expand Down
2 changes: 2 additions & 0 deletions src/AnnotationsToAttributesRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\PropertyTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\ReturnTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\SelfOutTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\TemplateTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\UsesTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\VarTagValueNode;
Expand Down Expand Up @@ -287,6 +288,7 @@ private function processAnnotations(PhpDocInfo $phpDocInfo): array
case $tagValueNode instanceof ImplementsTagValueNode:
case $tagValueNode instanceof MixinTagValueNode:
case $tagValueNode instanceof ReturnTagValueNode:
case $tagValueNode instanceof SelfOutTagValueNode:
case $tagValueNode instanceof UsesTagValueNode:
case $tagValueNode instanceof VarTagValueNode:
$args = [
Expand Down
99 changes: 99 additions & 0 deletions tests/Fixture/SelfOutAttributeTest.php.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?php

namespace test\PhpStaticAnalysis\RectorRule\Fixture;

use PhpStaticAnalysis\Attributes\Param;

class SelfOutAttributeTest
{
/**
* @phpstan-self-out self<T>
*/
public function add()
{
}

/**
* @phpstan-this-out self<T>
*/
public function addThis()
{
}

/**
* @codeCoverageIgnore
* @phpstan-self-out self<T>
*/
public function addMore()
{
}

/**
* @phpstan-self-out self<T>
*/
#[Param(name:'string')]
public function addAnother()
{
}

/**
* @phpstan-self-out self<T> this is the new type
*/
public function addUserName()
{
}

/**
* @psalm-self-out self<T>
*/
public function addPsalm()
{
}
}

?>
-----
<?php

namespace test\PhpStaticAnalysis\RectorRule\Fixture;

use PhpStaticAnalysis\Attributes\Param;

class SelfOutAttributeTest
{
#[\PhpStaticAnalysis\Attributes\SelfOut('self<T>')]
public function add()
{
}

#[\PhpStaticAnalysis\Attributes\SelfOut('self<T>')]
public function addThis()
{
}

/**
* @codeCoverageIgnore
*/
#[\PhpStaticAnalysis\Attributes\SelfOut('self<T>')]
public function addMore()
{
}

#[Param(name:'string')]
#[\PhpStaticAnalysis\Attributes\SelfOut('self<T>')]
public function addAnother()
{
}

#[\PhpStaticAnalysis\Attributes\SelfOut('self<T>')] // this is the new type
public function addUserName()
{
}

#[\PhpStaticAnalysis\Attributes\SelfOut('self<T>')]
public function addPsalm()
{
}
}

?>

0 comments on commit 3ffff94

Please sign in to comment.