From 1aad2a8205808ac768d5a04546515d29100dc67b Mon Sep 17 00:00:00 2001 From: Carlos Granados Date: Wed, 21 Feb 2024 16:33:34 +0100 Subject: [PATCH] Add Deprecated attribute --- README.md | 1 + composer.json | 4 +- tests/DeprecatedAttributeTest.php | 62 +++++++++++++++++++ .../Deprecated/ClassDeprecatedAttribute.php | 12 ++++ .../FunctionDeprecatedAttribute.php | 10 +++ .../InterfaceDeprecatedAttribute.php | 10 +++ .../InvalidMethodDeprecatedAttribute.php | 23 +++++++ .../Deprecated/MethodDeprecatedAttribute.php | 31 ++++++++++ .../PropertyDeprecatedAttribute.php | 14 +++++ .../Deprecated/TraitDeprecatedAttribute.php | 10 +++ 10 files changed, 175 insertions(+), 2 deletions(-) create mode 100644 tests/DeprecatedAttributeTest.php create mode 100644 tests/data/Deprecated/ClassDeprecatedAttribute.php create mode 100644 tests/data/Deprecated/FunctionDeprecatedAttribute.php create mode 100644 tests/data/Deprecated/InterfaceDeprecatedAttribute.php create mode 100644 tests/data/Deprecated/InvalidMethodDeprecatedAttribute.php create mode 100644 tests/data/Deprecated/MethodDeprecatedAttribute.php create mode 100644 tests/data/Deprecated/PropertyDeprecatedAttribute.php create mode 100644 tests/data/Deprecated/TraitDeprecatedAttribute.php diff --git a/README.md b/README.md index 946d765..b6738a4 100644 --- a/README.md +++ b/README.md @@ -99,6 +99,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` | | [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` | | [Param](https://github.com/php-static-analysis/attributes/blob/main/doc/Param.md) | `@param` | diff --git a/composer.json b/composer.json index 365e7d9..691a196 100644 --- a/composer.json +++ b/composer.json @@ -26,8 +26,8 @@ "require": { "php": ">=8.0", "ext-simplexml": "*", - "php-static-analysis/attributes": "^0.1.7 || dev-main", - "php-static-analysis/node-visitor": "^0.1.7 || dev-main", + "php-static-analysis/attributes": "^0.1.8 || dev-main", + "php-static-analysis/node-visitor": "^0.1.8 || dev-main", "vimeo/psalm": "^5" }, "require-dev": { diff --git a/tests/DeprecatedAttributeTest.php b/tests/DeprecatedAttributeTest.php new file mode 100644 index 0000000..dbc66b5 --- /dev/null +++ b/tests/DeprecatedAttributeTest.php @@ -0,0 +1,62 @@ +analyzeTestFile( '/data/Deprecated/ClassDeprecatedAttribute.php'); + $expectedErrors = [ + 'test\PhpStaticAnalysis\PsalmPlugin\data\Deprecated\ClassDeprecatedAttribute is marked deprecated' => 12, + ]; + + $this->checkExpectedErrors($errors, $expectedErrors); + } + + public function testTraitDeprecatedAttribute(): void + { + $errors = $this->analyzeTestFile( '/data/Deprecated/TraitDeprecatedAttribute.php'); + $this->assertCount(0, $errors); + } + + public function testInterfaceDeprecatedAttribute(): void + { + $errors = $this->analyzeTestFile('/data/Deprecated/InterfaceDeprecatedAttribute.php'); + $this->assertCount(0, $errors); + } + + public function testMethodDeprecatedAttribute(): void + { + $errors = $this->analyzeTestFile('/data/Deprecated/MethodDeprecatedAttribute.php'); + $expectedErrors = [ + 'The method test\PhpStaticAnalysis\PsalmPlugin\data\Deprecated\MethodDeprecatedAttribute::returnDeprecated has been marked as deprecated' => 31, + ]; + + $this->checkExpectedErrors($errors, $expectedErrors); + } + + public function testFunctionDeprecatedAttribute(): void + { + $errors = $this->analyzeTestFile('/data/Deprecated/FunctionDeprecatedAttribute.php'); + $this->assertCount(0, $errors); + } + + public function testProperyDeprecatedAttribute(): void + { + $errors = $this->analyzeTestFile('/data/Deprecated/PropertyDeprecatedAttribute.php'); + $this->assertCount(0, $errors); + } + + public function testInvalidMethodDeprecatedAttribute(): void + { + $errors = $this->analyzeTestFile('/data/Deprecated/InvalidMethodDeprecatedAttribute.php'); + + $expectedErrors = [ + 'Attribute Deprecated cannot be used on a function/method parameter' => 12, + 'Attribute Deprecated is not repeatable' => 19, + ]; + + $this->checkExpectedErrors($errors, $expectedErrors); + } +} diff --git a/tests/data/Deprecated/ClassDeprecatedAttribute.php b/tests/data/Deprecated/ClassDeprecatedAttribute.php new file mode 100644 index 0000000..d76631f --- /dev/null +++ b/tests/data/Deprecated/ClassDeprecatedAttribute.php @@ -0,0 +1,12 @@ +returnDeprecated(); diff --git a/tests/data/Deprecated/PropertyDeprecatedAttribute.php b/tests/data/Deprecated/PropertyDeprecatedAttribute.php new file mode 100644 index 0000000..a2e9fb4 --- /dev/null +++ b/tests/data/Deprecated/PropertyDeprecatedAttribute.php @@ -0,0 +1,14 @@ +