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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
use super::ExpressionAnalyzer;
use crate::flow_state::FlowState;
use mir_issues::{IssueKind, Severity};
use mir_types::{Atomic, Type};
use php_ast::owned::{ArrayAccessExpr, ArrayElement, Expr, ExprKind};
use std::sync::Arc;
impl<'a> ExpressionAnalyzer<'a> {
pub(super) fn analyze_array(&mut self, elements: &[ArrayElement], ctx: &mut FlowState) -> Type {
use mir_types::atomic::{ArrayKey, KeyedProperty};
if elements.is_empty() {
return Type::single(Atomic::TKeyedArray {
properties: indexmap::IndexMap::new(),
is_open: false,
is_list: true,
});
}
let mut keyed_props: indexmap::IndexMap<ArrayKey, KeyedProperty> =
indexmap::IndexMap::new();
let mut is_list = true;
let mut can_be_keyed = true;
let mut next_int_key: i64 = 0;
for elem in elements.iter() {
if elem.unpack {
self.analyze(&elem.value, ctx);
can_be_keyed = false;
break;
}
let value_ty = self.analyze(&elem.value, ctx);
let array_key = if let Some(key_expr) = &elem.key {
is_list = false;
let key_ty = self.analyze(key_expr, ctx);
// Float keys are silently truncated to int in PHP; TIntegralFloat is always
// whole-valued so the truncation is lossless — suppress the warning for it.
if key_ty.contains(|t| matches!(t, Atomic::TFloat | Atomic::TLiteralFloat(..)))
&& !key_ty.contains(|t| matches!(t, Atomic::TIntegralFloat))
{
self.emit(
IssueKind::ImplicitFloatToIntCast {
from: key_ty.to_string(),
},
Severity::Warning,
key_expr.span,
);
}
match key_ty.types.as_slice() {
[Atomic::TLiteralString(s)] => ArrayKey::String(s.clone()),
[Atomic::TLiteralInt(i)] => {
next_int_key = *i + 1;
ArrayKey::Int(*i)
}
_ => {
can_be_keyed = false;
break;
}
}
} else {
let k = ArrayKey::Int(next_int_key);
next_int_key += 1;
k
};
keyed_props.insert(
array_key,
KeyedProperty {
ty: value_ty,
optional: false,
},
);
}
if can_be_keyed {
return Type::single(Atomic::TKeyedArray {
properties: keyed_props,
is_open: false,
is_list,
});
}
// Fallback: generic TArray
let mut all_value_types = Type::empty();
let mut key_union = Type::empty();
let mut has_unpack = false;
for elem in elements.iter() {
let value_ty = self.analyze(&elem.value, ctx);
if elem.unpack {
has_unpack = true;
} else {
all_value_types.merge_with(&value_ty);
if let Some(key_expr) = &elem.key {
let key_ty = self.analyze(key_expr, ctx);
// Float keys are silently truncated to int in PHP; TIntegralFloat is
// always whole-valued so the truncation is lossless — no warning.
if key_ty.contains(|t| matches!(t, Atomic::TFloat | Atomic::TLiteralFloat(..)))
&& !key_ty.contains(|t| matches!(t, Atomic::TIntegralFloat))
{
self.emit(
IssueKind::ImplicitFloatToIntCast {
from: key_ty.to_string(),
},
Severity::Warning,
key_expr.span,
);
}
key_union.merge_with(&key_ty);
} else {
key_union.add_type(Atomic::TInt);
}
}
}
if has_unpack {
return Type::single(Atomic::TArray {
key: Box::new(Type::single(Atomic::TMixed)),
value: Box::new(Type::mixed()),
});
}
if key_union.is_empty() {
key_union.add_type(Atomic::TInt);
}
Type::single(Atomic::TArray {
key: Box::new(key_union),
value: Box::new(all_value_types),
})
}
pub(super) fn analyze_array_access(
&mut self,
aa: &ArrayAccessExpr,
expr: &Expr,
ctx: &mut FlowState,
) -> Type {
let arr_ty = self.analyze(&aa.array, ctx);
if let Some(idx) = &aa.index {
let idx_ty = self.analyze(idx, ctx);
// Float keys are silently truncated to int in PHP
if idx_ty.contains(|t| matches!(t, Atomic::TFloat | Atomic::TLiteralFloat(..))) {
self.emit(
IssueKind::ImplicitFloatToIntCast {
from: idx_ty.to_string(),
},
Severity::Warning,
idx.span,
);
} else if idx_ty.is_mixed() {
self.emit(IssueKind::MixedArrayOffset, Severity::Info, idx.span);
} else if !idx_ty.types.is_empty()
&& idx_ty.types.iter().all(|a| {
matches!(
a,
Atomic::TNamedObject { .. }
| Atomic::TObject
| Atomic::TArray { .. }
| Atomic::TList { .. }
| Atomic::TKeyedArray { .. }
| Atomic::TNonEmptyArray { .. }
| Atomic::TNonEmptyList { .. }
| Atomic::TClosure { .. }
)
})
{
self.emit(
IssueKind::InvalidArrayOffset {
expected: "array-key".to_string(),
actual: idx_ty.to_string(),
},
Severity::Error,
idx.span,
);
}
}
if arr_ty.is_mixed() {
self.emit(IssueKind::MixedArrayAccess, Severity::Info, expr.span);
return Type::mixed();
}
// InvalidArrayAccess: definitely non-subscriptable type (not array, not string, not object)
if !arr_ty.is_mixed()
&& !arr_ty.types.is_empty()
&& arr_ty.types.iter().all(|a| {
matches!(
a,
Atomic::TInt
| Atomic::TLiteralInt(_)
| Atomic::TIntRange { .. }
| Atomic::TPositiveInt
| Atomic::TFloat
| Atomic::TIntegralFloat
| Atomic::TLiteralFloat(_, _)
| Atomic::TBool
| Atomic::TTrue
| Atomic::TFalse
)
})
{
self.emit(
IssueKind::InvalidArrayAccess {
ty: arr_ty.to_string(),
},
Severity::Error,
expr.span,
);
return Type::mixed();
}
// PossiblyInvalidArrayAccess: union has some subscriptable members and some that aren't.
let is_invalid_for_access = |a: &Atomic| {
matches!(
a,
Atomic::TInt
| Atomic::TLiteralInt(_)
| Atomic::TIntRange { .. }
| Atomic::TPositiveInt
| Atomic::TFloat
| Atomic::TLiteralFloat(_, _)
| Atomic::TBool
| Atomic::TTrue
| Atomic::TFalse
)
};
if !arr_ty.is_mixed()
&& !arr_ty.types.is_empty()
&& !self.in_existence_check
&& arr_ty.types.iter().any(is_invalid_for_access)
&& !arr_ty.types.iter().all(is_invalid_for_access)
{
self.emit(
IssueKind::PossiblyInvalidArrayAccess {
ty: arr_ty.to_string(),
},
Severity::Info,
expr.span,
);
}
if arr_ty.contains(|t| matches!(t, Atomic::TNull)) && arr_ty.is_single() {
self.emit(IssueKind::NullArrayAccess, Severity::Error, expr.span);
return Type::mixed();
}
if arr_ty.is_nullable() {
self.emit(
IssueKind::PossiblyNullArrayAccess,
Severity::Info,
expr.span,
);
}
let literal_key: Option<mir_types::atomic::ArrayKey> =
aa.index.as_ref().and_then(|idx| match &idx.kind {
ExprKind::String(s) => {
Some(mir_types::atomic::ArrayKey::String(Arc::from(s.as_ref())))
}
ExprKind::Int(i) => Some(mir_types::atomic::ArrayKey::Int(*i)),
_ => None,
});
let idx_span = aa.index.as_ref().map(|i| i.span).unwrap_or(expr.span);
for atomic in &arr_ty.types {
match atomic {
Atomic::TKeyedArray {
properties,
is_open,
..
} => {
if let Some(ref key) = literal_key {
if let Some(prop) = properties.get(key) {
return prop.ty.clone();
}
if !is_open && !self.in_existence_check {
let key_str = match key {
mir_types::atomic::ArrayKey::String(s) => s.to_string(),
mir_types::atomic::ArrayKey::Int(i) => i.to_string(),
};
self.emit(
IssueKind::NonExistentArrayOffset { key: key_str },
Severity::Error,
idx_span,
);
return Type::mixed();
}
}
let mut result = Type::empty();
for prop in properties.values() {
result.merge_with(&prop.ty);
}
return if result.types.is_empty() {
Type::mixed()
} else {
result
};
}
Atomic::TArray { value, .. } | Atomic::TNonEmptyArray { value, .. } => {
return *value.clone();
}
Atomic::TList { value } | Atomic::TNonEmptyList { value } => {
return *value.clone();
}
Atomic::TString | Atomic::TLiteralString(_) => {
return Type::single(Atomic::TString);
}
_ => {}
}
}
Type::mixed()
}
}