@@ -5713,10 +5713,27 @@ static function (): void {
57135713 $ assignedExprType = $ scope ->getType ($ assignedExpr );
57145714 $ nodeCallback (new PropertyAssignNode ($ var , $ assignedExpr , $ isAssignOp ), $ scope );
57155715 if ($ propertyReflection !== null && $ propertyReflection ->canChangeTypeAfterAssignment ()) {
5716- if ($ propertyReflection ->hasNativeType () && $ scope ->isDeclareStrictTypes ()) {
5716+ if ($ propertyReflection ->hasNativeType ()) {
5717+ $ assignedNativeType = $ scope ->getNativeType ($ assignedExpr );
57175718 $ propertyNativeType = $ propertyReflection ->getNativeType ();
57185719
5719- $ scope = $ scope ->assignExpression ($ var , TypeCombinator::intersect ($ assignedExprType ->toCoercedArgumentType (true ), $ propertyNativeType ), TypeCombinator::intersect ($ scope ->getNativeType ($ assignedExpr )->toCoercedArgumentType (true ), $ propertyNativeType ));
5720+ $ assignedTypeIsCompatible = false ;
5721+ foreach (TypeUtils::flattenTypes ($ propertyNativeType ) as $ type ) {
5722+ if ($ type ->isSuperTypeOf ($ assignedNativeType )->yes ()) {
5723+ $ assignedTypeIsCompatible = true ;
5724+ break ;
5725+ }
5726+ }
5727+
5728+ if ($ assignedTypeIsCompatible ) {
5729+ $ scope = $ scope ->assignExpression ($ var , $ assignedExprType , $ assignedNativeType );
5730+ } elseif ($ scope ->isDeclareStrictTypes ()) {
5731+ $ scope = $ scope ->assignExpression (
5732+ $ var ,
5733+ TypeCombinator::intersect ($ assignedExprType ->toCoercedArgumentType (true ), $ propertyNativeType ),
5734+ TypeCombinator::intersect ($ assignedNativeType ->toCoercedArgumentType (true ), $ propertyNativeType ),
5735+ );
5736+ }
57205737 } else {
57215738 $ scope = $ scope ->assignExpression ($ var , $ assignedExprType , $ scope ->getNativeType ($ assignedExpr ));
57225739 }
0 commit comments