mir-issues 0.1.0

Issue definitions and reporting 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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
use std::fmt;
use std::sync::Arc;

use owo_colors::OwoColorize;
use serde::{Deserialize, Serialize};

// ---------------------------------------------------------------------------
// Severity
// ---------------------------------------------------------------------------

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
pub enum Severity {
    /// Only shown with `--show-info`
    Info,
    /// Warnings — shown at default level
    Warning,
    /// Errors — always shown; non-zero exit code
    Error,
}

impl fmt::Display for Severity {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Severity::Info => write!(f, "info"),
            Severity::Warning => write!(f, "warning"),
            Severity::Error => write!(f, "error"),
        }
    }
}

// ---------------------------------------------------------------------------
// Location
// ---------------------------------------------------------------------------

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Location {
    pub file: Arc<str>,
    pub line: u32,
    pub col_start: u16,
    pub col_end: u16,
}

impl fmt::Display for Location {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}:{}:{}", self.file, self.line, self.col_start)
    }
}

// ---------------------------------------------------------------------------
// IssueKind
// ---------------------------------------------------------------------------

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub enum IssueKind {
    // --- Undefined ----------------------------------------------------------
    UndefinedVariable { name: String },
    UndefinedFunction { name: String },
    UndefinedMethod { class: String, method: String },
    UndefinedClass { name: String },
    UndefinedProperty { class: String, property: String },
    UndefinedConstant { name: String },
    PossiblyUndefinedVariable { name: String },

    // --- Nullability --------------------------------------------------------
    NullArgument { param: String, fn_name: String },
    NullPropertyFetch { property: String },
    NullMethodCall { method: String },
    NullArrayAccess,
    PossiblyNullArgument { param: String, fn_name: String },
    PossiblyNullPropertyFetch { property: String },
    PossiblyNullMethodCall { method: String },
    PossiblyNullArrayAccess,
    NullableReturnStatement { expected: String, actual: String },

    // --- Type mismatches ----------------------------------------------------
    InvalidReturnType { expected: String, actual: String },
    InvalidArgument { param: String, fn_name: String, expected: String, actual: String },
    InvalidPropertyAssignment { property: String, expected: String, actual: String },
    InvalidCast { from: String, to: String },
    InvalidOperand { op: String, left: String, right: String },
    MismatchingDocblockReturnType { declared: String, inferred: String },
    MismatchingDocblockParamType { param: String, declared: String, inferred: String },

    // --- Array issues -------------------------------------------------------
    InvalidArrayOffset { expected: String, actual: String },
    NonExistentArrayOffset { key: String },
    PossiblyInvalidArrayOffset { expected: String, actual: String },

    // --- Redundancy ---------------------------------------------------------
    RedundantCondition { ty: String },
    RedundantCast { from: String, to: String },
    UnnecessaryVarAnnotation { var: String },
    TypeDoesNotContainType { left: String, right: String },

    // --- Dead code ----------------------------------------------------------
    UnusedVariable { name: String },
    UnusedParam { name: String },
    UnreachableCode,
    UnusedMethod { class: String, method: String },
    UnusedProperty { class: String, property: String },
    UnusedFunction { name: String },

    // --- Readonly -----------------------------------------------------------
    ReadonlyPropertyAssignment { class: String, property: String },

    // --- Inheritance --------------------------------------------------------
    UnimplementedAbstractMethod { class: String, method: String },
    UnimplementedInterfaceMethod { class: String, interface: String, method: String },
    MethodSignatureMismatch { class: String, method: String, detail: String },
    OverriddenMethodAccess { class: String, method: String },
    FinalClassExtended { parent: String, child: String },
    FinalMethodOverridden { class: String, method: String, parent: String },

    // --- Security (taint) ---------------------------------------------------
    TaintedInput { sink: String },
    TaintedHtml,
    TaintedSql,
    TaintedShell,

    // --- Generics -----------------------------------------------------------
    InvalidTemplateParam { name: String, expected_bound: String, actual: String },

    // --- Other --------------------------------------------------------------
    DeprecatedMethod { class: String, method: String },
    DeprecatedClass { name: String },
    InternalMethod { class: String, method: String },
    MissingReturnType { fn_name: String },
    MissingParamType { fn_name: String, param: String },
    InvalidThrow { ty: String },
    MissingThrowsDocblock { class: String },
    ParseError { message: String },
    InvalidDocblock { message: String },
    MixedArgument { param: String, fn_name: String },
    MixedAssignment { var: String },
    MixedMethodCall { method: String },
    MixedPropertyFetch { property: String },
}

