Skip to content

Commit

Permalink
Added support for @deprecated to all reflections
Browse files Browse the repository at this point in the history
  • Loading branch information
vudaltsov committed Sep 22, 2023
1 parent a8d0bde commit 83932f6
Show file tree
Hide file tree
Showing 11 changed files with 89 additions and 4 deletions.
2 changes: 1 addition & 1 deletion docs/compatibility.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@
| `isAbstract()` ||
| `isClosure()` ||
| `isConstructor()` ||
| `isDeprecated()` | ❌ TODO |
| `isDeprecated()` | |
| `isDestructor()` ||
| `isFinal()` ||
| `isGenerator()` ||
Expand Down
6 changes: 6 additions & 0 deletions src/ClassReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public function __construct(
private readonly bool $trait,
private readonly int $modifiers,
private readonly bool $anonymous,
private readonly bool $deprecated,
private readonly ?Type\NamedObjectType $parentType,
private readonly array $ownInterfaceTypes,
private readonly array $ownProperties,
Expand Down Expand Up @@ -293,6 +294,11 @@ public function isIterable(): bool
&& $this->implementsInterface(\Traversable::class);
}

public function isDeprecated(): bool
{
return $this->deprecated;
}

/**
* @return list<interface-string>
*/
Expand Down
6 changes: 6 additions & 0 deletions src/MethodReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public function __construct(
private readonly ?int $endLine,
private readonly bool $returnsReference,
private readonly bool $generator,
private readonly bool $deprecated,
/** @readonly */
private array $parameters,
/** @readonly */
Expand Down Expand Up @@ -254,6 +255,11 @@ public function returnsReference(): bool
return $this->returnsReference;
}

public function isDeprecated(): bool
{
return $this->deprecated;
}

/**
* @return int<0, max>
*/
Expand Down
6 changes: 6 additions & 0 deletions src/ParameterReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public function __construct(
private readonly bool $optional,
private readonly bool $variadic,
private readonly bool $promoted,
private readonly bool $deprecated,
/** @readonly */
private TypeReflection $type,
private readonly ?int $startLine,
Expand Down Expand Up @@ -93,6 +94,11 @@ public function isVariadic(): bool
return $this->variadic;
}

public function isDeprecated(): bool
{
return $this->deprecated;
}

public function __serialize(): array
{
$data = get_object_vars($this);
Expand Down
12 changes: 12 additions & 0 deletions src/PhpDocParser/PhpDoc.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Typhoon\Reflection\PhpDocParser;

use PHPStan\PhpDocParser\Ast\PhpDoc\DeprecatedTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\ExtendsTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\ImplementsTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\ParamTagValueNode;
Expand Down Expand Up @@ -65,6 +66,17 @@ public static function empty(): self
);
}

public function isDeprecated(): bool
{
foreach ($this->tags as $tag) {
if ($tag->value instanceof DeprecatedTagValueNode) {
return true;
}
}

return false;
}

