Skip to main content

mir_issues/
lib.rs

1use std::collections::HashSet;
2use std::fmt;
3use std::sync::Arc;
4
5use owo_colors::OwoColorize;
6use serde::{Deserialize, Serialize};
7
8// ---------------------------------------------------------------------------
9// Severity
10// ---------------------------------------------------------------------------
11
12#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
13pub enum Severity {
14    /// Only shown with `--show-info`
15    Info,
16    /// Warnings — shown at default level
17    Warning,
18    /// Errors — always shown; non-zero exit code
19    Error,
20}
21
22impl fmt::Display for Severity {
23    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
24        match self {
25            Severity::Info => write!(f, "info"),
26            Severity::Warning => write!(f, "warning"),
27            Severity::Error => write!(f, "error"),
28        }
29    }
30}
31
32// ---------------------------------------------------------------------------
33// Location
34// ---------------------------------------------------------------------------
35
36pub use mir_types::Location;
37
38// ---------------------------------------------------------------------------
39// IssueKind
40// ---------------------------------------------------------------------------
41
42#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
43#[non_exhaustive]
44pub enum IssueKind {
45    // --- Undefined ----------------------------------------------------------
46    /// Emitted by `mir-analyzer/src/expr/variables.rs`.
47    /// Fixtures: `tests/fixtures/by-kind/invalid_scope/`.
48    /// Emitted by `mir-analyzer/src/call/static_call.rs`.
49    /// Fixtures: `tests/fixtures/by-kind/invalid_scope/self_non_static_invocation.phpt`.
50    NonStaticSelfCall { class: String, method: String },
51    InvalidScope {
52        /// `true` when inside a class but in a static method; `false` when outside a class.
53        in_class: bool,
54    },
55    /// Emitted by `mir-analyzer/src/expr/variables.rs`.
56    /// Fixtures: `tests/fixtures/by-kind/undefined_variable/`.
57    UndefinedVariable { name: String },
58    /// Emitted by `mir-analyzer/src/call/function.rs`.
59    /// Fixtures: `tests/fixtures/by-kind/undefined_function/`.
60    UndefinedFunction { name: String },
61    /// Emitted by `mir-analyzer/src/call/static_call.rs`.
62    /// Fixtures: `tests/fixtures/by-kind/undefined_method/`.
63    UndefinedMethod { class: String, method: String },
64    /// Emitted by `mir-analyzer/src/batch.rs`.
65    /// Fixtures: `tests/fixtures/by-kind/undefined_class/`.
66    UndefinedClass { name: String },
67    /// Emitted by `mir-analyzer/src/expr/objects.rs`.
68    /// Fixtures: `tests/fixtures/by-kind/undefined_property/`.
69    UndefinedProperty { class: String, property: String },
70    /// Emitted by `mir-analyzer/src/expr/variables.rs`.
71    /// Fixtures: `tests/fixtures/by-kind/undefined_constant/`.
72    UndefinedConstant { name: String },
73    /// Emitted by `mir-analyzer/src/expr/objects.rs`.
74    /// Fixtures: `tests/fixtures/by-kind/invalid_argument/invalid_*_class_const_fetch*.phpt`.
75    InaccessibleClassConstant { class: String, constant: String },
76    /// Emitted by `mir-analyzer/src/expr/variables.rs`.
77    /// Fixtures: `tests/fixtures/by-kind/possibly_undefined_variable/`.
78    PossiblyUndefinedVariable { name: String },
79    /// Emitted by `mir-analyzer/src/body_analysis.rs`.
80    /// Fixtures: `tests/fixtures/by-kind/undefined_trait/`.
81    UndefinedTrait { name: String },
82    /// Emitted when `parent::` is used in a class that has no parent.
83    /// Fixtures: `tests/fixtures/by-kind/undefined_class/no_parent*.phpt`.
84    ParentNotFound,
85    /// Emitted by `mir-analyzer/src/expr/objects.rs`.
86    /// Fixtures: `tests/fixtures/by-kind/invalid_string_class/`.
87    InvalidStringClass { actual: String },
88
89    // --- Nullability --------------------------------------------------------
90    /// Emitted by `mir-analyzer/src/call/args.rs`.
91    /// Fixtures: `tests/fixtures/by-kind/null_argument/`.
92    NullArgument { param: String, fn_name: String },
93    /// Emitted by `mir-analyzer/src/expr/objects.rs`.
94    /// Fixtures: `tests/fixtures/by-kind/null_property_fetch/`.
95    NullPropertyFetch { property: String },
96    /// Emitted by `mir-analyzer/src/call/method.rs`.
97    /// Fixtures: `tests/fixtures/by-kind/null_method_call/`.
98    NullMethodCall { method: String },
99    /// Emitted by `mir-analyzer/src/expr/arrays.rs`.
100    /// Fixtures: `tests/fixtures/by-kind/null_array_access/`.
101    NullArrayAccess,
102    /// Emitted by `mir-analyzer/src/call/args.rs`.
103    /// Fixtures: `tests/fixtures/by-kind/possibly_null_argument/`.
104    PossiblyNullArgument { param: String, fn_name: String },
105    /// Emitted by `mir-analyzer/src/call/args.rs`.
106    /// Fixtures: `tests/fixtures/by-kind/possibly_invalid_argument/`.
107    PossiblyInvalidArgument {
108        param: String,
109        fn_name: String,
110        expected: String,
111        actual: String,
112    },
113    /// Emitted by `mir-analyzer/src/expr/objects.rs`.
114    /// Fixtures: `tests/fixtures/by-kind/possibly_null_property_fetch/`.
115    PossiblyNullPropertyFetch { property: String },
116    /// Emitted by `mir-analyzer/src/call/method.rs`.
117    /// Fixtures: `tests/fixtures/by-kind/possibly_null_method_call/`.
118    PossiblyNullMethodCall { method: String },
119    /// Emitted by `mir-analyzer/src/expr/arrays.rs`.
120    /// Fixtures: `tests/fixtures/by-kind/possibly_null_array_access/`.
121    PossiblyNullArrayAccess,
122    /// Not yet emitted. Fixtures: `tests/fixtures/by-kind/nullable_return_statement/` (planned).
123    NullableReturnStatement { expected: String, actual: String },
124
125    // --- Type mismatches ----------------------------------------------------
126    /// Emitted by `mir-analyzer/src/stmt/flow.rs`.
127    /// Fixtures: `tests/fixtures/by-kind/invalid_return_type/`.
128    InvalidReturnType { expected: String, actual: String },
129    /// Emitted by `mir-analyzer/src/call/args.rs`.
130    /// Fixtures: `tests/fixtures/by-kind/invalid_argument/`.
131    InvalidArgument {
132        param: String,
133        fn_name: String,
134        expected: String,
135        actual: String,
136    },
137    /// Emitted by `mir-analyzer/src/call/callable.rs`.
138    /// Fixtures: `tests/fixtures/by-kind/too_few_arguments/`.
139    TooFewArguments {
140        fn_name: String,
141        expected: usize,
142        actual: usize,
143    },
144    /// Emitted by `mir-analyzer/src/call/function.rs`.
145    /// Fixtures: `tests/fixtures/by-kind/too_many_arguments/`.
146    TooManyArguments {
147        fn_name: String,
148        expected: usize,
149        actual: usize,
150    },
151    /// Emitted by `mir-analyzer/src/call/args.rs`.
152    /// Fixtures: `tests/fixtures/by-kind/invalid_named_argument/`.
153    InvalidNamedArgument { fn_name: String, name: String },
154    /// Emitted when a function/method tagged `@no-named-arguments` is called with named args.
155    /// Fixtures: `tests/fixtures/by-kind/invalid_named_argument/`.
156    InvalidNamedArguments { fn_name: String },
157    /// Emitted by `mir-analyzer/src/call/args.rs`.
158    /// Fixtures: `tests/fixtures/by-kind/invalid_pass_by_reference/`.
159    InvalidPassByReference { fn_name: String, param: String },
160    /// Emitted by `mir-analyzer/src/expr/objects.rs`.
161    /// Fixtures: `tests/fixtures/by-kind/invalid_property_fetch/bad_fetch.phpt`.
162    InvalidPropertyFetch { ty: String },
163    /// Emitted by `mir-analyzer/src/expr/arrays.rs`.
164    /// Fixtures: `tests/fixtures/by-kind/invalid_array_access/`.
165    InvalidArrayAccess { ty: String },
166    /// Emitted by `mir-analyzer/src/expr/assignment.rs`.
167    /// Fixtures: `tests/fixtures/by-kind/invalid_array_assignment/`.
168    InvalidArrayAssignment { ty: String },
169    /// Emitted by `mir-analyzer/src/expr/assignment.rs`.
170    /// Fixtures: `tests/fixtures/by-kind/invalid_property_assignment/`.
171    InvalidPropertyAssignment {
172        property: String,
173        expected: String,
174        actual: String,
175    },
176    /// Emitted by `mir-analyzer/src/expr/casts.rs`.
177    /// Fixtures: `tests/fixtures/by-kind/invalid_cast/`.
178    InvalidCast { from: String, to: String },
179    /// Emitted by `mir-analyzer/src/call/static_call.rs`.
180    /// Fixtures: `tests/fixtures/by-kind/undefined_method/static_invocation*.phpt`.
181    InvalidStaticInvocation { class: String, method: String },
182    /// Emitted by `mir-analyzer/src/expr/binary.rs` and `unary.rs` for operations on
183    /// non-numeric or non-bitwise-compatible operands.
184    /// Fixtures: `tests/fixtures/by-kind/invalid_operand/`.
185    InvalidOperand {
186        op: String,
187        left: String,
188        right: String,
189    },
190    /// Emitted when a union-typed operand has some non-numeric/non-stringifiable members.
191    /// Fixtures: `tests/fixtures/by-kind/invalid_operand/`.
192    PossiblyInvalidOperand {
193        op: String,
194        left: String,
195        right: String,
196    },
197    /// Emitted when a divisor operand could be null (potential division by zero).
198    /// Fixtures: `tests/fixtures/by-kind/invalid_operand/`.
199    PossiblyNullOperand { op: String, ty: String },
200    /// Emitted when `yield from` is used with a non-iterable object (no Traversable).
201    /// Fixtures: `tests/fixtures/by-kind/invalid_operand/`.
202    RawObjectIteration { ty: String },
203    /// Emitted when `yield from` might be used with a non-iterable object.
204    /// Fixtures: `tests/fixtures/by-kind/invalid_operand/`.
205    PossiblyRawObjectIteration { ty: String },
206    /// Not yet emitted. Fixtures: `tests/fixtures/by-kind/mismatching_docblock_return_type/` (planned).
207    MismatchingDocblockReturnType { declared: String, inferred: String },
208    /// Not yet emitted. Fixtures: `tests/fixtures/by-kind/mismatching_docblock_param_type/` (planned).
209    MismatchingDocblockParamType {
210        param: String,
211        declared: String,
212        inferred: String,
213    },
214    /// Emitted by `mir-analyzer/src/stmt/mod.rs`.
215    /// Fixtures: `tests/fixtures/by-kind/type_check_mismatch/`.
216    TypeCheckMismatch {
217        var: String,
218        expected: String,
219        actual: String,
220    },
221
222    /// Emitted by `@trace $var` docblock annotation. Shows inferred type.
223    /// Fixtures: `tests/fixtures/by-kind/trace/`.
224    Trace { variable: String, type_info: String },
225
226    // --- Array issues -------------------------------------------------------
227    /// Not yet emitted. Fixtures: `tests/fixtures/by-kind/invalid_array_offset/` (planned).
228    InvalidArrayOffset { expected: String, actual: String },
229    /// Not yet emitted. No fixtures yet.
230    NonExistentArrayOffset { key: String },
231    /// Emitted by `mir-analyzer/src/expr/assignment.rs`.
232    /// Fixtures: `tests/fixtures/by-kind/possibly_invalid_array_offset/`.
233    PossiblyInvalidArrayOffset { expected: String, actual: String },
234
235    // --- Redundancy ---------------------------------------------------------
236    /// Emitted by `mir-analyzer/src/stmt/control_flow.rs`.
237    /// Fixtures: `tests/fixtures/by-kind/redundant_condition/`.
238    RedundantCondition { ty: String },
239    /// Emitted by `mir-analyzer/src/expr/casts.rs`.
240    /// Fixtures: `tests/fixtures/by-kind/redundant_cast/`.
241    RedundantCast { from: String, to: String },
242    /// Not yet emitted. Fixtures: `tests/fixtures/by-kind/unnecessary_var_annotation/` (planned).
243    UnnecessaryVarAnnotation { var: String },
244    /// Not yet emitted. Fixtures: `tests/fixtures/by-kind/type_does_not_contain_type/` (planned).
245    TypeDoesNotContainType { left: String, right: String },
246    /// Emitted by `mir-analyzer/src/stmt/control_flow.rs` and `mir-analyzer/src/expr/conditional.rs`.
247    /// Fixtures: `tests/fixtures/by-kind/paradoxical_condition/`.
248    ParadoxicalCondition { value: String },
249
250    // --- Dead code ----------------------------------------------------------
251    /// Emitted by `mir-analyzer/src/diagnostics.rs`.
252    /// Fixtures: `tests/fixtures/by-kind/unused_variable/`.
253    UnusedVariable { name: String },
254    /// Emitted by `mir-analyzer/src/diagnostics.rs`.
255    /// Fixtures: `tests/fixtures/by-kind/unused_param/`.
256    UnusedParam { name: String },
257    /// Emitted by `mir-analyzer/src/stmt/mod.rs`.
258    /// Fixtures: `tests/fixtures/by-kind/unreachable_code/`.
259    UnreachableCode,
260    /// Emitted by `mir-analyzer/src/expr/conditional.rs`.
261    /// Fixtures: `tests/fixtures/by-kind/unreachable_code/`.
262    UnhandledMatchCondition { detail: String },
263    /// Emitted by `mir-analyzer/src/dead_code.rs`.
264    /// Fixtures: `tests/fixtures/by-kind/unused_method/`.
265    UnusedMethod { class: String, method: String },
266    /// Emitted by `mir-analyzer/src/dead_code.rs`.
267    /// Fixtures: `tests/fixtures/by-kind/unused_property/`.
268    UnusedProperty { class: String, property: String },
269    /// Emitted by `mir-analyzer/src/dead_code.rs`.
270    /// Fixtures: `tests/fixtures/by-kind/unused_function/`.
271    UnusedFunction { name: String },
272    /// Emitted by `mir-analyzer/src/diagnostics.rs`.
273    /// Fixtures: `tests/fixtures/by-kind/unused_foreach_value/`.
274    UnusedForeachValue { name: String },
275
276    // --- Readonly -----------------------------------------------------------
277    /// Emitted by `mir-analyzer/src/expr/assignment.rs`.
278    /// Fixtures: `tests/fixtures/by-kind/readonly_property_assignment/`.
279    ReadonlyPropertyAssignment { class: String, property: String },
280
281    // --- Inheritance --------------------------------------------------------
282    /// Emitted by `mir-analyzer/src/class.rs`.
283    /// Fixtures: `tests/fixtures/by-kind/unimplemented_abstract_method/`.
284    UnimplementedAbstractMethod { class: String, method: String },
285    /// Emitted by `mir-analyzer/src/class.rs`.
286    /// Fixtures: `tests/fixtures/by-kind/unimplemented_interface_method/`.
287    UnimplementedInterfaceMethod {
288        class: String,
289        interface: String,
290        method: String,
291    },
292    /// Emitted by `mir-analyzer/src/class.rs`.
293    /// Fixtures: `tests/fixtures/by-kind/method_signature_mismatch/`.
294    MethodSignatureMismatch {
295        class: String,
296        method: String,
297        detail: String,
298    },
299    /// Emitted by `mir-analyzer/src/class.rs`.
300    /// Fixtures: `tests/fixtures/by-kind/overridden_method_access/`.
301    OverriddenMethodAccess { class: String, method: String },
302    /// Emitted by `mir-analyzer/src/class.rs`.
303    /// Fixtures: `tests/fixtures/by-kind/overridden_property_access/`.
304    OverriddenPropertyAccess { class: String, property: String },
305    /// Emitted by `mir-analyzer/src/call/method.rs`.
306    /// Fixtures: `tests/fixtures/by-kind/undefined_method/direct_constructor_call*.phpt`.
307    DirectConstructorCall { class: String },
308    /// Emitted by `mir-analyzer/src/class.rs`.
309    /// Fixtures: `tests/fixtures/by-kind/invalid_extend_class/`.
310    InvalidExtendClass { parent: String, child: String },
311    /// Emitted by `mir-analyzer/src/class.rs`.
312    /// Fixtures: `tests/fixtures/by-kind/final_method_overridden/`.
313    FinalMethodOverridden {
314        class: String,
315        method: String,
316        parent: String,
317    },
318    /// Emitted by `mir-analyzer/src/expr/objects.rs`.
319    /// Fixtures: `tests/fixtures/by-kind/abstract_instantiation/`.
320    AbstractInstantiation { class: String },
321    /// Emitted by `mir-analyzer/src/call/static_call.rs`.
322    /// Fixtures: `tests/fixtures/by-kind/abstract_instantiation/prevent_abstract_method_call.phpt`.
323    AbstractMethodCall { class: String, method: String },
324    /// Emitted by `mir-analyzer/src/expr/objects.rs`.
325    /// Fixtures: `tests/fixtures/by-kind/abstract_instantiation/interface_instantiation.phpt`.
326    InterfaceInstantiation { class: String },
327    /// Emitted by `mir-analyzer/src/class.rs` when `#[Override]` is declared
328    /// but no overridable parent method exists.
329    /// Fixtures: `tests/fixtures/by-kind/method_signature_mismatch/`.
330    InvalidOverride {
331        class: String,
332        method: String,
333        detail: String,
334    },
335
336    // --- Security (taint) ---------------------------------------------------
337    /// Not yet emitted (generic taint sink; specific sinks use `TaintedHtml`, `TaintedSql`, `TaintedShell`).
338    /// No fixtures yet.
339    TaintedInput { sink: String },
340    /// Emitted by `mir-analyzer/src/call/function.rs`.
341    /// Fixtures: `tests/fixtures/by-kind/tainted_html/`.
342    TaintedHtml,
343    /// Emitted by `mir-analyzer/src/call/function.rs`.
344    /// Fixtures: `tests/fixtures/by-kind/tainted_sql/`.
345    TaintedSql,
346    /// Emitted by `mir-analyzer/src/call/function.rs`.
347    /// Fixtures: `tests/fixtures/by-kind/tainted_shell/`.
348    TaintedShell,
349
350    // --- Generics -----------------------------------------------------------
351    /// Emitted by `mir-analyzer/src/call/function.rs`.
352    /// Fixtures: `tests/fixtures/by-kind/invalid_template_param/`.
353    InvalidTemplateParam {
354        name: String,
355        expected_bound: String,
356        actual: String,
357    },
358    /// Emitted by `mir-analyzer/src/call/method.rs`.
359    /// Fixtures: `tests/fixtures/by-kind/shadowed_template_param/`.
360    ShadowedTemplateParam { name: String },
361
362    // --- Other --------------------------------------------------------------
363    /// Emitted by `mir-analyzer/src/call/function.rs`.
364    /// Fixtures: `tests/fixtures/by-kind/deprecated_call/`.
365    DeprecatedCall {
366        name: String,
367        message: Option<Arc<str>>,
368    },
369    /// Emitted by `mir-analyzer/src/expr/objects.rs`.
370    /// Fixtures: `tests/fixtures/by-kind/undefined_property/deprecated_property_*.phpt`.
371    DeprecatedProperty {
372        class: String,
373        property: String,
374        message: Option<Arc<str>>,
375    },
376    /// Emitted by `mir-analyzer/src/expr/objects.rs`.
377    /// Fixtures: `tests/fixtures/by-kind/deprecated_call/deprecated_class_const_fetch*.phpt`.
378    DeprecatedConstant {
379        class: String,
380        constant: String,
381        message: Option<Arc<str>>,
382    },
383    /// Emitted by `mir-analyzer/src/class.rs`.
384    /// Fixtures: `tests/fixtures/by-kind/deprecated_class/deprecated_interface*.phpt`.
385    DeprecatedInterface {
386        name: String,
387        message: Option<Arc<str>>,
388    },
389    /// Emitted by `mir-analyzer/src/class.rs`.
390    /// Fixtures: `tests/fixtures/by-kind/deprecated_trait/`.
391    DeprecatedTrait {
392        name: String,
393        message: Option<Arc<str>>,
394    },
395    /// Emitted by `mir-analyzer/src/call/method.rs`.
396    /// Fixtures: `tests/fixtures/by-kind/deprecated_method_call/`.
397    DeprecatedMethodCall {
398        class: String,
399        method: String,
400        message: Option<Arc<str>>,
401    },
402    /// Emitted by `mir-analyzer/src/call/method.rs`.
403    /// Fixtures: `tests/fixtures/by-kind/deprecated_method/`.
404    DeprecatedMethod {
405        class: String,
406        method: String,
407        message: Option<Arc<str>>,
408    },
409    /// Emitted by `mir-analyzer/src/class.rs`.
410    /// Fixtures: `tests/fixtures/by-kind/deprecated_class/`.
411    DeprecatedClass {
412        name: String,
413        message: Option<Arc<str>>,
414    },
415    /// Emitted by `mir-analyzer/src/call/method.rs`.
416    /// Fixtures: `tests/fixtures/by-kind/internal_method/`.
417    InternalMethod { class: String, method: String },
418    /// Not yet emitted. Fixtures: `tests/fixtures/by-kind/missing_return_type/` (planned).
419    MissingReturnType { fn_name: String },
420    /// Not yet emitted. Fixtures: `tests/fixtures/by-kind/missing_param_type/` (planned).
421    MissingParamType { fn_name: String, param: String },
422    /// Emitted by `mir-analyzer/src/body_analysis.rs`.
423    /// Fixtures: `tests/fixtures/by-kind/missing_param_type/` (property variants).
424    MissingPropertyType { class: String, property: String },
425    /// Emitted by `mir-analyzer/src/stmt/flow.rs`.
426    /// Fixtures: `tests/fixtures/by-kind/invalid_throw/`.
427    InvalidThrow { ty: String },
428    /// Emitted by `mir-analyzer/src/stmt/control_flow.rs`.
429    /// Fixtures: `tests/fixtures/by-kind/invalid_catch/`.
430    InvalidCatch { ty: String },
431    /// Emitted by `mir-analyzer/src/stmt/flow.rs`.
432    /// Fixtures: `tests/fixtures/by-kind/missing_throws_docblock/`.
433    MissingThrowsDocblock { class: String },
434    /// Emitted by `mir-analyzer/src/stmt/expressions.rs`.
435    /// Fixtures: `tests/fixtures/by-kind/implicit_to_string_cast/`.
436    ImplicitToStringCast { class: String },
437    /// Emitted by `mir-analyzer/src/call/args.rs`.
438    /// Fixtures: `tests/fixtures/by-kind/implicit_float_to_int_cast/`.
439    ImplicitFloatToIntCast { from: String },
440    /// Emitted by `mir-analyzer/src/parser/mod.rs`.
441    /// Fixtures: `tests/fixtures/by-kind/parse_error/`.
442    ParseError { message: String },
443    /// Emitted by `mir-analyzer/src/collector/annotation.rs`.
444    /// Fixtures: `tests/fixtures/by-kind/invalid_docblock/`.
445    InvalidDocblock { message: String },
446    /// Not yet emitted. Fixtures: `tests/fixtures/by-kind/mixed_argument/` (planned).
447    MixedArgument { param: String, fn_name: String },
448    /// Not yet emitted. Fixtures: `tests/fixtures/by-kind/mixed_assignment/` (planned).
449    MixedAssignment { var: String },
450    /// Emitted by `mir-analyzer/src/call/method.rs`.
451    /// Fixtures: `tests/fixtures/by-kind/mixed_method_call/`.
452    MixedMethodCall { method: String },
453    /// Emitted by `mir-analyzer/src/expr/objects.rs`.
454    /// Fixtures: `tests/fixtures/by-kind/mixed_property_fetch/`.
455    MixedPropertyFetch { property: String },
456    /// Emitted by `mir-analyzer/src/expr/assignment.rs`.
457    /// Fixtures: `tests/fixtures/by-kind/mixed_property_assignment/`.
458    MixedPropertyAssignment { property: String },
459    /// Emitted by `mir-analyzer/src/expr/arrays.rs`.
460    /// Fixtures: `tests/fixtures/by-kind/mixed_array_access/`.
461    MixedArrayAccess,
462    /// Emitted by `mir-analyzer/src/expr/arrays.rs`.
463    /// Fixtures: `tests/fixtures/by-kind/mixed_array_offset/`.
464    MixedArrayOffset,
465    /// Emitted by `mir-analyzer/src/expr/mod.rs`.
466    /// Fixtures: `tests/fixtures/by-kind/mixed_clone/`.
467    MixedClone,
468    /// `clone` of a value that is definitely not an object (e.g. `int`, `string`).
469    /// Emitted by `mir-analyzer/src/expr/mod.rs`.
470    /// Fixtures: `tests/fixtures/by-kind/mixed_clone/`.
471    InvalidClone { ty: String },
472    /// `clone` of a union where some members are not objects (e.g. `int|Exception`).
473    /// Emitted by `mir-analyzer/src/expr/mod.rs`.
474    /// Fixtures: `tests/fixtures/by-kind/mixed_clone/`.
475    PossiblyInvalidClone { ty: String },
476    /// A `__toString` method that does not return a `string`.
477    /// Emitted by `mir-analyzer/src/body_analysis.rs`.
478    /// Fixtures: `tests/fixtures/by-kind/implicit_to_string_cast/`.
479    InvalidToString { class: String },
480    /// Emitted by `mir-analyzer/src/class.rs`.
481    /// Fixtures: `tests/fixtures/by-kind/circular_inheritance/`.
482    CircularInheritance { class: String },
483
484    // --- Trait constraints --------------------------------------------------
485    /// Emitted by `mir-analyzer/src/body_analysis.rs`.
486    /// Fixtures: `tests/fixtures/by-kind/invalid_trait_use/`.
487    InvalidTraitUse { trait_name: String, reason: String },
488    /// Emitted by `mir-analyzer/src/expr/mod.rs` and `mir-analyzer/src/call/function.rs`.
489    /// Fixtures: `tests/fixtures/by-kind/invalid_operand/` (var_dump, shell_exec, backtick).
490    ForbiddenCode { message: String },
491
492    // --- Attribute validation -----------------------------------------------
493    /// Emitted by `mir-analyzer/src/attributes.rs`.
494    /// Fixtures: `tests/fixtures/by-kind/invalid_attribute/`.
495    InvalidAttribute { message: String },
496    /// Emitted by `mir-analyzer/src/attributes.rs`.
497    /// Fixtures: `tests/fixtures/by-kind/undefined_class/missing_attribute_on_*.phpt`.
498    UndefinedAttributeClass { name: String },
499
500    // --- Case sensitivity (PHP 8.6 deprecation) -----------------------------
501    /// Emitted by `mir-analyzer/src/call/function.rs`.
502    /// Fixtures: `tests/fixtures/by-kind/wrong_case_function/`.
503    WrongCaseFunction { used: String, canonical: String },
504    /// Emitted by `mir-analyzer/src/call/method.rs` and `src/call/static_call.rs`.
505    /// Fixtures: `tests/fixtures/by-kind/wrong_case_method/`.
506    WrongCaseMethod {
507        class: String,
508        used: String,
509        canonical: String,
510    },
511    /// Emitted by `mir-analyzer/src/expr/objects.rs` and `src/call/static_call.rs`.
512    /// Fixtures: `tests/fixtures/by-kind/wrong_case_class/`.
513    WrongCaseClass { used: String, canonical: String },
514    /// Emitted by `mir-analyzer/src/body_analysis.rs`.
515    /// Fixtures: `tests/fixtures/by-kind/invalid_argument/class_redefinition*.phpt`.
516    DuplicateClass { name: String },
517    /// Emitted by `mir-analyzer/src/body_analysis.rs`.
518    /// Fixtures: `tests/fixtures/by-kind/invalid_argument/interface_redefinition*.phpt`.
519    DuplicateInterface { name: String },
520    /// Emitted by `mir-analyzer/src/body_analysis.rs`.
521    /// Fixtures: `tests/fixtures/by-kind/invalid_argument/trait_redefinition*.phpt`.
522    DuplicateTrait { name: String },
523    /// Emitted by `mir-analyzer/src/body_analysis.rs`.
524    /// Fixtures: `tests/fixtures/by-kind/invalid_argument/enum_redefinition*.phpt`.
525    DuplicateEnum { name: String },
526    /// Emitted by `mir-analyzer/src/body_analysis.rs`.
527    /// Fixtures: `tests/fixtures/by-kind/invalid_argument/function_redefinition*.phpt`.
528    DuplicateFunction { name: String },
529}
530
531fn append_deprecation_message(base: String, message: &Option<Arc<str>>) -> String {
532    match message.as_deref().filter(|m| !m.is_empty()) {
533        Some(msg) => format!("{base}: {msg}"),
534        None => base,
535    }
536}
537
538impl IssueKind {
539    /// Default severity for this issue kind.
540    pub fn default_severity(&self) -> Severity {
541        match self {
542            // Errors (always blocking)
543            IssueKind::NonStaticSelfCall { .. }
544            | IssueKind::DirectConstructorCall { .. }
545            | IssueKind::InvalidScope { .. }
546            | IssueKind::UndefinedVariable { .. }
547            | IssueKind::UndefinedFunction { .. }
548            | IssueKind::UndefinedMethod { .. }
549            | IssueKind::UndefinedClass { .. }
550            | IssueKind::UndefinedConstant { .. }
551            | IssueKind::InaccessibleClassConstant { .. }
552            | IssueKind::InvalidReturnType { .. }
553            | IssueKind::InvalidArgument { .. }
554            | IssueKind::TooFewArguments { .. }
555            | IssueKind::TooManyArguments { .. }
556            | IssueKind::InvalidNamedArgument { .. }
557            | IssueKind::InvalidNamedArguments { .. }
558            | IssueKind::InvalidPassByReference { .. }
559            | IssueKind::InvalidThrow { .. }
560            | IssueKind::InvalidCatch { .. }
561            | IssueKind::InvalidStaticInvocation { .. }
562            | IssueKind::UnimplementedAbstractMethod { .. }
563            | IssueKind::UnimplementedInterfaceMethod { .. }
564            | IssueKind::MethodSignatureMismatch { .. }
565            | IssueKind::InvalidExtendClass { .. }
566            | IssueKind::FinalMethodOverridden { .. }
567            | IssueKind::AbstractInstantiation { .. }
568            | IssueKind::AbstractMethodCall { .. }
569            | IssueKind::InterfaceInstantiation { .. }
570            | IssueKind::InvalidOverride { .. }
571            | IssueKind::InvalidTemplateParam { .. }
572            | IssueKind::ReadonlyPropertyAssignment { .. }
573            | IssueKind::ParseError { .. }
574            | IssueKind::TaintedInput { .. }
575            | IssueKind::TaintedHtml
576            | IssueKind::TaintedSql
577            | IssueKind::TaintedShell
578            | IssueKind::CircularInheritance { .. }
579            | IssueKind::InvalidTraitUse { .. }
580            | IssueKind::UndefinedTrait { .. }
581            | IssueKind::InvalidClone { .. }
582            | IssueKind::InvalidToString { .. }
583            | IssueKind::TypeCheckMismatch { .. }
584            | IssueKind::ParentNotFound => Severity::Error,
585            IssueKind::Trace { .. } => Severity::Info,
586
587            // Warnings (shown at default error level)
588            IssueKind::NullArgument { .. }
589            | IssueKind::NullPropertyFetch { .. }
590            | IssueKind::NullMethodCall { .. }
591            | IssueKind::NullArrayAccess
592            | IssueKind::NullableReturnStatement { .. }
593            | IssueKind::InvalidPropertyFetch { .. }
594            | IssueKind::InvalidArrayAccess { .. }
595            | IssueKind::InvalidArrayAssignment { .. }
596            | IssueKind::InvalidPropertyAssignment { .. }
597            | IssueKind::InvalidArrayOffset { .. }
598            | IssueKind::NonExistentArrayOffset { .. }
599            | IssueKind::PossiblyInvalidArrayOffset { .. }
600            | IssueKind::UndefinedProperty { .. }
601            | IssueKind::InvalidOperand { .. }
602            | IssueKind::OverriddenMethodAccess { .. }
603            | IssueKind::OverriddenPropertyAccess { .. }
604            | IssueKind::ImplicitToStringCast { .. }
605            | IssueKind::ImplicitFloatToIntCast { .. }
606            | IssueKind::UnusedVariable { .. }
607            | IssueKind::UnusedForeachValue { .. }
608            | IssueKind::ParadoxicalCondition { .. }
609            | IssueKind::UnhandledMatchCondition { .. }
610            | IssueKind::InvalidStringClass { .. }
611            | IssueKind::ForbiddenCode { .. } => Severity::Warning,
612
613            // PossiblyUndefined: shown at default error level (same as Warning)
614            IssueKind::PossiblyUndefinedVariable { .. } => Severity::Warning,
615
616            // Possibly-null / possibly-invalid (only shown in strict mode, level ≥ 7)
617            IssueKind::PossiblyNullArgument { .. }
618            | IssueKind::PossiblyInvalidArgument { .. }
619            | IssueKind::PossiblyNullPropertyFetch { .. }
620            | IssueKind::PossiblyNullMethodCall { .. }
621            | IssueKind::PossiblyNullArrayAccess
622            | IssueKind::PossiblyInvalidClone { .. }
623            | IssueKind::PossiblyInvalidOperand { .. }
624            | IssueKind::PossiblyNullOperand { .. }
625            | IssueKind::PossiblyRawObjectIteration { .. } => Severity::Info,
626
627            IssueKind::RawObjectIteration { .. } => Severity::Warning,
628
629            // Info
630            IssueKind::RedundantCondition { .. }
631            | IssueKind::RedundantCast { .. }
632            | IssueKind::UnnecessaryVarAnnotation { .. }
633            | IssueKind::TypeDoesNotContainType { .. }
634            | IssueKind::UnusedParam { .. }
635            | IssueKind::UnreachableCode
636            | IssueKind::UnusedMethod { .. }
637            | IssueKind::UnusedProperty { .. }
638            | IssueKind::UnusedFunction { .. }
639            | IssueKind::DeprecatedCall { .. }
640            | IssueKind::DeprecatedProperty { .. }
641            | IssueKind::DeprecatedConstant { .. }
642            | IssueKind::DeprecatedInterface { .. }
643            | IssueKind::DeprecatedTrait { .. }
644            | IssueKind::DeprecatedMethodCall { .. }
645            | IssueKind::DeprecatedMethod { .. }
646            | IssueKind::DeprecatedClass { .. }
647            | IssueKind::InternalMethod { .. }
648            | IssueKind::MissingReturnType { .. }
649            | IssueKind::MissingParamType { .. }
650            | IssueKind::MissingPropertyType { .. }
651            | IssueKind::MismatchingDocblockReturnType { .. }
652            | IssueKind::MismatchingDocblockParamType { .. }
653            | IssueKind::InvalidDocblock { .. }
654            | IssueKind::InvalidCast { .. }
655            | IssueKind::MixedArgument { .. }
656            | IssueKind::MixedAssignment { .. }
657            | IssueKind::MixedMethodCall { .. }
658            | IssueKind::MixedPropertyFetch { .. }
659            | IssueKind::MixedPropertyAssignment { .. }
660            | IssueKind::MixedArrayAccess
661            | IssueKind::MixedArrayOffset
662            | IssueKind::MixedClone
663            | IssueKind::ShadowedTemplateParam { .. }
664            | IssueKind::MissingThrowsDocblock { .. }
665            | IssueKind::WrongCaseFunction { .. }
666            | IssueKind::WrongCaseMethod { .. }
667            | IssueKind::WrongCaseClass { .. }
668            | IssueKind::InvalidAttribute { .. }
669            | IssueKind::UndefinedAttributeClass { .. } => Severity::Info,
670            IssueKind::DuplicateClass { .. }
671            | IssueKind::DuplicateInterface { .. }
672            | IssueKind::DuplicateTrait { .. }
673            | IssueKind::DuplicateEnum { .. }
674            | IssueKind::DuplicateFunction { .. } => Severity::Error,
675        }
676    }
677
678    /// Stable error code (e.g. `"MIR0005"`).
679    ///
680    /// Codes are assigned in bands by category and are part of the public API:
681    /// once a code ships, it must never be reused for a different issue kind.
682    /// New variants take the next free slot in their band; obsolete variants
683    /// retire their code (the slot stays burnt). Bands have headroom for growth.
684    ///
685    /// Bands:
686    ///
687    /// | Range         | Category                        |
688    /// |---------------|---------------------------------|
689    /// | 0001 – 0099   | Undefined symbols               |
690    /// | 0100 – 0199   | Nullability                     |
691    /// | 0200 – 0299   | Type mismatches                 |
692    /// | 0300 – 0399   | Array / offset                  |
693    /// | 0400 – 0499   | Redundancy                      |
694    /// | 0500 – 0599   | Dead code                       |
695    /// | 0600 – 0699   | Readonly                        |
696    /// | 0700 – 0799   | Inheritance                     |
697    /// | 0800 – 0899   | Security (taint)                |
698    /// | 0900 – 0999   | Generics                        |
699    /// | 1000 – 1099   | Deprecation / internal          |
700    /// | 1100 – 1199   | Missing types / docblocks       |
701    /// | 1200 – 1299   | Mixed                           |
702    /// | 1300 – 1399   | Trait                           |
703    /// | 1400 – 1499   | Parse                           |
704    /// | 1500 – 1599   | Other                           |
705    pub fn code(&self) -> &'static str {
706        match self {
707            // Undefined (0001-0099)
708            IssueKind::NonStaticSelfCall { .. } => "MIR0216",
709            IssueKind::DirectConstructorCall { .. } => "MIR0217",
710            IssueKind::InvalidScope { .. } => "MIR0001",
711            IssueKind::UndefinedVariable { .. } => "MIR0002",
712            IssueKind::UndefinedFunction { .. } => "MIR0003",
713            IssueKind::UndefinedMethod { .. } => "MIR0004",
714            IssueKind::UndefinedClass { .. } => "MIR0005",
715            IssueKind::UndefinedProperty { .. } => "MIR0006",
716            IssueKind::UndefinedConstant { .. } => "MIR0007",
717            IssueKind::InaccessibleClassConstant { .. } => "MIR0011",
718            IssueKind::PossiblyUndefinedVariable { .. } => "MIR0008",
719            IssueKind::UndefinedTrait { .. } => "MIR0009",
720            IssueKind::ParentNotFound => "MIR0010",
721
722            // Nullability (0100-0199)
723            IssueKind::NullArgument { .. } => "MIR0100",
724            IssueKind::NullPropertyFetch { .. } => "MIR0101",
725            IssueKind::NullMethodCall { .. } => "MIR0102",
726            IssueKind::NullArrayAccess => "MIR0103",
727            IssueKind::PossiblyNullArgument { .. } => "MIR0104",
728            IssueKind::PossiblyInvalidArgument { .. } => "MIR0105",
729            IssueKind::PossiblyNullPropertyFetch { .. } => "MIR0106",
730            IssueKind::PossiblyNullMethodCall { .. } => "MIR0107",
731            IssueKind::PossiblyNullArrayAccess => "MIR0108",
732            IssueKind::NullableReturnStatement { .. } => "MIR0109",
733
734            // Type mismatches (0200-0299)
735            IssueKind::InvalidReturnType { .. } => "MIR0200",
736            IssueKind::InvalidArgument { .. } => "MIR0201",
737            IssueKind::TooFewArguments { .. } => "MIR0202",
738            IssueKind::TooManyArguments { .. } => "MIR0203",
739            IssueKind::InvalidNamedArgument { .. } => "MIR0204",
740            IssueKind::InvalidNamedArguments { .. } => "MIR0224",
741            IssueKind::InvalidPassByReference { .. } => "MIR0205",
742            IssueKind::InvalidPropertyFetch { .. } => "MIR0218",
743            IssueKind::InvalidArrayAccess { .. } => "MIR0219",
744            IssueKind::InvalidArrayAssignment { .. } => "MIR0220",
745            IssueKind::InvalidPropertyAssignment { .. } => "MIR0206",
746            IssueKind::InvalidCast { .. } => "MIR0207",
747            IssueKind::InvalidStaticInvocation { .. } => "MIR0215",
748            IssueKind::InvalidOperand { .. } => "MIR0208",
749            IssueKind::PossiblyInvalidOperand { .. } => "MIR0213",
750            IssueKind::PossiblyNullOperand { .. } => "MIR0214",
751            IssueKind::RawObjectIteration { .. } => "MIR0222",
752            IssueKind::PossiblyRawObjectIteration { .. } => "MIR0223",
753            IssueKind::MismatchingDocblockReturnType { .. } => "MIR0209",
754            IssueKind::MismatchingDocblockParamType { .. } => "MIR0210",
755            IssueKind::InvalidStringClass { .. } => "MIR0211",
756            IssueKind::TypeCheckMismatch { .. } => "MIR0212",
757            IssueKind::Trace { .. } => "MIR0221",
758
759            // Array / offset (0300-0399)
760            IssueKind::InvalidArrayOffset { .. } => "MIR0300",
761            IssueKind::NonExistentArrayOffset { .. } => "MIR0301",
762            IssueKind::PossiblyInvalidArrayOffset { .. } => "MIR0302",
763
764            // Redundancy (0400-0499)
765            IssueKind::RedundantCondition { .. } => "MIR0400",
766            IssueKind::RedundantCast { .. } => "MIR0401",
767            IssueKind::UnnecessaryVarAnnotation { .. } => "MIR0402",
768            IssueKind::TypeDoesNotContainType { .. } => "MIR0403",
769            IssueKind::ParadoxicalCondition { .. } => "MIR0404",
770            IssueKind::UnhandledMatchCondition { .. } => "MIR0405",
771
772            // Dead code (0500-0599)
773            IssueKind::UnusedVariable { .. } => "MIR0500",
774            IssueKind::UnusedParam { .. } => "MIR0501",
775            IssueKind::UnreachableCode => "MIR0502",
776            IssueKind::UnusedMethod { .. } => "MIR0503",
777            IssueKind::UnusedProperty { .. } => "MIR0504",
778            IssueKind::UnusedFunction { .. } => "MIR0505",
779            IssueKind::UnusedForeachValue { .. } => "MIR0506",
780
781            // Readonly (0600-0699)
782            IssueKind::ReadonlyPropertyAssignment { .. } => "MIR0600",
783
784            // Inheritance (0700-0799)
785            IssueKind::UnimplementedAbstractMethod { .. } => "MIR0700",
786            IssueKind::UnimplementedInterfaceMethod { .. } => "MIR0701",
787            IssueKind::MethodSignatureMismatch { .. } => "MIR0702",
788            IssueKind::OverriddenMethodAccess { .. } => "MIR0703",
789            IssueKind::OverriddenPropertyAccess { .. } => "MIR0710",
790            IssueKind::InvalidExtendClass { .. } => "MIR0704",
791            IssueKind::FinalMethodOverridden { .. } => "MIR0705",
792            IssueKind::AbstractInstantiation { .. } => "MIR0706",
793            IssueKind::AbstractMethodCall { .. } => "MIR0711",
794            IssueKind::InterfaceInstantiation { .. } => "MIR0709",
795            IssueKind::CircularInheritance { .. } => "MIR0707",
796            IssueKind::InvalidOverride { .. } => "MIR0708",
797
798            // Security / taint (0800-0899)
799            IssueKind::TaintedInput { .. } => "MIR0800",
800            IssueKind::TaintedHtml => "MIR0801",
801            IssueKind::TaintedSql => "MIR0802",
802            IssueKind::TaintedShell => "MIR0803",
803
804            // Generics (0900-0999)
805            IssueKind::InvalidTemplateParam { .. } => "MIR0900",
806            IssueKind::ShadowedTemplateParam { .. } => "MIR0901",
807
808            // Deprecation / internal (1000-1099)
809            IssueKind::DeprecatedCall { .. } => "MIR1000",
810            IssueKind::WrongCaseFunction { .. } => "MIR1009",
811            IssueKind::WrongCaseMethod { .. } => "MIR1010",
812            IssueKind::WrongCaseClass { .. } => "MIR1011",
813            IssueKind::DeprecatedProperty { .. } => "MIR1005",
814            IssueKind::DeprecatedInterface { .. } => "MIR1006",
815            IssueKind::DeprecatedTrait { .. } => "MIR1007",
816            IssueKind::DeprecatedConstant { .. } => "MIR1008",
817            IssueKind::DeprecatedMethodCall { .. } => "MIR1001",
818            IssueKind::DeprecatedMethod { .. } => "MIR1002",
819            IssueKind::DeprecatedClass { .. } => "MIR1003",
820            IssueKind::InternalMethod { .. } => "MIR1004",
821
822            // Missing types / docblocks (1100-1199)
823            IssueKind::MissingReturnType { .. } => "MIR1100",
824            IssueKind::MissingParamType { .. } => "MIR1101",
825            IssueKind::MissingPropertyType { .. } => "MIR1104",
826            IssueKind::MissingThrowsDocblock { .. } => "MIR1102",
827            IssueKind::InvalidDocblock { .. } => "MIR1103",
828
829            // Mixed (1200-1299)
830            IssueKind::MixedArgument { .. } => "MIR1200",
831            IssueKind::MixedAssignment { .. } => "MIR1201",
832            IssueKind::MixedMethodCall { .. } => "MIR1202",
833            IssueKind::MixedPropertyFetch { .. } => "MIR1203",
834            IssueKind::MixedPropertyAssignment { .. } => "MIR1208",
835            IssueKind::MixedArrayAccess => "MIR1209",
836            IssueKind::MixedArrayOffset => "MIR1210",
837            IssueKind::MixedClone => "MIR1204",
838            IssueKind::InvalidClone { .. } => "MIR1205",
839            IssueKind::PossiblyInvalidClone { .. } => "MIR1206",
840            IssueKind::InvalidToString { .. } => "MIR1207",
841
842            // Trait (1300-1399)
843            IssueKind::InvalidTraitUse { .. } => "MIR1300",
844            IssueKind::ForbiddenCode { .. } => "MIR1301",
845
846            // Parse (1400-1499)
847            IssueKind::ParseError { .. } => "MIR1400",
848
849            // Attribute (1600-1699)
850            IssueKind::InvalidAttribute { .. } => "MIR1600",
851            IssueKind::UndefinedAttributeClass { .. } => "MIR1601",
852            IssueKind::DuplicateClass { .. } => "MIR1602",
853            IssueKind::DuplicateInterface { .. } => "MIR1603",
854            IssueKind::DuplicateTrait { .. } => "MIR1604",
855            IssueKind::DuplicateEnum { .. } => "MIR1605",
856            IssueKind::DuplicateFunction { .. } => "MIR1606",
857
858            // Other (1500-1599)
859            IssueKind::InvalidThrow { .. } => "MIR1500",
860            IssueKind::InvalidCatch { .. } => "MIR1503",
861            IssueKind::ImplicitToStringCast { .. } => "MIR1501",
862            IssueKind::ImplicitFloatToIntCast { .. } => "MIR1502",
863        }
864    }
865
866    /// Identifier name used in config and `@psalm-suppress` / `@suppress` annotations.
867    pub fn name(&self) -> &'static str {
868        match self {
869            IssueKind::NonStaticSelfCall { .. } => "NonStaticSelfCall",
870            IssueKind::DirectConstructorCall { .. } => "DirectConstructorCall",
871            IssueKind::InvalidScope { .. } => "InvalidScope",
872            IssueKind::UndefinedVariable { .. } => "UndefinedVariable",
873            IssueKind::UndefinedFunction { .. } => "UndefinedFunction",
874            IssueKind::UndefinedMethod { .. } => "UndefinedMethod",
875            IssueKind::UndefinedClass { .. } => "UndefinedClass",
876            IssueKind::UndefinedProperty { .. } => "UndefinedProperty",
877            IssueKind::UndefinedConstant { .. } => "UndefinedConstant",
878            IssueKind::InaccessibleClassConstant { .. } => "InaccessibleClassConstant",
879            IssueKind::PossiblyUndefinedVariable { .. } => "PossiblyUndefinedVariable",
880            IssueKind::UndefinedTrait { .. } => "UndefinedTrait",
881            IssueKind::ParentNotFound => "ParentNotFound",
882            IssueKind::InvalidStringClass { .. } => "InvalidStringClass",
883            IssueKind::NullArgument { .. } => "NullArgument",
884            IssueKind::NullPropertyFetch { .. } => "NullPropertyFetch",
885            IssueKind::NullMethodCall { .. } => "NullMethodCall",
886            IssueKind::NullArrayAccess => "NullArrayAccess",
887            IssueKind::PossiblyNullArgument { .. } => "PossiblyNullArgument",
888            IssueKind::PossiblyInvalidArgument { .. } => "PossiblyInvalidArgument",
889            IssueKind::PossiblyNullPropertyFetch { .. } => "PossiblyNullPropertyFetch",
890            IssueKind::PossiblyNullMethodCall { .. } => "PossiblyNullMethodCall",
891            IssueKind::PossiblyNullArrayAccess => "PossiblyNullArrayAccess",
892            IssueKind::NullableReturnStatement { .. } => "NullableReturnStatement",
893            IssueKind::InvalidReturnType { .. } => "InvalidReturnType",
894            IssueKind::InvalidArgument { .. } => "InvalidArgument",
895            IssueKind::TooFewArguments { .. } => "TooFewArguments",
896            IssueKind::TooManyArguments { .. } => "TooManyArguments",
897            IssueKind::InvalidNamedArgument { .. } => "InvalidNamedArgument",
898            IssueKind::InvalidNamedArguments { .. } => "InvalidNamedArguments",
899            IssueKind::InvalidPassByReference { .. } => "InvalidPassByReference",
900            IssueKind::InvalidPropertyFetch { .. } => "InvalidPropertyFetch",
901            IssueKind::InvalidArrayAccess { .. } => "InvalidArrayAccess",
902            IssueKind::InvalidArrayAssignment { .. } => "InvalidArrayAssignment",
903            IssueKind::InvalidPropertyAssignment { .. } => "InvalidPropertyAssignment",
904            IssueKind::InvalidCast { .. } => "InvalidCast",
905            IssueKind::InvalidStaticInvocation { .. } => "InvalidStaticInvocation",
906            IssueKind::InvalidOperand { .. } => "InvalidOperand",
907            IssueKind::PossiblyInvalidOperand { .. } => "PossiblyInvalidOperand",
908            IssueKind::PossiblyNullOperand { .. } => "PossiblyNullOperand",
909            IssueKind::RawObjectIteration { .. } => "RawObjectIteration",
910            IssueKind::PossiblyRawObjectIteration { .. } => "PossiblyRawObjectIteration",
911            IssueKind::MismatchingDocblockReturnType { .. } => "MismatchingDocblockReturnType",
912            IssueKind::MismatchingDocblockParamType { .. } => "MismatchingDocblockParamType",
913            IssueKind::TypeCheckMismatch { .. } => "TypeCheckMismatch",
914            IssueKind::Trace { .. } => "Trace",
915            IssueKind::InvalidArrayOffset { .. } => "InvalidArrayOffset",
916            IssueKind::NonExistentArrayOffset { .. } => "NonExistentArrayOffset",
917            IssueKind::PossiblyInvalidArrayOffset { .. } => "PossiblyInvalidArrayOffset",
918            IssueKind::RedundantCondition { .. } => "RedundantCondition",
919            IssueKind::RedundantCast { .. } => "RedundantCast",
920            IssueKind::UnnecessaryVarAnnotation { .. } => "UnnecessaryVarAnnotation",
921            IssueKind::TypeDoesNotContainType { .. } => "TypeDoesNotContainType",
922            IssueKind::ParadoxicalCondition { .. } => "ParadoxicalCondition",
923            IssueKind::UnhandledMatchCondition { .. } => "UnhandledMatchCondition",
924            IssueKind::UnusedVariable { .. } => "UnusedVariable",
925            IssueKind::UnusedParam { .. } => "UnusedParam",
926            IssueKind::UnreachableCode => "UnreachableCode",
927            IssueKind::UnusedMethod { .. } => "UnusedMethod",
928            IssueKind::UnusedProperty { .. } => "UnusedProperty",
929            IssueKind::UnusedFunction { .. } => "UnusedFunction",
930            IssueKind::UnusedForeachValue { .. } => "UnusedForeachValue",
931            IssueKind::UnimplementedAbstractMethod { .. } => "UnimplementedAbstractMethod",
932            IssueKind::UnimplementedInterfaceMethod { .. } => "UnimplementedInterfaceMethod",
933            IssueKind::MethodSignatureMismatch { .. } => "MethodSignatureMismatch",
934            IssueKind::OverriddenMethodAccess { .. } => "OverriddenMethodAccess",
935            IssueKind::OverriddenPropertyAccess { .. } => "OverriddenPropertyAccess",
936            IssueKind::InvalidExtendClass { .. } => "InvalidExtendClass",
937            IssueKind::FinalMethodOverridden { .. } => "FinalMethodOverridden",
938            IssueKind::AbstractInstantiation { .. } => "AbstractInstantiation",
939            IssueKind::AbstractMethodCall { .. } => "AbstractMethodCall",
940            IssueKind::InterfaceInstantiation { .. } => "InterfaceInstantiation",
941            IssueKind::InvalidOverride { .. } => "InvalidOverride",
942            IssueKind::ReadonlyPropertyAssignment { .. } => "ReadonlyPropertyAssignment",
943            IssueKind::InvalidTemplateParam { .. } => "InvalidTemplateParam",
944            IssueKind::ShadowedTemplateParam { .. } => "ShadowedTemplateParam",
945            IssueKind::TaintedInput { .. } => "TaintedInput",
946            IssueKind::TaintedHtml => "TaintedHtml",
947            IssueKind::TaintedSql => "TaintedSql",
948            IssueKind::TaintedShell => "TaintedShell",
949            IssueKind::DeprecatedCall { .. } => "DeprecatedCall",
950            IssueKind::DeprecatedProperty { .. } => "DeprecatedProperty",
951            IssueKind::DeprecatedConstant { .. } => "DeprecatedConstant",
952            IssueKind::DeprecatedInterface { .. } => "DeprecatedInterface",
953            IssueKind::DeprecatedTrait { .. } => "DeprecatedTrait",
954            IssueKind::DeprecatedMethodCall { .. } => "DeprecatedMethodCall",
955            IssueKind::DeprecatedMethod { .. } => "DeprecatedMethod",
956            IssueKind::DeprecatedClass { .. } => "DeprecatedClass",
957            IssueKind::InternalMethod { .. } => "InternalMethod",
958            IssueKind::MissingReturnType { .. } => "MissingReturnType",
959            IssueKind::MissingParamType { .. } => "MissingParamType",
960            IssueKind::MissingPropertyType { .. } => "MissingPropertyType",
961            IssueKind::InvalidThrow { .. } => "InvalidThrow",
962            IssueKind::InvalidCatch { .. } => "InvalidCatch",
963            IssueKind::MissingThrowsDocblock { .. } => "MissingThrowsDocblock",
964            IssueKind::ImplicitToStringCast { .. } => "ImplicitToStringCast",
965            IssueKind::ImplicitFloatToIntCast { .. } => "ImplicitFloatToIntCast",
966            IssueKind::ParseError { .. } => "ParseError",
967            IssueKind::InvalidDocblock { .. } => "InvalidDocblock",
968            IssueKind::MixedArgument { .. } => "MixedArgument",
969            IssueKind::MixedAssignment { .. } => "MixedAssignment",
970            IssueKind::MixedMethodCall { .. } => "MixedMethodCall",
971            IssueKind::MixedPropertyFetch { .. } => "MixedPropertyFetch",
972            IssueKind::MixedPropertyAssignment { .. } => "MixedPropertyAssignment",
973            IssueKind::MixedArrayAccess => "MixedArrayAccess",
974            IssueKind::MixedArrayOffset => "MixedArrayOffset",
975            IssueKind::MixedClone => "MixedClone",
976            IssueKind::InvalidClone { .. } => "InvalidClone",
977            IssueKind::PossiblyInvalidClone { .. } => "PossiblyInvalidClone",
978            IssueKind::InvalidToString { .. } => "InvalidToString",
979            IssueKind::CircularInheritance { .. } => "CircularInheritance",
980            IssueKind::InvalidTraitUse { .. } => "InvalidTraitUse",
981            IssueKind::ForbiddenCode { .. } => "ForbiddenCode",
982            IssueKind::WrongCaseFunction { .. } => "WrongCaseFunction",
983            IssueKind::WrongCaseMethod { .. } => "WrongCaseMethod",
984            IssueKind::WrongCaseClass { .. } => "WrongCaseClass",
985            IssueKind::InvalidAttribute { .. } => "InvalidAttribute",
986            IssueKind::UndefinedAttributeClass { .. } => "UndefinedAttributeClass",
987            IssueKind::DuplicateClass { .. } => "DuplicateClass",
988            IssueKind::DuplicateInterface { .. } => "DuplicateInterface",
989            IssueKind::DuplicateTrait { .. } => "DuplicateTrait",
990            IssueKind::DuplicateEnum { .. } => "DuplicateEnum",
991            IssueKind::DuplicateFunction { .. } => "DuplicateFunction",
992        }
993    }
994
995    /// Human-readable message for this issue.
996    pub fn message(&self) -> String {
997        match self {
998            IssueKind::NonStaticSelfCall { class, method } => {
999                format!("Non-static method {class}::{method}() cannot be called statically")
1000            }
1001            IssueKind::DirectConstructorCall { class } => {
1002                format!("Cannot call constructor of {class} directly")
1003            }
1004            IssueKind::InvalidScope { in_class } => {
1005                if *in_class {
1006                    "$this cannot be used in a static method".to_string()
1007                } else {
1008                    "$this cannot be used outside of a class".to_string()
1009                }
1010            }
1011            IssueKind::UndefinedVariable { name } => format!("Variable ${name} is not defined"),
1012            IssueKind::UndefinedFunction { name } => format!("Function {name}() is not defined"),
1013            IssueKind::UndefinedMethod { class, method } => {
1014                format!("Method {class}::{method}() does not exist")
1015            }
1016            IssueKind::UndefinedClass { name } => format!("Class {name} does not exist"),
1017            IssueKind::UndefinedProperty { class, property } => {
1018                format!("Property {class}::${property} does not exist")
1019            }
1020            IssueKind::UndefinedConstant { name } => format!("Constant {name} is not defined"),
1021            IssueKind::InaccessibleClassConstant { class, constant } => {
1022                format!("Cannot access constant {class}::{constant}")
1023            }
1024            IssueKind::PossiblyUndefinedVariable { name } => {
1025                format!("Variable ${name} might not be defined")
1026            }
1027            IssueKind::UndefinedTrait { name } => format!("Trait {name} does not exist"),
1028            IssueKind::ParentNotFound => {
1029                "Cannot use parent:: when current class has no parent".to_string()
1030            }
1031            IssueKind::InvalidStringClass { actual } => {
1032                format!("Dynamic class instantiation requires string or class-string type, got '{actual}'")
1033            }
1034
1035            IssueKind::NullArgument { param, fn_name } => {
1036                format!("Argument ${param} of {fn_name}() cannot be null")
1037            }
1038            IssueKind::NullPropertyFetch { property } => {
1039                format!("Cannot access property ${property} on null")
1040            }
1041            IssueKind::NullMethodCall { method } => {
1042                format!("Cannot call method {method}() on null")
1043            }
1044            IssueKind::NullArrayAccess => "Cannot access array on null".to_string(),
1045            IssueKind::PossiblyNullArgument { param, fn_name } => {
1046                format!("Argument ${param} of {fn_name}() might be null")
1047            }
1048            IssueKind::PossiblyInvalidArgument {
1049                param,
1050                fn_name,
1051                expected,
1052                actual,
1053            } => {
1054                format!("Argument ${param} of {fn_name}() expects '{expected}', possibly different type '{actual}' provided")
1055            }
1056            IssueKind::PossiblyNullPropertyFetch { property } => {
1057                format!("Cannot access property ${property} on possibly null value")
1058            }
1059            IssueKind::PossiblyNullMethodCall { method } => {
1060                format!("Cannot call method {method}() on possibly null value")
1061            }
1062            IssueKind::PossiblyNullArrayAccess => {
1063                "Cannot access array on possibly null value".to_string()
1064            }
1065            IssueKind::NullableReturnStatement { expected, actual } => {
1066                format!("Return type '{actual}' is not compatible with declared '{expected}'")
1067            }
1068
1069            IssueKind::InvalidReturnType { expected, actual } => {
1070                format!("Return type '{actual}' is not compatible with declared '{expected}'")
1071            }
1072            IssueKind::InvalidArgument {
1073                param,
1074                fn_name,
1075                expected,
1076                actual,
1077            } => {
1078                format!("Argument ${param} of {fn_name}() expects '{expected}', got '{actual}'")
1079            }
1080            IssueKind::TooFewArguments {
1081                fn_name,
1082                expected,
1083                actual,
1084            } => {
1085                format!(
1086                    "Too few arguments for {}(): expected {}, got {}",
1087                    fn_name, expected, actual
1088                )
1089            }
1090            IssueKind::TooManyArguments {
1091                fn_name,
1092                expected,
1093                actual,
1094            } => {
1095                format!(
1096                    "Too many arguments for {}(): expected {}, got {}",
1097                    fn_name, expected, actual
1098                )
1099            }
1100            IssueKind::InvalidNamedArgument { fn_name, name } => {
1101                format!("{}() has no parameter named ${}", fn_name, name)
1102            }
1103            IssueKind::InvalidNamedArguments { fn_name } => {
1104                format!("{}() does not accept named arguments", fn_name)
1105            }
1106            IssueKind::InvalidPassByReference { fn_name, param } => {
1107                format!(
1108                    "Argument ${} of {}() must be passed by reference",
1109                    param, fn_name
1110                )
1111            }
1112            IssueKind::InvalidPropertyFetch { ty } => {
1113                format!("Cannot fetch property on non-object type '{ty}'")
1114            }
1115            IssueKind::InvalidArrayAccess { ty } => {
1116                format!("Cannot use [] operator on non-array type '{ty}'")
1117            }
1118            IssueKind::InvalidArrayAssignment { ty } => {
1119                format!("Cannot use [] assignment on non-array type '{ty}'")
1120            }
1121            IssueKind::InvalidPropertyAssignment {
1122                property,
1123                expected,
1124                actual,
1125            } => {
1126                format!("Property ${property} expects '{expected}', cannot assign '{actual}'")
1127            }
1128            IssueKind::InvalidCast { from, to } => {
1129                format!("Cannot cast '{from}' to '{to}'")
1130            }
1131            IssueKind::InvalidStaticInvocation { class, method } => {
1132                format!("Non-static method {class}::{method}() cannot be called statically")
1133            }
1134            IssueKind::InvalidOperand { op, left, right } => {
1135                format!("Operator '{op}' not supported between '{left}' and '{right}'")
1136            }
1137            IssueKind::PossiblyInvalidOperand { op, left, right } => {
1138                format!("Operator '{op}' might not be supported between '{left}' and '{right}'")
1139            }
1140            IssueKind::PossiblyNullOperand { op, ty } => {
1141                format!("Operator '{op}' operand '{ty}' might be null")
1142            }
1143            IssueKind::RawObjectIteration { ty } => {
1144                format!("Cannot iterate over non-iterable object '{ty}'")
1145            }
1146            IssueKind::PossiblyRawObjectIteration { ty } => {
1147                format!("Cannot iterate over possibly non-iterable object '{ty}'")
1148            }
1149            IssueKind::MismatchingDocblockReturnType { declared, inferred } => {
1150                format!("Docblock return type '{declared}' does not match inferred '{inferred}'")
1151            }
1152            IssueKind::MismatchingDocblockParamType {
1153                param,
1154                declared,
1155                inferred,
1156            } => {
1157                format!(
1158                    "Docblock type '{declared}' for ${param} does not match inferred '{inferred}'"
1159                )
1160            }
1161            IssueKind::TypeCheckMismatch {
1162                var,
1163                expected,
1164                actual,
1165            } => {
1166                format!("Type of ${var} is expected to be {expected}, got {actual}")
1167            }
1168            IssueKind::Trace {
1169                variable,
1170                type_info,
1171            } => {
1172                format!("Type of ${variable} is {type_info}")
1173            }
1174
1175            IssueKind::InvalidArrayOffset { expected, actual } => {
1176                format!("Array offset expects '{expected}', got '{actual}'")
1177            }
1178            IssueKind::NonExistentArrayOffset { key } => {
1179                format!("Array offset '{key}' does not exist")
1180            }
1181            IssueKind::PossiblyInvalidArrayOffset { expected, actual } => {
1182                format!("Array offset might be invalid: expects '{expected}', got '{actual}'")
1183            }
1184
1185            IssueKind::RedundantCondition { ty } => {
1186                format!("Condition is always true/false for type '{ty}'")
1187            }
1188            IssueKind::RedundantCast { from, to } => {
1189                format!("Casting '{from}' to '{to}' is redundant")
1190            }
1191            IssueKind::UnnecessaryVarAnnotation { var } => {
1192                format!("@var annotation for ${var} is unnecessary")
1193            }
1194            IssueKind::TypeDoesNotContainType { left, right } => {
1195                format!("Type '{left}' can never contain type '{right}'")
1196            }
1197            IssueKind::ParadoxicalCondition { value } => {
1198                format!("Value {value} is duplicated; this branch can never be reached")
1199            }
1200            IssueKind::UnhandledMatchCondition { detail } => {
1201                format!("Unhandled match condition: {detail}")
1202            }
1203
1204            IssueKind::UnusedVariable { name } => format!("Variable ${name} is never read"),
1205            IssueKind::UnusedParam { name } => format!("Parameter ${name} is never used"),
1206            IssueKind::UnreachableCode => "Unreachable code detected".to_string(),
1207            IssueKind::UnusedMethod { class, method } => {
1208                format!("Private method {class}::{method}() is never called")
1209            }
1210            IssueKind::UnusedProperty { class, property } => {
1211                format!("Private property {class}::${property} is never read")
1212            }
1213            IssueKind::UnusedFunction { name } => {
1214                format!("Function {name}() is never called")
1215            }
1216            IssueKind::UnusedForeachValue { name } => {
1217                format!("Foreach value ${name} is never read")
1218            }
1219
1220            IssueKind::UnimplementedAbstractMethod { class, method } => {
1221                format!("Class {class} must implement abstract method {method}()")
1222            }
1223            IssueKind::UnimplementedInterfaceMethod {
1224                class,
1225                interface,
1226                method,
1227            } => {
1228                format!("Class {class} must implement {interface}::{method}() from interface")
1229            }
1230            IssueKind::MethodSignatureMismatch {
1231                class,
1232                method,
1233                detail,
1234            } => {
1235                format!("Method {class}::{method}() signature mismatch: {detail}")
1236            }
1237            IssueKind::OverriddenMethodAccess { class, method } => {
1238                format!("Method {class}::{method}() overrides with less visibility")
1239            }
1240            IssueKind::OverriddenPropertyAccess { class, property } => {
1241                format!("Property {class}::${property} overrides with less visibility")
1242            }
1243            IssueKind::ReadonlyPropertyAssignment { class, property } => {
1244                format!(
1245                    "Cannot assign to readonly property {class}::${property} outside of constructor"
1246                )
1247            }
1248            IssueKind::InvalidExtendClass { parent, child } => {
1249                format!("Class {child} cannot extend final class {parent}")
1250            }
1251            IssueKind::InvalidTemplateParam {
1252                name,
1253                expected_bound,
1254                actual,
1255            } => {
1256                format!(
1257                    "Template type '{name}' inferred as '{actual}' does not satisfy bound '{expected_bound}'"
1258                )
1259            }
1260            IssueKind::ShadowedTemplateParam { name } => {
1261                format!(
1262                    "Method template parameter '{name}' shadows class-level template parameter with the same name"
1263                )
1264            }
1265            IssueKind::FinalMethodOverridden {
1266                class,
1267                method,
1268                parent,
1269            } => {
1270                format!("Method {class}::{method}() cannot override final method from {parent}")
1271            }
1272            IssueKind::AbstractInstantiation { class } => {
1273                format!("Cannot instantiate abstract class {class}")
1274            }
1275            IssueKind::AbstractMethodCall { class, method } => {
1276                format!("Cannot call abstract method {class}::{method}()")
1277            }
1278            IssueKind::InterfaceInstantiation { class } => {
1279                format!("Cannot instantiate interface {class}")
1280            }
1281            IssueKind::InvalidOverride {
1282                class,
1283                method,
1284                detail,
1285            } => {
1286                format!("Method {class}::{method}() has #[Override] but {detail}")
1287            }
1288
1289            IssueKind::TaintedInput { sink } => format!("Tainted input reaching sink '{sink}'"),
1290            IssueKind::TaintedHtml => "Tainted HTML output — possible XSS".to_string(),
1291            IssueKind::TaintedSql => "Tainted SQL query — possible SQL injection".to_string(),
1292            IssueKind::TaintedShell => {
1293                "Tainted shell command — possible command injection".to_string()
1294            }
1295
1296            IssueKind::DeprecatedCall { name, message } => {
1297                let base = format!("Call to deprecated function {name}");
1298                append_deprecation_message(base, message)
1299            }
1300            IssueKind::DeprecatedProperty {
1301                class,
1302                property,
1303                message,
1304            } => {
1305                let base = format!("Property {class}::${property} is deprecated");
1306                append_deprecation_message(base, message)
1307            }
1308            IssueKind::DeprecatedConstant {
1309                class,
1310                constant,
1311                message,
1312            } => {
1313                let base = format!("Constant {class}::{constant} is deprecated");
1314                append_deprecation_message(base, message)
1315            }
1316            IssueKind::DeprecatedInterface { name, message } => {
1317                let base = format!("Interface {name} is deprecated");
1318                append_deprecation_message(base, message)
1319            }
1320            IssueKind::DeprecatedTrait { name, message } => {
1321                let base = format!("Trait {name} is deprecated");
1322                append_deprecation_message(base, message)
1323            }
1324            IssueKind::DeprecatedMethodCall {
1325                class,
1326                method,
1327                message,
1328            } => {
1329                let base = format!("Call to deprecated method {class}::{method}");
1330                append_deprecation_message(base, message)
1331            }
1332            IssueKind::DeprecatedMethod {
1333                class,
1334                method,
1335                message,
1336            } => {
1337                let base = format!("Method {class}::{method}() is deprecated");
1338                append_deprecation_message(base, message)
1339            }
1340            IssueKind::DeprecatedClass { name, message } => {
1341                let base = format!("Class {name} is deprecated");
1342                append_deprecation_message(base, message)
1343            }
1344            IssueKind::InternalMethod { class, method } => {
1345                format!("Method {class}::{method}() is marked @internal")
1346            }
1347            IssueKind::MissingReturnType { fn_name } => {
1348                format!("Function {fn_name}() has no return type annotation")
1349            }
1350            IssueKind::MissingParamType { fn_name, param } => {
1351                format!("Parameter ${param} of {fn_name}() has no type annotation")
1352            }
1353            IssueKind::MissingPropertyType { class, property } => {
1354                format!("Property {class}::${property} has no type annotation")
1355            }
1356            IssueKind::InvalidThrow { ty } => {
1357                format!("Thrown type '{ty}' does not extend Throwable")
1358            }
1359            IssueKind::InvalidCatch { ty } => {
1360                format!("Caught type '{ty}' does not extend Throwable")
1361            }
1362            IssueKind::MissingThrowsDocblock { class } => {
1363                format!("Exception {class} is thrown but not declared in @throws")
1364            }
1365            IssueKind::ImplicitToStringCast { class } => {
1366                format!("Class {class} is implicitly cast to string")
1367            }
1368            IssueKind::ImplicitFloatToIntCast { from } => {
1369                format!("Implicit cast from {from} to int truncates the fractional part")
1370            }
1371            IssueKind::ParseError { message } => format!("Parse error: {message}"),
1372            IssueKind::InvalidDocblock { message } => format!("Invalid docblock: {message}"),
1373            IssueKind::MixedArgument { param, fn_name } => {
1374                format!("Argument ${param} of {fn_name}() is mixed")
1375            }
1376            IssueKind::MixedAssignment { var } => {
1377                format!("Variable ${var} is assigned a mixed type")
1378            }
1379            IssueKind::MixedMethodCall { method } => {
1380                format!("Method {method}() called on mixed type")
1381            }
1382            IssueKind::MixedPropertyFetch { property } => {
1383                format!("Property ${property} fetched on mixed type")
1384            }
1385            IssueKind::MixedPropertyAssignment { property } => {
1386                format!("Property ${property} assigned on mixed type")
1387            }
1388            IssueKind::MixedArrayAccess => "Array access on mixed type".to_string(),
1389            IssueKind::MixedArrayOffset => "Mixed type used as array offset".to_string(),
1390            IssueKind::MixedClone => "cannot clone mixed".to_string(),
1391            IssueKind::InvalidClone { ty } => format!("cannot clone non-object {ty}"),
1392            IssueKind::PossiblyInvalidClone { ty } => {
1393                format!("cannot clone possibly non-object {ty}")
1394            }
1395            IssueKind::InvalidToString { class } => {
1396                format!("Method {class}::__toString() must return a string")
1397            }
1398            IssueKind::CircularInheritance { class } => {
1399                format!("Class {class} has a circular inheritance chain")
1400            }
1401            IssueKind::InvalidTraitUse { trait_name, reason } => {
1402                format!("Trait {trait_name} used incorrectly: {reason}")
1403            }
1404            IssueKind::WrongCaseFunction { used, canonical } => {
1405                format!("Function name '{used}' has incorrect casing; use '{canonical}'")
1406            }
1407            IssueKind::WrongCaseMethod {
1408                class,
1409                used,
1410                canonical,
1411            } => {
1412                format!("Method name '{class}::{used}' has incorrect casing; use '{canonical}'")
1413            }
1414            IssueKind::WrongCaseClass { used, canonical } => {
1415                format!("Class name '{used}' has incorrect casing; use '{canonical}'")
1416            }
1417            IssueKind::InvalidAttribute { message } => message.clone(),
1418            IssueKind::UndefinedAttributeClass { name } => {
1419                format!("Attribute class {name} does not exist")
1420            }
1421            IssueKind::ForbiddenCode { message } => message.clone(),
1422            IssueKind::DuplicateClass { name } => {
1423                format!("Class {name} has already been defined")
1424            }
1425            IssueKind::DuplicateInterface { name } => {
1426                format!("Interface {name} has already been defined")
1427            }
1428            IssueKind::DuplicateTrait { name } => {
1429                format!("Trait {name} has already been defined")
1430            }
1431            IssueKind::DuplicateEnum { name } => {
1432                format!("Enum {name} has already been defined")
1433            }
1434            IssueKind::DuplicateFunction { name } => {
1435                format!("Function {name}() has already been defined")
1436            }
1437        }
1438    }
1439}
1440
1441// ---------------------------------------------------------------------------
1442// Issue
1443// ---------------------------------------------------------------------------
1444
1445#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
1446pub struct Issue {
1447    pub kind: IssueKind,
1448    pub severity: Severity,
1449    pub location: Location,
1450    pub snippet: Option<String>,
1451    pub suppressed: bool,
1452}
1453
1454impl Issue {
1455    pub fn new(kind: IssueKind, location: Location) -> Self {
1456        let severity = kind.default_severity();
1457        Self {
1458            severity,
1459            kind,
1460            location,
1461            snippet: None,
1462            suppressed: false,
1463        }
1464    }
1465
1466    pub fn with_snippet(mut self, snippet: impl Into<String>) -> Self {
1467        self.snippet = Some(snippet.into());
1468        self
1469    }
1470
1471    pub fn suppress(mut self) -> Self {
1472        self.suppressed = true;
1473        self
1474    }
1475}
1476
1477impl fmt::Display for Issue {
1478    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1479        let sev = match self.severity {
1480            Severity::Error => "error".red().to_string(),
1481            Severity::Warning => "warning".yellow().to_string(),
1482            Severity::Info => "info".blue().to_string(),
1483        };
1484        write!(
1485            f,
1486            "{} {}[{}] {}: {}",
1487            self.location.bright_black(),
1488            sev,
1489            self.kind.code().bright_black(),
1490            self.kind.name().bold(),
1491            self.kind.message()
1492        )
1493    }
1494}
1495
1496// ---------------------------------------------------------------------------
1497// IssueBuffer — collects issues for a single file pass
1498// ---------------------------------------------------------------------------
1499
1500#[derive(Debug, Default)]
1501pub struct IssueBuffer {
1502    issues: Vec<Issue>,
1503    seen: HashSet<(&'static str, Arc<str>, u32, u16)>,
1504    /// Issue names suppressed at the file level (from `@psalm-suppress` / `@suppress` on the file docblock)
1505    file_suppressions: Vec<String>,
1506}
1507
1508impl IssueBuffer {
1509    pub fn new() -> Self {
1510        Self::default()
1511    }
1512
1513    pub fn add(&mut self, issue: Issue) {
1514        let key = (
1515            issue.kind.name(),
1516            issue.location.file.clone(),
1517            issue.location.line,
1518            issue.location.col_start,
1519        );
1520        if self.seen.insert(key) {
1521            self.issues.push(issue);
1522        }
1523    }
1524
1525    pub fn add_suppression(&mut self, name: impl Into<String>) {
1526        self.file_suppressions.push(name.into());
1527    }
1528
1529    /// Consume the buffer and return unsuppressed issues.
1530    pub fn into_issues(self) -> Vec<Issue> {
1531        self.issues
1532            .into_iter()
1533            .filter(|i| !i.suppressed)
1534            .filter(|i| !self.file_suppressions.contains(&i.kind.name().to_string()))
1535            .collect()
1536    }
1537
1538    /// Mark all issues added since index `from` as suppressed if their issue
1539    /// name appears in `suppressions`. Used for `@psalm-suppress` / `@suppress` on statements.
1540    pub fn suppress_range(&mut self, from: usize, suppressions: &[String]) {
1541        if suppressions.is_empty() {
1542            return;
1543        }
1544        for issue in self.issues[from..].iter_mut() {
1545            if suppressions.iter().any(|s| s == issue.kind.name()) {
1546                issue.suppressed = true;
1547            }
1548        }
1549    }
1550
1551    /// Current number of buffered issues. Use before analyzing a statement to
1552    /// get the `from` index for `suppress_range`.
1553    pub fn issue_count(&self) -> usize {
1554        self.issues.len()
1555    }
1556
1557    pub fn is_empty(&self) -> bool {
1558        self.issues.is_empty()
1559    }
1560
1561    pub fn len(&self) -> usize {
1562        self.issues.len()
1563    }
1564
1565    pub fn error_count(&self) -> usize {
1566        self.issues
1567            .iter()
1568            .filter(|i| !i.suppressed && i.severity == Severity::Error)
1569            .count()
1570    }
1571
1572    pub fn warning_count(&self) -> usize {
1573        self.issues
1574            .iter()
1575            .filter(|i| !i.suppressed && i.severity == Severity::Warning)
1576            .count()
1577    }
1578}
1579
1580#[cfg(test)]
1581mod code_tests {
1582    use super::*;
1583    use std::collections::HashSet;
1584
1585    /// Returns one instance of every `IssueKind` variant.
1586    ///
1587    /// Updating `IssueKind` without updating this list will compile (it's a
1588    /// regular `Vec`), but `codes_cover_every_variant` will catch the omission
1589    /// — the test below asserts the count matches the exhaustive `code()` arm.
1590    fn one_of_each() -> Vec<IssueKind> {
1591        let s = || String::new();
1592        vec![
1593            IssueKind::InvalidScope { in_class: false },
1594            IssueKind::NonStaticSelfCall {
1595                class: s(),
1596                method: s(),
1597            },
1598            IssueKind::DirectConstructorCall { class: s() },
1599            IssueKind::UndefinedVariable { name: s() },
1600            IssueKind::UndefinedFunction { name: s() },
1601            IssueKind::UndefinedMethod {
1602                class: s(),
1603                method: s(),
1604            },
1605            IssueKind::UndefinedClass { name: s() },
1606            IssueKind::UndefinedProperty {
1607                class: s(),
1608                property: s(),
1609            },
1610            IssueKind::UndefinedConstant { name: s() },
1611            IssueKind::InaccessibleClassConstant {
1612                class: s(),
1613                constant: s(),
1614            },
1615            IssueKind::PossiblyUndefinedVariable { name: s() },
1616            IssueKind::UndefinedTrait { name: s() },
1617            IssueKind::ParentNotFound,
1618            IssueKind::NullArgument {
1619                param: s(),
1620                fn_name: s(),
1621            },
1622            IssueKind::NullPropertyFetch { property: s() },
1623            IssueKind::NullMethodCall { method: s() },
1624            IssueKind::NullArrayAccess,
1625            IssueKind::PossiblyNullArgument {
1626                param: s(),
1627                fn_name: s(),
1628            },
1629            IssueKind::PossiblyInvalidArgument {
1630                param: s(),
1631                fn_name: s(),
1632                expected: s(),
1633                actual: s(),
1634            },
1635            IssueKind::PossiblyNullPropertyFetch { property: s() },
1636            IssueKind::PossiblyNullMethodCall { method: s() },
1637            IssueKind::PossiblyNullArrayAccess,
1638            IssueKind::NullableReturnStatement {
1639                expected: s(),
1640                actual: s(),
1641            },
1642            IssueKind::InvalidReturnType {
1643                expected: s(),
1644                actual: s(),
1645            },
1646            IssueKind::InvalidArgument {
1647                param: s(),
1648                fn_name: s(),
1649                expected: s(),
1650                actual: s(),
1651            },
1652            IssueKind::TooFewArguments {
1653                fn_name: s(),
1654                expected: 0,
1655                actual: 0,
1656            },
1657            IssueKind::TooManyArguments {
1658                fn_name: s(),
1659                expected: 0,
1660                actual: 0,
1661            },
1662            IssueKind::InvalidNamedArgument {
1663                fn_name: s(),
1664                name: s(),
1665            },
1666            IssueKind::InvalidNamedArguments { fn_name: s() },
1667            IssueKind::InvalidPassByReference {
1668                fn_name: s(),
1669                param: s(),
1670            },
1671            IssueKind::InvalidPropertyFetch { ty: s() },
1672            IssueKind::InvalidArrayAccess { ty: s() },
1673            IssueKind::InvalidArrayAssignment { ty: s() },
1674            IssueKind::InvalidPropertyAssignment {
1675                property: s(),
1676                expected: s(),
1677                actual: s(),
1678            },
1679            IssueKind::InvalidCast { from: s(), to: s() },
1680            IssueKind::InvalidStaticInvocation {
1681                class: s(),
1682                method: s(),
1683            },
1684            IssueKind::InvalidOperand {
1685                op: s(),
1686                left: s(),
1687                right: s(),
1688            },
1689            IssueKind::PossiblyInvalidOperand {
1690                op: s(),
1691                left: s(),
1692                right: s(),
1693            },
1694            IssueKind::PossiblyNullOperand { op: s(), ty: s() },
1695            IssueKind::RawObjectIteration { ty: s() },
1696            IssueKind::PossiblyRawObjectIteration { ty: s() },
1697            IssueKind::MismatchingDocblockReturnType {
1698                declared: s(),
1699                inferred: s(),
1700            },
1701            IssueKind::MismatchingDocblockParamType {
1702                param: s(),
1703                declared: s(),
1704                inferred: s(),
1705            },
1706            IssueKind::TypeCheckMismatch {
1707                var: s(),
1708                expected: s(),
1709                actual: s(),
1710            },
1711            IssueKind::Trace {
1712                variable: s(),
1713                type_info: s(),
1714            },
1715            IssueKind::InvalidArrayOffset {
1716                expected: s(),
1717                actual: s(),
1718            },
1719            IssueKind::NonExistentArrayOffset { key: s() },
1720            IssueKind::PossiblyInvalidArrayOffset {
1721                expected: s(),
1722                actual: s(),
1723            },
1724            IssueKind::RedundantCondition { ty: s() },
1725            IssueKind::RedundantCast { from: s(), to: s() },
1726            IssueKind::UnnecessaryVarAnnotation { var: s() },
1727            IssueKind::TypeDoesNotContainType {
1728                left: s(),
1729                right: s(),
1730            },
1731            IssueKind::UnusedVariable { name: s() },
1732            IssueKind::UnusedParam { name: s() },
1733            IssueKind::UnreachableCode,
1734            IssueKind::UnhandledMatchCondition { detail: s() },
1735            IssueKind::UnusedMethod {
1736                class: s(),
1737                method: s(),
1738            },
1739            IssueKind::UnusedProperty {
1740                class: s(),
1741                property: s(),
1742            },
1743            IssueKind::UnusedFunction { name: s() },
1744            IssueKind::UnusedForeachValue { name: s() },
1745            IssueKind::ReadonlyPropertyAssignment {
1746                class: s(),
1747                property: s(),
1748            },
1749            IssueKind::UnimplementedAbstractMethod {
1750                class: s(),
1751                method: s(),
1752            },
1753            IssueKind::UnimplementedInterfaceMethod {
1754                class: s(),
1755                interface: s(),
1756                method: s(),
1757            },
1758            IssueKind::MethodSignatureMismatch {
1759                class: s(),
1760                method: s(),
1761                detail: s(),
1762            },
1763            IssueKind::OverriddenMethodAccess {
1764                class: s(),
1765                method: s(),
1766            },
1767            IssueKind::OverriddenPropertyAccess {
1768                class: s(),
1769                property: s(),
1770            },
1771            IssueKind::InvalidExtendClass {
1772                parent: s(),
1773                child: s(),
1774            },
1775            IssueKind::FinalMethodOverridden {
1776                class: s(),
1777                method: s(),
1778                parent: s(),
1779            },
1780            IssueKind::AbstractInstantiation { class: s() },
1781            IssueKind::AbstractMethodCall {
1782                class: s(),
1783                method: s(),
1784            },
1785            IssueKind::InterfaceInstantiation { class: s() },
1786            IssueKind::InvalidOverride {
1787                class: s(),
1788                method: s(),
1789                detail: s(),
1790            },
1791            IssueKind::CircularInheritance { class: s() },
1792            IssueKind::TaintedInput { sink: s() },
1793            IssueKind::TaintedHtml,
1794            IssueKind::TaintedSql,
1795            IssueKind::TaintedShell,
1796            IssueKind::InvalidTemplateParam {
1797                name: s(),
1798                expected_bound: s(),
1799                actual: s(),
1800            },
1801            IssueKind::ShadowedTemplateParam { name: s() },
1802            IssueKind::DeprecatedCall {
1803                name: s(),
1804                message: None,
1805            },
1806            IssueKind::DeprecatedProperty {
1807                class: s(),
1808                property: s(),
1809                message: None,
1810            },
1811            IssueKind::DeprecatedConstant {
1812                class: s(),
1813                constant: s(),
1814                message: None,
1815            },
1816            IssueKind::DeprecatedInterface {
1817                name: s(),
1818                message: None,
1819            },
1820            IssueKind::DeprecatedTrait {
1821                name: s(),
1822                message: None,
1823            },
1824            IssueKind::DeprecatedMethodCall {
1825                class: s(),
1826                method: s(),
1827                message: None,
1828            },
1829            IssueKind::DeprecatedMethod {
1830                class: s(),
1831                method: s(),
1832                message: None,
1833            },
1834            IssueKind::DeprecatedClass {
1835                name: s(),
1836                message: None,
1837            },
1838            IssueKind::InternalMethod {
1839                class: s(),
1840                method: s(),
1841            },
1842            IssueKind::MissingReturnType { fn_name: s() },
1843            IssueKind::MissingParamType {
1844                fn_name: s(),
1845                param: s(),
1846            },
1847            IssueKind::MissingPropertyType {
1848                class: s(),
1849                property: s(),
1850            },
1851            IssueKind::MissingThrowsDocblock { class: s() },
1852            IssueKind::InvalidDocblock { message: s() },
1853            IssueKind::MixedArgument {
1854                param: s(),
1855                fn_name: s(),
1856            },
1857            IssueKind::MixedAssignment { var: s() },
1858            IssueKind::MixedMethodCall { method: s() },
1859            IssueKind::MixedPropertyFetch { property: s() },
1860            IssueKind::MixedPropertyAssignment { property: s() },
1861            IssueKind::MixedArrayAccess,
1862            IssueKind::MixedArrayOffset,
1863            IssueKind::MixedClone,
1864            IssueKind::InvalidClone { ty: s() },
1865            IssueKind::PossiblyInvalidClone { ty: s() },
1866            IssueKind::InvalidToString { class: s() },
1867            IssueKind::InvalidTraitUse {
1868                trait_name: s(),
1869                reason: s(),
1870            },
1871            IssueKind::ParseError { message: s() },
1872            IssueKind::InvalidThrow { ty: s() },
1873            IssueKind::InvalidCatch { ty: s() },
1874            IssueKind::ImplicitToStringCast { class: s() },
1875            IssueKind::ImplicitFloatToIntCast { from: s() },
1876            IssueKind::WrongCaseFunction {
1877                used: s(),
1878                canonical: s(),
1879            },
1880            IssueKind::WrongCaseMethod {
1881                class: s(),
1882                used: s(),
1883                canonical: s(),
1884            },
1885            IssueKind::WrongCaseClass {
1886                used: s(),
1887                canonical: s(),
1888            },
1889            IssueKind::InvalidAttribute { message: s() },
1890            IssueKind::UndefinedAttributeClass { name: s() },
1891            IssueKind::ForbiddenCode { message: s() },
1892            IssueKind::DuplicateClass { name: s() },
1893            IssueKind::DuplicateInterface { name: s() },
1894            IssueKind::DuplicateTrait { name: s() },
1895            IssueKind::DuplicateEnum { name: s() },
1896            IssueKind::DuplicateFunction { name: s() },
1897        ]
1898    }
1899
1900    #[test]
1901    fn codes_have_expected_shape() {
1902        for kind in one_of_each() {
1903            let code = kind.code();
1904            assert!(
1905                code.len() == 7
1906                    && code.starts_with("MIR")
1907                    && code[3..].chars().all(|c| c.is_ascii_digit()),
1908                "code {code:?} for {} does not match MIR####",
1909                kind.name(),
1910            );
1911        }
1912    }
1913
1914    #[test]
1915    fn codes_are_unique() {
1916        let kinds = one_of_each();
1917        let mut seen: HashSet<&'static str> = HashSet::new();
1918        for kind in &kinds {
1919            assert!(
1920                seen.insert(kind.code()),
1921                "duplicate code {} (variant {})",
1922                kind.code(),
1923                kind.name(),
1924            );
1925        }
1926    }
1927
1928    #[test]
1929    fn display_includes_code() {
1930        let issue = Issue::new(
1931            IssueKind::UndefinedClass {
1932                name: "Foo".to_string(),
1933            },
1934            Location {
1935                file: Arc::from("src/x.php"),
1936                line: 1,
1937                line_end: 1,
1938                col_start: 0,
1939                col_end: 3,
1940            },
1941        );
1942        // Strip ANSI escape sequences so the assertion isn't dependent on
1943        // owo-colors' tty detection.
1944        let raw = format!("{issue}");
1945        let stripped: String = {
1946            let mut out = String::new();
1947            let mut chars = raw.chars();
1948            while let Some(c) = chars.next() {
1949                if c == '\u{1b}' {
1950                    for c2 in chars.by_ref() {
1951                        if c2 == 'm' {
1952                            break;
1953                        }
1954                    }
1955                } else {
1956                    out.push(c);
1957                }
1958            }
1959            out
1960        };
1961        assert!(
1962            stripped.contains("error[MIR0005] UndefinedClass:"),
1963            "Display output missing code/name segment: {stripped:?}",
1964        );
1965    }
1966
1967    /// Guards against forgetting to add a new variant to `one_of_each()`.
1968    /// If you add a variant, add it to `one_of_each()` *and* bump this count.
1969    #[test]
1970    fn one_of_each_has_every_variant() {
1971        // If this assertion fires after you added a new variant, also add it
1972        // to `one_of_each()` so the uniqueness and shape tests cover it.
1973        assert_eq!(one_of_each().len(), 121);
1974    }
1975}