impl IssueKind {
    /// Default severity for this issue kind.
    pub fn default_severity(&self) -> Severity {
        match self {
            // Errors (always blocking)
            IssueKind::UndefinedVariable { .. }
            | IssueKind::UndefinedFunction { .. }
            | IssueKind::UndefinedMethod { .. }
            | IssueKind::UndefinedClass { .. }
            | IssueKind::UndefinedConstant { .. }
            | IssueKind::InvalidReturnType { .. }
            | IssueKind::InvalidArgument { .. }
            | IssueKind::InvalidThrow { .. }
            | IssueKind::UnimplementedAbstractMethod { .. }
            | IssueKind::UnimplementedInterfaceMethod { .. }
            | IssueKind::MethodSignatureMismatch { .. }
            | IssueKind::FinalClassExtended { .. }
            | IssueKind::FinalMethodOverridden { .. }
            | IssueKind::InvalidTemplateParam { .. }
            | IssueKind::ReadonlyPropertyAssignment { .. }
            | IssueKind::ParseError { .. }
            | IssueKind::TaintedInput { .. }
            | IssueKind::TaintedHtml
            | IssueKind::TaintedSql
            | IssueKind::TaintedShell => Severity::Error,

            // Warnings (shown at default error level)
            IssueKind::NullArgument { .. }
            | IssueKind::NullPropertyFetch { .. }
            | IssueKind::NullMethodCall { .. }
            | IssueKind::NullArrayAccess
            | IssueKind::NullableReturnStatement { .. }
            | IssueKind::InvalidPropertyAssignment { .. }
            | IssueKind::InvalidArrayOffset { .. }
            | IssueKind::NonExistentArrayOffset { .. }
            | IssueKind::PossiblyInvalidArrayOffset { .. }
            | IssueKind::UndefinedProperty { .. }
            | IssueKind::InvalidOperand { .. }
            | IssueKind::OverriddenMethodAccess { .. }
            | IssueKind::MissingThrowsDocblock { .. } => Severity::Warning,

            // Possibly-null / possibly-undefined (only shown in strict mode, level ≥ 7)
            IssueKind::PossiblyUndefinedVariable { .. }
            | IssueKind::PossiblyNullArgument { .. }
            | IssueKind::PossiblyNullPropertyFetch { .. }
            | IssueKind::PossiblyNullMethodCall { .. }
            | IssueKind::PossiblyNullArrayAccess => Severity::Info,

            // Info
            IssueKind::RedundantCondition { .. }
            | IssueKind::RedundantCast { .. }
            | IssueKind::UnnecessaryVarAnnotation { .. }
            | IssueKind::TypeDoesNotContainType { .. }
            | IssueKind::UnusedVariable { .. }
            | IssueKind::UnusedParam { .. }
            | IssueKind::UnreachableCode
            | IssueKind::UnusedMethod { .. }
            | IssueKind::UnusedProperty { .. }
            | IssueKind::UnusedFunction { .. }
            | IssueKind::DeprecatedMethod { .. }
            | IssueKind::DeprecatedClass { .. }
            | IssueKind::InternalMethod { .. }
            | IssueKind::MissingReturnType { .. }
            | IssueKind::MissingParamType { .. }
            | IssueKind::MismatchingDocblockReturnType { .. }
            | IssueKind::MismatchingDocblockParamType { .. }
            | IssueKind::InvalidDocblock { .. }
            | IssueKind::InvalidCast { .. }
            | IssueKind::MixedArgument { .. }
            | IssueKind::MixedAssignment { .. }
            | IssueKind::MixedMethodCall { .. }
            | IssueKind::MixedPropertyFetch { .. } => Severity::Info,
        }
    }

