cha_core/plugins/
brain_method.rs1use crate::{AnalysisContext, Finding, Location, Plugin, Severity, SmellCategory};
2
3pub struct BrainMethodAnalyzer {
20 pub min_lines: usize,
22 pub min_complexity: usize,
24 pub min_external_refs: usize,
26}
27
28impl Default for BrainMethodAnalyzer {
29 fn default() -> Self {
30 Self {
31 min_lines: 65,
32 min_complexity: 4,
33 min_external_refs: 7,
34 }
35 }
36}
37
38impl Plugin for BrainMethodAnalyzer {
39 fn name(&self) -> &str {
40 "brain_method"
41 }
42
43 fn smells(&self) -> Vec<String> {
44 vec!["brain_method".into()]
45 }
46
47 fn description(&self) -> &str {
48 "Brain Method: too long, complex, and coupled"
49 }
50
51 fn analyze(&self, ctx: &AnalysisContext) -> Vec<Finding> {
52 ctx.model
53 .functions
54 .iter()
55 .filter(|f| {
56 f.line_count >= self.min_lines
57 && f.complexity >= self.min_complexity
58 && f.external_refs.len() >= self.min_external_refs
59 })
60 .map(|f| Finding {
61 smell_name: "brain_method".into(),
62 category: SmellCategory::Bloaters,
63 severity: Severity::Warning,
64 location: Location {
65 path: ctx.file.path.clone(),
66 start_line: f.start_line,
67 start_col: f.name_col,
68 end_line: f.start_line,
69 end_col: f.name_end_col,
70 name: Some(f.name.clone()),
71 },
72 message: format!(
73 "Function `{}` is a Brain Method ({}L, complexity {}, {} external refs)",
74 f.name,
75 f.line_count,
76 f.complexity,
77 f.external_refs.len()
78 ),
79 suggested_refactorings: vec!["Extract Method".into(), "Move Method".into()],
80 ..Default::default()
81 })
82 .collect()
83 }
84}