From d18e6ea3626dbc6f4d108167be12d745698fe799 Mon Sep 17 00:00:00 2001 From: Carlos Granados Date: Wed, 14 Feb 2024 09:09:58 +0100 Subject: [PATCH] Allow using the Type attribute to specify the return type of a function or method --- composer.json | 4 ++-- tests/TypeAttributeTest.php | 12 ++++++------ tests/data/InvalidPropertyTypeAttribute.php | 7 +------ tests/data/PropertyTypeAttribute.php | 6 ++++++ 4 files changed, 15 insertions(+), 14 deletions(-) diff --git a/composer.json b/composer.json index 073e55c..0615327 100644 --- a/composer.json +++ b/composer.json @@ -24,8 +24,8 @@ "prefer-stable": true, "require": { "php": ">=8.0", - "php-static-analysis/attributes": "^0.1.1 || dev-main", - "php-static-analysis/node-visitor": "^0.1.1 || dev-main", + "php-static-analysis/attributes": "^0.1.2 || dev-main", + "php-static-analysis/node-visitor": "^0.1.2 || dev-main", "phpstan/phpstan": "^1.8" }, "require-dev": { diff --git a/tests/TypeAttributeTest.php b/tests/TypeAttributeTest.php index 2cdff7e..5f3384b 100644 --- a/tests/TypeAttributeTest.php +++ b/tests/TypeAttributeTest.php @@ -15,12 +15,12 @@ public function testInvalidPropertyTypeAttribute(): void $errors = $this->analyse(__DIR__ . '/data/InvalidPropertyTypeAttribute.php'); $expectedErrors = [ - 'PHPDoc tag @var has invalid value (): Unexpected token "\n ", expected type at offset 11' => 9, - 'Parameter #1 $type of attribute class PhpStaticAnalysis\Attributes\Type constructor expects string, int given.' => 9, - 'Attribute class PhpStaticAnalysis\Attributes\Type is not repeatable but is already present above the property.' => 13, - 'Attribute class PhpStaticAnalysis\Attributes\Type constructor invoked with 2 parameters, 1 required.' => 16, - 'PHPDoc tag @var has invalid value ($a + $b): Unexpected token "$a", expected type at offset 12' => 19, - 'Attribute class PhpStaticAnalysis\Attributes\Type does not have the method target.' => 22, + 'Attribute class PhpStaticAnalysis\Attributes\Type does not have the class target.' => 7, + 'PHPDoc tag @var has invalid value (): Unexpected token "\n ", expected type at offset 11' => 10, + 'Parameter #1 $type of attribute class PhpStaticAnalysis\Attributes\Type constructor expects string, int given.' => 10, + 'Attribute class PhpStaticAnalysis\Attributes\Type is not repeatable but is already present above the property.' => 14, + 'Attribute class PhpStaticAnalysis\Attributes\Type constructor invoked with 2 parameters, 1 required.' => 17, + 'PHPDoc tag @var has invalid value ($a + $b): Unexpected token "$a", expected type at offset 12' => 20, ]; $this->checkExpectedErrors($errors, $expectedErrors); diff --git a/tests/data/InvalidPropertyTypeAttribute.php b/tests/data/InvalidPropertyTypeAttribute.php index 8501bde..ee9df29 100644 --- a/tests/data/InvalidPropertyTypeAttribute.php +++ b/tests/data/InvalidPropertyTypeAttribute.php @@ -4,6 +4,7 @@ use PhpStaticAnalysis\Attributes\Type; +#[Type('string')] class InvalidPropertyTypeAttribute { #[Type(0)] @@ -18,10 +19,4 @@ class InvalidPropertyTypeAttribute #[Type('$a + $b')] public string $andAnotherinvalidProperty; - - #[Type('string')] - public function getString(): string - { - return 'hello'; - } } diff --git a/tests/data/PropertyTypeAttribute.php b/tests/data/PropertyTypeAttribute.php index e7d2e74..8be4446 100644 --- a/tests/data/PropertyTypeAttribute.php +++ b/tests/data/PropertyTypeAttribute.php @@ -16,4 +16,10 @@ class PropertyTypeAttribute * @var string */ public string $string; + + #[Type('string')] + public function getString(): string + { + return 'hello'; + } }