From e1e3ea2267147cf4d86add1aa1c63c0441c159b2 Mon Sep 17 00:00:00 2001 From: Carlos Granados Date: Thu, 22 Feb 2024 15:01:31 +0100 Subject: [PATCH] Add Mixin attribute --- README.md | 1 + composer.json | 4 +- tests/MixinAttributeTest.php | 38 +++++++++++++++++++ tests/data/Mixin/ClassMixinAttribute.php | 36 ++++++++++++++++++ tests/data/Mixin/InterfaceMixinAttribute.php | 14 +++++++ .../data/Mixin/InvalidClassMixinAttribute.php | 16 ++++++++ tests/data/Mixin/TraitMixinAttribute.php | 14 +++++++ 7 files changed, 121 insertions(+), 2 deletions(-) create mode 100644 tests/MixinAttributeTest.php create mode 100644 tests/data/Mixin/ClassMixinAttribute.php create mode 100644 tests/data/Mixin/InterfaceMixinAttribute.php create mode 100644 tests/data/Mixin/InvalidClassMixinAttribute.php create mode 100644 tests/data/Mixin/TraitMixinAttribute.php diff --git a/README.md b/README.md index d5551d1..c45b852 100644 --- a/README.md +++ b/README.md @@ -100,6 +100,7 @@ These are the available attributes and their corresponding PHPDoc annotations: | [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` | | [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` | | [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` | diff --git a/composer.json b/composer.json index 697320b..bab480d 100644 --- a/composer.json +++ b/composer.json @@ -24,8 +24,8 @@ "prefer-stable": true, "require": { "php": ">=8.0", - "php-static-analysis/attributes": "^0.1.9 || dev-main", - "php-static-analysis/node-visitor": "^0.1.9 || dev-main", + "php-static-analysis/attributes": "^0.1.10 || dev-main", + "php-static-analysis/node-visitor": "^0.1.10 || dev-main", "phpstan/phpstan": "^1.8" }, "require-dev": { diff --git a/tests/MixinAttributeTest.php b/tests/MixinAttributeTest.php new file mode 100644 index 0000000..1e9340b --- /dev/null +++ b/tests/MixinAttributeTest.php @@ -0,0 +1,38 @@ +analyse(__DIR__ . '/data/Mixin/ClassMixinAttribute.php'); + $this->assertCount(0, $errors); + } + + public function testInterfaceMixinAttribute(): void + { + $errors = $this->analyse(__DIR__ . '/data/Mixin/InterfaceMixinAttribute.php'); + $this->assertCount(0, $errors); + } + + public function testTraitMixinAttribute(): void + { + $errors = $this->analyse(__DIR__ . '/data/Mixin/TraitMixinAttribute.php'); + $this->assertCount(0, $errors); + } + + public function testInvalidClassMixinAttribute(): void + { + $errors = $this->analyse(__DIR__ . '/data/Mixin/InvalidClassMixinAttribute.php'); + + $expectedErrors = [ + 'PHPDoc tag @mixin contains unknown class test\PhpStaticAnalysis\PHPStanExtension\data\Mixin\count.' => 7, + 'PHPDoc tag @mixin has invalid value (): Unexpected token "\n * ", expected type at offset 13' => 7, + 'Parameter #1 ...$params of attribute class PhpStaticAnalysis\Attributes\Mixin constructor expects string, int given.' => 7, + 'Attribute class PhpStaticAnalysis\Attributes\Mixin does not have the method target.' => 11, + ]; + + $this->checkExpectedErrors($errors, $expectedErrors); + } +} diff --git a/tests/data/Mixin/ClassMixinAttribute.php b/tests/data/Mixin/ClassMixinAttribute.php new file mode 100644 index 0000000..db47c9e --- /dev/null +++ b/tests/data/Mixin/ClassMixinAttribute.php @@ -0,0 +1,36 @@ +$name(...$arguments); + } +} + +$proxy = new ClassMixinAttributeProxy(); +$proxy->proxied(); diff --git a/tests/data/Mixin/InterfaceMixinAttribute.php b/tests/data/Mixin/InterfaceMixinAttribute.php new file mode 100644 index 0000000..0fada63 --- /dev/null +++ b/tests/data/Mixin/InterfaceMixinAttribute.php @@ -0,0 +1,14 @@ +