Commit f5e067e
Fix false positive "offset might not exist" on array with all constant keys set in loop
When a constant array with all expected keys (e.g. array{-1: 0, 0: 0, 1: 0, 2: 0})
was modified inside a foreach loop via a union offset type covering all keys,
the intermediate expression type was derived top-down from the root property type,
producing a general array (array<-1|0|1|2, int>) that lost the constant structure.
During loop generalization, this general array couldn't confirm offset existence,
causing a false "Offset ... might not exist" error.
The fix computes improved intermediate types bottom-up using the scope's tracked
constant array types and setExistingOffsetValueType, preserving the constant array
structure through loop generalization.
Closes phpstan/phpstan#136691 parent e50b57c commit f5e067e
File tree
3 files changed
+91
-1
lines changed- src/Analyser
- tests/PHPStan/Rules/Arrays
- data
3 files changed
+91
-1
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
6808 | 6808 | | |
6809 | 6809 | | |
6810 | 6810 | | |
6811 | | - | |
6812 | 6811 | | |
| 6812 | + | |
| 6813 | + | |
| 6814 | + | |
| 6815 | + | |
| 6816 | + | |
| 6817 | + | |
| 6818 | + | |
| 6819 | + | |
| 6820 | + | |
| 6821 | + | |
| 6822 | + | |
| 6823 | + | |
| 6824 | + | |
| 6825 | + | |
| 6826 | + | |
| 6827 | + | |
| 6828 | + | |
| 6829 | + | |
| 6830 | + | |
| 6831 | + | |
| 6832 | + | |
| 6833 | + | |
| 6834 | + | |
| 6835 | + | |
| 6836 | + | |
| 6837 | + | |
| 6838 | + | |
| 6839 | + | |
| 6840 | + | |
| 6841 | + | |
| 6842 | + | |
| 6843 | + | |
6813 | 6844 | | |
6814 | 6845 | | |
6815 | 6846 | | |
6816 | 6847 | | |
6817 | 6848 | | |
6818 | 6849 | | |
6819 | 6850 | | |
| 6851 | + | |
| 6852 | + | |
6820 | 6853 | | |
6821 | 6854 | | |
6822 | 6855 | | |
| |||
Lines changed: 7 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1145 | 1145 | | |
1146 | 1146 | | |
1147 | 1147 | | |
| 1148 | + | |
| 1149 | + | |
| 1150 | + | |
| 1151 | + | |
| 1152 | + | |
| 1153 | + | |
| 1154 | + | |
1148 | 1155 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
0 commit comments