Skip to content

Commit 7c82673

Browse files
ondrejmirtesclaude
andcommitted
Preserve one-sided conditional holders whose consequent already holds in the other branch
An if-merge intersects conditional expression holders by exact key, so a holder recorded only in one branch is dropped even when the other branch's flat state for the holder's target already satisfies the consequent - a subtype with at-least-as-strong certainty. In that case the consequent holds on both paths whenever the guard matches, so firing the holder later is sound and the holder can survive the merge alongside the vacuous-guard rescue. Closes phpstan/phpstan#9628 Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01CGhnJQpUWRRpg6H5LuWJkA
1 parent f0b92e1 commit 7c82673

2 files changed

Lines changed: 47 additions & 1 deletion

File tree

‎src/Analyser/MutatingScope.php‎

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4504,6 +4504,14 @@ private function withoutPreciseClassConstantFetches(
45044504
}
45054505

45064506
/**
4507+
* Rescues one-sided conditional holders across an if-merge.
4508+
*
4509+
* A holder missing from the intersection survives when either some guard's
4510+
* recorded type is impossible in the other branch (the holder is vacuously
4511+
* true there), or the other branch's flat state for the holder's target
4512+
* already satisfies the holder's consequent (subtype with at-least-as-strong
4513+
* certainty) - the consequent then holds on both paths under the guard.
4514+
*
45074515
* @param array<string, ConditionalExpressionHolder[]> $currentConditionalExpressions
45084516
* @param array<string, ConditionalExpressionHolder[]> $sourceConditionalExpressions
45094517
* @param array<string, ExpressionTypeHolder> $otherExpressionTypes
@@ -4536,9 +4544,22 @@ private function preserveVacuousConditionalExpressions(
45364544

45374545
if ($otherType->isSuperTypeOf($guardType)->no()) {
45384546
$currentConditionalExpressions[$exprString][$key] = $holder;
4539-
break;
4547+
continue 2;
45404548
}
45414549
}
4550+
4551+
if ($typeHolder->getCertainty()->no() || !array_key_exists($exprString, $otherExpressionTypes)) {
4552+
continue;
4553+
}
4554+
4555+
$otherTargetHolder = $otherExpressionTypes[$exprString];
4556+
$otherTargetCertainty = $otherTargetHolder->getCertainty();
4557+
if (
4558+
($otherTargetCertainty->yes() || $otherTargetCertainty->equals($typeHolder->getCertainty()))
4559+
&& $typeHolder->getType()->isSuperTypeOf($otherTargetHolder->getType())->yes()
4560+
) {
4561+
$currentConditionalExpressions[$exprString][$key] = $holder;
4562+
}
45424563
}
45434564
}
45444565

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Bug9628;
4+
5+
use function PHPStan\Testing\assertType;
6+
use function is_null;
7+
use function rand;
8+
9+
function findFoo(int $fooId): \stdClass
10+
{
11+
return new \stdClass();
12+
}
13+
14+
function processFoo(?\stdClass $foo): void
15+
{
16+
$fooId = rand(0, 1);
17+
18+
if (is_null($foo) && 0 !== $fooId) {
19+
$foo = findFoo($fooId);
20+
}
21+
22+
if (0 !== $fooId) {
23+
assertType('stdClass', $foo); // it's always non-nullable stdClass inside this condition
24+
}
25+
}

0 commit comments

Comments
 (0)