Skip to content

Commit 4473157

Browse files
phpstan-botclaude
andcommitted
Add tests for direct instantiation of ReflectionClass with non-final class
Demonstrates that `new ReflectionClass(NonFinalClass::class)` returns precise constant types since the generic parameter T is invariant, not covariant. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 3ffb67d commit 4473157

1 file changed

Lines changed: 16 additions & 0 deletions

File tree

tests/PHPStan/Analyser/nsrt/reflection-class-get-constants.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,3 +247,19 @@ function testGetConstantCovariantEnum(ReflectionClass $ref): void
247247
assertType('ReflectionClassGetConstants\SimpleEnum::Hearts', $ref->getConstant('Hearts'));
248248
assertType('array{Hearts: ReflectionClassGetConstants\SimpleEnum::Hearts, Diamonds: ReflectionClassGetConstants\SimpleEnum::Diamonds}', $ref->getConstants());
249249
}
250+
251+
function testGetConstantDirectInstantiation(): void
252+
{
253+
$ref = new ReflectionClass(Foo::class);
254+
assertType('1', $ref->getConstant('A'));
255+
assertType("'hello'", $ref->getConstant('B'));
256+
assertType('false', $ref->getConstant('nonExistent'));
257+
assertType("array{A: 1, B: 'hello', C: 3.14, D: true}", $ref->getConstants());
258+
}
259+
260+
function testGetConstantDirectInstantiationFinalClass(): void
261+
{
262+
$ref = new ReflectionClass(FinalClass::class);
263+
assertType('1', $ref->getConstant('ONE'));
264+
assertType('array{ONE: 1, TWO: 2}', $ref->getConstants());
265+
}

0 commit comments

Comments
 (0)