Skip to content

Fix phpstan/phpstan#14211: incorrect type inference with dependent types#5092

Open
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
phpstan-bot:create-pull-request/patch-m8rwl25
Open

Fix phpstan/phpstan#14211: incorrect type inference with dependent types#5092
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
phpstan-bot:create-pull-request/patch-m8rwl25

Conversation

@phpstan-bot
Copy link
Collaborator

Summary

Fixes a regression introduced by #5048 (commit c5bfadb) where a boolean variable assigned from isset() would incorrectly be narrowed to *NEVER* on the second if check within the same function.

The issue manifested when:

  1. An array was first narrowed via isset($data['x'])
  2. A boolean $m = isset($data['y']) was assigned
  3. The first if ($m) correctly narrowed $m to true
  4. The second if ($m) incorrectly narrowed $m to *NEVER*

Changes

  • Modified conditional expression resolution in src/Analyser/MutatingScope.php (filterBySpecifiedTypes method) to intersect types when multiple conditional expressions fire for the same target expression, instead of overwriting with the last match
  • Added regression test tests/PHPStan/Analyser/nsrt/bug-14211.php

Root cause

When $m = isset($data['y']) is processed, PHPStan creates conditional expressions for the dependent types. Two conditionals are created for $data with the same guard {$m → true}:

  1. From processSureTypesForConditionalExpressionsAfterAssign: $data with hasOffset('y') (correct, more specific)
  2. From processSureNotTypesForConditionalExpressionsAfterAssign: $data without hasOffset('y') (from removing null from a non-nullable array type — effectively a no-op)

When both conditionals fired in the resolution loop, specifiedExpressions[$data] was overwritten by the last match (the less specific type without hasOffset('y')). This caused a cascading failure:

  • A conditional "when $data = type-without-y, $data['y'] = unset" matched incorrectly
  • A conditional "when $data = type-without-y, $m = false" also matched
  • The intersection of true and false for $m produced *NEVER*

The fix intersects the types when accumulating specifiedExpressions from multiple matching conditionals (when both have Yes certainty), ensuring the most specific type is used for subsequent guard matching.

Test

The regression test tests/PHPStan/Analyser/nsrt/bug-14211.php reproduces the exact scenario from the issue: an isset guard followed by a boolean assignment from another isset, with two consecutive if checks on the boolean variable. It verifies that the second if ($m) correctly narrows $m to true (not *NEVER*).

Fixes phpstan/phpstan#14211

- Fixed conditional expression resolution in filterBySpecifiedTypes to intersect types
  instead of overwriting when multiple conditional expressions fire for the same target
- When two conditionals with the same guard produced different types (one more specific
  than the other), the less specific type could overwrite specifiedExpressions, causing
  cascading wrong matches that led to variables being incorrectly narrowed to *NEVER*
- New regression test in tests/PHPStan/Analyser/nsrt/bug-14211.php

Closes phpstan/phpstan#14211
@staabm staabm changed the title Fix #14211: incorrect type inference with dependent types Fix phpstan/phpstan#14211: incorrect type inference with dependent types Feb 28, 2026
if (array_key_exists($conditionalExprString, $specifiedExpressions) && $specifiedExpressions[$conditionalExprString]->getCertainty()->yes() && $newTypeHolder->getCertainty()->yes()) {
$specifiedExpressions[$conditionalExprString] = ExpressionTypeHolder::createYes(
$newTypeHolder->getExpr(),
TypeCombinator::intersect($specifiedExpressions[$conditionalExprString]->getType(), $newTypeHolder->getType()),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might add a lot of possibly useless intersect calls.

For instance if the next newTypeHolder does not have a certainty of Yes. we will override the value.

Might be better to check it first by computing all the newTypeHolder and

  • if they all have a yes certainty we intersect them
  • if not, we take the last one

WDYT @staabm ?

$conditions[$conditionalExprString][] = $conditionalExpression;
$specifiedExpressions[$conditionalExprString] = $conditionalExpression->getTypeHolder();
$newTypeHolder = $conditionalExpression->getTypeHolder();
if (array_key_exists($conditionalExprString, $specifiedExpressions) && $specifiedExpressions[$conditionalExprString]->getCertainty()->yes() && $newTypeHolder->getCertainty()->yes()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's unclear to me why we're only concerned about Yes & Yes.

Looking at this code, the last newTypeHolder override all the previous one if there is some Maybe certainty in the game.

Is this condition clear to you @staabm ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants