From 3f1c657409b233f1178cf06dc8cf0a7686a0aa53 Mon Sep 17 00:00:00 2001 From: Carlos Granados Date: Wed, 28 Feb 2024 18:36:25 +0100 Subject: [PATCH] Add Immutable attribute --- README.md | 1 + composer.json | 4 +- tests/ImmutableAttributeTest.php | 40 +++++++++++++++++++ .../Immutable/ClassImmutableAttribute.php | 14 +++++++ .../Immutable/InterfaceImmutableAttribute.php | 10 +++++ .../InvalidClassImmutableAttribute.php | 15 +++++++ .../Immutable/TraitImmutableAttribute.php | 10 +++++ 7 files changed, 92 insertions(+), 2 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 8c37236..744116e 100644 --- a/README.md +++ b/README.md @@ -97,6 +97,7 @@ 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` | +| [Immmutable](https://github.com/php-static-analysis/attributes/blob/main/doc/Immmutable.md) | `@immmutable` | | [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` | diff --git a/composer.json b/composer.json index 8b755b6..d0cb790 100644 --- a/composer.json +++ b/composer.json @@ -24,8 +24,8 @@ "prefer-stable": true, "require": { "php": ">=8.0", - "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", "phpstan/phpstan": "^1.8" }, "require-dev": { diff --git a/tests/ImmutableAttributeTest.php b/tests/ImmutableAttributeTest.php new file mode 100644 index 0000000..e61fcff --- /dev/null +++ b/tests/ImmutableAttributeTest.php @@ -0,0 +1,40 @@ +analyse(__DIR__ . '/data/Immutable/ClassImmutableAttribute.php'); + $expectedErrors = [ + ]; + + $this->checkExpectedErrors($errors, $expectedErrors); + } + + public function testTraitImmutableAttribute(): void + { + $errors = $this->analyse(__DIR__ . '/data/Immutable/TraitImmutableAttribute.php'); + $this->assertCount(0, $errors); + } + + public function testInterfaceImmutableAttribute(): void + { + $errors = $this->analyse(__DIR__ . '/data/Immutable/InterfaceImmutableAttribute.php'); + $this->assertCount(0, $errors); + } + + public function testInvalidClassImmutableAttribute(): void + { + $errors = $this->analyse(__DIR__ . '/data/Immutable/InvalidClassImmutableAttribute.php'); + + $expectedErrors = [ + 'Attribute class PhpStaticAnalysis\Attributes\Immutable is not repeatable but is already present above the class.' => 10, + 'Attribute class PhpStaticAnalysis\Attributes\Immutable does not have the property target.' => 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..bd9c6ec --- /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..df1cd08 --- /dev/null +++ b/tests/data/Immutable/InterfaceImmutableAttribute.php @@ -0,0 +1,10 @@ +