From 2c65f4ed3b08d4c9a2d37a6de33dab973a9f77f4 Mon Sep 17 00:00:00 2001 From: Carlos Granados Date: Wed, 28 Feb 2024 18:43:56 +0100 Subject: [PATCH] Add Immutable attribute --- README.md | 48 +++++++++---------- composer.json | 4 +- tests/ImmutableAttributeTest.php | 41 ++++++++++++++++ .../Immutable/ClassImmutableAttribute.php | 14 ++++++ .../Immutable/InterfaceImmutableAttribute.php | 10 ++++ .../InvalidClassImmutableAttribute.php | 15 ++++++ .../Immutable/TraitImmutableAttribute.php | 10 ++++ 7 files changed, 116 insertions(+), 26 deletions(-) create mode 100644 tests/ImmutableAttributeTest.php create mode 100644 tests/data/Immutable/ClassImmutableAttribute.php create mode 100644 tests/data/Immutable/InterfaceImmutableAttribute.php create mode 100644 tests/data/Immutable/InvalidClassImmutableAttribute.php create mode 100644 tests/data/Immutable/TraitImmutableAttribute.php diff --git a/README.md b/README.md index 046f0ed..23fb0a9 100644 --- a/README.md +++ b/README.md @@ -97,27 +97,27 @@ This extension works by interacting with the parser that Psalm uses to parse the 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` | -| [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` | -| [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` | +| [Immmutable](https://github.com/php-static-analysis/attributes/blob/main/doc/Immmutable.md) | `@immmutable` | +| [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` | +| [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 84dc743..2115633 100644 --- a/composer.json +++ b/composer.json @@ -26,8 +26,8 @@ "require": { "php": ">=8.0", "ext-simplexml": "*", - "php-static-analysis/attributes": "^0.1.16 || dev-main", - "php-static-analysis/node-visitor": "^0.1.16 || dev-main", + "php-static-analysis/attributes": "^0.1.17 || dev-main", + "php-static-analysis/node-visitor": "^0.1.17 || dev-main", "vimeo/psalm": "^5" }, "require-dev": { diff --git a/tests/ImmutableAttributeTest.php b/tests/ImmutableAttributeTest.php new file mode 100644 index 0000000..f591f76 --- /dev/null +++ b/tests/ImmutableAttributeTest.php @@ -0,0 +1,41 @@ +analyzeTestFile( '/data/Immutable/ClassImmutableAttribute.php'); + $expectedErrors = [ + 'test\PhpStaticAnalysis\PsalmPlugin\data\Immutable\ClassImmutableAttribute::$name is marked readonly' => 14, + ]; + + $this->checkExpectedErrors($errors, $expectedErrors); + } + + public function testTraitImmutableAttribute(): void + { + $errors = $this->analyzeTestFile( '/data/Immutable/TraitImmutableAttribute.php'); + $this->assertCount(0, $errors); + } + + public function testInterfaceImmutableAttribute(): void + { + $errors = $this->analyzeTestFile( '/data/Immutable/InterfaceImmutableAttribute.php'); + $this->assertCount(0, $errors); + } + + public function testInvalidClassImmutableAttribute(): void + { + $errors = $this->analyzeTestFile( '/data/Immutable/InvalidClassImmutableAttribute.php'); + + $expectedErrors = [ + 'Attribute Immutable is not repeatable' => 10, + 'Attribute Immutable cannot be used on a property' => 13, + ]; + + $this->checkExpectedErrors($errors, $expectedErrors); + } +} diff --git a/tests/data/Immutable/ClassImmutableAttribute.php b/tests/data/Immutable/ClassImmutableAttribute.php new file mode 100644 index 0000000..d5f4f5f --- /dev/null +++ b/tests/data/Immutable/ClassImmutableAttribute.php @@ -0,0 +1,14 @@ +name = 'John'; diff --git a/tests/data/Immutable/InterfaceImmutableAttribute.php b/tests/data/Immutable/InterfaceImmutableAttribute.php new file mode 100644 index 0000000..1e280f6 --- /dev/null +++ b/tests/data/Immutable/InterfaceImmutableAttribute.php @@ -0,0 +1,10 @@ +