diff --git a/src/Type/TypeCombinator.php b/src/Type/TypeCombinator.php index af60550c859..df18959ae24 100644 --- a/src/Type/TypeCombinator.php +++ b/src/Type/TypeCombinator.php @@ -353,8 +353,9 @@ public static function union(Type ...$types): Type [$a, $b] = $compareResult; if ($a !== null) { $types[$i] = $a; - array_splice($scalarTypeItems, $j--, 1); + array_splice($scalarTypeItems, $j, 1); $scalarTypeItemsCount--; + $j = -1; continue 1; } if ($b !== null) { diff --git a/tests/PHPStan/Analyser/nsrt/bug-14610.php b/tests/PHPStan/Analyser/nsrt/bug-14610.php new file mode 100644 index 00000000000..3ad65a6e069 --- /dev/null +++ b/tests/PHPStan/Analyser/nsrt/bug-14610.php @@ -0,0 +1,68 @@ +', $value); + + if ($value == 0) { + assertType('array', $_SESSION); + $result = isset($_SESSION['test']); // should not be reported as always exists + } +} + +function testWithOtherSuperglobals(): void +{ + $value = 0; + + if (isset($_GET['key'])) { + $value = rand(0,3); + if ($value == 1) { + } + } + + if ($value == 0) { + $result = isset($_GET['key']); + } +} + +function testWithStrictComparison(): void +{ + $value = 0; + + if (isset($_SESSION['test'])) { + $value = rand(0,3); + if ($value === 1) { + } + } + + if ($value === 0) { + $result = isset($_SESSION['test']); + } +} + +function testWithDifferentKey(): void +{ + $value = 0; + + if (isset($_SESSION['test'])) { + $value = rand(0,3); + if ($value == 1) { + } + } + + if ($value == 0) { + $result = isset($_SESSION['other']); + } +} diff --git a/tests/PHPStan/Rules/Variables/IssetRuleTest.php b/tests/PHPStan/Rules/Variables/IssetRuleTest.php index c1492e62901..ff2cf199417 100644 --- a/tests/PHPStan/Rules/Variables/IssetRuleTest.php +++ b/tests/PHPStan/Rules/Variables/IssetRuleTest.php @@ -574,4 +574,11 @@ public function testBug14393(): void ]); } + public function testBug14610(): void + { + $this->treatPhpDocTypesAsCertain = true; + + $this->analyse([__DIR__ . '/../../Analyser/nsrt/bug-14610.php'], []); + } + } diff --git a/tests/PHPStan/Type/TypeCombinatorTest.php b/tests/PHPStan/Type/TypeCombinatorTest.php index 55f79c743cc..f325c638da4 100644 --- a/tests/PHPStan/Type/TypeCombinatorTest.php +++ b/tests/PHPStan/Type/TypeCombinatorTest.php @@ -1642,6 +1642,42 @@ public static function dataUnion(): iterable IntegerType::class, 'int', ], + [ + [ + new UnionType([ + new ConstantIntegerType(0), + IntegerRangeType::fromInterval(2, 3), + ]), + new ConstantIntegerType(1), + ], + IntegerRangeType::class, + 'int<0, 3>', + ], + [ + [ + new ConstantIntegerType(1), + new UnionType([ + new ConstantIntegerType(0), + IntegerRangeType::fromInterval(2, 3), + ]), + ], + IntegerRangeType::class, + 'int<0, 3>', + ], + [ + [ + new UnionType([ + new ConstantIntegerType(0), + IntegerRangeType::fromInterval(2, 3), + ]), + new UnionType([ + new ConstantIntegerType(10), + new ConstantIntegerType(1), + ]), + ], + UnionType::class, + '10|int<0, 3>', + ], [ [ new MixedType(),