File tree Expand file tree Collapse file tree 5 files changed +53
-6
lines changed Expand file tree Collapse file tree 5 files changed +53
-6
lines changed Original file line number Diff line number Diff line change @@ -676,12 +676,7 @@ public function getBackedEnumType(): ?Type
676
676
return null ;
677
677
}
678
678
679
- $ reflectionType = $ this ->reflection ->getBackingType ();
680
- if ($ reflectionType === null ) {
681
- return null ;
682
- }
683
-
684
- return TypehintHelper::decideTypeFromReflection ($ reflectionType );
679
+ return TypehintHelper::decideTypeFromReflection ($ this ->reflection ->getBackingType ());
685
680
}
686
681
687
682
public function hasEnumCase (string $ name ): bool
Original file line number Diff line number Diff line change @@ -20,6 +20,7 @@ class ReflectionEnum extends ReflectionClass
20
20
21
21
/**
22
22
* @phpstan-assert-if-true self<BackedEnum> $this
23
+ * @phpstan-assert-if-true !null $this->getBackingType()
23
24
*/
24
25
public function isBacked(): bool {}
25
26
Original file line number Diff line number Diff line change @@ -256,6 +256,11 @@ public function dataFileAsserts(): iterable
256
256
yield from $ this ->gatherAssertTypes (__DIR__ . '/data/bug-9734.php ' );
257
257
yield from $ this ->gatherAssertTypes (__DIR__ . '/data/enum-reflection.php ' );
258
258
}
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
+ }
259
264
260
265
yield from $ this ->gatherAssertTypes (__DIR__ . '/data/match-expr.php ' );
261
266
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments