@@ -5625,18 +5625,27 @@ static function (): void {
5625
5625
$ assignedExprType = $ scope ->getType ($ assignedExpr );
5626
5626
$ nodeCallback (new PropertyAssignNode ($ var , $ assignedExpr , $ isAssignOp ), $ scope );
5627
5627
if ($ propertyReflection ->canChangeTypeAfterAssignment ()) {
5628
- if ($ propertyReflection ->hasNativeType () && $ scope -> isDeclareStrictTypes () ) {
5628
+ if ($ propertyReflection ->hasNativeType ()) {
5629
5629
$ assignedNativeType = $ scope ->getNativeType ($ assignedExpr );
5630
5630
$ propertyNativeType = $ propertyReflection ->getNativeType ();
5631
5631
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
+ }
5637
5638
}
5638
5639
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
+ }
5640
5649
} else {
5641
5650
$ scope = $ scope ->assignExpression ($ var , $ assignedExprType , $ scope ->getNativeType ($ assignedExpr ));
5642
5651
}
0 commit comments