From 02d1a4aaaaa876a535284353829a19adfa0cb27b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jaroslav=20Hansl=C3=ADk?= Date: Sat, 23 Nov 2024 21:42:03 +0100 Subject: [PATCH] Parameter can be marked as deprecated by #[Deprecated] --- .../Deprecated/DeprecatedHelper.php | 7 ++++- src/Reflection/ReflectionParameter.php | 6 ++++ .../Reflection/ReflectionParameterTest.php | 31 +++++++++++++++++++ 3 files changed, 43 insertions(+), 1 deletion(-) diff --git a/src/Reflection/Deprecated/DeprecatedHelper.php b/src/Reflection/Deprecated/DeprecatedHelper.php index 85e57f022..bc5d2e671 100644 --- a/src/Reflection/Deprecated/DeprecatedHelper.php +++ b/src/Reflection/Deprecated/DeprecatedHelper.php @@ -11,19 +11,24 @@ use Roave\BetterReflection\Reflection\ReflectionEnumCase; use Roave\BetterReflection\Reflection\ReflectionFunction; use Roave\BetterReflection\Reflection\ReflectionMethod; +use Roave\BetterReflection\Reflection\ReflectionParameter; use Roave\BetterReflection\Reflection\ReflectionProperty; /** @internal */ final class DeprecatedHelper { /** @psalm-pure */ - public static function isDeprecated(ReflectionClass|ReflectionMethod|ReflectionFunction|ReflectionClassConstant|ReflectionEnumCase|ReflectionProperty $reflection): bool + public static function isDeprecated(ReflectionClass|ReflectionMethod|ReflectionFunction|ReflectionClassConstant|ReflectionEnumCase|ReflectionProperty|ReflectionParameter $reflection): bool { // We don't use Deprecated::class because the class is currently missing in stubs if (ReflectionAttributeHelper::filterAttributesByName($reflection->getAttributes(), 'Deprecated') !== []) { return true; } + if ($reflection instanceof ReflectionParameter) { + return false; + } + return AnnotationHelper::isDeprecated($reflection->getDocComment()); } } diff --git a/src/Reflection/ReflectionParameter.php b/src/Reflection/ReflectionParameter.php index a7bf8044c..3855c5251 100644 --- a/src/Reflection/ReflectionParameter.php +++ b/src/Reflection/ReflectionParameter.php @@ -16,6 +16,7 @@ use Roave\BetterReflection\NodeCompiler\CompilerContext; use Roave\BetterReflection\NodeCompiler\Exception\UnableToCompileNode; use Roave\BetterReflection\Reflection\Attribute\ReflectionAttributeHelper; +use Roave\BetterReflection\Reflection\Deprecated\DeprecatedHelper; use Roave\BetterReflection\Reflection\Exception\CodeLocationMissing; use Roave\BetterReflection\Reflection\StringCast\ReflectionParameterStringCast; use Roave\BetterReflection\Reflector\Reflector; @@ -537,6 +538,11 @@ public function getEndColumn(): int return $this->endColumn; } + public function isDeprecated(): bool + { + return DeprecatedHelper::isDeprecated($this); + } + /** @return list */ public function getAttributes(): array { diff --git a/test/unit/Reflection/ReflectionParameterTest.php b/test/unit/Reflection/ReflectionParameterTest.php index a258adad6..8186aeb57 100644 --- a/test/unit/Reflection/ReflectionParameterTest.php +++ b/test/unit/Reflection/ReflectionParameterTest.php @@ -779,4 +779,35 @@ public function testWithFunction(): void self::assertCount(2, $cloneAttributes); self::assertNotSame($attributes[0], $cloneAttributes[0]); } + + /** @return list */ + public static function deprecatedProvider(): array + { + return [ + [ + '#[Deprecated]', + true, + ], + [ + '', + false, + ], + ]; + } + + #[DataProvider('deprecatedProvider')] + public function testIsDeprecated(string $deprecatedCode, bool $isDeprecated): void + { + $php = sprintf('astLocator())); + $functionReflection = $reflector->reflectFunction('foo'); + $parameterReflection = $functionReflection->getParameter('parameter'); + + self::assertSame($isDeprecated, $parameterReflection->isDeprecated()); + } }