    /// Identifier name used in config and `@psalm-suppress` / `@suppress` annotations.
    pub fn name(&self) -> &'static str {
        match self {
            IssueKind::UndefinedVariable { .. } => "UndefinedVariable",
            IssueKind::UndefinedFunction { .. } => "UndefinedFunction",
            IssueKind::UndefinedMethod { .. } => "UndefinedMethod",
            IssueKind::UndefinedClass { .. } => "UndefinedClass",
            IssueKind::UndefinedProperty { .. } => "UndefinedProperty",
            IssueKind::UndefinedConstant { .. } => "UndefinedConstant",
            IssueKind::PossiblyUndefinedVariable { .. } => "PossiblyUndefinedVariable",
            IssueKind::NullArgument { .. } => "NullArgument",
            IssueKind::NullPropertyFetch { .. } => "NullPropertyFetch",
            IssueKind::NullMethodCall { .. } => "NullMethodCall",
            IssueKind::NullArrayAccess => "NullArrayAccess",
            IssueKind::PossiblyNullArgument { .. } => "PossiblyNullArgument",
            IssueKind::PossiblyNullPropertyFetch { .. } => "PossiblyNullPropertyFetch",
            IssueKind::PossiblyNullMethodCall { .. } => "PossiblyNullMethodCall",
            IssueKind::PossiblyNullArrayAccess => "PossiblyNullArrayAccess",
            IssueKind::NullableReturnStatement { .. } => "NullableReturnStatement",
            IssueKind::InvalidReturnType { .. } => "InvalidReturnType",
            IssueKind::InvalidArgument { .. } => "InvalidArgument",
            IssueKind::InvalidPropertyAssignment { .. } => "InvalidPropertyAssignment",
            IssueKind::InvalidCast { .. } => "InvalidCast",
            IssueKind::InvalidOperand { .. } => "InvalidOperand",
            IssueKind::MismatchingDocblockReturnType { .. } => "MismatchingDocblockReturnType",
            IssueKind::MismatchingDocblockParamType { .. } => "MismatchingDocblockParamType",
            IssueKind::InvalidArrayOffset { .. } => "InvalidArrayOffset",
            IssueKind::NonExistentArrayOffset { .. } => "NonExistentArrayOffset",
            IssueKind::PossiblyInvalidArrayOffset { .. } => "PossiblyInvalidArrayOffset",
            IssueKind::RedundantCondition { .. } => "RedundantCondition",
            IssueKind::RedundantCast { .. } => "RedundantCast",
            IssueKind::UnnecessaryVarAnnotation { .. } => "UnnecessaryVarAnnotation",
            IssueKind::TypeDoesNotContainType { .. } => "TypeDoesNotContainType",
            IssueKind::UnusedVariable { .. } => "UnusedVariable",
            IssueKind::UnusedParam { .. } => "UnusedParam",
            IssueKind::UnreachableCode => "UnreachableCode",
            IssueKind::UnusedMethod { .. } => "UnusedMethod",
            IssueKind::UnusedProperty { .. } => "UnusedProperty",
            IssueKind::UnusedFunction { .. } => "UnusedFunction",
            IssueKind::UnimplementedAbstractMethod { .. } => "UnimplementedAbstractMethod",
            IssueKind::UnimplementedInterfaceMethod { .. } => "UnimplementedInterfaceMethod",
            IssueKind::MethodSignatureMismatch { .. } => "MethodSignatureMismatch",
            IssueKind::OverriddenMethodAccess { .. } => "OverriddenMethodAccess",
            IssueKind::FinalClassExtended { .. } => "FinalClassExtended",
            IssueKind::FinalMethodOverridden { .. } => "FinalMethodOverridden",
            IssueKind::ReadonlyPropertyAssignment { .. } => "ReadonlyPropertyAssignment",
            IssueKind::InvalidTemplateParam { .. } => "InvalidTemplateParam",
            IssueKind::TaintedInput { .. } => "TaintedInput",
            IssueKind::TaintedHtml => "TaintedHtml",
            IssueKind::TaintedSql => "TaintedSql",
            IssueKind::TaintedShell => "TaintedShell",
            IssueKind::DeprecatedMethod { .. } => "DeprecatedMethod",
            IssueKind::DeprecatedClass { .. } => "DeprecatedClass",
            IssueKind::InternalMethod { .. } => "InternalMethod",
            IssueKind::MissingReturnType { .. } => "MissingReturnType",
            IssueKind::MissingParamType { .. } => "MissingParamType",
            IssueKind::InvalidThrow { .. } => "InvalidThrow",
            IssueKind::MissingThrowsDocblock { .. } => "MissingThrowsDocblock",
            IssueKind::ParseError { .. } => "ParseError",
            IssueKind::InvalidDocblock { .. } => "InvalidDocblock",
            IssueKind::MixedArgument { .. } => "MixedArgument",
            IssueKind::MixedAssignment { .. } => "MixedAssignment",
            IssueKind::MixedMethodCall { .. } => "MixedMethodCall",
            IssueKind::MixedPropertyFetch { .. } => "MixedPropertyFetch",
        }
    }

    /// Human-readable message for this issue.
    pub fn message(&self) -> String {
        match self {
            IssueKind::UndefinedVariable { name } => format!("Variable ${} is not defined", name),
            IssueKind::UndefinedFunction { name } => format!("Function {}() is not defined", name),
            IssueKind::UndefinedMethod { class, method } => {
                format!("Method {}::{}() does not exist", class, method)
            }
            IssueKind::UndefinedClass { name } => format!("Class {} does not exist", name),
            IssueKind::UndefinedProperty { class, property } => {
                format!("Property {}::${} does not exist", class, property)
            }
            IssueKind::UndefinedConstant { name } => format!("Constant {} is not defined", name),
            IssueKind::PossiblyUndefinedVariable { name } => {
                format!("Variable ${} might not be defined", name)
            }

            IssueKind::NullArgument { param, fn_name } => {
                format!("Argument ${} of {}() cannot be null", param, fn_name)
            }
            IssueKind::NullPropertyFetch { property } => {
                format!("Cannot access property ${} on null", property)
            }
            IssueKind::NullMethodCall { method } => {
                format!("Cannot call method {}() on null", method)
            }
            IssueKind::NullArrayAccess => "Cannot access array on null".to_string(),
            IssueKind::PossiblyNullArgument { param, fn_name } => {
                format!("Argument ${} of {}() might be null", param, fn_name)
            }
            IssueKind::PossiblyNullPropertyFetch { property } => {
                format!("Cannot access property ${} on possibly null value", property)
            }
            IssueKind::PossiblyNullMethodCall { method } => {
                format!("Cannot call method {}() on possibly null value", method)
            }
            IssueKind::PossiblyNullArrayAccess => {
                "Cannot access array on possibly null value".to_string()
            }
            IssueKind::NullableReturnStatement { expected, actual } => {
                format!("Return type '{}' is not compatible with declared '{}'", actual, expected)
            }

            IssueKind::InvalidReturnType { expected, actual } => {
                format!("Return type '{}' is not compatible with declared '{}'", actual, expected)
            }
            IssueKind::InvalidArgument { param, fn_name, expected, actual } => {
                format!(
                    "Argument ${} of {}() expects '{}', got '{}'",
                    param, fn_name, expected, actual
                )
            }
            IssueKind::InvalidPropertyAssignment { property, expected, actual } => {
                format!(
                    "Property ${} expects '{}', cannot assign '{}'",
                    property, expected, actual
                )
            }
            IssueKind::InvalidCast { from, to } => {
                format!("Cannot cast '{}' to '{}'", from, to)
            }
            IssueKind::InvalidOperand { op, left, right } => {
                format!("Operator '{}' not supported between '{}' and '{}'", op, left, right)
            }
            IssueKind::MismatchingDocblockReturnType { declared, inferred } => {
                format!("Docblock return type '{}' does not match inferred '{}'", declared, inferred)
            }
            IssueKind::MismatchingDocblockParamType { param, declared, inferred } => {
                format!(
                    "Docblock type '{}' for ${} does not match inferred '{}'",
                    declared, param, inferred
                )
            }

            IssueKind::InvalidArrayOffset { expected, actual } => {
                format!("Array offset expects '{}', got '{}'", expected, actual)
            }
            IssueKind::NonExistentArrayOffset { key } => {
                format!("Array offset '{}' does not exist", key)
            }
            IssueKind::PossiblyInvalidArrayOffset { expected, actual } => {
                format!("Array offset might be invalid: expects '{}', got '{}'", expected, actual)
            }

            IssueKind::RedundantCondition { ty } => {
                format!("Condition is always true/false for type '{}'", ty)
            }
            IssueKind::RedundantCast { from, to } => {
                format!("Casting '{}' to '{}' is redundant", from, to)
            }
            IssueKind::UnnecessaryVarAnnotation { var } => {
                format!("@var annotation for ${} is unnecessary", var)
            }
            IssueKind::TypeDoesNotContainType { left, right } => {
                format!("Type '{}' can never contain type '{}'", left, right)
            }

            IssueKind::UnusedVariable { name } => format!("Variable ${} is never read", name),
            IssueKind::UnusedParam { name } => format!("Parameter ${} is never used", name),
            IssueKind::UnreachableCode => "Unreachable code detected".to_string(),
            IssueKind::UnusedMethod { class, method } => {
                format!("Private method {}::{}() is never called", class, method)
            }
            IssueKind::UnusedProperty { class, property } => {
                format!("Private property {}::${} is never read", class, property)
            }
            IssueKind::UnusedFunction { name } => {
                format!("Function {}() is never called", name)
            }

            IssueKind::UnimplementedAbstractMethod { class, method } => {
                format!("Class {} must implement abstract method {}()", class, method)
            }
            IssueKind::UnimplementedInterfaceMethod { class, interface, method } => {
                format!(
                    "Class {} must implement {}::{}() from interface",
                    class, interface, method
                )
            }
            IssueKind::MethodSignatureMismatch { class, method, detail } => {
                format!("Method {}::{}() signature mismatch: {}", class, method, detail)
            }
            IssueKind::OverriddenMethodAccess { class, method } => {
                format!(
                    "Method {}::{}() overrides with less visibility",
                    class, method
                )
            }
            IssueKind::ReadonlyPropertyAssignment { class, property } => {
                format!("Cannot assign to readonly property {}::${} outside of constructor", class, property)
            }
            IssueKind::FinalClassExtended { parent, child } => {
                format!("Class {} cannot extend final class {}", child, parent)
            }
            IssueKind::InvalidTemplateParam { name, expected_bound, actual } => {
                format!(
                    "Template type '{}' inferred as '{}' does not satisfy bound '{}'",
                    name, actual, expected_bound
                )
            }
            IssueKind::FinalMethodOverridden { class, method, parent } => {
                format!(
                    "Method {}::{}() cannot override final method from {}",
                    class, method, parent
                )
            }

            IssueKind::TaintedInput { sink } => format!("Tainted input reaching sink '{}'", sink),
            IssueKind::TaintedHtml => "Tainted HTML output — possible XSS".to_string(),
            IssueKind::TaintedSql => "Tainted SQL query — possible SQL injection".to_string(),
            IssueKind::TaintedShell => {
                "Tainted shell command — possible command injection".to_string()
            }

            IssueKind::DeprecatedMethod { class, method } => {
                format!("Method {}::{}() is deprecated", class, method)
            }
            IssueKind::DeprecatedClass { name } => format!("Class {} is deprecated", name),
            IssueKind::InternalMethod { class, method } => {
                format!("Method {}::{}() is marked @internal", class, method)
            }
            IssueKind::MissingReturnType { fn_name } => {
                format!("Function {}() has no return type annotation", fn_name)
            }
            IssueKind::MissingParamType { fn_name, param } => {
                format!("Parameter ${} of {}() has no type annotation", param, fn_name)
            }
            IssueKind::InvalidThrow { ty } => {
                format!("Thrown type '{}' does not extend Throwable", ty)
            }
            IssueKind::MissingThrowsDocblock { class } => {
                format!("Exception {} is thrown but not declared in @throws", class)
            }
            IssueKind::ParseError { message } => format!("Parse error: {}", message),
            IssueKind::InvalidDocblock { message } => format!("Invalid docblock: {}", message),
            IssueKind::MixedArgument { param, fn_name } => {
                format!("Argument ${} of {}() is mixed", param, fn_name)
            }
            IssueKind::MixedAssignment { var } => {
                format!("Variable ${} is assigned a mixed type", var)
            }
            IssueKind::MixedMethodCall { method } => {
                format!("Method {}() called on mixed type", method)
            }
            IssueKind::MixedPropertyFetch { property } => {
                format!("Property ${} fetched on mixed type", property)
            }
        }
    }
}

