diff --git a/src/Reflection/InitializerExprTypeResolver.php b/src/Reflection/InitializerExprTypeResolver.php index 80eb2f63a4..3615ac70b4 100644 --- a/src/Reflection/InitializerExprTypeResolver.php +++ b/src/Reflection/InitializerExprTypeResolver.php @@ -1095,9 +1095,17 @@ public function getPlusType(Expr $left, Expr $right, callable $getTypeCallback): new BooleanType(), ]); - if ($plusable->isSuperTypeOf($leftType)->yes() && $plusable->isSuperTypeOf($rightType)->yes()) { + $plusableSuperTypeOfLeft = $plusable->isSuperTypeOf($leftType)->yes(); + $plusableSuperTypeOfRight = $plusable->isSuperTypeOf($rightType)->yes(); + if ($plusableSuperTypeOfLeft && $plusableSuperTypeOfRight) { return TypeCombinator::union($leftType, $rightType); } + if ($plusableSuperTypeOfLeft && $rightType instanceof MixedType) { + return $leftType; + } + if ($plusableSuperTypeOfRight && $leftType instanceof MixedType) { + return $rightType; + } } return $this->resolveCommonMath(new BinaryOp\Plus($left, $right), $leftType, $rightType); diff --git a/tests/PHPStan/Rules/Methods/ReturnTypeRuleTest.php b/tests/PHPStan/Rules/Methods/ReturnTypeRuleTest.php index 6aecee85fe..6303bdcdc4 100644 --- a/tests/PHPStan/Rules/Methods/ReturnTypeRuleTest.php +++ b/tests/PHPStan/Rules/Methods/ReturnTypeRuleTest.php @@ -1039,4 +1039,9 @@ public function testBug11491(): void $this->analyse([__DIR__ . '/data/bug-11491.php'], []); } + public function testBug3759(): void + { + $this->analyse([__DIR__ . '/data/bug-3759.php'], []); + } + } diff --git a/tests/PHPStan/Rules/Methods/data/bug-3759.php b/tests/PHPStan/Rules/Methods/data/bug-3759.php new file mode 100644 index 0000000000..ddbafbefdc --- /dev/null +++ b/tests/PHPStan/Rules/Methods/data/bug-3759.php @@ -0,0 +1,31 @@ + ['x' => 'x'], + 'minor' => ['y' => 'y'], + 'patch' => ['z' => 'z'], + ]; + } +}