Commit 5e30e5d
Fix phpstan/phpstan#9691: Preserve per-key array types during loop generalization when values mix arrays and scalars
When an array has different keys holding structurally different value
types (e.g., one key holds an int, another holds an array), loop type
generalization would collapse all per-key type information into a
general array<K, V>, causing false "Cannot access offset" errors when
accessing keys that are known to hold array values.
Add a new branch in MutatingScope::generalizeType() that detects when
the wider array (B) has keys that are a superset of the narrower (A)
and the value types span both array and non-array types. In this case,
perform per-key value type merging to preserve structural type info.1 parent 04a99c1 commit 5e30e5d
File tree
2 files changed
+76
-3
lines changed- src/Analyser
- tests/PHPStan/Analyser/nsrt
2 files changed
+76
-3
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
4086 | 4086 | | |
4087 | 4087 | | |
4088 | 4088 | | |
| 4089 | + | |
| 4090 | + | |
4089 | 4091 | | |
4090 | | - | |
| 4092 | + | |
4091 | 4093 | | |
4092 | 4094 | | |
4093 | 4095 | | |
4094 | | - | |
| 4096 | + | |
4095 | 4097 | | |
4096 | 4098 | | |
4097 | 4099 | | |
| |||
4103 | 4105 | | |
4104 | 4106 | | |
4105 | 4107 | | |
| 4108 | + | |
| 4109 | + | |
| 4110 | + | |
| 4111 | + | |
| 4112 | + | |
| 4113 | + | |
| 4114 | + | |
| 4115 | + | |
| 4116 | + | |
| 4117 | + | |
| 4118 | + | |
| 4119 | + | |
| 4120 | + | |
| 4121 | + | |
| 4122 | + | |
| 4123 | + | |
| 4124 | + | |
| 4125 | + | |
| 4126 | + | |
| 4127 | + | |
| 4128 | + | |
| 4129 | + | |
| 4130 | + | |
| 4131 | + | |
| 4132 | + | |
| 4133 | + | |
| 4134 | + | |
| 4135 | + | |
| 4136 | + | |
| 4137 | + | |
4106 | 4138 | | |
4107 | 4139 | | |
4108 | 4140 | | |
4109 | | - | |
| 4141 | + | |
4110 | 4142 | | |
4111 | 4143 | | |
4112 | 4144 | | |
| |||
4349 | 4381 | | |
4350 | 4382 | | |
4351 | 4383 | | |
| 4384 | + | |
| 4385 | + | |
| 4386 | + | |
| 4387 | + | |
| 4388 | + | |
| 4389 | + | |
| 4390 | + | |
| 4391 | + | |
| 4392 | + | |
| 4393 | + | |
| 4394 | + | |
| 4395 | + | |
| 4396 | + | |
| 4397 | + | |
| 4398 | + | |
| 4399 | + | |
| 4400 | + | |
| 4401 | + | |
4352 | 4402 | | |
4353 | 4403 | | |
4354 | 4404 | | |
| |||
| 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 | + | |
0 commit comments