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 start_col: f.name_col,
64 end_line: f.start_line,
65 end_col: f.name_end_col,
66 name: Some(f.name.clone()),
67 },
68 message: format!(
69 "Function `{}` is a Brain Method ({}L, complexity {}, {} external refs)",
70 f.name,
71 f.line_count,
72 f.complexity,
73 f.external_refs.len()
74 ),
75 suggested_refactorings: vec!["Extract Method".into(), "Move Method".into()],
76 ..Default::default()
77 })
78 .collect()
79 }
80}