@@ -102,26 +102,18 @@ public static function create(array $types): ?self
102102 */
103103 public static function key (Type $ type ): ?string
104104 {
105- if ($ type instanceof TemplateType) {
105+ if ($ type instanceof TemplateType || $ type instanceof UnionType || $ type instanceof IntersectionType ) {
106106 return null ;
107107 }
108108
109- // Only a bare case is safe to key by class + case name: for anything else -
110- // $this & Enum::C, a whole single-case enum, an enum subtracted to one case -
111- // EnumCaseObjectType::equals() is false because it requires an EnumCaseObjectType,
112- // which makes instanceof exactly the question being asked here. Type::getEnumCases()
113- // would answer it too, but only by resolving a ClassReflection - and a key has to be
114- // derivable from the type alone, on every comparison, without reflection.
115- // Key by class + case name, the identity equals() compares (describe() would also
116- // fold in a subtracted type, which equals() ignores).
117109 $ enumCaseObject = $ type ->getEnumCaseObject ();
118- if ($ enumCaseObject !== null && $ enumCaseObject -> equals ( $ type ) ) {
110+ if ($ enumCaseObject !== null ) {
119111 return self ::ENUM_CASE_KEY_PREFIX . $ enumCaseObject ->getClassName () . ':: ' . $ enumCaseObject ->getEnumCaseName ();
120112 }
121113
122- $ scalarTypes = $ type ->getConstantScalarTypes ();
123- if (count ($ scalarTypes ) === 1 && $ scalarTypes [ 0 ]-> equals ( $ type ) ) {
124- $ value = $ scalarTypes [0 ]-> getValue () ;
114+ $ scalarValues = $ type ->getConstantScalarValues ();
115+ if (count ($ scalarValues ) === 1 ) {
116+ $ value = $ scalarValues [0 ];
125117 if ($ value === null ) {
126118 return self ::NULL_KEY ;
127119 }
@@ -152,9 +144,13 @@ public static function key(Type $type): ?string
152144 */
153145 private static function kind (Type $ type ): string
154146 {
155- $ enumCaseObject = $ type ->getEnumCaseObject ();
156- if ($ enumCaseObject !== null && $ enumCaseObject ->equals ($ type )) {
157- return self ::ENUM_CASE_KEY_PREFIX . $ enumCaseObject ->getClassName ();
147+ if (!$ type instanceof UnionType
148+ && !$ type instanceof IntersectionType
149+ ) {
150+ $ enumCaseObject = $ type ->getEnumCaseObject ();
151+ if ($ enumCaseObject !== null ) {
152+ return self ::ENUM_CASE_KEY_PREFIX . $ enumCaseObject ->getClassName ();
153+ }
158154 }
159155
160156 return get_class ($ type );
0 commit comments