@@ -5628,25 +5628,28 @@ static function (): void {
5628
5628
$ nodeCallback (new PropertyAssignNode ($ var , $ assignedExpr , $ isAssignOp ), $ scope );
5629
5629
if ($ propertyReflection ->canChangeTypeAfterAssignment ()) {
5630
5630
if ($ propertyReflection ->hasNativeType ()) {
5631
- $ assignedNativeType = $ scope ->getNativeType ($ assignedExpr );
5632
5631
$ propertyNativeType = $ propertyReflection ->getNativeType ();
5633
5632
5634
- $ assignedTypeIsCompatible = false ;
5635
- foreach (TypeUtils::flattenTypes ($ propertyNativeType ) as $ type ) {
5636
- if ($ type ->isSuperTypeOf ($ assignedNativeType )->yes ()) {
5637
- $ assignedTypeIsCompatible = true ;
5638
- break ;
5633
+ $ assignedTypeIsCompatible = $ propertyNativeType ->isSuperTypeOf ($ assignedExprType )->yes ();
5634
+ if (!$ assignedTypeIsCompatible ) {
5635
+ foreach (TypeUtils::flattenTypes ($ propertyNativeType ) as $ type ) {
5636
+ if ($ type ->isSuperTypeOf ($ assignedExprType )->yes ()) {
5637
+ $ assignedTypeIsCompatible = true ;
5638
+ break ;
5639
+ }
5639
5640
}
5640
5641
}
5641
5642
5642
5643
if ($ assignedTypeIsCompatible ) {
5643
- $ scope = $ scope ->assignExpression ($ var , $ assignedExprType , $ assignedNativeType );
5644
+ $ scope = $ scope ->assignExpression ($ var , $ assignedExprType , $ scope -> getNativeType ( $ assignedExpr ) );
5644
5645
} elseif ($ scope ->isDeclareStrictTypes ()) {
5645
5646
$ scope = $ scope ->assignExpression (
5646
5647
$ var ,
5647
5648
TypeCombinator::intersect ($ assignedExprType ->toCoercedArgumentType (true ), $ propertyNativeType ),
5648
- TypeCombinator::intersect ($ assignedNativeType ->toCoercedArgumentType (true ), $ propertyNativeType ),
5649
+ TypeCombinator::intersect ($ scope -> getNativeType ( $ assignedExpr ) ->toCoercedArgumentType (true ), $ propertyNativeType ),
5649
5650
);
5651
+ } else {
5652
+ $ scope = $ scope ->assignExpression ($ var , $ assignedExprType , $ scope ->getNativeType ($ assignedExpr ));
5650
5653
}
5651
5654
} else {
5652
5655
$ scope = $ scope ->assignExpression ($ var , $ assignedExprType , $ scope ->getNativeType ($ assignedExpr ));
@@ -5716,25 +5719,28 @@ static function (): void {
5716
5719
$ nodeCallback (new PropertyAssignNode ($ var , $ assignedExpr , $ isAssignOp ), $ scope );
5717
5720
if ($ propertyReflection !== null && $ propertyReflection ->canChangeTypeAfterAssignment ()) {
5718
5721
if ($ propertyReflection ->hasNativeType ()) {
5719
- $ assignedNativeType = $ scope ->getNativeType ($ assignedExpr );
5720
5722
$ propertyNativeType = $ propertyReflection ->getNativeType ();
5723
+ $ assignedTypeIsCompatible = $ propertyNativeType ->isSuperTypeOf ($ assignedExprType )->yes ();
5721
5724
5722
- $ assignedTypeIsCompatible = false ;
5723
- foreach (TypeUtils::flattenTypes ($ propertyNativeType ) as $ type ) {
5724
- if ($ type ->isSuperTypeOf ($ assignedNativeType )->yes ()) {
5725
- $ assignedTypeIsCompatible = true ;
5726
- break ;
5725
+ if (!$ assignedTypeIsCompatible ) {
5726
+ foreach (TypeUtils::flattenTypes ($ propertyNativeType ) as $ type ) {
5727
+ if ($ type ->isSuperTypeOf ($ assignedExprType )->yes ()) {
5728
+ $ assignedTypeIsCompatible = true ;
5729
+ break ;
5730
+ }
5727
5731
}
5728
5732
}
5729
5733
5730
5734
if ($ assignedTypeIsCompatible ) {
5731
- $ scope = $ scope ->assignExpression ($ var , $ assignedExprType , $ assignedNativeType );
5735
+ $ scope = $ scope ->assignExpression ($ var , $ assignedExprType , $ scope -> getNativeType ( $ assignedExpr ) );
5732
5736
} elseif ($ scope ->isDeclareStrictTypes ()) {
5733
5737
$ scope = $ scope ->assignExpression (
5734
5738
$ var ,
5735
5739
TypeCombinator::intersect ($ assignedExprType ->toCoercedArgumentType (true ), $ propertyNativeType ),
5736
- TypeCombinator::intersect ($ assignedNativeType ->toCoercedArgumentType (true ), $ propertyNativeType ),
5740
+ TypeCombinator::intersect ($ scope -> getNativeType ( $ assignedExpr ) ->toCoercedArgumentType (true ), $ propertyNativeType ),
5737
5741
);
5742
+ } else {
5743
+ $ scope = $ scope ->assignExpression ($ var , $ assignedExprType , $ scope ->getNativeType ($ assignedExpr ));
5738
5744
}
5739
5745
} else {
5740
5746
$ scope = $ scope ->assignExpression ($ var , $ assignedExprType , $ scope ->getNativeType ($ assignedExpr ));
0 commit comments