diff --git a/stubs/ReflectionClass.stub b/stubs/ReflectionClass.stub index 06f8e08a2e..39f65a4e82 100644 --- a/stubs/ReflectionClass.stub +++ b/stubs/ReflectionClass.stub @@ -48,4 +48,15 @@ class ReflectionClass public function getAttributes(?string $name = null, int $flags = 0) { } + + /** + * @phpstan-assert-if-true class-string<\UnitEnum> $this->name + * @phpstan-assert-if-true class-string<\UnitEnum> $this->getName() + * @phpstan-assert-if-true ReflectionClass $this + * @return (T is \UnitEnum ? true: false) + */ + public function isEnum(): bool + { + } + } diff --git a/stubs/ReflectionClassWithLazyObjects.stub b/stubs/ReflectionClassWithLazyObjects.stub index 1a53ae8750..373e3deaaa 100644 --- a/stubs/ReflectionClassWithLazyObjects.stub +++ b/stubs/ReflectionClassWithLazyObjects.stub @@ -110,4 +110,14 @@ class ReflectionClass public function isUninitializedLazyObject(object $object): bool { } + + /** + * @phpstan-assert-if-true class-string<\UnitEnum> $this->name + * @phpstan-assert-if-true class-string<\UnitEnum> $this->getName() + * @phpstan-assert-if-true ReflectionClass $this + * @return (T is \UnitEnum ? true: false) + */ + public function isEnum(): bool + { + } } diff --git a/tests/PHPStan/Analyser/data/enum-reflection-php81.php b/tests/PHPStan/Analyser/data/enum-reflection-php81.php index 502de6eebd..57e6f67ee7 100644 --- a/tests/PHPStan/Analyser/data/enum-reflection-php81.php +++ b/tests/PHPStan/Analyser/data/enum-reflection-php81.php @@ -3,8 +3,6 @@ namespace EnumReflection81; use ReflectionEnum; -use ReflectionEnumBackedCase; -use ReflectionEnumUnitCase; use function PHPStan\Testing\assertType; enum Foo: int diff --git a/tests/PHPStan/Analyser/nsrt/reflectionclass-isEnum.php b/tests/PHPStan/Analyser/nsrt/reflectionclass-isEnum.php new file mode 100644 index 0000000000..2937e75789 --- /dev/null +++ b/tests/PHPStan/Analyser/nsrt/reflectionclass-isEnum.php @@ -0,0 +1,38 @@ += 8.1 + +namespace ReflectionClassIsEnum; + +use ReflectionClass; +use function PHPStan\Testing\assertType; + +/** + * @param class-string $class + */ +function testNarrowClassAfterIsEnum(string $class): void { + $r = new ReflectionClass($class); + if ($r->isEnum()) { + assertType('class-string', $r->name); + assertType('class-string', $r->getName()); + + assertType('UnitEnum', $r->newInstance()); + + + // Todo: + //assertType('ReflectionClass', $r); + + } + + + +} + +function testTemplateAssertions(): void { + $enumR = new ReflectionClass(Foo::class); + assertType('ReflectionClass', $enumR); + assertType('true', $enumR->isEnum()); +} + + +enum Foo { + case Bar; +}