diff --git a/src/Analyser/TypeSpecifier.php b/src/Analyser/TypeSpecifier.php index cf7c6f9fdf..5cfa27df48 100644 --- a/src/Analyser/TypeSpecifier.php +++ b/src/Analyser/TypeSpecifier.php @@ -342,9 +342,8 @@ public function specifyTypesInCondition( } if ($context->true()) { - $type = TypeCombinator::intersect($scope->getType($expr->right), $scope->getType($expr->left)); - $leftTypes = $this->create($expr->left, $type, $context, false, $scope, $rootExpr); - $rightTypes = $this->create($expr->right, $type, $context, false, $scope, $rootExpr); + $leftTypes = $this->create($expr->left, $exprRightType, $context, false, $scope, $rootExpr); + $rightTypes = $this->create($expr->right, $exprLeftType, $context, false, $scope, $rootExpr); return $leftTypes->unionWith($rightTypes); } elseif ($context->false()) { return $this->create($expr->left, $exprLeftType, $context, false, $scope, $rootExpr)->normalize($scope) diff --git a/src/Type/TypeCombinator.php b/src/Type/TypeCombinator.php index ce77dda2ee..e18a75cf1b 100644 --- a/src/Type/TypeCombinator.php +++ b/src/Type/TypeCombinator.php @@ -638,11 +638,13 @@ public static function intersect(Type ...$types): Type $topLevelUnionSubTypes = []; $innerTypes = $type->getTypes(); usort($innerTypes, $sortTypes); + $slice1 = array_slice($types, 0, $i); + $slice2 = array_slice($types, $i + 1); foreach ($innerTypes as $innerUnionSubType) { $topLevelUnionSubTypes[] = self::intersect( $innerUnionSubType, - ...array_slice($types, 0, $i), - ...array_slice($types, $i + 1), + ...$slice1, + ...$slice2, ); } diff --git a/tests/PHPStan/Analyser/TypeSpecifierTest.php b/tests/PHPStan/Analyser/TypeSpecifierTest.php index f93374d8db..5838d222c7 100644 --- a/tests/PHPStan/Analyser/TypeSpecifierTest.php +++ b/tests/PHPStan/Analyser/TypeSpecifierTest.php @@ -422,7 +422,7 @@ public function dataCondition(): array new Variable('foo'), new Variable('bar'), ), - ['$foo' => 'Bar', '$bar' => 'Bar'], + ['$foo' => 'Bar', '$bar' => 'mixed'], // could be '$bar' => 'Bar' [], ], [ @@ -1067,7 +1067,7 @@ public function dataCondition(): array ), [ '$foo' => 'array', - 'array_filter($foo, \'is_string\', ARRAY_FILTER_USE_KEY)' => 'array', + 'array_filter($foo, \'is_string\', ARRAY_FILTER_USE_KEY)' => 'array', // could be 'array' ], [], ],