Skip to content

Commit ad96bf8

Browse files
ondrejmirtesclaude
andcommitted
Union one-sided conditional holders that share a target and guard set across merges
Loop convergence merges the body-end scope with the loop-entry scope, and both sides often carry a Yes-certainty holder for the same target under the same guard - just with different consequent types (the pre-loop assignment's type on one side, the body reassignment's type on the other). The exact-key intersection dropped both, losing the guard-implies-defined fact and producing "might not be defined" next to "if condition is always true". Whichever side a merged path came from, the guard matching later implies one of the recorded consequents, so a holder with the union of the consequent types under their shared certainty holds on every merged path. Closes phpstan/phpstan#6830 Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01CGhnJQpUWRRpg6H5LuWJkA
1 parent 7c82673 commit ad96bf8

3 files changed

Lines changed: 127 additions & 0 deletions

File tree

‎src/Analyser/MutatingScope.php‎

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4412,6 +4412,11 @@ private function mergeWithVariableState(?self $otherScope, bool $preserveVacuous
44124412
$ourExpressionTypes,
44134413
);
44144414
}
4415+
$conditionalExpressions = $this->mergeSameGuardConditionalExpressions(
4416+
$conditionalExpressions,
4417+
$this->conditionalExpressions,
4418+
$otherScope->conditionalExpressions,
4419+
);
44154420
$conditionalExpressions = ScopeOps::createConditionalExpressions(
44164421
$conditionalExpressions,
44174422
$ourExpressionTypes,
@@ -4566,6 +4571,80 @@ private function preserveVacuousConditionalExpressions(
45664571
return $currentConditionalExpressions;
45674572
}
45684573

4574+
/**
4575+
* Merges one-sided holders that share a target and an identical guard set:
4576+
* whichever branch a merged path came from, the guard matching later implies
4577+
* one of the recorded consequents, so the union of the consequent types
4578+
* (under their shared certainty) holds on every merged path.
4579+
*
4580+
* @param array<string, ConditionalExpressionHolder[]> $currentConditionalExpressions
4581+
* @param array<string, ConditionalExpressionHolder[]> $ourConditionalExpressions
4582+
* @param array<string, ConditionalExpressionHolder[]> $theirConditionalExpressions
4583+
* @return array<string, ConditionalExpressionHolder[]>
4584+
*/
4585+
private function mergeSameGuardConditionalExpressions(
4586+
array $currentConditionalExpressions,
4587+
array $ourConditionalExpressions,
4588+
array $theirConditionalExpressions,
4589+
): array
4590+
{
4591+
foreach ($ourConditionalExpressions as $exprString => $ourHolders) {
4592+
if (!array_key_exists($exprString, $theirConditionalExpressions)) {
4593+
continue;
4594+
}
4595+
4596+
$theirHolders = $theirConditionalExpressions[$exprString];
4597+
foreach ($ourHolders as $ourKey => $ourHolder) {
4598+
if (isset($currentConditionalExpressions[$exprString][$ourKey])) {
4599+
continue;
4600+
}
4601+
4602+
$ourTypeHolder = $ourHolder->getTypeHolder();
4603+
if ($ourTypeHolder->getCertainty()->no()) {
4604+
continue;
4605+
}
4606+
4607+
foreach ($theirHolders as $theirKey => $theirHolder) {
4608+
if (isset($currentConditionalExpressions[$exprString][$theirKey])) {
4609+
continue;
4610+
}
4611+
4612+
$theirTypeHolder = $theirHolder->getTypeHolder();
4613+
if (!$theirTypeHolder->getCertainty()->equals($ourTypeHolder->getCertainty())) {
4614+
continue;
4615+
}
4616+
4617+
$ourGuards = $ourHolder->getConditionExpressionTypeHolders();
4618+
$theirGuards = $theirHolder->getConditionExpressionTypeHolders();
4619+
if (count($ourGuards) !== count($theirGuards)) {
4620+
continue;
4621+
}
4622+
4623+
foreach ($ourGuards as $guardExprString => $ourGuardHolder) {
4624+
if (
4625+
!array_key_exists($guardExprString, $theirGuards)
4626+
|| !$ourGuardHolder->equals($theirGuards[$guardExprString])
4627+
) {
4628+
continue 2;
4629+
}
4630+
}
4631+
4632+
$unionHolder = new ConditionalExpressionHolder(
4633+
$ourGuards,
4634+
new ExpressionTypeHolder(
4635+
$ourTypeHolder->getExpr(),
4636+
TypeCombinator::union($ourTypeHolder->getType(), $theirTypeHolder->getType()),
4637+
$ourTypeHolder->getCertainty(),
4638+
),
4639+
);
4640+
$currentConditionalExpressions[$exprString][$unionHolder->getKey()] = $unionHolder;
4641+
}
4642+
}
4643+
}
4644+
4645+
return $currentConditionalExpressions;
4646+
}
4647+
45694648
/**
45704649
* @param array<string, ConditionalExpressionHolder[]> $newConditionalExpressions
45714650
* @param array<string, ConditionalExpressionHolder[]> $existingConditionalExpressions
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace Bug6830Nsrt;
4+
5+
use PHPStan\TrinaryLogic;
6+
use function PHPStan\Testing\assertType;
7+
use function PHPStan\Testing\assertVariableCertainty;
8+
9+
function test(bool $do): void {
10+
11+
if ($do) {
12+
13+
$x = 9999;
14+
}
15+
16+
foreach([1, 2, 3] as $whatever) {
17+
18+
if ($do) {
19+
20+
assertVariableCertainty(TrinaryLogic::createYes(), $x);
21+
assertType('123|9999', $x);
22+
23+
if ($x) {
24+
25+
$x = 123;
26+
}
27+
}
28+
}
29+
}

‎tests/PHPStan/Rules/Variables/data/bug-6830.php‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,22 @@ function test(array $bools): void
1616
}
1717
}
1818
}
19+
20+
function test2(bool $do): void {
21+
22+
if ($do) {
23+
24+
$x = 9999;
25+
}
26+
27+
foreach([1, 2, 3] as $whatever) {
28+
29+
if ($do) {
30+
31+
if ($x) {
32+
33+
$x = 123;
34+
}
35+
}
36+
}
37+
}

0 commit comments

Comments
 (0)