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                        start_col: c.name_col,
46                        end_line: c.start_line,
47                        end_col: c.name_end_col,
48                        name: Some(c.name.clone()),
49                    },
50                    message: format!(
51                        "Class `{}` has only {} method(s) and {} lines, consider Inline Class",
52                        c.name, c.method_count, c.line_count
53                    ),
54                    suggested_refactorings: vec!["Inline Class".into()],
55                    actual_value: Some(c.method_count as f64),
56                    threshold: Some(self.max_methods as f64),
57                })
58            })
59            .collect()
60    }
61}