Skip to content

Commit 1f9a054

Browse files
authored
Narrow ReflectionEnum::getBackingType() after ReflectionEnum::isBacked()
1 parent 1d9ee88 commit 1f9a054

File tree

5 files changed

+53
-6
lines changed

5 files changed

+53
-6
lines changed

src/Reflection/ClassReflection.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -660,12 +660,7 @@ public function getBackedEnumType(): ?Type
660660
return null;
661661
}
662662

663-
$reflectionType = $this->reflection->getBackingType();
664-
if ($reflectionType === null) {
665-
return null;
666-
}
667-
668-
return TypehintHelper::decideTypeFromReflection($reflectionType);
663+
return TypehintHelper::decideTypeFromReflection($this->reflection->getBackingType());
669664
}
670665

671666
public function hasEnumCase(string $name): bool

stubs/ReflectionEnum.stub

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ class ReflectionEnum extends ReflectionClass
2020

2121
/**
2222
* @phpstan-assert-if-true self<BackedEnum> $this
23+
* @phpstan-assert-if-true !null $this->getBackingType()
2324
*/
2425
public function isBacked(): bool {}
2526

tests/PHPStan/Analyser/NodeScopeResolverTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,11 @@ public function dataFileAsserts(): iterable
256256
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-9734.php');
257257
yield from $this->gatherAssertTypes(__DIR__ . '/data/enum-reflection.php');
258258
}
259+
if (PHP_VERSION_ID >= 80200) {
260+
yield from $this->gatherAssertTypes(__DIR__ . '/data/enum-reflection-php82.php');
261+
} elseif (PHP_VERSION_ID >= 80100) {
262+
yield from $this->gatherAssertTypes(__DIR__ . '/data/enum-reflection-php81.php');
263+
}
259264

260265
yield from $this->gatherAssertTypes(__DIR__ . '/data/match-expr.php');
261266

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php // lint >= 8.1
2+
3+
namespace EnumReflection81;
4+
5+
use ReflectionEnum;
6+
use ReflectionEnumBackedCase;
7+
use ReflectionEnumUnitCase;
8+
use function PHPStan\Testing\assertType;
9+
10+
enum Foo: int
11+
{
12+
13+
case FOO = 1;
14+
case BAR = 2;
15+
}
16+
17+
function testNarrowGetBackingTypeAfterIsBacked() {
18+
$r = new ReflectionEnum(Foo::class);
19+
assertType('ReflectionType|null', $r->getBackingType());
20+
if ($r->isBacked()) {
21+
assertType('ReflectionType', $r->getBackingType());
22+
}
23+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php // lint >= 8.1
2+
3+
namespace EnumReflection82;
4+
5+
use ReflectionEnum;
6+
use ReflectionEnumBackedCase;
7+
use ReflectionEnumUnitCase;
8+
use function PHPStan\Testing\assertType;
9+
10+
enum Foo: int
11+
{
12+
13+
case FOO = 1;
14+
case BAR = 2;
15+
}
16+
17+
function testNarrowGetBackingTypeAfterIsBacked() {
18+
$r = new ReflectionEnum(Foo::class);
19+
assertType('ReflectionNamedType|null', $r->getBackingType());
20+
if ($r->isBacked()) {
21+
assertType('ReflectionNamedType', $r->getBackingType());
22+
}
23+
}

0 commit comments

Comments
 (0)