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
use super::return_type::{
declared_return_has_template, named_object_return_compatible, return_arrays_compatible,
};
/// Flow-control statement handlers extracted from `analyze_stmt`.
///
/// Each method corresponds to one match arm in the parent `analyze_stmt`.
use super::StatementsAnalyzer;
use mir_issues::{IssueKind, Location};
use mir_types::{Atomic, Union};
impl<'a> StatementsAnalyzer<'a> {
// -----------------------------------------------------------------------
// Return
// -----------------------------------------------------------------------
pub(super) fn analyze_return_stmt<'arena, 'src>(
&mut self,
opt_expr: &Option<&php_ast::ast::Expr<'arena, 'src>>,
stmt_span: php_ast::Span,
ctx: &mut crate::context::Context,
) {
if let Some(expr) = opt_expr {
let ret_ty = self.expr_analyzer(ctx).analyze(expr, ctx);
// If there's a bare `@var Type` (no variable name) on the return statement,
// use the annotated type for the return-type compatibility check.
// `@var Type $name` with a variable name narrows the variable (handled in
// analyze_stmts loop), not the return type.
let check_ty = if let Some((None, var_ty)) = self.extract_var_annotation(stmt_span) {
var_ty
} else {
ret_ty.clone()
};
// Check against declared return type
if let Some(declared) = &ctx.fn_return_type.clone() {
// Check return type compatibility. Special case: `void` functions must not
// return any value (named_object_return_compatible considers TVoid compatible
// with TNull, so handle void separately to avoid false suppression).
if !declared.contains(|t| matches!(t, Atomic::TConditional { .. }))
&& ((declared.is_void() && !check_ty.is_void() && !check_ty.is_mixed())
|| (!check_ty.is_subtype_of_simple(declared)
&& !declared.is_mixed()
&& !check_ty.is_mixed()
&& !named_object_return_compatible(&check_ty, declared, self.db, &self.file)
// Also check without null (handles `null|T` where T implements declared).
// Guard: if check_ty is purely null, remove_null() is empty and would
// vacuously return true, incorrectly suppressing the error.
&& (check_ty.remove_null().is_empty() || !named_object_return_compatible(&check_ty.remove_null(), declared, self.db, &self.file))
&& !declared_return_has_template(declared, self.db)
&& !declared_return_has_template(&check_ty, self.db)
&& !return_arrays_compatible(&check_ty, declared, self.db, &self.file)
// Skip coercions: declared is more specific than actual
&& !declared.is_subtype_of_simple(&check_ty)
&& !declared.remove_null().is_subtype_of_simple(&check_ty)
// Skip when actual is compatible after removing null/false.
// Guard against empty union (e.g. pure-null type): removing null
// from `null` alone gives an empty union which vacuously passes
// is_subtype_of_simple — that would incorrectly suppress the error.
&& (check_ty.remove_null().is_empty() || !check_ty.remove_null().is_subtype_of_simple(declared))
&& !check_ty.remove_false().is_subtype_of_simple(declared)
// Suppress LessSpecificReturnStatement (level 4): actual is a
// supertype of declared (not flagged at default error level).
&& !named_object_return_compatible(declared, &check_ty, self.db, &self.file)
&& !named_object_return_compatible(&declared.remove_null(), &check_ty.remove_null(), self.db, &self.file)))
{
let (line, col_start) = self.offset_to_line_col(stmt_span.start);
let (line_end, col_end) = if stmt_span.start < stmt_span.end {
let (end_line, end_col) = self.offset_to_line_col(stmt_span.end);
(end_line, end_col)
} else {
(line, col_start)
};
self.issues.add(
mir_issues::Issue::new(
IssueKind::InvalidReturnType {
expected: format!("{declared}"),
actual: format!("{ret_ty}"),
},
Location {
file: self.file.clone(),
line,
line_end,
col_start,
col_end: col_end.max(col_start + 1),
},
)
.with_snippet(
crate::parser::span_text(self.source, stmt_span).unwrap_or_default(),
),
);
}
}
self.return_types.push(ret_ty);
} else {
self.return_types.push(Union::single(Atomic::TVoid));
// Bare `return;` from a non-void declared function is an error.
if let Some(declared) = &ctx.fn_return_type.clone() {
if !declared.is_void() && !declared.is_mixed() {
let (line, col_start) = self.offset_to_line_col(stmt_span.start);
let (line_end, col_end) = if stmt_span.start < stmt_span.end {
let (end_line, end_col) = self.offset_to_line_col(stmt_span.end);
(end_line, end_col)
} else {
(line, col_start)
};
self.issues.add(
mir_issues::Issue::new(
IssueKind::InvalidReturnType {
expected: format!("{declared}"),
actual: "void".to_string(),
},
Location {
file: self.file.clone(),
line,
line_end,
col_start,
col_end: col_end.max(col_start + 1),
},
)
.with_snippet(
crate::parser::span_text(self.source, stmt_span).unwrap_or_default(),
),
);
}
}
}
ctx.diverges = true;
}
// -----------------------------------------------------------------------
// Throw
// -----------------------------------------------------------------------
pub(super) fn analyze_throw_stmt<'arena, 'src>(
&mut self,
expr: &php_ast::ast::Expr<'arena, 'src>,
stmt_span: php_ast::Span,
ctx: &mut crate::context::Context,
) {
let thrown_ty = self.expr_analyzer(ctx).analyze(expr, ctx);
// Validate that the thrown type extends Throwable
for atomic in &thrown_ty.types {
match atomic {
mir_types::Atomic::TNamedObject { fqcn, .. } => {
let resolved = crate::db::resolve_name_via_db(self.db, &self.file, fqcn);
let is_throwable = resolved == "Throwable"
|| resolved == "Exception"
|| resolved == "Error"
|| fqcn.as_ref() == "Throwable"
|| fqcn.as_ref() == "Exception"
|| fqcn.as_ref() == "Error"
|| crate::db::extends_or_implements_via_db(self.db, &resolved, "Throwable")
|| crate::db::extends_or_implements_via_db(self.db, &resolved, "Exception")
|| crate::db::extends_or_implements_via_db(self.db, &resolved, "Error")
|| crate::db::extends_or_implements_via_db(self.db, fqcn, "Throwable")
|| crate::db::extends_or_implements_via_db(self.db, fqcn, "Exception")
|| crate::db::extends_or_implements_via_db(self.db, fqcn, "Error")
// Suppress if class has unknown ancestors (might be Throwable)
|| crate::db::has_unknown_ancestor_via_db(self.db, &resolved)
|| crate::db::has_unknown_ancestor_via_db(self.db, fqcn)
// Suppress if class is not in codebase at all (could be extension class)
|| (!crate::db::type_exists_via_db(self.db, &resolved) && !crate::db::type_exists_via_db(self.db, fqcn));
if !is_throwable {
let (line, col_start) = self.offset_to_line_col(stmt_span.start);
let (line_end, col_end) = if stmt_span.start < stmt_span.end {
let (end_line, end_col) = self.offset_to_line_col(stmt_span.end);
(end_line, end_col)
} else {
(line, col_start)
};
self.issues.add(mir_issues::Issue::new(
IssueKind::InvalidThrow {
ty: fqcn.to_string(),
},
Location {
file: self.file.clone(),
line,
line_end,
col_start,
col_end: col_end.max(col_start + 1),
},
));
}
}
// self/static/parent resolve to the class itself — check via fqcn
mir_types::Atomic::TSelf { fqcn }
| mir_types::Atomic::TStaticObject { fqcn }
| mir_types::Atomic::TParent { fqcn } => {
let resolved = crate::db::resolve_name_via_db(self.db, &self.file, fqcn);
let is_throwable = resolved == "Throwable"
|| resolved == "Exception"
|| resolved == "Error"
|| crate::db::extends_or_implements_via_db(self.db, &resolved, "Throwable")
|| crate::db::extends_or_implements_via_db(self.db, &resolved, "Exception")
|| crate::db::extends_or_implements_via_db(self.db, &resolved, "Error")
|| crate::db::extends_or_implements_via_db(self.db, fqcn, "Throwable")
|| crate::db::extends_or_implements_via_db(self.db, fqcn, "Exception")
|| crate::db::extends_or_implements_via_db(self.db, fqcn, "Error")
|| crate::db::has_unknown_ancestor_via_db(self.db, &resolved)
|| crate::db::has_unknown_ancestor_via_db(self.db, fqcn);
if !is_throwable {
let (line, col_start) = self.offset_to_line_col(stmt_span.start);
let (line_end, col_end) = if stmt_span.start < stmt_span.end {
let (end_line, end_col) = self.offset_to_line_col(stmt_span.end);
(end_line, end_col)
} else {
(line, col_start)
};
self.issues.add(mir_issues::Issue::new(
IssueKind::InvalidThrow {
ty: fqcn.to_string(),
},
Location {
file: self.file.clone(),
line,
line_end,
col_start,
col_end: col_end.max(col_start + 1),
},
));
}
}
mir_types::Atomic::TMixed | mir_types::Atomic::TObject => {}
_ => {
let (line, col_start) = self.offset_to_line_col(stmt_span.start);
let (line_end, col_end) = if stmt_span.start < stmt_span.end {
let (end_line, end_col) = self.offset_to_line_col(stmt_span.end);
(end_line, end_col)
} else {
(line, col_start)
};
self.issues.add(mir_issues::Issue::new(
IssueKind::InvalidThrow {
ty: format!("{thrown_ty}"),
},
Location {
file: self.file.clone(),
line,
line_end,
col_start,
col_end: col_end.max(col_start + 1),
},
));
}
}
}
ctx.diverges = true;
}
// -----------------------------------------------------------------------
// Break
// -----------------------------------------------------------------------
pub(super) fn analyze_break_stmt(&mut self, ctx: &mut crate::context::Context) {
// Save the context at the break point so the post-loop context
// accounts for this early-exit path.
if let Some(break_ctxs) = self.break_ctx_stack.last_mut() {
break_ctxs.push(ctx.clone());
}
// Context after an unconditional break is dead; don't continue
// emitting issues for code after this point.
ctx.diverges = true;
}
// -----------------------------------------------------------------------
// Continue
// -----------------------------------------------------------------------
pub(super) fn analyze_continue_stmt(&mut self, ctx: &mut crate::context::Context) {
// continue goes back to the loop condition — no context to save,
// the widening pass already re-analyses the body.
ctx.diverges = true;
}
// -----------------------------------------------------------------------
// Unset
// -----------------------------------------------------------------------
pub(super) fn analyze_unset_stmt<'arena, 'src>(
&mut self,
vars: &php_ast::ast::ArenaVec<'arena, php_ast::ast::Expr<'arena, 'src>>,
ctx: &mut crate::context::Context,
) {
for var in vars.iter() {
if let php_ast::ast::ExprKind::Variable(name) = &var.kind {
ctx.unset_var(name.as_str().trim_start_matches('$'));
}
}
}
// -----------------------------------------------------------------------
// Static variable declaration
// -----------------------------------------------------------------------
pub(super) fn analyze_static_var_stmt<'arena, 'src>(
&mut self,
vars: &php_ast::ast::ArenaVec<'arena, php_ast::ast::StaticVar<'arena, 'src>>,
ctx: &mut crate::context::Context,
) {
for sv in vars.iter() {
let ty = Union::mixed(); // static vars are indeterminate on entry
ctx.set_var(sv.name.trim_start_matches('$'), ty);
}
}
// -----------------------------------------------------------------------
// Global declaration
// -----------------------------------------------------------------------
pub(super) fn analyze_global_stmt<'arena, 'src>(
&mut self,
vars: &php_ast::ast::ArenaVec<'arena, php_ast::ast::Expr<'arena, 'src>>,
ctx: &mut crate::context::Context,
) {
for var in vars.iter() {
if let php_ast::ast::ExprKind::Variable(name) = &var.kind {
let var_name = name.as_str().trim_start_matches('$');
let ty = self
.db
.global_var_type(var_name)
.unwrap_or_else(Union::mixed);
ctx.set_var(var_name, ty);
}
}
}
// -----------------------------------------------------------------------
// Declare
// -----------------------------------------------------------------------
pub(super) fn analyze_declare_stmt<'arena, 'src>(
&mut self,
d: &php_ast::ast::DeclareStmt<'arena, 'src>,
ctx: &mut crate::context::Context,
) {
for (name, _val) in d.directives.iter() {
if *name == "strict_types" {
ctx.strict_types = true;
}
}
if let Some(body) = &d.body {
self.analyze_stmt(body, ctx);
}
}
}