Skip to content

test coalesce with int ranges#5111

Merged
staabm merged 9 commits intophpstan:2.1.xfrom
phpstan-bot:create-pull-request/patch-0w0qgs8
Mar 1, 2026
Merged

test coalesce with int ranges#5111
staabm merged 9 commits intophpstan:2.1.xfrom
phpstan-bot:create-pull-request/patch-0w0qgs8

Conversation

@phpstan-bot
Copy link
Collaborator

@phpstan-bot phpstan-bot commented Mar 1, 2026

covers coalesce with int ranges in tests

- Fixed getCoalesceType() in MutatingScope to avoid over-narrowing the right
  side of ?? when conditional expressions from scope merging make correlated
  variables all null together
- The fix uses the narrowed scope only when it successfully removes null from
  the right type (useful for `if ($a || $b) { $a ?? $b }` patterns), and
  falls back to the unfiltered type otherwise
- New regression test in tests/PHPStan/Analyser/nsrt/bug-14213.php

Closes phpstan/phpstan#14213
@staabm staabm changed the title Fix #14213: coalesce on int ranges Fix phpstan/phpstan#14213: coalesce on int ranges Mar 1, 2026
@staabm staabm self-assigned this Mar 1, 2026
@staabm staabm requested a review from VincentLanglet March 1, 2026 07:55
@staabm
Copy link
Contributor

staabm commented Mar 1, 2026

this PR fixes the type-inference part, but does not fix the "Variable $x1 on left side of ?? always exists and is always null." error.

I would open a new issue after this one merged and would let the bot give another try on fixing this error.

(and initially I tried this examples because I want to see slow analysis of COALESCE similar to what we saw with AND - wich I found and will be another followup after this blocker is out of the way)

@VincentLanglet
Copy link
Contributor

VincentLanglet commented Mar 1, 2026

I feel like there is no issue to fix

The message is not clear but when the variable x0 is null it's true that the variable x1 is always null too ! So it's useless to write x0 ?? x1

And the inference was right to me too, it's int<0, 1>|null because the variable x1 and x2 are not null only when the variable x0 is not null, so we cannot reach those values

@staabm staabm changed the title Fix phpstan/phpstan#14213: coalesce on int ranges test coalesce with int ranges Mar 1, 2026
@staabm
Copy link
Contributor

staabm commented Mar 1, 2026

ohh my you are right. I am dumb - adjusted the PR to just add tests

@staabm staabm merged commit 11f518a into phpstan:2.1.x Mar 1, 2026
372 of 375 checks passed
@staabm staabm deleted the create-pull-request/patch-0w0qgs8 branch March 1, 2026 10:25
@staabm
Copy link
Contributor

staabm commented Mar 1, 2026

thank you

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