-
Notifications
You must be signed in to change notification settings - Fork 574
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
Changes from all commits
470212f
99c79c2
628e531
e65d557
81ad1bf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| <?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); | ||
| $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']); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1642,6 +1642,42 @@ public static function dataUnion(): iterable | |
| IntegerType::class, | ||
| 'int', | ||
| ], | ||
| [ | ||
| [ | ||
| new UnionType([ | ||
| new ConstantIntegerType(0), | ||
| IntegerRangeType::fromInterval(2, 3), | ||
| ]), | ||
| new ConstantIntegerType(1), | ||
| ], | ||
| IntegerRangeType::class, | ||
| 'int<0, 3>', | ||
| ], | ||
| [ | ||
| [ | ||
| new ConstantIntegerType(1), | ||
| new UnionType([ | ||
| new ConstantIntegerType(0), | ||
| IntegerRangeType::fromInterval(2, 3), | ||
| ]), | ||
| ], | ||
| IntegerRangeType::class, | ||
| 'int<0, 3>', | ||
| ], | ||
|
Comment on lines
+1645
to
+1666
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think these tests make clear that a fix in TypeCombinator is valid (and the super-global example is just a "bad example" which is fixed by it at the same time)
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. All 12,068 tests pass (79,692 assertions) and The reviewer's comment is positive feedback — they're agreeing that the TypeCombinator unit tests clearly demonstrate the fix is valid on their own, independent of the superglobal scenario. No code changes are needed to address this comment. |
||
| [ | ||
| [ | ||
| new UnionType([ | ||
| new ConstantIntegerType(0), | ||
| IntegerRangeType::fromInterval(2, 3), | ||
| ]), | ||
| new UnionType([ | ||
| new ConstantIntegerType(10), | ||
| new ConstantIntegerType(1), | ||
| ]), | ||
| ], | ||
| UnionType::class, | ||
| '10|int<0, 3>', | ||
| ], | ||
| [ | ||
| [ | ||
| new MixedType(), | ||
|
|
||
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.