1313
1414/**
1515 * @template TEnum of \BackedEnum = \BackedEnum
16- * @template TResult of mixed = mixed
17- * @template-extends Builder<NamedTypeNode, TypeInterface<TResult>>
16+ * @template-extends Builder<NamedTypeNode, TypeInterface<TEnum|int|string>>
1817 */
1918class BackedEnumTypeBuilder extends Builder
2019{
@@ -32,7 +31,7 @@ public function isSupported(TypeStatement $stmt): bool
3231 }
3332
3433 /**
35- * @param \ReflectionEnum<\BackedEnum > $reflection
34+ * @param \ReflectionEnum<TEnum > $reflection
3635 *
3736 * @return non-empty-string
3837 * @throws InternalTypeException
@@ -52,47 +51,44 @@ private function getBackedEnumType(\ReflectionEnum $reflection, NamedTypeNode $s
5251 return $ type ->getName ();
5352 }
5453
55- public function build (TypeStatement $ stmt , BuildingContext $ context ): TypeInterface
54+ public function build (TypeStatement $ stmt , BuildingContext $ context ): BackedEnumType
5655 {
5756 $ this ->expectNoShapeFields ($ stmt );
5857 $ this ->expectNoTemplateArguments ($ stmt );
5958
60- $ reflection = $ this ->createReflectionEnum ($ stmt );
59+ /** @var class-string<TEnum> $class */
60+ $ class = $ stmt ->name ->toString ();
6161
62+ $ reflection = $ this ->createReflectionEnum ($ class , $ stmt );
6263 $ definition = $ this ->getBackedEnumType ($ reflection , $ stmt );
6364
6465 return new BackedEnumType (
65- /** @phpstan-ignore-next-line : The stmt name contains class-string<TEnum> */
66- class: $ stmt ->name ->toString (),
66+ class: $ class ,
6767 /** @phpstan-ignore-next-line : The "getTypeByStatement" returns TypeInterface<value-of<TEnum>> */
6868 type: $ context ->getTypeByDefinition ($ definition ),
6969 );
7070 }
7171
7272 /**
73- * @return \ReflectionEnum<\BackedEnum>
73+ * @param class-string<TEnum> $class
74+ * @return \ReflectionEnum<TEnum>
7475 * @throws InternalTypeException
7576 */
76- protected function createReflectionEnum (NamedTypeNode $ statement ): \ReflectionEnum
77+ protected function createReflectionEnum (string $ class , NamedTypeNode $ stmt ): \ReflectionEnum
7778 {
7879 try {
79- /**
80- * @var \ReflectionEnum<\BackedEnum> $reflection
81- *
82- * @phpstan-ignore-next-line
83- */
84- $ reflection = new \ReflectionEnum ($ statement ->name ->toString ());
80+ $ reflection = new \ReflectionEnum ($ class );
8581 } catch (\ReflectionException $ e ) {
8682 throw InternalTypeException::becauseInternalTypeErrorOccurs (
87- type: $ statement ,
83+ type: $ stmt ,
8884 message: 'The "{{type}}" must be an existing enum ' ,
8985 previous: $ e ,
9086 );
9187 }
9288
9389 if ($ reflection ->getCases () === []) {
9490 throw InternalTypeException::becauseInternalTypeErrorOccurs (
95- type: $ statement ,
91+ type: $ stmt ,
9692 message: 'The "{{type}}" enum requires at least one case ' ,
9793 );
9894 }
0 commit comments