mir-analyzer 0.17.3

Analysis engine for the mir PHP static analyzer
Documentation
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
use std::sync::Arc;

use php_ast::ast::ExprKind;

use mir_issues::{Issue, IssueKind, Location};
use mir_types::{Atomic, Union};

use crate::context::Context;
use crate::db;
use crate::expr::{extract_destructure_vars, extract_simple_var};
use crate::narrowing::narrow_from_condition;
use crate::parser;

use super::loops::infer_foreach_types;
use super::StatementsAnalyzer;

impl<'a> StatementsAnalyzer<'a> {
    pub(super) fn analyze_if_stmt<'arena, 'src>(
        &mut self,
        if_stmt: &php_ast::ast::IfStmt<'arena, 'src>,
        ctx: &mut Context,
    ) {
        let pre_ctx = ctx.clone();

        let cond_type = self.expr_analyzer(ctx).analyze(&if_stmt.condition, ctx);
        let pre_diverges = ctx.diverges;

        let mut then_ctx = ctx.fork();
        narrow_from_condition(&if_stmt.condition, &mut then_ctx, true, self.db, &self.file);
        let then_unreachable_from_narrowing = then_ctx.diverges;
        if !then_ctx.diverges {
            self.analyze_stmt(if_stmt.then_branch, &mut then_ctx);
        }

        let mut elseif_ctxs: Vec<Context> = vec![];
        for elseif in if_stmt.elseif_branches.iter() {
            let mut pre_elseif = ctx.fork();
            narrow_from_condition(
                &if_stmt.condition,
                &mut pre_elseif,
                false,
                self.db,
                &self.file,
            );
            let pre_elseif_diverges = pre_elseif.diverges;

            let mut elseif_true_ctx = pre_elseif.clone();
            narrow_from_condition(
                &elseif.condition,
                &mut elseif_true_ctx,
                true,
                self.db,
                &self.file,
            );
            let mut elseif_false_ctx = pre_elseif.clone();
            narrow_from_condition(
                &elseif.condition,
                &mut elseif_false_ctx,
                false,
                self.db,
                &self.file,
            );
            if !pre_elseif_diverges && (elseif_true_ctx.diverges || elseif_false_ctx.diverges) {
                let (line, col_start) = self.offset_to_line_col(elseif.condition.span.start);
                let (line_end, col_end) = if elseif.condition.span.start < elseif.condition.span.end
                {
                    let (end_line, end_col) = self.offset_to_line_col(elseif.condition.span.end);
                    (end_line, end_col)
                } else {
                    (line, col_start)
                };
                let elseif_cond_type = self
                    .expr_analyzer(ctx)
                    .analyze(&elseif.condition, &mut ctx.fork());
                self.issues.add(
                    mir_issues::Issue::new(
                        IssueKind::RedundantCondition {
                            ty: format!("{elseif_cond_type}"),
                        },
                        mir_issues::Location {
                            file: self.file.clone(),
                            line,
                            line_end,
                            col_start,
                            col_end: col_end.max(col_start + 1),
                        },
                    )
                    .with_snippet(
                        parser::span_text(self.source, elseif.condition.span).unwrap_or_default(),
                    ),
                );
            }

            let mut branch_ctx = elseif_true_ctx;
            self.expr_analyzer(&branch_ctx)
                .analyze(&elseif.condition, &mut branch_ctx);
            if !branch_ctx.diverges {
                self.analyze_stmt(&elseif.body, &mut branch_ctx);
            }
            elseif_ctxs.push(branch_ctx);
        }

        let mut else_ctx = ctx.fork();
        narrow_from_condition(
            &if_stmt.condition,
            &mut else_ctx,
            false,
            self.db,
            &self.file,
        );
        let else_unreachable_from_narrowing = else_ctx.diverges;
        if !else_ctx.diverges {
            if let Some(else_branch) = &if_stmt.else_branch {
                self.analyze_stmt(else_branch, &mut else_ctx);
            }
        }

        if !pre_diverges && (then_unreachable_from_narrowing || else_unreachable_from_narrowing) {
            let (line, col_start) = self.offset_to_line_col(if_stmt.condition.span.start);
            let (line_end, col_end) = if if_stmt.condition.span.start < if_stmt.condition.span.end {
                let (end_line, end_col) = self.offset_to_line_col(if_stmt.condition.span.end);
                (end_line, end_col)
            } else {
                (line, col_start)
            };
            self.issues.add(
                Issue::new(
                    IssueKind::RedundantCondition {
                        ty: format!("{cond_type}"),
                    },
                    Location {
                        file: self.file.clone(),
                        line,
                        line_end,
                        col_start,
                        col_end: col_end.max(col_start + 1),
                    },
                )
                .with_snippet(
                    parser::span_text(self.source, if_stmt.condition.span).unwrap_or_default(),
                ),
            );
        }

        *ctx = Context::merge_branches(&pre_ctx, then_ctx, Some(else_ctx));
        for ec in elseif_ctxs {
            *ctx = Context::merge_branches(&pre_ctx, ec, Some(ctx.clone()));
        }
    }

