Skip to main content

cha_core/plugins/
coupling.rs

1use crate::{AnalysisContext, Finding, Location, Plugin, Severity, SmellCategory};
2
3/// Detect high coupling via excessive imports.
4pub struct CouplingAnalyzer {
5    pub max_imports: usize,
6}
7
8impl Default for CouplingAnalyzer {
9    fn default() -> Self {
10        Self { max_imports: 15 }
11    }
12}
13
14impl Plugin for CouplingAnalyzer {
15    fn name(&self) -> &str {
16        "coupling"
17    }
18
19    fn description(&self) -> &str {
20        "Too many imports (high coupling)"
21    }
22
23    fn analyze(&self, ctx: &AnalysisContext) -> Vec<Finding> {
24        let mut findings = Vec::new();
25
26        if ctx.model.imports.len() > self.max_imports {
27            findings.push(Finding {
28                smell_name: "high_coupling".into(),
29                category: SmellCategory::Couplers,
30                severity: if ctx.model.imports.len() > self.max_imports * 2 {
31                    Severity::Error
32                } else {
33                    Severity::Warning
34                },
35                location: Location {
36                    path: ctx.file.path.clone(),
37                    start_line: 1,
38                    end_line: ctx.model.total_lines,
39                    name: None,
40                },
41                message: format!(
42                    "File has {} imports (threshold: {})",
43                    ctx.model.imports.len(),
44                    self.max_imports
45                ),
46                suggested_refactorings: vec!["Move Method".into(), "Extract Class".into()],
47                actual_value: Some(ctx.model.imports.len() as f64),
48                threshold: Some(self.max_imports as f64),
49            });
50        }
51
52        findings
53    }
54}