@@ -5625,18 +5625,27 @@ static function (): void {
56255625 $ assignedExprType = $ scope ->getType ($ assignedExpr );
56265626 $ nodeCallback (new PropertyAssignNode ($ var , $ assignedExpr , $ isAssignOp ), $ scope );
56275627 if ($ propertyReflection ->canChangeTypeAfterAssignment ()) {
5628- if ($ propertyReflection ->hasNativeType () && $ scope -> isDeclareStrictTypes () ) {
5628+ if ($ propertyReflection ->hasNativeType ()) {
56295629 $ assignedNativeType = $ scope ->getNativeType ($ assignedExpr );
56305630 $ propertyNativeType = $ propertyReflection ->getNativeType ();
56315631
5632- $ newAssignedType = TypeCombinator::intersect ($ assignedExprType , $ propertyNativeType );
5633- $ newAssignedNativeType = TypeCombinator::intersect ($ assignedNativeType , $ propertyNativeType );
5634- if ($ newAssignedType instanceof NeverType || $ newAssignedNativeType instanceof NeverType) {
5635- $ newAssignedType = TypeCombinator::intersect ($ assignedExprType ->toCoercedArgumentType (true ), $ propertyNativeType );
5636- $ newAssignedNativeType = TypeCombinator::intersect ($ assignedNativeType ->toCoercedArgumentType (true ), $ propertyNativeType );
5632+ $ assignedTypeIsCompatible = false ;
5633+ foreach (TypeUtils::flattenTypes ($ propertyNativeType ) as $ type ) {
5634+ if ($ type ->isSuperTypeOf ($ assignedNativeType )->yes ()) {
5635+ $ assignedTypeIsCompatible = true ;
5636+ break ;
5637+ }
56375638 }
56385639
5639- $ scope = $ scope ->assignExpression ($ var , $ newAssignedType , $ newAssignedNativeType );
5640+ if (!$ assignedTypeIsCompatible && $ scope ->isDeclareStrictTypes ()) {
5641+ $ scope = $ scope ->assignExpression (
5642+ $ var ,
5643+ TypeCombinator::intersect ($ assignedExprType ->toCoercedArgumentType (true ), $ propertyNativeType ),
5644+ TypeCombinator::intersect ($ assignedNativeType ->toCoercedArgumentType (true ), $ propertyNativeType )
5645+ );
5646+ } else {
5647+ $ scope = $ scope ->assignExpression ($ var , $ assignedExprType , $ assignedNativeType );
5648+ }
56405649 } else {
56415650 $ scope = $ scope ->assignExpression ($ var , $ assignedExprType , $ scope ->getNativeType ($ assignedExpr ));
56425651 }
0 commit comments