Skip to content

Commit

Permalink
Narrow ReflectionEnum::getName() after ReflectionEnum::isBacked()
Browse files Browse the repository at this point in the history
  • Loading branch information
franmomu authored and ondrejmirtes committed Dec 7, 2023
1 parent 63a6133 commit 1b126c9
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
5 changes: 5 additions & 0 deletions stubs/ReflectionEnum.stub
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,9 @@ class ReflectionEnum extends ReflectionClass
*/
public function getCase(string $name): ReflectionEnumUnitCase {}

/**
* @phpstan-assert-if-true self<BackedEnum> $this
*/
public function isBacked(): bool {}

}
9 changes: 9 additions & 0 deletions tests/PHPStan/Analyser/data/enum-reflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,12 @@ public function doFoo(): void
}

}

/** @param class-string<UnitEnum> $class */
function testNarrowGetNameTypeAfterIsBacked(string $class) {
$r = new ReflectionEnum($class);
assertType('class-string<UnitEnum>', $r->getName());
if ($r->isBacked()) {
assertType('class-string<BackedEnum>', $r->getName());
}
}

0 comments on commit 1b126c9

Please sign in to comment.