public function varType(): ?TypeNode
{
if ($this->varType !== false) {
Expand Down
6 changes: 6 additions & 0 deletions src/PropertyReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public function __construct(
private readonly bool $hasDefaultValue,
private readonly bool $promoted,
private readonly int $modifiers,
private readonly bool $deprecated,
/** @readonly */
private TypeReflection $type,
private readonly ?int $startLine,
Expand Down Expand Up @@ -122,6 +123,11 @@ public function isPromoted(): bool
return $this->promoted;
}

public function isDeprecated(): bool
{
return $this->deprecated;
}

public function setValue(?object $object, mixed $value): void
{
if ($this->isStatic()) {
Expand Down
4 changes: 4 additions & 0 deletions src/Reflector/NativeReflectionReflector.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ enum: $class->isEnum(),
trait: $class->isTrait(),
modifiers: $class->getModifiers(),
anonymous: $class->isAnonymous(),
deprecated: false,
parentType: $this->reflectParent($class),
ownInterfaceTypes: array_map(
static fn (string $interface): Type\NamedObjectType => types::object($interface),
Expand Down Expand Up @@ -103,6 +104,7 @@ class: $class->name,
hasDefaultValue: $property->hasDefaultValue(),
promoted: $property->isPromoted(),
modifiers: $modifiers,
deprecated: false,
type: $this->reflectType($property->getType(), $class->name),
startLine: null,
endLine: null,
Expand Down Expand Up @@ -137,6 +139,7 @@ class: $class->name,
endLine: $method->getEndLine() ?: null,
returnsReference: $method->returnsReference(),
generator: $method->isGenerator(),
deprecated: $method->isDeprecated(),
parameters: $this->reflectParameters($method, $class->name),
returnType: $this->reflectType($method->getReturnType(), $class->name),
);
Expand Down Expand Up @@ -168,6 +171,7 @@ function: $reflectedFunction,
optional: $parameter->isOptional(),
variadic: $parameter->isVariadic(),
promoted: $parameter->isPromoted(),
deprecated: false,
type: $this->reflectType($parameter->getType(), $class),
startLine: null,
endLine: null,
Expand Down
1 change: 1 addition & 0 deletions src/Reflector/PhpDocParsingVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public function enterNode(Node $node): ?int
$node instanceof Node\Stmt\ClassLike
|| $node instanceof Node\Stmt\Property
|| $node instanceof Node\Stmt\ClassMethod
|| $node instanceof Node\Param
)) {
return null;
}
Expand Down
17 changes: 15 additions & 2 deletions src/Reflector/PhpParserReflector.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ enum: $node instanceof Stmt\Enum_,
trait: $node instanceof Stmt\Trait_,
modifiers: $this->reflectClassModifiers($node),
anonymous: $node->name === null,
deprecated: $phpDoc->isDeprecated(),
parentType: $this->reflectParent($node, $phpDoc),
ownInterfaceTypes: $this->reflectOwnInterfaceTypes($node, $phpDoc),
ownProperties: $this->reflectOwnProperties(class: $name, classNode: $node),
Expand All @@ -80,6 +81,7 @@ class: $class,
hasDefaultValue: false,
promoted: false,
modifiers: PropertyReflection::IS_PUBLIC + PropertyReflection::IS_READONLY,
deprecated: false,
type: new TypeReflection(native: types::string, phpDoc: types::nonEmptyString),
startLine: null,
endLine: null,
Expand Down Expand Up @@ -204,6 +206,7 @@ class: $class,
hasDefaultValue: $property->default !== null || $node->type === null,
promoted: false,
modifiers: $this->reflectPropertyModifiers($node, $classReadOnly),
deprecated: $phpDoc->isDeprecated(),
type: $type,
startLine: $node->getStartLine() > 0 ? $node->getStartLine() : null,
endLine: $node->getEndLine() > 0 ? $node->getEndLine() : null,
Expand Down Expand Up @@ -235,6 +238,7 @@ class: $class,
hasDefaultValue: $node->default !== null || $node->type === null,
promoted: true,
modifiers: $modifiers,
deprecated: $phpDoc->isDeprecated(),
type: $this->reflectType($node->type, $phpDoc->paramTypes()[$name] ?? null),
startLine: $node->getStartLine() > 0 ? $node->getStartLine() : null,
endLine: $node->getEndLine() > 0 ? $node->getEndLine() : null,
Expand Down Expand Up @@ -309,6 +313,7 @@ class: $class,
endLine: $node->getEndLine() > 0 ? $node->getEndLine() : null,
returnsReference: $node->byRef,
generator: $this->reflectIsGenerator($node),
deprecated: $phpDoc->isDeprecated(),
parameters: $this->reflectParameters([$class, $name], $node->params, $phpDoc),
returnType: $this->reflectType($node->returnType, $phpDoc->returnType()),
);
Expand Down Expand Up @@ -371,14 +376,15 @@ public function enterNode(Node $node): ?int
* @param array<Node\Param> $nodes
* @return list<ParameterReflection>
*/
private function reflectParameters(string|array $function, array $nodes, PhpDoc $phpDoc): array
private function reflectParameters(string|array $function, array $nodes, PhpDoc $methodPhpDoc): array
{
$parameters = [];
$isOptional = false;

foreach (array_values($nodes) as $position => $node) {
\assert($node->var instanceof Expr\Variable && \is_string($node->var->name));
$name = $node->var->name;
$phpDoc = PhpDocParsingVisitor::fromNode($node);
$isOptional = $isOptional || $node->default !== null || $node->variadic;
$parameters[] = new ParameterReflection(
function: $function,
Expand All @@ -389,7 +395,8 @@ function: $function,
optional: $isOptional,
variadic: $node->variadic,
promoted: $this->isParameterPromoted($node),
type: $this->reflectType($node->type, $phpDoc->paramTypes()[$name] ?? null),
deprecated: $phpDoc->isDeprecated(),
type: $this->reflectType($node->type, $methodPhpDoc->paramTypes()[$name] ?? null),
startLine: $node->getStartLine() > 0 ? $node->getStartLine() : null,
endLine: $node->getEndLine() > 0 ? $node->getEndLine() : null,
);
Expand Down Expand Up @@ -540,6 +547,7 @@ class: $class,
hasDefaultValue: false,
promoted: false,
modifiers: PropertyReflection::IS_PUBLIC + PropertyReflection::IS_READONLY,
deprecated: false,
type: $this->reflectType($scalarType, null),
startLine: null,
endLine: null,
Expand All @@ -564,6 +572,7 @@ class: $class,
endLine: null,
returnsReference: false,
generator: false,
deprecated: false,
parameters: [],
returnType: new TypeReflection(types::array(), types::list(types::object($class))),
);
Expand Down Expand Up @@ -591,6 +600,7 @@ class: $class,
endLine: null,
returnsReference: false,
generator: false,
deprecated: false,
parameters: [
new ParameterReflection(
function: [$class, 'from'],
Expand All @@ -601,6 +611,7 @@ function: [$class, 'from'],
optional: false,
variadic: false,
promoted: false,
deprecated: false,
type: $valueType,
startLine: null,
endLine: null,
Expand All @@ -621,6 +632,7 @@ class: $class,
endLine: null,
returnsReference: false,
generator: false,
deprecated: false,
parameters: [
new ParameterReflection(
function: [$class, 'tryFrom'],
Expand All @@ -631,6 +643,7 @@ function: [$class, 'tryFrom'],
optional: false,
variadic: false,
promoted: false,
deprecated: false,
type: $valueType,
startLine: null,
endLine: null,
Expand Down
31 changes: 31 additions & 0 deletions tests/unit/PhpDocParser/PhpDocParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,37 @@
#[CoversClass(PhpDoc::class)]
final class PhpDocParserTest extends TestCase
{
public function testIsDeprecatedReturnsFalseIfNoDeprecatedTag(): void
{
$parser = new PhpDocParser();

$deprecated = $parser->parsePhpDoc(
<<<'PHP'
/**
* @example
*/
PHP,
)->isDeprecated();

self::assertFalse($deprecated);
}

public function testIsDeprecatedReturnsTrueIfDeprecated(): void
{
$parser = new PhpDocParser();

$deprecated = $parser->parsePhpDoc(
<<<'PHP'
/**
* @example
* @deprecated
*/
PHP,
)->isDeprecated();

self::assertTrue($deprecated);
}

public function testItReturnsNullVarTypeWhenNoVarTag(): void
{
$parser = new PhpDocParser();
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/ReflectorCompatibilityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ private function assertMethodEquals(\ReflectionMethod $native, MethodReflection
self::assertSame($native->isAbstract(), $typhoon->isAbstract(), $messagePrefix . '.isAbstract()');
self::assertSame($native->isClosure(), $typhoon->isClosure(), $messagePrefix . '.isClosure()');
self::assertSame($native->isConstructor(), $typhoon->isConstructor(), $messagePrefix . '.isConstructor()');
// self::assertSame($native->isDeprecated(), $typhoon->isDeprecated(), $messagePrefix . '.isDeprecated()');
self::assertSame($native->isDeprecated(), $typhoon->isDeprecated(), $messagePrefix . '.isDeprecated()');
self::assertSame($native->isDestructor(), $typhoon->isDestructor(), $messagePrefix . '.isDestructor()');
self::assertSame($native->isFinal(), $typhoon->isFinal(), $messagePrefix . '.isFinal()');
self::assertSame($native->isGenerator(), $typhoon->isGenerator(), $messagePrefix . '.isGenerator()');
Expand Down

0 comments on commit 83932f6

Please sign in to comment.