From 29992c6e43d873b4f29b2588d70a7d1b2ff1e1b8 Mon Sep 17 00:00:00 2001 From: Carlos Granados Date: Sun, 25 Feb 2024 23:18:53 +0100 Subject: [PATCH] Add Throws attribute --- composer.json | 4 ++-- tests/data/Mixin/ClassMixinAttribute.php | 6 +++--- tests/data/Param/MethodParamAttribute.php | 7 +++++++ tests/data/ParamOut/MethodParamOutAttribute.php | 7 +++++++ tests/data/Property/ClassPropertyAttribute.php | 2 ++ tests/data/PropertyRead/ClassPropertyReadAttribute.php | 2 ++ tests/data/PropertyWrite/ClassPropertyWriteAttribute.php | 2 ++ .../data/RequireExtends/TraitRequireExtendsAttribute.php | 2 +- .../TraitRequireImplementsAttribute.php | 6 +++--- tests/data/Returns/MethodReturnsAttribute.php | 7 +++++++ tests/data/Type/PropertyTypeAttribute.php | 9 +++++++++ 11 files changed, 45 insertions(+), 9 deletions(-) diff --git a/composer.json b/composer.json index bdfbcff..84dc743 100644 --- a/composer.json +++ b/composer.json @@ -26,8 +26,8 @@ "require": { "php": ">=8.0", "ext-simplexml": "*", - "php-static-analysis/attributes": "^0.1.15 || dev-main", - "php-static-analysis/node-visitor": "^0.1.15 || dev-main", + "php-static-analysis/attributes": "^0.1.16 || dev-main", + "php-static-analysis/node-visitor": "^0.1.16 || dev-main", "vimeo/psalm": "^5" }, "require-dev": { diff --git a/tests/data/Mixin/ClassMixinAttribute.php b/tests/data/Mixin/ClassMixinAttribute.php index ecc48a0..6505716 100644 --- a/tests/data/Mixin/ClassMixinAttribute.php +++ b/tests/data/Mixin/ClassMixinAttribute.php @@ -19,10 +19,10 @@ class Another { } -#[Mixin('ClassMixinAttribute')] // this is the proxied class +#[Mixin(ClassMixinAttribute::class)] // this is the proxied class #[Mixin( - 'MyClass', - 'Another', + MyClass::class, + Another::class, )] class ClassMixinAttributeProxy { diff --git a/tests/data/Param/MethodParamAttribute.php b/tests/data/Param/MethodParamAttribute.php index ed6b311..27e8582 100644 --- a/tests/data/Param/MethodParamAttribute.php +++ b/tests/data/Param/MethodParamAttribute.php @@ -2,6 +2,7 @@ namespace test\PhpStaticAnalysis\PsalmPlugin\data\Param; +use Exception; use PhpStaticAnalysis\Attributes\Param; class MethodParamAttribute @@ -12,6 +13,12 @@ public function countNames($names): int return count($names); } + #[Param(exception: Exception::class)] + public function throwException($exception): void + { + throw $exception; + } + #[Param('string[] $names')] public function countUnnamedNames($names): int { diff --git a/tests/data/ParamOut/MethodParamOutAttribute.php b/tests/data/ParamOut/MethodParamOutAttribute.php index 9a276a7..63321a1 100644 --- a/tests/data/ParamOut/MethodParamOutAttribute.php +++ b/tests/data/ParamOut/MethodParamOutAttribute.php @@ -2,6 +2,7 @@ namespace test\PhpStaticAnalysis\PsalmPlugin\data\ParamOut; +use Exception; use PhpStaticAnalysis\Attributes\ParamOut; class MethodParamOutAttribute @@ -12,6 +13,12 @@ public function setNames(mixed &$names): void $names = 1; } + #[ParamOut(exception: Exception::class)] + public function setException(mixed &$exception): void + { + $exception = new Exception(); + } + #[ParamOut('int $names')] public function setUnnamedNames(mixed &$names): void { diff --git a/tests/data/Property/ClassPropertyAttribute.php b/tests/data/Property/ClassPropertyAttribute.php index b0a2798..c92c30e 100644 --- a/tests/data/Property/ClassPropertyAttribute.php +++ b/tests/data/Property/ClassPropertyAttribute.php @@ -2,9 +2,11 @@ namespace test\PhpStaticAnalysis\PsalmPlugin\data\Property; +use Exception; use PhpStaticAnalysis\Attributes\Property; #[Property(name: 'string')] // the name of the user +#[Property(exception: Exception::class)] #[Property('int $age')] #[Property( index1: 'string[]', diff --git a/tests/data/PropertyRead/ClassPropertyReadAttribute.php b/tests/data/PropertyRead/ClassPropertyReadAttribute.php index 01abae2..fd88527 100644 --- a/tests/data/PropertyRead/ClassPropertyReadAttribute.php +++ b/tests/data/PropertyRead/ClassPropertyReadAttribute.php @@ -2,9 +2,11 @@ namespace test\PhpStaticAnalysis\PsalmPlugin\data\PropertyRead; +use Exception; use PhpStaticAnalysis\Attributes\PropertyRead; #[PropertyRead(name: 'string')] // cannot be written to +#[PropertyRead(exception: Exception::class)] #[PropertyRead('int $age')] #[PropertyRead( index1: 'string[]', diff --git a/tests/data/PropertyWrite/ClassPropertyWriteAttribute.php b/tests/data/PropertyWrite/ClassPropertyWriteAttribute.php index a58f5e2..a850a62 100644 --- a/tests/data/PropertyWrite/ClassPropertyWriteAttribute.php +++ b/tests/data/PropertyWrite/ClassPropertyWriteAttribute.php @@ -2,9 +2,11 @@ namespace test\PhpStaticAnalysis\PsalmPlugin\data\PropertyWrite; +use Exception; use PhpStaticAnalysis\Attributes\PropertyWrite; #[PropertyWrite(name: 'string')] // cannot be read +#[PropertyWrite(exception: Exception::class)] #[PropertyWrite('int $age')] #[PropertyWrite( index1: 'string[]', diff --git a/tests/data/RequireExtends/TraitRequireExtendsAttribute.php b/tests/data/RequireExtends/TraitRequireExtendsAttribute.php index 7a27d5a..257860e 100644 --- a/tests/data/RequireExtends/TraitRequireExtendsAttribute.php +++ b/tests/data/RequireExtends/TraitRequireExtendsAttribute.php @@ -4,7 +4,7 @@ use PhpStaticAnalysis\Attributes\RequireExtends; -#[RequireExtends('ClassRequireExtendsAttribute')] // the class using this trait needs to extend this class +#[RequireExtends(ClassRequireExtendsAttribute::class)] // the class using this trait needs to extend this class trait TraitRequireExtendsAttribute { } diff --git a/tests/data/RequireImplements/TraitRequireImplementsAttribute.php b/tests/data/RequireImplements/TraitRequireImplementsAttribute.php index 7d5b838..177f30f 100644 --- a/tests/data/RequireImplements/TraitRequireImplementsAttribute.php +++ b/tests/data/RequireImplements/TraitRequireImplementsAttribute.php @@ -4,10 +4,10 @@ use PhpStaticAnalysis\Attributes\RequireImplements; -#[RequireImplements('InterfaceRequireImplementsAttribute')] // the class that uses this trait needs to implement these interfaces +#[RequireImplements(InterfaceRequireImplementsAttribute::class)] // the class that uses this trait needs to implement these interfaces #[RequireImplements( - 'InterfaceRequireImplementsAttribute2', - 'InterfaceRequireImplementsAttribute3' + InterfaceRequireImplementsAttribute2::class, + InterfaceRequireImplementsAttribute3::class )] trait TraitRequireImplementsAttribute { diff --git a/tests/data/Returns/MethodReturnsAttribute.php b/tests/data/Returns/MethodReturnsAttribute.php index 2f9c79e..4ac9ecb 100644 --- a/tests/data/Returns/MethodReturnsAttribute.php +++ b/tests/data/Returns/MethodReturnsAttribute.php @@ -2,6 +2,7 @@ namespace test\PhpStaticAnalysis\PsalmPlugin\data\Returns; +use Exception; use PhpStaticAnalysis\Attributes\Returns; class MethodReturnsAttribute @@ -12,6 +13,12 @@ public function getNames(): array return ['hello', 'world']; } + #[Returns(Exception::class)] + public function getException() + { + return new Exception(); + } + /** * @deprecated */ diff --git a/tests/data/Type/PropertyTypeAttribute.php b/tests/data/Type/PropertyTypeAttribute.php index a3145ab..da1d3c3 100644 --- a/tests/data/Type/PropertyTypeAttribute.php +++ b/tests/data/Type/PropertyTypeAttribute.php @@ -2,6 +2,7 @@ namespace test\PhpStaticAnalysis\PsalmPlugin\data\Type; +use Exception; use PhpStaticAnalysis\Attributes\Type; class PropertyTypeAttribute @@ -12,11 +13,19 @@ class PropertyTypeAttribute #[Type('int[]')] // number of items public array $nums = []; + #[Type(Exception::class)] + public $exception; + /** * @var string */ public string $string = ''; + public function __construct() + { + $this->exception = new Exception(); + } + #[Type('string')] public function getString(): string {