Skip to content

Commit

Permalink
Add Method attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
carlos-granados committed Feb 20, 2024
1 parent 629f2f4 commit 83ce0f0
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ These are the available attributes and their corresponding PHPDoc annotations:
| Attribute | PHPDoc Annotations |
|---------------------------------------------------------------------------------------------------|--------------------|
| [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` |
| [Param](https://github.com/php-static-analysis/attributes/blob/main/doc/Param.md) | `@param` |
| [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` |
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.5 || dev-main",
"php-static-analysis/attributes": "^0.1.6 || dev-main",
"rector/rector": "^0.19 || ^1.0"
},
"require-dev": {
Expand Down
2 changes: 2 additions & 0 deletions config/sets/php-static-analysis-annotations-to-attributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

declare(strict_types=1);

use PhpStaticAnalysis\Attributes\Method;
use PhpStaticAnalysis\Attributes\PropertyRead;
use PhpStaticAnalysis\Attributes\PropertyWrite;
use PhpStaticAnalysis\Attributes\TemplateContravariant;
Expand All @@ -21,6 +22,7 @@
AnnotationsToAttributesRector::class,
[
new AnnotationToAttribute('param', Param::class),
new AnnotationToAttribute('method', Method::class),
new AnnotationToAttribute('property', Property::class),
new AnnotationToAttribute('property_read', PropertyRead::class),
new AnnotationToAttribute('property_write', PropertyWrite::class),
Expand Down
6 changes: 6 additions & 0 deletions src/AnnotationsToAttributesRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use PhpParser\Node\Scalar;
use PhpParser\Node\Stmt;
use PHPStan\PhpDocParser\Ast\PhpDoc\GenericTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\MethodTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\ParamTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\PropertyTagValueNode;
Expand Down Expand Up @@ -225,6 +226,11 @@ private function processAnnotations(PhpDocInfo $phpDocInfo): array

$tagValueNode = $phpDocChildNode->value;
switch (true) {
case $tagValueNode instanceof MethodTagValueNode:
$args = [
new Node\Arg(new Scalar\String_((string)($tagValueNode)))
];
break;
case $tagValueNode instanceof ParamTagValueNode:
$args = [
new Node\Arg(
Expand Down
37 changes: 37 additions & 0 deletions tests/Fixture/MethodAttributeTest.php.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace test\PhpStaticAnalysis\RectorRule\Fixture;

use PhpStaticAnalysis\Attributes\Template;

/**
* @deprecated
* @method string getString()
* @psalm-method void setString(string $text)
* @phpstan-method static string staticGetter()
*/
#[Template('T')]
class MethodAttributeTest
{
}

?>
-----
<?php

namespace test\PhpStaticAnalysis\RectorRule\Fixture;

use PhpStaticAnalysis\Attributes\Template;

/**
* @deprecated
*/
#[Template('T')]
#[\PhpStaticAnalysis\Attributes\Method('string getString()')]
#[\PhpStaticAnalysis\Attributes\Method('void setString(string $text)')]
#[\PhpStaticAnalysis\Attributes\Method('static string staticGetter()')]
class MethodAttributeTest
{
}

?>

0 comments on commit 83ce0f0

Please sign in to comment.