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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
/// Statement analyzer — walks statement nodes threading context through
/// control flow (if/else, loops, try/catch, return).
mod control_flow;
mod declarations;
mod expressions;
mod flow;
mod loops;
mod return_type;
use loops::{vars_stabilized, widen_unstable};
pub(crate) use return_type::named_object_return_compatible;
use return_type::resolve_union_for_file;
use std::sync::Arc;
use php_ast::ast::StmtKind;
use mir_issues::{Issue, IssueBuffer, IssueKind, Location};
use mir_types::Union;
use crate::context::Context;
use crate::db::MirDatabase;
use crate::expr::ExpressionAnalyzer;
use crate::php_version::PhpVersion;
use crate::symbol::ResolvedSymbol;
// ---------------------------------------------------------------------------
// StatementsAnalyzer
// ---------------------------------------------------------------------------
pub struct StatementsAnalyzer<'a> {
pub db: &'a dyn MirDatabase,
pub file: Arc<str>,
pub source: &'a str,
pub source_map: &'a php_rs_parser::source_map::SourceMap,
pub issues: &'a mut IssueBuffer,
pub symbols: &'a mut Vec<ResolvedSymbol>,
pub php_version: PhpVersion,
pub inference_only: bool,
/// Accumulated inferred return types for the current function.
pub return_types: Vec<Union>,
/// Break-context stack: one entry per active loop nesting level.
/// Each entry collects the context states at every `break` in that loop.
break_ctx_stack: Vec<Vec<Context>>,
}
impl<'a> StatementsAnalyzer<'a> {
#[allow(clippy::too_many_arguments)]
pub fn new(
db: &'a dyn MirDatabase,
file: Arc<str>,
source: &'a str,
source_map: &'a php_rs_parser::source_map::SourceMap,
issues: &'a mut IssueBuffer,
symbols: &'a mut Vec<ResolvedSymbol>,
php_version: PhpVersion,
inference_only: bool,
) -> Self {
Self {
db,
file,
source,
source_map,
issues,
symbols,
php_version,
inference_only,
return_types: Vec::new(),
break_ctx_stack: Vec::new(),
}
}
pub fn analyze_stmts<'arena, 'src>(
&mut self,
stmts: &php_ast::ast::ArenaVec<'arena, php_ast::ast::Stmt<'arena, 'src>>,
ctx: &mut Context,
) {
for stmt in stmts.iter() {
// @psalm-suppress / @suppress per-statement (call-site suppression)
let suppressions = self.extract_statement_suppressions(stmt.span);
let before = self.issues.issue_count();
if ctx.diverges {
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 + 1)
};
self.issues.add(
Issue::new(
IssueKind::UnreachableCode,
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(),
),
);
if !suppressions.is_empty() {
self.issues.suppress_range(before, &suppressions);
}
break;
}
// Extract @var annotation for this statement.
let var_annotation = self.extract_var_annotation(stmt.span);
// Pre-narrow: `@var Type $varname` before any statement narrows that variable.
// Special cases: before `return` or before `foreach ... as $valvar` (value override).
if let Some((Some(ref var_name), ref var_ty)) = var_annotation {
ctx.set_var(var_name.as_str(), var_ty.clone());
}
self.analyze_stmt(stmt, ctx);
// Post-narrow: `@var Type $varname` before `$varname = expr()` overrides
// the inferred type with the annotated type. Only applies when the assignment
// target IS the annotated variable.
if let Some((Some(ref var_name), ref var_ty)) = var_annotation {
if let php_ast::ast::StmtKind::Expression(e) = &stmt.kind {
if let php_ast::ast::ExprKind::Assign(a) = &e.kind {
if matches!(&a.op, php_ast::ast::AssignOp::Assign) {
if let php_ast::ast::ExprKind::Variable(lhs_name) = &a.target.kind {
let lhs = lhs_name.trim_start_matches('$');
if lhs == var_name.as_str() {
ctx.set_var(var_name.as_str(), var_ty.clone());
}
}
}
}
}
}
if !suppressions.is_empty() {
self.issues.suppress_range(before, &suppressions);
}
}
}
pub fn analyze_stmt<'arena, 'src>(
&mut self,
stmt: &php_ast::ast::Stmt<'arena, 'src>,
ctx: &mut Context,
) {
match &stmt.kind {
// ---- Expression statement ----------------------------------------
StmtKind::Expression(expr) => {
self.analyze_expression_stmt(expr, ctx);
}
// ---- Echo ---------------------------------------------------------
StmtKind::Echo(exprs) => {
self.analyze_echo_stmt(exprs, stmt.span, ctx);
}
// ---- Return -------------------------------------------------------
StmtKind::Return(opt_expr) => {
self.analyze_return_stmt(opt_expr, stmt.span, ctx);
}
// ---- Throw --------------------------------------------------------
StmtKind::Throw(expr) => {
self.analyze_throw_stmt(expr, stmt.span, ctx);
}
// ---- If -----------------------------------------------------------
StmtKind::If(if_stmt) => {
self.analyze_if_stmt(if_stmt, ctx);
}
// ---- While --------------------------------------------------------
StmtKind::While(w) => {
self.analyze_while_stmt(w, ctx);
}
// ---- Do-while -----------------------------------------------------
StmtKind::DoWhile(dw) => {
self.analyze_dowhile_stmt(dw, ctx);
}
// ---- For ----------------------------------------------------------
StmtKind::For(f) => {
self.analyze_for_stmt(f, ctx);
}
// ---- Foreach ------------------------------------------------------
StmtKind::Foreach(fe) => {
self.analyze_foreach_stmt(fe, stmt.span, ctx);
}
// ---- Switch -------------------------------------------------------
StmtKind::Switch(sw) => {
self.analyze_switch_stmt(sw, ctx);
}
// ---- Try/catch/finally -------------------------------------------
StmtKind::TryCatch(tc) => {
self.analyze_trycatch_stmt(tc, ctx);
}
// ---- Block --------------------------------------------------------
StmtKind::Block(stmts) => {
self.analyze_stmts(stmts, ctx);
}
// ---- Break --------------------------------------------------------
StmtKind::Break(_) => {
self.analyze_break_stmt(ctx);
}
// ---- Continue ----------------------------------------------------
StmtKind::Continue(_) => {
self.analyze_continue_stmt(ctx);
}
// ---- Unset --------------------------------------------------------
StmtKind::Unset(vars) => {
self.analyze_unset_stmt(vars, ctx);
}
// ---- Static variable declaration ---------------------------------
StmtKind::StaticVar(vars) => {
self.analyze_static_var_stmt(vars, ctx);
}
// ---- Global declaration ------------------------------------------
StmtKind::Global(vars) => {
self.analyze_global_stmt(vars, ctx);
}
// ---- Declare -----------------------------------------------------
StmtKind::Declare(d) => {
self.analyze_declare_stmt(d, ctx);
}
// ---- Nested declarations (inside function bodies) ----------------
StmtKind::Function(decl) => {
self.analyze_function_decl_stmt(decl, ctx);
}
StmtKind::Class(decl) => {
self.analyze_class_decl_stmt(decl, ctx);
}
StmtKind::Interface(_) | StmtKind::Trait(_) | StmtKind::Enum(_) => {
// Interfaces/traits/enums are collected in Pass 1 — skip here
}
// ---- Namespace / use (at file level, already handled in Pass 1) --
StmtKind::Namespace(_) | StmtKind::Use(_) | StmtKind::Const(_) => {}
// ---- Inert --------------------------------------------------------
StmtKind::InlineHtml(_)
| StmtKind::Nop
| StmtKind::Goto(_)
| StmtKind::Label(_)
| StmtKind::HaltCompiler(_) => {}
StmtKind::Error => {}
}
}
// -----------------------------------------------------------------------
// Helper: create a short-lived ExpressionAnalyzer borrowing our fields
// -----------------------------------------------------------------------
fn expr_analyzer<'b>(&'b mut self, _ctx: &Context) -> ExpressionAnalyzer<'b>
where
'a: 'b,
{
ExpressionAnalyzer::new(
self.db,
self.file.clone(),
self.source,
self.source_map,
self.issues,
self.symbols,
self.php_version,
self.inference_only,
)
}
/// Convert a byte offset to a Unicode char-count column on a given line.
/// Returns (line, col) where col is a 0-based Unicode code-point count.
fn offset_to_line_col(&self, offset: u32) -> (u32, u16) {
let lc = self.source_map.offset_to_line_col(offset);
let line = lc.line + 1;
let byte_offset = offset as usize;
let line_start_byte = if byte_offset == 0 {
0
} else {
self.source[..byte_offset]
.rfind('\n')
.map(|p| p + 1)
.unwrap_or(0)
};
let col = self.source[line_start_byte..byte_offset].chars().count() as u16;
(line, col)
}
/// Emit `UndefinedClass` for a `Name` AST node if the resolved class does not exist.
fn check_name_undefined_class(&mut self, name: &php_ast::ast::Name<'_, '_>) {
let raw = crate::parser::name_to_string(name);
let resolved = crate::db::resolve_name_via_db(self.db, &self.file, &raw);
if matches!(resolved.as_str(), "self" | "static" | "parent") {
return;
}
if crate::db::type_exists_via_db(self.db, &resolved) {
return;
}
let span = name.span();
let (line, col_start) = self.offset_to_line_col(span.start);
let (line_end, col_end) = self.offset_to_line_col(span.end);
self.issues.add(Issue::new(
IssueKind::UndefinedClass { name: resolved },
Location {
file: self.file.clone(),
line,
line_end,
col_start,
col_end: col_end.max(col_start + 1),
},
));
}
// -----------------------------------------------------------------------
// @psalm-suppress / @suppress per-statement
// -----------------------------------------------------------------------
/// Extract suppression names from the `@psalm-suppress` / `@suppress`
/// annotation in the docblock immediately preceding `span`.
fn extract_statement_suppressions(&self, span: php_ast::Span) -> Vec<String> {
let Some(doc) = crate::parser::find_preceding_docblock(self.source, span.start) else {
return vec![];
};
let mut suppressions = Vec::new();
for line in doc.lines() {
let line = line.trim().trim_start_matches('*').trim();
let rest = if let Some(r) = line.strip_prefix("@psalm-suppress ") {
r
} else if let Some(r) = line.strip_prefix("@suppress ") {
r
} else {
continue;
};
for name in rest.split_whitespace() {
suppressions.push(name.to_string());
}
}
suppressions
}
/// Extract `@var Type [$varname]` from the docblock immediately preceding `span`.
/// Returns `(optional_var_name, resolved_type)` if an annotation exists.
/// The type is resolved through the codebase's file-level imports/namespace.
fn extract_var_annotation(
&self,
span: php_ast::Span,
) -> Option<(Option<String>, mir_types::Union)> {
let doc = crate::parser::find_preceding_docblock(self.source, span.start)?;
let parsed = crate::parser::DocblockParser::parse(&doc);
let ty = parsed.var_type?;
let resolved = resolve_union_for_file(ty, self.db, &self.file);
Some((parsed.var_name, resolved))
}
// -----------------------------------------------------------------------
// Fixed-point loop widening (M12)
// -----------------------------------------------------------------------
/// Analyse a loop body with a fixed-point widening algorithm (≤ 3 passes).
///
/// * `pre` — context *before* the loop (used as the merge base)
/// * `entry` — context on first iteration entry (may be narrowed / seeded)
/// * `body` — closure that analyses one loop iteration, receives `&mut Self`
/// and `&mut Context` for the current iteration context
/// * `loop_guaranteed` — whether the loop is guaranteed to execute at least once
///
/// Returns the post-loop context that merges:
/// - the stable widened context after normal loop exit
/// - any contexts captured at `break` statements
fn analyze_loop_widened<F>(
&mut self,
pre: &Context,
entry: Context,
mut body: F,
loop_guaranteed: bool,
) -> Context
where
F: FnMut(&mut Self, &mut Context),
{
const MAX_ITERS: usize = 3;
// Push a fresh break-context bucket for this loop level
self.break_ctx_stack.push(Vec::new());
let mut current = entry;
current.inside_loop = true;
for _ in 0..MAX_ITERS {
let prev_vars = current.vars.clone();
let mut iter = current.clone();
body(self, &mut iter);
let next = Context::merge_branches(pre, iter, None);
if vars_stabilized(&prev_vars, &next.vars) {
current = next;
break;
}
current = next;
}
// Widen any variable still unstable after MAX_ITERS to the union of types
widen_unstable(&pre.vars, &mut current.vars, loop_guaranteed);
// Pop break contexts and merge them into the post-loop result
let break_ctxs = self.break_ctx_stack.pop().unwrap_or_default();
for bctx in break_ctxs {
current = Context::merge_branches(pre, current, Some(bctx));
}
current
}
}