Skip to main content

cha_core/plugins/
lazy_class.rs

1use crate::{AnalysisContext, Finding, Location, Plugin, Severity, SmellCategory};
2
3/// Detect classes with very few methods and lines (nearly empty wrappers).
4pub struct LazyClassAnalyzer {
5    pub max_methods: usize,
6    pub max_lines: usize,
7}
8
9impl Default for LazyClassAnalyzer {
10    fn default() -> Self {
11        Self {
12            max_methods: 1,
13            max_lines: 10,
14        }
15    }
16}
17
18impl Plugin for LazyClassAnalyzer {
19    fn name(&self) -> &str {
20        "lazy_class"
21    }
22
23    fn description(&self) -> &str {
24        "Class too small to justify its existence"
25    }
26
27    fn analyze(&self, ctx: &AnalysisContext) -> Vec<Finding> {
28        ctx.model
29            .classes
30            .iter()
31            .filter_map(|c| {
32                if c.is_interface
33                    || c.method_count > self.max_methods
34                    || c.line_count > self.max_lines
35                {
36                    return None;
37                }
38                Some(Finding {
39                    smell_name: "lazy_class".into(),
40                    category: SmellCategory::Dispensables,
41                    severity: Severity::Hint,
42                    location: Location {
43                        path: ctx.file.path.clone(),
44                        start_line: c.start_line,
45                        end_line: c.end_line,
46                        name: Some(c.name.clone()),
47                    },
48                    message: format!(
49                        "Class `{}` has only {} method(s) and {} lines, consider Inline Class",
50                        c.name, c.method_count, c.line_count
51                    ),
52                    suggested_refactorings: vec!["Inline Class".into()],
53                    actual_value: Some(c.method_count as f64),
54                    threshold: Some(self.max_methods as f64),
55                })
56            })
57            .collect()
58    }
59}