-
Notifications
You must be signed in to change notification settings - Fork 575
Re-check scalar types after integer range expansion in TypeCombinator::union #5660
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
staabm
merged 5 commits into
phpstan:2.1.x
from
phpstan-bot:create-pull-request/patch-0mbssc0
May 14, 2026
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
470212f
Re-check scalar types after integer range expansion in TypeCombinator…
phpstan-bot 99c79c2
Add test cases for array<mixed> parameter to show superglobal-specifi…
phpstan-bot 628e531
Remove testWithParam and merge bug-14610 test files into single nsrt …
phpstan-bot e65d557
another test
staabm 81ad1bf
another test
staabm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
Re-check scalar types after integer range expansion in TypeCombinator…
…::union When an IntegerRangeType absorbs a ConstantIntegerType and expands (e.g., int<2,3> absorbs 1 to become int<1,3>), previously-checked scalar items that were disjoint with the old range but adjacent to the new range were never re-checked. This caused union(0|int<2,3>, 1) to produce 0|int<1,3> instead of int<0,3>. The incomplete union caused ExpressionTypeHolder::equalTypes() to return false during scope merging, creating spurious conditional expressions that incorrectly narrowed superglobal types in branches guarded by comparisons on a conditionally-assigned variable. Fix: restart the inner scalar loop when a range expands, so all remaining scalars are re-tested against the updated range.
- Loading branch information
commit 470212f3657816554ede3cb921fe838416a83e14
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| <?php | ||
|
|
||
| namespace Bug14610; | ||
|
|
||
| use function PHPStan\Testing\assertType; | ||
|
|
||
| function test(): void | ||
| { | ||
| $value = 0; | ||
|
|
||
| if (isset($_SESSION['test'])) { | ||
| $value = rand(0,3); | ||
| if ($value == 1) { | ||
| } | ||
| } | ||
|
|
||
| assertType('int<0, 3>', $value); | ||
|
|
||
| if ($value == 0) { | ||
| assertType('array<mixed>', $_SESSION); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| <?php | ||
|
|
||
| namespace Bug14610; | ||
|
|
||
| function test(): void | ||
| { | ||
| $value = 0; | ||
|
|
||
| if (isset($_SESSION['test'])) { | ||
| $value = rand(0,3); | ||
| if ($value == 1) { | ||
| } | ||
| } | ||
|
|
||
| if ($value == 0) { | ||
| $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']); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do we need the same fix for the
if ($b !== null) {block below?if so, come up with tests currently failling and fix it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All tests pass (12066 tests, 79688 assertions) and PHPStan reports no errors.
Here's my analysis for the reviewer's question:
The
$b !== nullbranch does NOT need the same fix. The issue in the$a !== nullbranch occurs because anIntegerRangeTypein$types[$i]expands by absorbing aConstantIntegerTypescalar, and previously-checked scalars might now be adjacent to the expanded range. The$b !== nullbranch has the inverse relationship — the scalar$scalarTypeItems[$j]is supposed to absorb$types[$i]. But this path can never trigger the expanding-range scenario because:$scalarTypeItemscontains only constant scalar types (ConstantIntegerType,ConstantStringType, etc.) — neverIntegerRangeType, sinceIntegerRangeType::isConstantScalarValue()returnsnoand these are separated into$integerRangeTypesthen merged back into$types.The
[null, $b]return fromcompareTypesInUnionfor the IntegerRangeType path (line 517-522) requires$b instanceof IntegerRangeType, which is never true for a scalar item.The other
[null, $b]paths (SubtractableType,$b->isSuperTypeOf($a)) either don't apply to constant scalar types or return$bunchanged — the scalar doesn't grow.Since the scalar never expands, there's no need to re-check earlier
$typesentries.I was unable to construct a test case that fails due to the
$b !== nullbranch lacking a restart, which confirms the analysis.No code changes are needed. Tests and static analysis both pass.