1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#![cfg_attr(coverage_nightly, coverage(off))]
//! Report formatting and analysis methods for entropy reports.
use super::types::EntropyReport;
impl EntropyReport {
/// Calculate total estimated lines of code reduction from all actionable violations
///
/// Sums up the estimated LOC reduction from all detected patterns that can be
/// refactored into reusable functions or modules.
///
/// # Examples
///
/// ```rust
/// use pmat::entropy::entropy_calculator::{EntropyReport, EntropyMetrics};
/// use pmat::entropy::violation_detector::PatternSummary;
/// use pmat::entropy::PatternType;
/// use std::collections::BTreeMap;
///
/// // Create a mock report with empty violations for demonstration
/// let pattern_summary = PatternSummary {
/// pattern_type: PatternType::ErrorHandling,
/// repetitions: 0,
/// variation_score: 0.0,
/// example_code: String::new(),
/// };
///
/// let report = EntropyReport {
/// total_files_analyzed: 5,
/// actionable_violations: vec![], // Empty for simplicity
/// pattern_summary,
/// entropy_metrics: EntropyMetrics {
/// file_level_entropy: Some(0.7),
/// module_level_entropy: Some(0.6),
/// project_level_entropy: Some(0.55),
/// pattern_diversity: Some(0.6),
/// total_patterns: 8,
/// total_instances: 24,
/// total_loc: 500,
/// patterns_by_type: BTreeMap::new(),
/// },
/// measurement_note: None,
/// };
///
/// // With no actionable violations, LOC reduction should be 0
/// assert_eq!(report.total_loc_reduction(), 0);
/// ```
#[must_use]
#[provable_contracts_macros::contract("pmat-core.yaml", equation = "check_compliance")]
pub fn total_loc_reduction(&self) -> usize {
// Violations whose saving could not be estimated contribute nothing
// rather than a plausible default (`filter_map`, not `unwrap_or(0)` on a
// number we would then have printed as measured).
self.actionable_violations
.iter()
.filter_map(|v| v.estimated_loc_reduction)
.sum()
}
/// Calculate percentage of codebase that could be reduced through refactoring
///
/// Returns the potential LOC reduction as a percentage of total analyzed code.
/// Higher percentages indicate more duplication and better refactoring opportunities.
///
/// # Examples
///
/// ```rust
/// use pmat::entropy::entropy_calculator::{EntropyReport, EntropyMetrics};
/// use pmat::entropy::violation_detector::PatternSummary;
/// use pmat::entropy::PatternType;
/// use std::collections::BTreeMap;
///
/// let pattern_summary = PatternSummary {
/// pattern_type: PatternType::ErrorHandling,
/// repetitions: 0,
/// variation_score: 0.0,
/// example_code: String::new(),
/// };
///
/// let report = EntropyReport {
/// total_files_analyzed: 10,
/// actionable_violations: vec![], // Empty violations
/// pattern_summary,
/// entropy_metrics: EntropyMetrics {
/// file_level_entropy: Some(0.8),
/// module_level_entropy: Some(0.75),
/// project_level_entropy: Some(0.7),
/// pattern_diversity: Some(0.75),
/// total_patterns: 0,
/// total_instances: 0,
/// total_loc: 1000, // Total lines analyzed
/// patterns_by_type: BTreeMap::new(),
/// },
/// measurement_note: None,
/// };
///
/// // With no violations, reduction percentage should be 0
/// assert_eq!(report.reduction_percentage(), 0.0);
/// ```
#[must_use]
#[provable_contracts_macros::contract("pmat-core.yaml", equation = "check_compliance")]
pub fn reduction_percentage(&self) -> f64 {
if self.entropy_metrics.total_loc > 0 {
(self.total_loc_reduction() as f64 / self.entropy_metrics.total_loc as f64) * 100.0
} else {
0.0
}
}
/// Format as human-readable report, listing every violation.
#[must_use]
#[provable_contracts_macros::contract("pmat-core.yaml", equation = "check_compliance")]
pub fn format_report(&self) -> String {
self.format_report_top(0)
}
/// Format as human-readable report, listing at most `top_n` violations
/// (`0` = all) in the same "highest priority first" order `--top-violations`
/// uses everywhere else.
///
/// `--format detailed` called `format_report()` and threw `--top-violations`
/// away, so `--top-violations 1` printed all 26 violations, byte-identically
/// to `--top-violations 100`, while `--format summary` and
/// `--format markdown` honoured the same flag on the same run. Two renderers
/// of one command disagreed about how many violations the run reports.
///
/// The LIMIT is a display control and must not touch a measurement: the
/// "Actionable Violations" header keeps the full count and says how many of
/// them are listed, so a narrowed report can never be read as a smaller
/// finding.
#[must_use]
pub fn format_report_top(&self, top_n: usize) -> String {
let shown: &[crate::entropy::violation_detector::ActionableViolation] =
if top_n > 0 && self.actionable_violations.len() > top_n {
&self.actionable_violations[..top_n]
} else {
&self.actionable_violations
};
let mut report = String::new();
report.push_str("Entropy Analysis Results\n");
report.push_str("========================\n\n");
report.push_str(&format!("Files Analyzed: {}\n", self.total_files_analyzed));
report.push_str(&format!(
"Source Lines Analyzed: {}\n",
self.entropy_metrics.total_loc
));
report.push_str(&format!(
"Pattern Diversity: {}\n",
Self::render_measurement(self.entropy_metrics.pattern_diversity)
));
report.push_str(&format!(
"Actionable Violations: {}\n",
self.actionable_violations.len()
));
if shown.len() < self.actionable_violations.len() {
report.push_str(&format!(
"Listing: top {} of {} (--top-violations)\n",
shown.len(),
self.actionable_violations.len()
));
}
if let Some(note) = &self.measurement_note {
report.push_str(&format!("Note: {note}\n"));
}
report.push('\n');
// Group violations by severity
let mut high = Vec::new();
let mut medium = Vec::new();
let mut low = Vec::new();
for violation in shown {
match violation.severity {
crate::entropy::violation_detector::Severity::High => high.push(violation),
crate::entropy::violation_detector::Severity::Medium => medium.push(violation),
crate::entropy::violation_detector::Severity::Low => low.push(violation),
}
}
// Every band is rendered. LOW was omitted, so with `--min-severity low`
// the "Actionable Violations: N" header counted rows the report never
// listed.
Self::push_severity_section(&mut report, "HIGH", &high);
Self::push_severity_section(&mut report, "MEDIUM", &medium);
Self::push_severity_section(&mut report, "LOW", &low);
report.push_str(&format!(
"Total Potential Reduction: {} lines ({:.1}% of analyzed code)\n",
self.total_loc_reduction(),
self.reduction_percentage()
));
report
}
/// Render one severity band. `saves` reads "not estimated" where the saving
/// was never derived from a measured pattern size, rather than "0 lines".
fn push_severity_section(
report: &mut String,
label: &str,
violations: &[&crate::entropy::violation_detector::ActionableViolation],
) {
if violations.is_empty() {
return;
}
report.push_str(&format!("{label} SEVERITY ({}):\n", violations.len()));
for (i, v) in violations.iter().enumerate() {
report.push_str(&format!(
"{}. {}\n Fix: {} - saves {}\n\n",
i + 1,
v.message,
v.fix_suggestion,
v.render_loc_reduction()
));
}
}
/// Render an optional measurement without ever printing a placeholder number.
#[must_use]
pub fn render_measurement(value: Option<f64>) -> String {
value.map_or_else(|| "not measured".to_string(), |v| format!("{v:.3}"))
}
}