    pub(super) fn analyze_while_stmt<'arena, 'src>(
        &mut self,
        w: &php_ast::ast::WhileStmt<'arena, 'src>,
        ctx: &mut Context,
    ) {
        self.expr_analyzer(ctx).analyze(&w.condition, ctx);
        let pre = ctx.clone();

        let mut entry = ctx.fork();
        narrow_from_condition(&w.condition, &mut entry, true, self.db, &self.file);

        let post = self.analyze_loop_widened(&pre, entry, |sa, iter| {
            sa.analyze_stmt(w.body, iter);
            sa.expr_analyzer(iter).analyze(&w.condition, iter);
        });
        *ctx = post;
    }

    pub(super) fn analyze_dowhile_stmt<'arena, 'src>(
        &mut self,
        dw: &php_ast::ast::DoWhileStmt<'arena, 'src>,
        ctx: &mut Context,
    ) {
        let pre = ctx.clone();
        let entry = ctx.fork();
        let post = self.analyze_loop_widened(&pre, entry, |sa, iter| {
            sa.analyze_stmt(dw.body, iter);
            sa.expr_analyzer(iter).analyze(&dw.condition, iter);
        });
        *ctx = post;
    }

    pub(super) fn analyze_for_stmt<'arena, 'src>(
        &mut self,
        f: &php_ast::ast::ForStmt<'arena, 'src>,
        ctx: &mut Context,
    ) {
        for init in f.init.iter() {
            self.expr_analyzer(ctx).analyze(init, ctx);
        }
        let pre = ctx.clone();
        let mut entry = ctx.fork();
        for cond in f.condition.iter() {
            self.expr_analyzer(&entry).analyze(cond, &mut entry);
        }

        let post = self.analyze_loop_widened(&pre, entry, |sa, iter| {
            sa.analyze_stmt(f.body, iter);
            for update in f.update.iter() {
                sa.expr_analyzer(iter).analyze(update, iter);
            }
            for cond in f.condition.iter() {
                sa.expr_analyzer(iter).analyze(cond, iter);
            }
        });
        *ctx = post;
    }

    pub(super) fn analyze_foreach_stmt<'arena, 'src>(
        &mut self,
        fe: &php_ast::ast::ForeachStmt<'arena, 'src>,
        stmt_span: php_ast::Span,
        ctx: &mut Context,
    ) {
        let arr_ty = self.expr_analyzer(ctx).analyze(&fe.expr, ctx);
        let (key_ty, mut value_ty) = infer_foreach_types(&arr_ty);

        if let Some(vname) = extract_simple_var(&fe.value) {
            if let Some((Some(ann_var), ann_ty)) = self.extract_var_annotation(stmt_span) {
                if ann_var == vname {
                    value_ty = ann_ty;
                }
            }
        }

        let pre = ctx.clone();
        let mut entry = ctx.fork();

        if let Some(key_expr) = &fe.key {
            if let Some(var_name) = extract_simple_var(key_expr) {
                entry.set_var(var_name, key_ty.clone());
            }
        }
        let value_var = extract_simple_var(&fe.value);
        let value_destructure_vars = extract_destructure_vars(&fe.value);
        if let Some(ref vname) = value_var {
            entry.set_var(vname.as_str(), value_ty.clone());
        } else {
            for vname in &value_destructure_vars {
                entry.set_var(vname, Union::mixed());
            }
        }

        let post = self.analyze_loop_widened(&pre, entry, |sa, iter| {
            if let Some(key_expr) = &fe.key {
                if let Some(var_name) = extract_simple_var(key_expr) {
                    iter.set_var(var_name, key_ty.clone());
                }
            }
            if let Some(ref vname) = value_var {
                iter.set_var(vname.as_str(), value_ty.clone());
            } else {
                for vname in &value_destructure_vars {
                    iter.set_var(vname, Union::mixed());
                }
            }
            sa.analyze_stmt(fe.body, iter);
        });
        *ctx = post;
    }

