@@ -5713,10 +5713,27 @@ static function (): void {
5713
5713
$ assignedExprType = $ scope ->getType ($ assignedExpr );
5714
5714
$ nodeCallback (new PropertyAssignNode ($ var , $ assignedExpr , $ isAssignOp ), $ scope );
5715
5715
if ($ propertyReflection !== null && $ propertyReflection ->canChangeTypeAfterAssignment ()) {
5716
- if ($ propertyReflection ->hasNativeType () && $ scope ->isDeclareStrictTypes ()) {
5716
+ if ($ propertyReflection ->hasNativeType ()) {
5717
+ $ assignedNativeType = $ scope ->getNativeType ($ assignedExpr );
5717
5718
$ propertyNativeType = $ propertyReflection ->getNativeType ();
5718
5719
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
+ }
5720
5737
} else {
5721
5738
$ scope = $ scope ->assignExpression ($ var , $ assignedExprType , $ scope ->getNativeType ($ assignedExpr ));
5722
5739
}
0 commit comments