@@ -702,43 +702,47 @@ public function getCastType(Expr\Cast $expr, callable $getTypeCallback): Type
702702 return $ getTypeCallback ($ expr ->expr )->toArray ();
703703 }
704704 if ($ expr instanceof Object_) {
705- $ castToObject = static function (Type $ type ): Type {
706- $ constantArrays = $ type ->getConstantArrays ();
707- if (count ($ constantArrays ) > 0 ) {
708- $ objects = [];
709- foreach ($ constantArrays as $ constantArray ) {
710- $ properties = [];
711- $ optionalProperties = [];
712- foreach ($ constantArray ->getKeyTypes () as $ i => $ keyType ) {
713- $ valueType = $ constantArray ->getValueTypes ()[$ i ];
714- $ optional = $ constantArray ->isOptionalKey ($ i );
715- if ($ optional ) {
716- $ optionalProperties [] = $ keyType ->getValue ();
717- }
718- $ properties [$ keyType ->getValue ()] = $ valueType ;
719- }
705+ return $ this ->getCastObjectType ($ getTypeCallback ($ expr ->expr ));
706+ }
720707
721- $ objects [] = TypeCombinator::intersect (new ObjectShapeType ($ properties , $ optionalProperties ), new ObjectType (stdClass::class));
708+ return new MixedType ();
709+ }
710+
711+ public function getCastObjectType (Type $ exprType ): Type
712+ {
713+ $ castToObject = static function (Type $ type ): Type {
714+ $ constantArrays = $ type ->getConstantArrays ();
715+ if (count ($ constantArrays ) > 0 ) {
716+ $ objects = [];
717+ foreach ($ constantArrays as $ constantArray ) {
718+ $ properties = [];
719+ $ optionalProperties = [];
720+ foreach ($ constantArray ->getKeyTypes () as $ i => $ keyType ) {
721+ $ valueType = $ constantArray ->getValueTypes ()[$ i ];
722+ $ optional = $ constantArray ->isOptionalKey ($ i );
723+ if ($ optional ) {
724+ $ optionalProperties [] = $ keyType ->getValue ();
725+ }
726+ $ properties [$ keyType ->getValue ()] = $ valueType ;
722727 }
723728
724- return TypeCombinator::union (...$ objects );
725- }
726- if ($ type ->isObject ()->yes ()) {
727- return $ type ;
729+ $ objects [] = TypeCombinator::intersect (new ObjectShapeType ($ properties , $ optionalProperties ), new ObjectType (stdClass::class));
728730 }
729731
730- return new ObjectType ('stdClass ' );
731- };
732-
733- $ exprType = $ getTypeCallback ($ expr ->expr );
734- if ($ exprType instanceof UnionType) {
735- return TypeCombinator::union (...array_map ($ castToObject , $ exprType ->getTypes ()));
732+ return TypeCombinator::union (...$ objects );
736733 }
734+ if ($ type ->isObject ()->yes ()) {
735+ return $ type ;
736+ }
737+
738+ return new ObjectType ('stdClass ' );
739+ };
737740
738- return $ castToObject ($ exprType );
741+ if ($ exprType instanceof UnionType) {
742+ return TypeCombinator::union (...array_map ($ castToObject , $ exprType ->getTypes ()));
739743 }
740744
741- return new MixedType ( );
745+ return $ castToObject ( $ exprType );
742746 }
743747
744748 /**
0 commit comments