// ---------------------------------------------------------------------------
// Issue
// ---------------------------------------------------------------------------

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Issue {
    pub kind: IssueKind,
    pub severity: Severity,
    pub location: Location,
    pub snippet: Option<String>,
    pub suppressed: bool,
}

impl Issue {
    pub fn new(kind: IssueKind, location: Location) -> Self {
        let severity = kind.default_severity();
        Self {
            severity,
            kind,
            location,
            snippet: None,
            suppressed: false,
        }
    }

    pub fn with_snippet(mut self, snippet: impl Into<String>) -> Self {
        self.snippet = Some(snippet.into());
        self
    }

    pub fn suppress(mut self) -> Self {
        self.suppressed = true;
        self
    }
}

impl fmt::Display for Issue {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let sev = match self.severity {
            Severity::Error => "error".red().to_string(),
            Severity::Warning => "warning".yellow().to_string(),
            Severity::Info => "info".blue().to_string(),
        };
        write!(
            f,
            "{} {} {}: {}",
            self.location.bright_black(),
            sev,
            self.kind.name().bold(),
            self.kind.message()
        )
    }
}

// ---------------------------------------------------------------------------
// IssueBuffer — collects issues for a single file pass
// ---------------------------------------------------------------------------

#[derive(Debug, Default)]
pub struct IssueBuffer {
    issues: Vec<Issue>,
    /// Issue names suppressed at the file level (from `@psalm-suppress` / `@suppress` on the file docblock)
    file_suppressions: Vec<String>,
}

