diff --git a/README.md b/README.md index a5d0d62..115f5fd 100644 --- a/README.md +++ b/README.md @@ -96,6 +96,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 9e25317..a2bebf0 100644 --- a/composer.json +++ b/composer.json @@ -24,12 +24,13 @@ "prefer-stable": true, "require": { "php": ">=8.0", - "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", "phpstan/phpstan": "^1.8" }, "require-dev": { "php-static-analysis/psalm-plugin": "dev-main", + "phpstan/phpstan-deprecation-rules": "^1.1", "phpunit/phpunit": "^9.0", "symplify/easy-coding-standard": "^12.1", "vimeo/psalm": "^5" diff --git a/tests/DeprecatedAttributeTest.php b/tests/DeprecatedAttributeTest.php new file mode 100644 index 0000000..90d705d --- /dev/null +++ b/tests/DeprecatedAttributeTest.php @@ -0,0 +1,72 @@ +analyse(__DIR__ . '/data/Deprecated/ClassDeprecatedAttribute.php'); + $expectedErrors = [ + 'Instantiation of deprecated class test\PhpStaticAnalysis\PHPStanExtension\data\Deprecated\ClassDeprecatedAttribute.' => 12, + ]; + + $this->checkExpectedErrors($errors, $expectedErrors); + } + + public function testTraitDeprecatedAttribute(): void + { + $errors = $this->analyse(__DIR__ . '/data/Deprecated/TraitDeprecatedAttribute.php'); + $this->assertCount(0, $errors); + } + + public function testInterfaceDeprecatedAttribute(): void + { + $errors = $this->analyse(__DIR__ . '/data/Deprecated/InterfaceDeprecatedAttribute.php'); + $this->assertCount(0, $errors); + } + + public function testMethodDeprecatedAttribute(): void + { + $errors = $this->analyse(__DIR__ . '/data/Deprecated/MethodDeprecatedAttribute.php'); + $expectedErrors = [ + 'Call to deprecated method returnDeprecated() of class test\PhpStaticAnalysis\PHPStanExtension\data\Deprecated\MethodDeprecatedAttribute.' => 31, + ]; + + $this->checkExpectedErrors($errors, $expectedErrors); + } + + public function testFunctionDeprecatedAttribute(): void + { + $errors = $this->analyse(__DIR__ . '/data/Deprecated/FunctionDeprecatedAttribute.php'); + $this->assertCount(0, $errors); + } + + public function testProperyDeprecatedAttribute(): void + { + $errors = $this->analyse(__DIR__ . '/data/Deprecated/PropertyDeprecatedAttribute.php'); + $this->assertCount(0, $errors); + } + + public function testInvalidMethodDeprecatedAttribute(): void + { + $errors = $this->analyse(__DIR__ . '/data/Deprecated/InvalidMethodDeprecatedAttribute.php'); + + $expectedErrors = [ + 'Attribute class PhpStaticAnalysis\Attributes\Deprecated does not have the parameter target.' => 12, + 'Attribute class PhpStaticAnalysis\Attributes\Deprecated is not repeatable but is already present above the method.' => 19, + ]; + + $this->checkExpectedErrors($errors, $expectedErrors); + } + + public static function getAdditionalConfigFiles(): array + { + return array_merge( + parent::getAdditionalConfigFiles(), + [ + __DIR__ . '/conf/deprecated.neon', + ] + ); + } +} diff --git a/tests/conf/deprecated.neon b/tests/conf/deprecated.neon new file mode 100644 index 0000000..af06b71 --- /dev/null +++ b/tests/conf/deprecated.neon @@ -0,0 +1,2 @@ +includes: + - ../../vendor/phpstan/phpstan-deprecation-rules/rules.neon diff --git a/tests/data/Deprecated/ClassDeprecatedAttribute.php b/tests/data/Deprecated/ClassDeprecatedAttribute.php new file mode 100644 index 0000000..f1a7315 --- /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..bd91928 --- /dev/null +++ b/tests/data/Deprecated/PropertyDeprecatedAttribute.php @@ -0,0 +1,14 @@ +