1use std::collections::HashSet;
2use std::fmt;
3use std::sync::Arc;
4
5use owo_colors::OwoColorize;
6use serde::{Deserialize, Serialize};
7
8#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
13pub enum Severity {
14 Info,
16 Warning,
18 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
32pub use mir_types::Location;
37
38#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
43#[non_exhaustive]
44pub enum IssueKind {
45 NonStaticSelfCall { class: String, method: String },
51 InvalidScope {
52 in_class: bool,
54 },
55 UndefinedVariable { name: String },
58 UndefinedFunction { name: String },
61 UndefinedMethod { class: String, method: String },
64 UndefinedClass { name: String },
67 UndefinedProperty { class: String, property: String },
70 UndefinedConstant { name: String },
73 InaccessibleClassConstant { class: String, constant: String },
76 PossiblyUndefinedVariable { name: String },
79 UndefinedTrait { name: String },
82 ParentNotFound,
85 InvalidStringClass { actual: String },
88
89 NullArgument { param: String, fn_name: String },
93 NullPropertyFetch { property: String },
96 NullMethodCall { method: String },
99 NullArrayAccess,
102 PossiblyNullArgument { param: String, fn_name: String },
105 PossiblyInvalidArgument {
108 param: String,
109 fn_name: String,
110 expected: String,
111 actual: String,
112 },
113 PossiblyNullPropertyFetch { property: String },
116 PossiblyNullMethodCall { method: String },
119 PossiblyNullArrayAccess,
122 NullableReturnStatement { expected: String, actual: String },
124
125 InvalidReturnType { expected: String, actual: String },
129 InvalidArgument {
132 param: String,
133 fn_name: String,
134 expected: String,
135 actual: String,
136 },
137 TooFewArguments {
140 fn_name: String,
141 expected: usize,
142 actual: usize,
143 },
144 TooManyArguments {
147 fn_name: String,
148 expected: usize,
149 actual: usize,
150 },
151 InvalidNamedArgument { fn_name: String, name: String },
154 InvalidNamedArguments { fn_name: String },
157 InvalidPassByReference { fn_name: String, param: String },
160 InvalidPropertyFetch { ty: String },
163 InvalidArrayAccess { ty: String },
166 InvalidArrayAssignment { ty: String },
169 InvalidPropertyAssignment {
172 property: String,
173 expected: String,
174 actual: String,
175 },
176 InvalidCast { from: String, to: String },
179 InvalidStaticInvocation { class: String, method: String },
182 InvalidOperand {
186 op: String,
187 left: String,
188 right: String,
189 },
190 PossiblyInvalidOperand {
193 op: String,
194 left: String,
195 right: String,
196 },
197 PossiblyNullOperand { op: String, ty: String },
200 RawObjectIteration { ty: String },
203 PossiblyRawObjectIteration { ty: String },
206 MismatchingDocblockReturnType { declared: String, inferred: String },
208 MismatchingDocblockParamType {
210 param: String,
211 declared: String,
212 inferred: String,
213 },
214 TypeCheckMismatch {
217 var: String,
218 expected: String,
219 actual: String,
220 },
221
222 Trace { variable: String, type_info: String },
225
226 InvalidArrayOffset { expected: String, actual: String },
229 NonExistentArrayOffset { key: String },
231 PossiblyInvalidArrayOffset { expected: String, actual: String },
234
235 RedundantCondition { ty: String },
239 RedundantCast { from: String, to: String },
242 UnnecessaryVarAnnotation { var: String },
244 TypeDoesNotContainType { left: String, right: String },
246 ParadoxicalCondition { value: String },
249
250 UnusedVariable { name: String },
254 UnusedParam { name: String },
257 UnreachableCode,
260 UnhandledMatchCondition { detail: String },
263 UnusedMethod { class: String, method: String },
266 UnusedProperty { class: String, property: String },
269 UnusedFunction { name: String },
272 UnusedForeachValue { name: String },
275
276 ReadonlyPropertyAssignment { class: String, property: String },
280
281 UnimplementedAbstractMethod { class: String, method: String },
285 UnimplementedInterfaceMethod {
288 class: String,
289 interface: String,
290 method: String,
291 },
292 MethodSignatureMismatch {
295 class: String,
296 method: String,
297 detail: String,
298 },
299 OverriddenMethodAccess { class: String, method: String },
302 OverriddenPropertyAccess { class: String, property: String },
305 DirectConstructorCall { class: String },
308 InvalidExtendClass { parent: String, child: String },
311 FinalMethodOverridden {
314 class: String,
315 method: String,
316 parent: String,
317 },
318 AbstractInstantiation { class: String },
321 AbstractMethodCall { class: String, method: String },
324 InterfaceInstantiation { class: String },
327 InvalidOverride {
331 class: String,
332 method: String,
333 detail: String,
334 },
335
336 TaintedInput { sink: String },
340 TaintedHtml,
343 TaintedSql,
346 TaintedShell,
349
350 InvalidTemplateParam {
354 name: String,
355 expected_bound: String,
356 actual: String,
357 },
358 ShadowedTemplateParam { name: String },
361
362 DeprecatedCall {
366 name: String,
367 message: Option<Arc<str>>,
368 },
369 DeprecatedProperty {
372 class: String,
373 property: String,
374 message: Option<Arc<str>>,
375 },
376 DeprecatedConstant {
379 class: String,
380 constant: String,
381 message: Option<Arc<str>>,
382 },
383 DeprecatedInterface {
386 name: String,
387 message: Option<Arc<str>>,
388 },
389 DeprecatedTrait {
392 name: String,
393 message: Option<Arc<str>>,
394 },
395 DeprecatedMethodCall {
398 class: String,
399 method: String,
400 message: Option<Arc<str>>,
401 },
402 DeprecatedMethod {
405 class: String,
406 method: String,
407 message: Option<Arc<str>>,
408 },
409 DeprecatedClass {
412 name: String,
413 message: Option<Arc<str>>,
414 },
415 InternalMethod { class: String, method: String },
418 MissingReturnType { fn_name: String },
420 MissingParamType { fn_name: String, param: String },
422 MissingPropertyType { class: String, property: String },
425 InvalidThrow { ty: String },
428 InvalidCatch { ty: String },
431 MissingThrowsDocblock { class: String },
434 ImplicitToStringCast { class: String },
437 ImplicitFloatToIntCast { from: String },
440 ParseError { message: String },
443 InvalidDocblock { message: String },
446 MixedArgument { param: String, fn_name: String },
448 MixedAssignment { var: String },
450 MixedMethodCall { method: String },
453 MixedPropertyFetch { property: String },
456 MixedPropertyAssignment { property: String },
459 MixedArrayAccess,
462 MixedArrayOffset,
465 MixedClone,
468 InvalidClone { ty: String },
472 PossiblyInvalidClone { ty: String },
476 InvalidToString { class: String },
480 CircularInheritance { class: String },
483
484 InvalidTraitUse { trait_name: String, reason: String },
488 ForbiddenCode { message: String },
491
492 InvalidAttribute { message: String },
496 UndefinedAttributeClass { name: String },
499
500 WrongCaseFunction { used: String, canonical: String },
504 WrongCaseMethod {
507 class: String,
508 used: String,
509 canonical: String,
510 },
511 WrongCaseClass { used: String, canonical: String },
514 DuplicateClass { name: String },
517 DuplicateInterface { name: String },
520 DuplicateTrait { name: String },
523 DuplicateEnum { name: String },
526 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 pub fn default_severity(&self) -> Severity {
541 match self {
542 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 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 IssueKind::PossiblyUndefinedVariable { .. } => Severity::Warning,
615
616 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 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 pub fn code(&self) -> &'static str {
706 match self {
707 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 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 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 IssueKind::InvalidArrayOffset { .. } => "MIR0300",
761 IssueKind::NonExistentArrayOffset { .. } => "MIR0301",
762 IssueKind::PossiblyInvalidArrayOffset { .. } => "MIR0302",
763
764 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 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 IssueKind::ReadonlyPropertyAssignment { .. } => "MIR0600",
783
784 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 IssueKind::TaintedInput { .. } => "MIR0800",
800 IssueKind::TaintedHtml => "MIR0801",
801 IssueKind::TaintedSql => "MIR0802",
802 IssueKind::TaintedShell => "MIR0803",
803
804 IssueKind::InvalidTemplateParam { .. } => "MIR0900",
806 IssueKind::ShadowedTemplateParam { .. } => "MIR0901",
807
808 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 IssueKind::MissingReturnType { .. } => "MIR1100",
824 IssueKind::MissingParamType { .. } => "MIR1101",
825 IssueKind::MissingPropertyType { .. } => "MIR1104",
826 IssueKind::MissingThrowsDocblock { .. } => "MIR1102",
827 IssueKind::InvalidDocblock { .. } => "MIR1103",
828
829 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 IssueKind::InvalidTraitUse { .. } => "MIR1300",
844 IssueKind::ForbiddenCode { .. } => "MIR1301",
845
846 IssueKind::ParseError { .. } => "MIR1400",
848
849 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 IssueKind::InvalidThrow { .. } => "MIR1500",
860 IssueKind::InvalidCatch { .. } => "MIR1503",
861 IssueKind::ImplicitToStringCast { .. } => "MIR1501",
862 IssueKind::ImplicitFloatToIntCast { .. } => "MIR1502",
863 }
864 }
865
866 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 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#[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#[derive(Debug, Default)]
1501pub struct IssueBuffer {
1502 issues: Vec<Issue>,
1503 seen: HashSet<(&'static str, Arc<str>, u32, u16)>,
1504 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 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 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 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 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 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 #[test]
1970 fn one_of_each_has_every_variant() {
1971 assert_eq!(one_of_each().len(), 121);
1974 }
1975}