Skip to content

Commit

Permalink
Parameter can be marked as deprecated by #[Deprecated]
Browse files Browse the repository at this point in the history
  • Loading branch information
kukulich committed Nov 23, 2024
1 parent 37be40b commit 02d1a4a
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/Reflection/Deprecated/DeprecatedHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
6 changes: 6 additions & 0 deletions src/Reflection/ReflectionParameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -537,6 +538,11 @@ public function getEndColumn(): int
return $this->endColumn;
}

public function isDeprecated(): bool
{
return DeprecatedHelper::isDeprecated($this);
}

/** @return list<ReflectionAttribute> */
public function getAttributes(): array
{
Expand Down
31 changes: 31 additions & 0 deletions test/unit/Reflection/ReflectionParameterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -779,4 +779,35 @@ public function testWithFunction(): void
self::assertCount(2, $cloneAttributes);
self::assertNotSame($attributes[0], $cloneAttributes[0]);
}

/** @return list<array{0: string, 1: bool}> */
public static function deprecatedProvider(): array
{
return [
[
'#[Deprecated]',
true,
],
[
'',
false,
],
];
}

#[DataProvider('deprecatedProvider')]
public function testIsDeprecated(string $deprecatedCode, bool $isDeprecated): void
{
$php = sprintf('<?php
function foo (
%s
$parameter
) {}', $deprecatedCode);

$reflector = new DefaultReflector(new StringSourceLocator($php, BetterReflectionSingleton::instance()->astLocator()));
$functionReflection = $reflector->reflectFunction('foo');
$parameterReflection = $functionReflection->getParameter('parameter');

self::assertSame($isDeprecated, $parameterReflection->isDeprecated());
}
}

0 comments on commit 02d1a4a

Please sign in to comment.