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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
use super::helpers::is_non_empty_when_concat;
use super::ExpressionAnalyzer;
use crate::flow_state::FlowState;
use mir_issues::{IssueKind, Severity};
use mir_types::{Atomic, Type};
use php_ast::ast::CastKind;
use php_ast::owned::Expr;
/// Try to fold a `(int)` cast of a single known literal to a literal int result.
fn fold_int_cast(ty: &Type) -> Option<Type> {
if ty.types.len() != 1 {
return None;
}
let lit = match &ty.types[0] {
Atomic::TLiteralInt(n) => *n,
Atomic::TTrue => 1,
Atomic::TFalse => 0,
Atomic::TLiteralString(s) => {
let t = s.trim();
// PHP truncates floats: "3.7" → 3
if let Ok(n) = t.parse::<i64>() {
n
} else if let Ok(f) = t.parse::<f64>() {
f as i64
} else {
return None;
}
}
_ => return None,
};
Some(Type::single(Atomic::TLiteralInt(lit)))
}
/// Try to fold a `(string)` cast of a single known literal to a literal string result.
fn fold_string_cast(ty: &Type) -> Option<Type> {
if ty.types.len() != 1 {
return None;
}
let s = match &ty.types[0] {
Atomic::TLiteralInt(n) => n.to_string(),
Atomic::TTrue => "1".to_string(),
Atomic::TFalse => String::new(),
Atomic::TLiteralString(s) => s.as_ref().to_string(),
Atomic::TLiteralFloat(hi, lo) => {
let bits = ((*hi as u64) << 32) | (*lo as u32 as u64);
f64::from_bits(bits).to_string()
}
_ => return None,
};
Some(Type::single(Atomic::TLiteralString(s.into())))
}
/// Try to fold a `(bool)` cast of a single known literal to `TTrue` or `TFalse`.
fn fold_bool_cast(ty: &Type) -> Option<Type> {
if ty.types.len() != 1 {
return None;
}
let result = match &ty.types[0] {
Atomic::TLiteralInt(0) | Atomic::TFalse => false,
Atomic::TLiteralInt(_) | Atomic::TTrue => true,
Atomic::TLiteralString(s) if s.is_empty() || s.as_ref() == "0" => false,
Atomic::TLiteralString(_) => true,
_ => return None,
};
Some(Type::single(if result {
Atomic::TTrue
} else {
Atomic::TFalse
}))
}
/// Returns true for atomic types that are safely castable to any scalar.
/// Used to suppress InvalidCast on union types that include both "bad" atoms
/// (arrays/objects) and "good" atoms (scalars) — e.g. `string|array|bool|null`
/// from console option() return types.
fn is_scalar_safe(t: &Atomic) -> bool {
t.is_string()
|| t.is_int()
|| matches!(
t,
Atomic::TFloat
| Atomic::TLiteralFloat(..)
| Atomic::TBool
| Atomic::TTrue
| Atomic::TFalse
| Atomic::TNull
| Atomic::TMixed
| Atomic::TScalar
)
}
impl<'a> ExpressionAnalyzer<'a> {
pub(super) fn analyze_cast(
&mut self,
kind: &CastKind,
inner: &Expr,
ctx: &mut FlowState,
) -> Type {
let inner_ty = self.analyze(inner, ctx);
match kind {
CastKind::Int => {
// Literal fold: (int)42, (int)"123", (int)true, etc.
if let Some(folded) = fold_int_cast(&inner_ty) {
// TLiteralInt input is a redundant cast.
if inner_ty.is_single() && inner_ty.contains(|t| t.is_int()) {
self.emit(
IssueKind::RedundantCast {
from: inner_ty.to_string(),
to: "int".to_string(),
},
Severity::Info,
inner.span,
);
}
return folded;
}
// Check for RedundantCast when already int (non-literal)
if inner_ty.is_single() && inner_ty.contains(|t| t.is_int()) {
self.emit(
IssueKind::RedundantCast {
from: inner_ty.to_string(),
to: "int".to_string(),
},
Severity::Info,
inner.span,
);
}
// Check for InvalidCast from array/object — only when the union has no
// scalar-safe atoms. If the union also contains string/bool/null/int/float,
// the cast is valid for those branches and we suppress the warning to avoid
// FPs from over-broad return types (e.g. console option()).
else if inner_ty.contains(|t| {
matches!(
t,
Atomic::TArray { .. }
| Atomic::TNonEmptyArray { .. }
| Atomic::TList { .. }
| Atomic::TNonEmptyList { .. }
| Atomic::TKeyedArray { .. }
| Atomic::TNamedObject { .. }
| Atomic::TObject
)
}) && !inner_ty.contains(is_scalar_safe)
{
self.emit(
IssueKind::InvalidCast {
from: inner_ty.to_string(),
to: "int".to_string(),
},
Severity::Warning,
inner.span,
);
}
Type::single(Atomic::TInt)
}
CastKind::Float => {
// Check for RedundantCast when already float
if inner_ty.is_single()
&& inner_ty
.contains(|t| matches!(t, Atomic::TFloat | Atomic::TLiteralFloat(..)))
{
self.emit(
IssueKind::RedundantCast {
from: inner_ty.to_string(),
to: "float".to_string(),
},
Severity::Info,
inner.span,
);
}
// Check for InvalidCast from array/object — same "no scalars" guard as int.
else if inner_ty.contains(|t| {
matches!(
t,
Atomic::TArray { .. }
| Atomic::TNonEmptyArray { .. }
| Atomic::TList { .. }
| Atomic::TNonEmptyList { .. }
| Atomic::TKeyedArray { .. }
| Atomic::TNamedObject { .. }
| Atomic::TObject
)
}) && !inner_ty.contains(is_scalar_safe)
{
self.emit(
IssueKind::InvalidCast {
from: inner_ty.to_string(),
to: "float".to_string(),
},
Severity::Warning,
inner.span,
);
}
Type::single(Atomic::TFloat)
}
CastKind::String => {
// Literal fold: (string)42 → "42", (string)true → "1", etc.
if let Some(folded) = fold_string_cast(&inner_ty) {
// TLiteralString input is a redundant cast.
if inner_ty.is_single() && inner_ty.contains(|t| t.is_string()) {
self.emit(
IssueKind::RedundantCast {
from: inner_ty.to_string(),
to: "string".to_string(),
},
Severity::Info,
inner.span,
);
}
return folded;
}
// Check for RedundantCast when already string (non-literal)
if inner_ty.is_single() && inner_ty.contains(|t| t.is_string()) {
self.emit(
IssueKind::RedundantCast {
from: inner_ty.to_string(),
to: "string".to_string(),
},
Severity::Info,
inner.span,
);
}
// Check for InvalidCast from array
else if inner_ty.contains(|t| {
matches!(
t,
Atomic::TArray { .. }
| Atomic::TNonEmptyArray { .. }
| Atomic::TList { .. }
| Atomic::TNonEmptyList { .. }
| Atomic::TKeyedArray { .. }
)
}) {
self.emit(
IssueKind::InvalidCast {
from: inner_ty.to_string(),
to: "string".to_string(),
},
Severity::Warning,
inner.span,
);
}
// Check for InvalidCast from a concrete class without __toString.
// Interfaces/abstract classes are skipped since an implementing subclass
// may provide __toString; only flag concrete (instantiatable) classes.
else {
for atom in inner_ty.types.iter() {
if let Atomic::TNamedObject { fqcn, .. } = atom {
let fqcn_str = fqcn.as_str();
let here = crate::db::Fqcn::from_str(self.db, fqcn_str);
let is_concrete = crate::db::find_class_like(self.db, here)
.map(|c| matches!(c, crate::db::ClassLike::Class(_)))
.unwrap_or(false);
if !is_concrete {
continue;
}
let has_to_string = crate::db::find_method_in_chain(
self.db,
crate::db::Fqcn::from_str(self.db, fqcn_str),
"__tostring",
)
.is_some();
if !has_to_string {
self.emit(
IssueKind::InvalidCast {
from: inner_ty.to_string(),
to: "string".to_string(),
},
Severity::Warning,
inner.span,
);
break;
}
}
}
}
if is_non_empty_when_concat(&inner_ty) {
Type::single(Atomic::TNonEmptyString)
} else {
Type::single(Atomic::TString)
}
}
CastKind::Bool => {
// Literal fold: (bool)0 → false, (bool)1 → true, (bool)"" → false, etc.
if let Some(folded) = fold_bool_cast(&inner_ty) {
// TTrue/TFalse input is a redundant cast.
if inner_ty.is_single()
&& inner_ty.contains(|t| {
matches!(t, Atomic::TBool | Atomic::TTrue | Atomic::TFalse)
})
{
self.emit(
IssueKind::RedundantCast {
from: inner_ty.to_string(),
to: "bool".to_string(),
},
Severity::Info,
inner.span,
);
}
return folded;
}
// Check for RedundantCast when already bool (non-literal)
if inner_ty.is_single()
&& inner_ty
.contains(|t| matches!(t, Atomic::TBool | Atomic::TTrue | Atomic::TFalse))
{
self.emit(
IssueKind::RedundantCast {
from: inner_ty.to_string(),
to: "bool".to_string(),
},
Severity::Info,
inner.span,
);
}
Type::single(Atomic::TBool)
}
CastKind::Array => {
// Check for RedundantCast when already array
if inner_ty.is_single()
&& inner_ty.contains(|t| {
matches!(
t,
Atomic::TArray { .. }
| Atomic::TNonEmptyArray { .. }
| Atomic::TList { .. }
| Atomic::TNonEmptyList { .. }
| Atomic::TKeyedArray { .. }
)
})
{
self.emit(
IssueKind::RedundantCast {
from: inner_ty.to_string(),
to: "array".to_string(),
},
Severity::Info,
inner.span,
);
}
Type::single(Atomic::TArray {
key: Box::new(Type::single(Atomic::TMixed)),
value: Box::new(Type::mixed()),
})
}
CastKind::Object => Type::single(Atomic::TObject),
CastKind::Unset | CastKind::Void => Type::single(Atomic::TNull),
}
}
}