From 3394d5dcdb1cc3d755bde547669fb9455f065f75 Mon Sep 17 00:00:00 2001 From: Carlos Granados Date: Fri, 23 Feb 2024 23:36:38 +0100 Subject: [PATCH] Add SelfOut attribute --- README.md | 1 + composer.json | 4 +- tests/SelfOutAttributeTest.php | 26 ++++++++++ .../SelfOut/InvalidMethodSelfOutAttribute.php | 29 ++++++++++++ tests/data/SelfOut/MethodSelfOutAttribute.php | 47 +++++++++++++++++++ 5 files changed, 105 insertions(+), 2 deletions(-) create mode 100644 tests/SelfOutAttributeTest.php create mode 100644 tests/data/SelfOut/InvalidMethodSelfOutAttribute.php create mode 100644 tests/data/SelfOut/MethodSelfOutAttribute.php diff --git a/README.md b/README.md index 508ffe2..54941f5 100644 --- a/README.md +++ b/README.md @@ -107,6 +107,7 @@ These are the available attributes and their corresponding PHPDoc annotations: | [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` | +| [SelfOut](https://github.com/php-static-analysis/attributes/blob/main/doc/SelfOut.md) | `@self-out` `@this-out` | | [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` | diff --git a/composer.json b/composer.json index d1c1749..18b7fa1 100644 --- a/composer.json +++ b/composer.json @@ -24,8 +24,8 @@ "prefer-stable": true, "require": { "php": ">=8.0", - "php-static-analysis/attributes": "^0.1.12 || dev-main", - "php-static-analysis/node-visitor": "^0.1.12 || dev-main", + "php-static-analysis/attributes": "^0.1.13 || dev-main", + "php-static-analysis/node-visitor": "^0.1.13 || dev-main", "phpstan/phpstan": "^1.8" }, "require-dev": { diff --git a/tests/SelfOutAttributeTest.php b/tests/SelfOutAttributeTest.php new file mode 100644 index 0000000..8067f3c --- /dev/null +++ b/tests/SelfOutAttributeTest.php @@ -0,0 +1,26 @@ +analyse(__DIR__ . '/data/SelfOut/MethodSelfOutAttribute.php'); + $this->assertCount(0, $errors); + } + + public function testInvalidMethodSelfOutAttribute(): void + { + $errors = $this->analyse(__DIR__ . '/data/SelfOut/InvalidMethodSelfOutAttribute.php'); + + $expectedErrors = [ + 'PHPDoc tag @phpstan-self-out has invalid value (): Unexpected token "\n ", expected type at offset 75' => 12, + 'Parameter #1 $type of attribute class PhpStaticAnalysis\Attributes\SelfOut constructor expects string, int given.' => 14, + 'Attribute class PhpStaticAnalysis\Attributes\SelfOut is not repeatable but is already present above the method.' => 22, + 'Attribute class PhpStaticAnalysis\Attributes\SelfOut does not have the property target.' => 27, + ]; + + $this->checkExpectedErrors($errors, $expectedErrors); + } +} diff --git a/tests/data/SelfOut/InvalidMethodSelfOutAttribute.php b/tests/data/SelfOut/InvalidMethodSelfOutAttribute.php new file mode 100644 index 0000000..329cc9d --- /dev/null +++ b/tests/data/SelfOut/InvalidMethodSelfOutAttribute.php @@ -0,0 +1,29 @@ +')] + #[SelfOut('self')] + public function addMore($item): void + { + } + + #[SelfOut('self')] + public string $property; +} diff --git a/tests/data/SelfOut/MethodSelfOutAttribute.php b/tests/data/SelfOut/MethodSelfOutAttribute.php new file mode 100644 index 0000000..f348fdb --- /dev/null +++ b/tests/data/SelfOut/MethodSelfOutAttribute.php @@ -0,0 +1,47 @@ +')] // we specify the new type + public function add($item): void + { + } + + /** + * @deprecated + */ + #[Template('TItemValue')] + #[Param(item: 'TItemValue')] + #[SelfOut('self')] + public function addMore($item): void + { + } + + /** + * @self-out self + */ + #[Template('TItemValue')] + #[Param(item: 'TItemValue')] + #[SelfOut('self')] + public function addEvenMore($item): void + { + } + + /** + * @self-out self + */ + #[Template('TItemValue')] + #[Param(item: 'TItemValue')] + public function addMoreAndMore($item): void + { + } +}