From 089d8a8258ed0aeefdc7b68b6c3d25572ebfdbaa Mon Sep 17 00:00:00 2001 From: Ondrej Mirtes Date: Wed, 7 Jun 2023 11:51:18 +0200 Subject: [PATCH] DeprecatedScopeResolver - documentation in README --- README.md | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/README.md b/README.md index 3a93df7..5967702 100644 --- a/README.md +++ b/README.md @@ -36,3 +36,48 @@ In case you don't own the code which you want to be considered deprecated, use [ /** @deprecated */ class ThirdPartyClass {} ``` + + +## Custom deprecated scopes + +Usage of deprecated code is not reported in code that is also deprecated: + +```php +/** @deprecated */ +function doFoo(): void +{ + // not reported: + anotherDeprecatedFunction(); +} +``` + +If you have [a different way](https://github.com/phpstan/phpstan-deprecation-rules/issues/64) of marking code that calls deprecated symbols on purpose and you don't want these calls to be reported either, you can write an extension by implementing the [`DeprecatedScopeResolver`](https://github.com/phpstan/phpstan-deprecation-rules/blob/1.1.x/src/Rules/Deprecations/DeprecatedScopeResolver.php) interface. + +For example if you mark your PHPUnit tests that test deprecated code with `@group legacy`, you can implement the extension this way: + +```php +class GroupLegacyScopeResolver implements DeprecatedScopeResolver +{ + + public function isScopeDeprecated(Scope $scope): bool + { + $function = $scope->getFunction(); + return $function !== null + && $function->getDocComment() !== null + && strpos($function->getDocComment(), '@group legacy') !== false; + } + +} +``` + +And register it in your [configuration file](https://phpstan.org/config-reference): + +```neon +services: + - + class: GroupLegacyScopeResolver + tags: + - phpstan.deprecations.deprecatedScopeResolver +``` + +[Learn more about Scope](https://phpstan.org/developing-extensions/scope), a core concept for implementing custom PHPStan extensions.