Commit a269f2e
Advance the implicit index by an unpacked item's integer keys in array literals
- Add `PHPStan\Type\ArrayUnpackingHelper`, which resolves how many implicit indices
an unpacked item (`...$a`) takes up and which keys it contributes: integer keys are
always renumbered, string keys keep their name since PHP 8.1.
- `AssignHandler::processArrayByRefItems()` counts an unpacked item's integer keys through
the helper, replacing `advanceImplicitIndexByUnpackedArray()`. Besides a single sealed
constant array, this also keeps the implicit index known after a union of constant
arrays with the same integer key count and after an array with only string keys, which
takes up no index. The index still stops at `PHP_INT_MAX` through `advanceImplicitIndex()`.
- By-reference items inside an unpacked array literal whose items are all keyless
(`[...[&$x], &$y]`) are flattened into the surrounding literal, so `$x` is linked to
`$a[0]` instead of losing its link: `$x = 10` now gives `array{10, int}`.
- `DuplicateKeysInLiteralArraysRule` no longer stops tracking keys after every unpacked
item. It asks the helper for the keys the unpacked item really contributes, so
`[...[1, 2], 0 => 'x']` and `['a' => 1, ...['a' => 2]]` are reported, and stops
tracking the auto-generated index only when they can't be determined or would pass
`PHP_INT_MAX`.
- In the same rule, an explicit integer key no longer resurrects the auto-generated
index after it became unknown (`max(false, 5)` used to evaluate to `5`), which fixes
false positives like `[$k => 'z', 5 => 'a', 'b', 6 => 'c']` and
`[...$list, 5 => 'a', 'b', 6 => 'c']`.
- String keys contributed by an unpacked item are printed with quotes in the rule's
error message.
- `testBug15244` now requires PHP 8.1 like its `// lint >= 8.1` data file: before 8.1
the string-keyed spread in it renumbers, which the rule now reports.1 parent 8f89e5f commit a269f2e
9 files changed
Lines changed: 574 additions & 103 deletions
File tree
- src
- Analyser/ExprHandler
- Rules/Arrays
- Type
- tests/PHPStan
- Analyser/nsrt
- Rules/Arrays
- data
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
63 | 63 | | |
64 | 64 | | |
65 | 65 | | |
| 66 | + | |
66 | 67 | | |
67 | 68 | | |
68 | 69 | | |
| |||
121 | 122 | | |
122 | 123 | | |
123 | 124 | | |
| 125 | + | |
124 | 126 | | |
125 | 127 | | |
126 | 128 | | |
| |||
1935 | 1937 | | |
1936 | 1938 | | |
1937 | 1939 | | |
1938 | | - | |
| 1940 | + | |
| 1941 | + | |
| 1942 | + | |
| 1943 | + | |
| 1944 | + | |
| 1945 | + | |
| 1946 | + | |
| 1947 | + | |
| 1948 | + | |
| 1949 | + | |
| 1950 | + | |
| 1951 | + | |
1939 | 1952 | | |
1940 | 1953 | | |
1941 | | - | |
1942 | | - | |
1943 | | - | |
| 1954 | + | |
| 1955 | + | |
| 1956 | + | |
| 1957 | + | |
| 1958 | + | |
| 1959 | + | |
| 1960 | + | |
1944 | 1961 | | |
1945 | | - | |
| 1962 | + | |
| 1963 | + | |
| 1964 | + | |
| 1965 | + | |
| 1966 | + | |
| 1967 | + | |
| 1968 | + | |
| 1969 | + | |
1946 | 1970 | | |
| 1971 | + | |
1947 | 1972 | | |
1948 | 1973 | | |
1949 | 1974 | | |
| |||
1974 | 1999 | | |
1975 | 2000 | | |
1976 | 2001 | | |
1977 | | - | |
| 2002 | + | |
1978 | 2003 | | |
1979 | 2004 | | |
1980 | 2005 | | |
| |||
2001 | 2026 | | |
2002 | 2027 | | |
2003 | 2028 | | |
2004 | | - | |
| 2029 | + | |
| 2030 | + | |
| 2031 | + | |
| 2032 | + | |
| 2033 | + | |
| 2034 | + | |
| 2035 | + | |
| 2036 | + | |
| 2037 | + | |
| 2038 | + | |
| 2039 | + | |
| 2040 | + | |
| 2041 | + | |
| 2042 | + | |
| 2043 | + | |
| 2044 | + | |
2005 | 2045 | | |
2006 | 2046 | | |
2007 | 2047 | | |
| |||
2016 | 2056 | | |
2017 | 2057 | | |
2018 | 2058 | | |
2019 | | - | |
2020 | | - | |
2021 | | - | |
2022 | | - | |
2023 | | - | |
2024 | | - | |
2025 | | - | |
2026 | | - | |
2027 | | - | |
2028 | | - | |
2029 | | - | |
2030 | | - | |
2031 | | - | |
2032 | | - | |
2033 | | - | |
2034 | | - | |
2035 | | - | |
2036 | | - | |
2037 | | - | |
2038 | | - | |
2039 | | - | |
2040 | | - | |
2041 | | - | |
2042 | | - | |
2043 | | - | |
2044 | | - | |
2045 | | - | |
2046 | | - | |
2047 | | - | |
2048 | | - | |
2049 | | - | |
2050 | | - | |
2051 | | - | |
2052 | 2059 | | |
2053 | 2060 | | |
2054 | 2061 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
9 | 9 | | |
10 | 10 | | |
11 | 11 | | |
| 12 | + | |
12 | 13 | | |
13 | 14 | | |
14 | 15 | | |
| |||
18 | 19 | | |
19 | 20 | | |
20 | 21 | | |
| 22 | + | |
21 | 23 | | |
22 | 24 | | |
23 | 25 | | |
| |||
32 | 34 | | |
33 | 35 | | |
34 | 36 | | |
| 37 | + | |
35 | 38 | | |
36 | 39 | | |
37 | 40 | | |
| |||
64 | 67 | | |
65 | 68 | | |
66 | 69 | | |
| 70 | + | |
67 | 71 | | |
68 | | - | |
69 | | - | |
70 | | - | |
71 | | - | |
72 | | - | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
73 | 75 | | |
74 | | - | |
75 | | - | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
76 | 105 | | |
77 | 106 | | |
78 | 107 | | |
79 | 108 | | |
80 | 109 | | |
81 | 110 | | |
82 | | - | |
| 111 | + | |
83 | 112 | | |
84 | 113 | | |
85 | 114 | | |
86 | 115 | | |
87 | 116 | | |
88 | 117 | | |
89 | | - | |
| 118 | + | |
90 | 119 | | |
91 | 120 | | |
92 | 121 | | |
93 | 122 | | |
94 | | - | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
95 | 126 | | |
96 | 127 | | |
97 | 128 | | |
98 | 129 | | |
99 | | - | |
100 | 130 | | |
101 | | - | |
102 | | - | |
103 | | - | |
104 | | - | |
| 131 | + | |
105 | 132 | | |
106 | 133 | | |
107 | | - | |
108 | | - | |
109 | | - | |
110 | | - | |
111 | | - | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
112 | 139 | | |
113 | 140 | | |
114 | | - | |
115 | | - | |
116 | | - | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
117 | 152 | | |
118 | | - | |
119 | 153 | | |
120 | | - | |
121 | | - | |
122 | | - | |
123 | | - | |
124 | | - | |
125 | | - | |
126 | | - | |
127 | | - | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
128 | 162 | | |
129 | | - | |
| 163 | + | |
130 | 164 | | |
131 | | - | |
132 | | - | |
133 | | - | |
134 | | - | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
135 | 169 | | |
136 | | - | |
137 | | - | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
138 | 176 | | |
139 | | - | |
140 | | - | |
141 | | - | |
142 | 177 | | |
143 | | - | |
144 | 178 | | |
145 | | - | |
146 | | - | |
147 | | - | |
148 | | - | |
149 | | - | |
150 | | - | |
151 | | - | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
152 | 186 | | |
153 | | - | |
154 | | - | |
155 | | - | |
156 | | - | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
157 | 191 | | |
158 | | - | |
159 | | - | |
160 | | - | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
161 | 195 | | |
162 | | - | |
163 | | - | |
164 | | - | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
165 | 199 | | |
166 | | - | |
| 200 | + | |
| 201 | + | |
167 | 202 | | |
168 | 203 | | |
169 | 204 | | |
| |||
0 commit comments