File tree Expand file tree Collapse file tree 2 files changed +31
-0
lines changed Expand file tree Collapse file tree 2 files changed +31
-0
lines changed Original file line number Diff line number Diff line change 2929use PHPStan \Type \Generic \TemplateTypeScope ;
3030use PHPStan \Type \Type ;
3131use PHPStan \Type \TypeAlias ;
32+ use PHPStan \Type \TypehintHelper ;
3233use PHPStan \Type \VerbosityLevel ;
3334use ReflectionClass ;
3435use ReflectionEnum ;
@@ -512,6 +513,24 @@ public function isBackedEnum(): bool
512513 return $ this ->reflection ->isBacked ();
513514 }
514515
516+ public function getBackedEnumType (): ?Type
517+ {
518+ if (!$ this ->reflection instanceof ReflectionEnum) {
519+ return null ;
520+ }
521+
522+ if (!$ this ->reflection ->isBacked ()) {
523+ return null ;
524+ }
525+
526+ $ reflectionType = $ this ->reflection ->getBackingType ();
527+ if ($ reflectionType === null ) {
528+ return null ;
529+ }
530+
531+ return TypehintHelper::decideTypeFromReflection ($ reflectionType );
532+ }
533+
515534 public function hasEnumCase (string $ name ): bool
516535 {
517536 if (!$ this ->isEnum ()) {
Original file line number Diff line number Diff line change 3333use PHPStan \PhpDoc \StubPhpDocProvider ;
3434use PHPStan \Testing \PHPStanTestCase ;
3535use PHPStan \Type \FileTypeMapper ;
36+ use PHPStan \Type \IntegerType ;
3637use ReflectionClass ;
3738use ReflectionEnum ;
3839use WrongClassConstantFile \SecuredRouter ;
@@ -339,4 +340,15 @@ public function testEnumIsFinal(): void
339340 $ this ->assertTrue ($ enum ->isFinalByKeyword ());
340341 }
341342
343+ public function testBackedEnumType (): void
344+ {
345+ if (PHP_VERSION_ID < 80100 ) {
346+ $ this ->markTestSkipped ('Test requires PHP 8.1. ' );
347+ }
348+
349+ $ reflectionProvider = $ this ->createReflectionProvider ();
350+ $ enum = $ reflectionProvider ->getClass ('PHPStan\Fixture\TestEnum ' );
351+ $ this ->assertInstanceOf (IntegerType::class, $ enum ->getBackedEnumType ());
352+ }
353+
342354}
You can’t perform that action at this time.
0 commit comments