Skip to content

Commit fb48fb4

Browse files
committed
array_key_exist inference is lost when adding a false condition
1 parent dd2717b commit fb48fb4

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

src/Analyser/TypeSpecifier.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -663,7 +663,17 @@ public function specifyTypesInCondition(
663663
$leftTypes = $this->specifyTypesInCondition($scope, $expr->left, $context)->setRootExpr($expr);
664664
$rightScope = $scope->filterByFalseyValue($expr->left);
665665
$rightTypes = $this->specifyTypesInCondition($rightScope, $expr->right, $context)->setRootExpr($expr);
666-
$types = $context->true() ? $leftTypes->normalize($scope)->intersectWith($rightTypes->normalize($rightScope)) : $leftTypes->unionWith($rightTypes);
666+
667+
if ($context->true()) {
668+
if ($scope->getType($expr->left)->isFalse()->yes()) {
669+
$types = $rightTypes->normalize($rightScope);
670+
} else {
671+
$types = $leftTypes->normalize($scope)->intersectWith($rightTypes->normalize($rightScope));
672+
}
673+
} else {
674+
$types = $leftTypes->unionWith($rightTypes);
675+
}
676+
667677
if ($context->true()) {
668678
return (new SpecifiedTypes(
669679
$types->getSureTypes(),
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
use function PHPStan\Testing\assertType;
4+
5+
function sayEqualArrayShape(int $i, $arr): void
6+
{
7+
if (
8+
false || array_key_exists($i, $arr)
9+
) {
10+
assertType('non-empty-array', $arr);
11+
}
12+
13+
if (array_key_exists($i, $arr)) {
14+
assertType('non-empty-array', $arr);
15+
}
16+
}

0 commit comments

Comments
 (0)