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 description(&self) -> &str {
44 "Brain Method: too long, complex, and coupled"
45 }
46
47 fn analyze(&self, ctx: &AnalysisContext) -> Vec<Finding> {
48 ctx.model
49 .functions
50 .iter()
51 .filter(|f| {
52 f.line_count >= self.min_lines
53 && f.complexity >= self.min_complexity
54 && f.external_refs.len() >= self.min_external_refs
55 })
56 .map(|f| Finding {
57 smell_name: "brain_method".into(),
58 category: SmellCategory::Bloaters,
59 severity: Severity::Warning,
60 location: Location {
61 path: ctx.file.path.clone(),
62 start_line: f.start_line,
63 end_line: f.end_line,
64 name: Some(f.name.clone()),
65 },
66 message: format!(
67 "Function `{}` is a Brain Method ({}L, complexity {}, {} external refs)",
68 f.name,
69 f.line_count,
70 f.complexity,
71 f.external_refs.len()
72 ),
73 suggested_refactorings: vec!["Extract Method".into(), "Move Method".into()],
74 ..Default::default()
75 })
76 .collect()
77 }
78}