impl IssueBuffer {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn add(&mut self, issue: Issue) {
        // Deduplicate: skip if the same issue (kind + location) was already added.
        if self.issues.iter().any(|existing| {
            existing.kind.name() == issue.kind.name()
                && existing.location.file == issue.location.file
                && existing.location.line == issue.location.line
                && existing.location.col_start == issue.location.col_start
        }) {
            return;
        }
        self.issues.push(issue);
    }

    pub fn add_suppression(&mut self, name: impl Into<String>) {
        self.file_suppressions.push(name.into());
    }

    /// Consume the buffer and return unsuppressed issues.
    pub fn into_issues(self) -> Vec<Issue> {
        self.issues
            .into_iter()
            .filter(|i| !i.suppressed)
            .filter(|i| !self.file_suppressions.contains(&i.kind.name().to_string()))
            .collect()
    }

    /// Mark all issues added since index `from` as suppressed if their issue
    /// name appears in `suppressions`. Used for `@psalm-suppress` / `@suppress` on statements.
    pub fn suppress_range(&mut self, from: usize, suppressions: &[String]) {
        if suppressions.is_empty() {
            return;
        }
        for issue in self.issues[from..].iter_mut() {
            if suppressions.iter().any(|s| s == issue.kind.name()) {
                issue.suppressed = true;
            }
        }
    }

    /// Current number of buffered issues. Use before analyzing a statement to
    /// get the `from` index for `suppress_range`.
    pub fn issue_count(&self) -> usize {
        self.issues.len()
    }

    pub fn is_empty(&self) -> bool {
        self.issues.is_empty()
    }

    pub fn len(&self) -> usize {
        self.issues.len()
    }

    pub fn error_count(&self) -> usize {
        self.issues
            .iter()
            .filter(|i| !i.suppressed && i.severity == Severity::Error)
            .count()
    }

    pub fn warning_count(&self) -> usize {
        self.issues
            .iter()
            .filter(|i| !i.suppressed && i.severity == Severity::Warning)
            .count()
    }
}