From 9876dee8b422e42f2f4216d72943a63bbadf6bc8 Mon Sep 17 00:00:00 2001 From: Carlos Granados Date: Sun, 25 Feb 2024 15:41:27 +0100 Subject: [PATCH] Add Pure and Impure attributes --- README.md | 48 ++++++++++--------- composer.json | 4 +- tests/ImpureAttributeTest.php | 30 ++++++++++++ tests/PureAttributeTest.php | 30 ++++++++++++ tests/data/Impure/FunctionImpureAttribute.php | 16 +++++++ .../Impure/InvalidMethodImpureAttribute.php | 19 ++++++++ tests/data/Impure/MethodImpureAttribute.php | 36 ++++++++++++++ tests/data/Pure/FunctionPureAttribute.php | 11 +++++ .../data/Pure/InvalidMethodPureAttribute.php | 19 ++++++++ tests/data/Pure/MethodPureAttribute.php | 31 ++++++++++++ 10 files changed, 219 insertions(+), 25 deletions(-) create mode 100644 tests/ImpureAttributeTest.php create mode 100644 tests/PureAttributeTest.php create mode 100644 tests/data/Impure/FunctionImpureAttribute.php create mode 100644 tests/data/Impure/InvalidMethodImpureAttribute.php create mode 100644 tests/data/Impure/MethodImpureAttribute.php create mode 100644 tests/data/Pure/FunctionPureAttribute.php create mode 100644 tests/data/Pure/InvalidMethodPureAttribute.php create mode 100644 tests/data/Pure/MethodPureAttribute.php diff --git a/README.md b/README.md index e7dee75..f6b1b5a 100644 --- a/README.md +++ b/README.md @@ -94,29 +94,31 @@ This extension works by interacting with the parser that PHPStan uses to parse t These are the available attributes and their corresponding PHPDoc annotations: -| Attribute | PHPDoc Annotations | -|------------------------------------------------------------------------------------------------------------|--------------------------------------| -| [Deprecated](https://github.com/php-static-analysis/attributes/blob/main/doc/Deprecated.md) | `@deprecated` | -| [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` | -| [ParamOut](https://github.com/php-static-analysis/attributes/blob/main/doc/ParamOut.md) | `@param-out` | -| [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` | -| [PropertyWrite](https://github.com/php-static-analysis/attributes/blob/main/doc/PropertyWrite.md) | `@property-write` | -| [RequireExtends](https://github.com/php-static-analysis/attributes/blob/main/doc/RequireExtends.md) | `@require-extends` | -| [RequireImplements](https://github.com/php-static-analysis/attributes/blob/main/doc/RequireImplements.md) | `@require-implements` | -| [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` | -| [TemplateExtends](https://github.com/php-static-analysis/attributes/blob/main/doc/TemplateExtends.md) | `@extends` `@template-extends` | -| [TemplateImplements](https://github.com/php-static-analysis/attributes/blob/main/doc/TemplateImplements.md) | `@implements` `@template-implements` | -| [TemplateUse](https://github.com/php-static-analysis/attributes/blob/main/doc/TemplateUse.md) | `@use` `@template-use` | -| [Type](https://github.com/php-static-analysis/attributes/blob/main/doc/Type.md) | `@var` `@return` | +| Attribute | PHPDoc Annotations | +|--------------------------------------------------------------------------------------------------------------------|--------------------------------------| +| [Deprecated](https://github.com/php-static-analysis/attributes/blob/main/doc/Deprecated.md) | `@deprecated` | +| [Impure](https://github.com/php-static-analysis/attributes/blob/main/doc/Impure.md) | `@impure` | +| [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` | +| [ParamOut](https://github.com/php-static-analysis/attributes/blob/main/doc/ParamOut.md) | `@param-out` | +| [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` | +| [PropertyWrite](https://github.com/php-static-analysis/attributes/blob/main/doc/PropertyWrite.md) | `@property-write` | +| [Pure](https://github.com/php-static-analysis/attributes/blob/main/doc/Pure.md) | `@pure` | +| [RequireExtends](https://github.com/php-static-analysis/attributes/blob/main/doc/RequireExtends.md) | `@require-extends` | +| [RequireImplements](https://github.com/php-static-analysis/attributes/blob/main/doc/RequireImplements.md) | `@require-implements` | +| [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` | +| [TemplateExtends](https://github.com/php-static-analysis/attributes/blob/main/doc/TemplateExtends.md) | `@extends` `@template-extends` | +| [TemplateImplements](https://github.com/php-static-analysis/attributes/blob/main/doc/TemplateImplements.md) | `@implements` `@template-implements` | +| [TemplateUse](https://github.com/php-static-analysis/attributes/blob/main/doc/TemplateUse.md) | `@use` `@template-use` | +| [Type](https://github.com/php-static-analysis/attributes/blob/main/doc/Type.md) | `@var` `@return` | diff --git a/composer.json b/composer.json index 946a26d..f1bb957 100644 --- a/composer.json +++ b/composer.json @@ -24,8 +24,8 @@ "prefer-stable": true, "require": { "php": ">=8.0", - "php-static-analysis/attributes": "^0.1.14 || dev-main", - "php-static-analysis/node-visitor": "^0.1.14 || dev-main", + "php-static-analysis/attributes": "^0.1.15 || dev-main", + "php-static-analysis/node-visitor": "^0.1.15 || dev-main", "phpstan/phpstan": "^1.8" }, "require-dev": { diff --git a/tests/ImpureAttributeTest.php b/tests/ImpureAttributeTest.php new file mode 100644 index 0000000..25baa28 --- /dev/null +++ b/tests/ImpureAttributeTest.php @@ -0,0 +1,30 @@ +analyse(__DIR__ . '/data/Impure/MethodImpureAttribute.php'); + $this->assertCount(0, $errors); + } + + public function testFunctionImpureAttribute(): void + { + $errors = $this->analyse(__DIR__ . '/data/Impure/FunctionImpureAttribute.php'); + $this->assertCount(0, $errors); + } + + public function testInvalidMethodImpureAttribute(): void + { + $errors = $this->analyse(__DIR__ . '/data/Impure/InvalidMethodImpureAttribute.php'); + + $expectedErrors = [ + 'Attribute class PhpStaticAnalysis\Attributes\Impure does not have the property target.' => 11, + 'Attribute class PhpStaticAnalysis\Attributes\Impure is not repeatable but is already present above the method.' => 15, + ]; + + $this->checkExpectedErrors($errors, $expectedErrors); + } +} diff --git a/tests/PureAttributeTest.php b/tests/PureAttributeTest.php new file mode 100644 index 0000000..82662ed --- /dev/null +++ b/tests/PureAttributeTest.php @@ -0,0 +1,30 @@ +analyse(__DIR__ . '/data/Pure/MethodPureAttribute.php'); + $this->assertCount(0, $errors); + } + + public function testFunctionPureAttribute(): void + { + $errors = $this->analyse(__DIR__ . '/data/Pure/FunctionPureAttribute.php'); + $this->assertCount(0, $errors); + } + + public function testInvalidMethodPureAttribute(): void + { + $errors = $this->analyse(__DIR__ . '/data/Pure/InvalidMethodPureAttribute.php'); + + $expectedErrors = [ + 'Attribute class PhpStaticAnalysis\Attributes\Pure does not have the property target.' => 11, + 'Attribute class PhpStaticAnalysis\Attributes\Pure is not repeatable but is already present above the method.' => 15, + ]; + + $this->checkExpectedErrors($errors, $expectedErrors); + } +} diff --git a/tests/data/Impure/FunctionImpureAttribute.php b/tests/data/Impure/FunctionImpureAttribute.php new file mode 100644 index 0000000..e44f2e5 --- /dev/null +++ b/tests/data/Impure/FunctionImpureAttribute.php @@ -0,0 +1,16 @@ +