Commit 9a34eb4
Replace instanceof checks with getFiniteTypes() for subtype matching guard
Use count($guardType->getFiniteTypes()) > 1 instead of
($guardType instanceof UnionType || $guardType instanceof IntegerRangeType)
to determine whether a guard type supports subtype matching. This uses a
Type interface method instead of structural instanceof checks, as
suggested in review.
The getFiniteTypes() method correctly identifies types that represent
bounded sets of alternatives (e.g. 1|2, int<0,5>, bool) where narrowing
to one member validly satisfies the guard. Non-finite types are excluded
because their broad subtype relationships can fire unrelated conditionals.
Updated test expectations for cases where non-finite guards cannot match:
- bug-4090 bar(): int<0, max> intermediate variable has no finite types
- bug-12597-non-finite: int|string guard has no finite types
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>1 parent c0ac89b commit 9a34eb4
File tree
3 files changed
+16
-11
lines changed- src/Analyser
- tests/PHPStan
- Analyser/nsrt
- Rules/Variables
3 files changed
+16
-11
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3227 | 3227 | | |
3228 | 3228 | | |
3229 | 3229 | | |
3230 | | - | |
3231 | | - | |
3232 | | - | |
3233 | | - | |
3234 | | - | |
3235 | | - | |
| 3230 | + | |
3236 | 3231 | | |
3237 | 3232 | | |
3238 | 3233 | | |
3239 | 3234 | | |
3240 | | - | |
3241 | | - | |
| 3235 | + | |
| 3236 | + | |
3242 | 3237 | | |
3243 | 3238 | | |
3244 | 3239 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
24 | 24 | | |
25 | 25 | | |
26 | 26 | | |
27 | | - | |
28 | | - | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
29 | 31 | | |
30 | 32 | | |
31 | 33 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1492 | 1492 | | |
1493 | 1493 | | |
1494 | 1494 | | |
1495 | | - | |
| 1495 | + | |
| 1496 | + | |
| 1497 | + | |
| 1498 | + | |
| 1499 | + | |
| 1500 | + | |
| 1501 | + | |
| 1502 | + | |
| 1503 | + | |
1496 | 1504 | | |
1497 | 1505 | | |
1498 | 1506 | | |
| |||
0 commit comments