    pub(super) fn analyze_switch_stmt<'arena, 'src>(
        &mut self,
        sw: &php_ast::ast::SwitchStmt<'arena, 'src>,
        ctx: &mut Context,
    ) {
        let _subject_ty = self.expr_analyzer(ctx).analyze(&sw.expr, ctx);
        let subject_var: Option<String> = match &sw.expr.kind {
            ExprKind::Variable(name) => Some(name.as_str().trim_start_matches('$').to_string()),
            _ => None,
        };
        let switch_on_true = matches!(&sw.expr.kind, ExprKind::Bool(true));

        let pre_ctx = ctx.clone();
        self.break_ctx_stack.push(Vec::new());

        let has_default = sw.cases.iter().any(|c| c.value.is_none());

        let mut case_results: Vec<Context> = Vec::new();
        for case in sw.cases.iter() {
            let mut case_ctx = pre_ctx.fork();
            if let Some(val) = &case.value {
                if switch_on_true {
                    narrow_from_condition(val, &mut case_ctx, true, self.db, &self.file);
                } else if let Some(ref var_name) = subject_var {
                    let narrow_ty = match &val.kind {
                        ExprKind::Int(n) => Some(Union::single(Atomic::TLiteralInt(*n))),
                        ExprKind::String(s) => {
                            Some(Union::single(Atomic::TLiteralString(Arc::from(&**s))))
                        }
                        ExprKind::Bool(b) => Some(Union::single(if *b {
                            Atomic::TTrue
                        } else {
                            Atomic::TFalse
                        })),
                        ExprKind::Null => Some(Union::single(Atomic::TNull)),
                        _ => None,
                    };
                    if let Some(narrowed) = narrow_ty {
                        case_ctx.set_var(var_name, narrowed);
                    }
                }
                self.expr_analyzer(&case_ctx).analyze(val, &mut case_ctx);
            }
            self.analyze_stmts(&case.body, &mut case_ctx);
            case_results.push(case_ctx);
        }

        let n = case_results.len();
        let mut effective_diverges = vec![false; n];
        for i in (0..n).rev() {
            if case_results[i].diverges {
                effective_diverges[i] = true;
            } else if i + 1 < n {
                effective_diverges[i] = effective_diverges[i + 1];
            }
        }

        let mut all_cases_diverge = true;
        let mut fallthrough_ctxs: Vec<Context> = Vec::new();
        for (i, case_ctx) in case_results.into_iter().enumerate() {
            if !effective_diverges[i] {
                all_cases_diverge = false;
                fallthrough_ctxs.push(case_ctx);
            }
        }

        let break_ctxs = self.break_ctx_stack.pop().unwrap_or_default();

        let mut merged = if has_default
            && all_cases_diverge
            && break_ctxs.is_empty()
            && fallthrough_ctxs.is_empty()
        {
            let mut m = pre_ctx.clone();
            m.diverges = true;
            m
        } else {
            pre_ctx.clone()
        };

        for bctx in break_ctxs {
            merged = Context::merge_branches(&pre_ctx, bctx, Some(merged));
        }
        for fctx in fallthrough_ctxs {
            merged = Context::merge_branches(&pre_ctx, fctx, Some(merged));
        }

        *ctx = merged;
    }

    pub(super) fn analyze_trycatch_stmt<'arena, 'src>(
        &mut self,
        tc: &php_ast::ast::TryCatchStmt<'arena, 'src>,
        ctx: &mut Context,
    ) {
        let pre_ctx = ctx.clone();
        let mut try_ctx = ctx.fork();
        self.analyze_stmts(&tc.body, &mut try_ctx);

        let catch_base = Context::merge_branches(&pre_ctx, try_ctx.clone(), None);

        let mut non_diverging_catches: Vec<Context> = vec![];
        for catch in tc.catches.iter() {
            let mut catch_ctx = catch_base.clone();
            for catch_ty in catch.types.iter() {
                self.check_name_undefined_class(catch_ty);
            }
            if let Some(var) = catch.var {
                let exc_ty = if catch.types.is_empty() {
                    Union::single(Atomic::TObject)
                } else {
                    let mut u = Union::empty();
                    for catch_ty in catch.types.iter() {
                        let raw = parser::name_to_string(catch_ty);
                        let resolved = db::resolve_name_via_db(self.db, &self.file, &raw);
                        u.add_type(Atomic::TNamedObject {
                            fqcn: resolved.into(),
                            type_params: vec![],
                        });
                    }
                    u
                };
                catch_ctx.set_var(var.trim_start_matches('$'), exc_ty);
            }
            self.analyze_stmts(&catch.body, &mut catch_ctx);
            if !catch_ctx.diverges {
                non_diverging_catches.push(catch_ctx);
            }
        }

        let mut result = if non_diverging_catches.is_empty() {
            let mut r = try_ctx;
            r.diverges = false;
            r
        } else {
            let mut r = try_ctx;
            for catch_ctx in non_diverging_catches {
                r = Context::merge_branches(&pre_ctx, r, Some(catch_ctx));
            }
            r
        };

        if let Some(finally_stmts) = &tc.finally {
            let mut finally_ctx = result.clone();
            finally_ctx.inside_finally = true;
            self.analyze_stmts(finally_stmts, &mut finally_ctx);
            if finally_ctx.diverges {
                result.diverges = true;
            }
        }

        *ctx = result;
    }
}