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 @@ -660,12 +660,7 @@ public function getBackedEnumType(): ?Type
660
660
return null ;
661
661
}
662
662
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 ());
669
664
}
670
665
671
666
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