pmat 3.15.0

PMAT - Zero-config AI context generation and code quality toolkit (CLI, MCP, HTTP)
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
#![cfg_attr(coverage_nightly, coverage(off))]

use crate::cli::ComprehensiveOutputFormat;
use crate::services::facades::analysis_orchestrator::ComprehensiveAnalysisResult;
use anyhow::Result;
use std::path::PathBuf;

/// Output results in the requested format
pub(super) async fn output_results(
    result: ComprehensiveAnalysisResult,
    format: ComprehensiveOutputFormat,
    executive_summary: bool,
    output: Option<PathBuf>,
) -> Result<()> {
    let content = format_result(result, format, executive_summary)?;

    if let Some(output_path) = output {
        tokio::fs::write(&output_path, &content).await?;
        eprintln!("📄 Report written to: {}", output_path.display());
    } else {
        println!("{content}");
    }

    Ok(())
}

/// Format the analysis result
pub(super) fn format_result(
    result: ComprehensiveAnalysisResult,
    format: ComprehensiveOutputFormat,
    executive_summary: bool,
) -> Result<String> {
    match format {
        ComprehensiveOutputFormat::Json => format_as_json(&result),
        ComprehensiveOutputFormat::Markdown => format_as_markdown(&result, executive_summary),
        ComprehensiveOutputFormat::Sarif => format_as_sarif(&result),
        ComprehensiveOutputFormat::Summary => format_as_text(&result, true),
        ComprehensiveOutputFormat::Detailed => format_as_text(&result, false),
    }
}

/// Format as JSON
pub(super) fn format_as_json(result: &ComprehensiveAnalysisResult) -> Result<String> {
    serde_json::to_string_pretty(result).map_err(Into::into)
}

/// Format as Markdown
pub(super) fn format_as_markdown(
    result: &ComprehensiveAnalysisResult,
    executive_summary: bool,
) -> Result<String> {
    use std::fmt::Write;

    let mut output = String::new();
    writeln!(&mut output, "# Comprehensive Code Analysis Report\n")?;

    if executive_summary {
        format_executive_summary(&mut output, &result.summary)?;
    }

    // Delegate each section to specialized functions
    if let Some(complexity) = &result.complexity {
        format_complexity_section(&mut output, complexity)?;
    }

    if let Some(dead_code) = &result.dead_code {
        format_dead_code_section(&mut output, dead_code)?;
    }

    if let Some(satd) = &result.satd {
        format_satd_section(&mut output, satd)?;
    }

    Ok(output)
}

// Helper functions to reduce complexity below 20

pub(super) fn format_executive_summary(
    output: &mut String,
    summary: &crate::services::facades::analysis_orchestrator::AnalysisSummary,
) -> Result<()> {
    use std::fmt::Write;

    writeln!(output, "## Executive Summary\n")?;
    writeln!(
        output,
        "Project analysis completed with {} total files analyzed.\n",
        summary.total_files
    )?;

    writeln!(output, "- **Quality Score**: {:.1}%", summary.quality_score)?;
    writeln!(output, "- **Total Files**: {}", summary.total_files)?;
    writeln!(output, "- **Total Issues**: {}", summary.total_issues)?;
    writeln!(output, "- **Critical Issues**: {}", summary.critical_issues)?;
    writeln!(output)?;

    if !summary.recommendations.is_empty() {
        writeln!(output, "### Key Recommendations\n")?;
        for rec in &summary.recommendations {
            writeln!(output, "- {rec}")?;
        }
        writeln!(output)?;
    }

    Ok(())
}

pub(super) fn format_complexity_section(
    output: &mut String,
    complexity: &crate::services::facades::complexity_facade::ComplexityAnalysisResult,
) -> Result<()> {
    use std::fmt::Write;

    writeln!(output, "## Complexity Analysis\n")?;
    writeln!(output, "- **Files Analyzed**: {}", complexity.total_files)?;
    writeln!(
        output,
        "- **Average Complexity**: {:.1}",
        complexity.average_complexity
    )?;
    writeln!(
        output,
        "- **Max Complexity**: {}",
        complexity.max_complexity
    )?;
    writeln!(output, "- **Violations**: {}", complexity.violations.len())?;

    if !complexity.violations.is_empty() {
        writeln!(output, "\n### Top Complexity Violations\n")?;
        for (i, violation) in complexity.violations.iter().take(5).enumerate() {
            writeln!(
                output,
                "{}. {} - {} (complexity: {})",
                i + 1,
                violation.file_path,
                violation.function_name,
                violation.complexity
            )?;
        }
    }
    writeln!(output)?;
    Ok(())
}

pub(super) fn format_dead_code_section(
    output: &mut String,
    dead_code: &crate::services::facades::dead_code_facade::DeadCodeAnalysisResult,
) -> Result<()> {
    use std::fmt::Write;

    writeln!(output, "## Dead Code Analysis\n")?;
    writeln!(output, "- **Files Analyzed**: {}", dead_code.total_files)?;
    writeln!(output, "- **Dead Items**: {}", dead_code.dead_items.len())?;
    writeln!(
        output,
        "- **Dead Code %**: {:.1}%",
        dead_code.dead_percentage
    )?;

    if !dead_code.dead_items.is_empty() {
        writeln!(output, "\n### Dead Code Items\n")?;
        for (i, item) in dead_code.dead_items.iter().take(5).enumerate() {
            writeln!(
                output,
                "{}. {} - {} ({:?})",
                i + 1,
                item.file_path,
                item.item_name,
                item.item_type
            )?;
        }
    }
    writeln!(output)?;
    Ok(())
}

pub(super) fn format_satd_section(
    output: &mut String,
    satd: &crate::services::facades::satd_facade::SatdAnalysisResult,
) -> Result<()> {
    use std::fmt::Write;

    writeln!(output, "## Technical Debt (SATD) Analysis\n")?;
    writeln!(output, "- **Files Analyzed**: {}", satd.total_files)?;
    writeln!(output, "- **Violations**: {}", satd.violations.len())?;

    if !satd.violations.is_empty() {
        writeln!(output, "\n### SATD Violations\n")?;
        for (i, violation) in satd.violations.iter().take(5).enumerate() {
            writeln!(
                output,
                "{}. {}:{} - {} ({:?})",
                i + 1,
                violation.file_path,
                violation.line_number,
                violation.violation_type,
                violation.severity
            )?;
        }
    }
    writeln!(output)?;
    Ok(())
}

/// Format as colorized text for terminal output (Summary / Detailed)
pub(super) fn format_as_text(
    result: &ComprehensiveAnalysisResult,
    executive_summary: bool,
) -> Result<String> {
    use crate::cli::colors as c;
    use std::fmt::Write;

    let mut output = String::new();
    writeln!(
        &mut output,
        "{}",
        c::header("Comprehensive Code Analysis Report")
    )?;
    writeln!(&mut output)?;

    if executive_summary {
        writeln!(&mut output, "{}\n", c::subheader("Executive Summary"))?;
        writeln!(
            &mut output,
            "  Project analysis completed with {} total files analyzed.\n",
            c::number(&result.summary.total_files.to_string())
        )?;
        writeln!(
            &mut output,
            "  Quality Score: {}",
            c::pct(result.summary.quality_score, 80.0, 50.0)
        )?;
        writeln!(
            &mut output,
            "  Total Files:   {}",
            c::number(&result.summary.total_files.to_string())
        )?;
        writeln!(
            &mut output,
            "  Total Issues:  {}",
            c::number(&result.summary.total_issues.to_string())
        )?;
        writeln!(
            &mut output,
            "  Critical:      {}",
            if result.summary.critical_issues > 0 {
                format!(
                    "{}{}{}",
                    c::BOLD_RED,
                    result.summary.critical_issues,
                    c::RESET
                )
            } else {
                c::number(&result.summary.critical_issues.to_string())
            }
        )?;
        writeln!(&mut output)?;
        if !result.summary.recommendations.is_empty() {
            writeln!(
                &mut output,
                "  {}Key Recommendations{}\n",
                c::BOLD,
                c::RESET
            )?;
            for rec in &result.summary.recommendations {
                writeln!(&mut output, "    {} {rec}", c::dim("-"))?;
            }
            writeln!(&mut output)?;
        }
    }

    if let Some(complexity) = &result.complexity {
        writeln!(&mut output, "{}\n", c::subheader("Complexity Analysis"))?;
        writeln!(
            &mut output,
            "  Files Analyzed:     {}",
            c::number(&complexity.total_files.to_string())
        )?;
        writeln!(
            &mut output,
            "  Average Complexity: {}",
            c::number(&format!("{:.1}", complexity.average_complexity))
        )?;
        writeln!(
            &mut output,
            "  Max Complexity:     {}",
            c::number(&complexity.max_complexity.to_string())
        )?;
        writeln!(
            &mut output,
            "  Violations:         {}",
            if complexity.violations.is_empty() {
                c::number("0")
            } else {
                format!("{}{}{}", c::YELLOW, complexity.violations.len(), c::RESET)
            }
        )?;
        if !complexity.violations.is_empty() {
            writeln!(
                &mut output,
                "\n  {}Top Complexity Violations{}\n",
                c::BOLD,
                c::RESET
            )?;
            for (i, v) in complexity.violations.iter().take(5).enumerate() {
                writeln!(
                    &mut output,
                    "    {}. {} - {} (complexity: {})",
                    c::number(&(i + 1).to_string()),
                    c::path(&v.file_path),
                    c::label(&v.function_name),
                    c::number(&v.complexity.to_string())
                )?;
            }
        }
        writeln!(&mut output)?;
    }

    if let Some(dead_code) = &result.dead_code {
        writeln!(&mut output, "{}\n", c::subheader("Dead Code Analysis"))?;
        writeln!(
            &mut output,
            "  Files Analyzed: {}",
            c::number(&dead_code.total_files.to_string())
        )?;
        writeln!(
            &mut output,
            "  Dead Items:     {}",
            c::number(&dead_code.dead_items.len().to_string())
        )?;
        writeln!(
            &mut output,
            "  Dead Code:      {}",
            c::pct(dead_code.dead_percentage, 5.0, 15.0)
        )?;
        if !dead_code.dead_items.is_empty() {
            writeln!(&mut output, "\n  {}Dead Code Items{}\n", c::BOLD, c::RESET)?;
            for (i, item) in dead_code.dead_items.iter().take(5).enumerate() {
                writeln!(
                    &mut output,
                    "    {}. {} - {} ({:?})",
                    c::number(&(i + 1).to_string()),
                    c::path(&item.file_path),
                    c::label(&item.item_name),
                    item.item_type
                )?;
            }
        }
        writeln!(&mut output)?;
    }

    if let Some(satd) = &result.satd {
        writeln!(
            &mut output,
            "{}\n",
            c::subheader("Technical Debt (SATD) Analysis")
        )?;
        writeln!(
            &mut output,
            "  Files Analyzed: {}",
            c::number(&satd.total_files.to_string())
        )?;
        writeln!(
            &mut output,
            "  Violations:     {}",
            if satd.violations.is_empty() {
                c::number("0")
            } else {
                format!("{}{}{}", c::YELLOW, satd.violations.len(), c::RESET)
            }
        )?;
        if !satd.violations.is_empty() {
            writeln!(&mut output, "\n  {}SATD Violations{}\n", c::BOLD, c::RESET)?;
            for (i, v) in satd.violations.iter().take(5).enumerate() {
                writeln!(
                    &mut output,
                    "    {}. {}:{} - {} ({:?})",
                    c::number(&(i + 1).to_string()),
                    c::path(&v.file_path),
                    c::number(&v.line_number.to_string()),
                    v.violation_type,
                    v.severity
                )?;
            }
        }
        writeln!(&mut output)?;
    }

    Ok(output)
}

/// Format as SARIF
pub(super) fn format_as_sarif(result: &ComprehensiveAnalysisResult) -> Result<String> {
    let mut results = Vec::new();

    // Add complexity violations as SARIF results
    if let Some(complexity) = &result.complexity {
        for violation in &complexity.violations {
            if violation.complexity > 20 {
                results.push(serde_json::json!({
                    "ruleId": "high-complexity",
                    "level": if violation.complexity > 30 { "error" } else { "warning" },
                    "message": {
                        "text": format!("Function {} has complexity {}", violation.function_name, violation.complexity)
                    },
                    "locations": [{
                        "physicalLocation": {
                            "artifactLocation": {
                                "uri": violation.file_path.clone()
                            },
                            "region": {
                                "startLine": violation.line_number
                            }
                        }
                    }]
                }));
            }
        }
    }

    // Add SATD violations
    if let Some(satd) = &result.satd {
        for violation in &satd.violations {
            results.push(serde_json::json!({
                "ruleId": "technical-debt",
                "level": "warning",
                "message": {
                    "text": format!("{}: {}", violation.violation_type, violation.message)
                },
                "locations": [{
                    "physicalLocation": {
                        "artifactLocation": {
                            "uri": violation.file_path.clone()
                        },
                        "region": {
                            "startLine": violation.line_number
                        }
                    }
                }]
            }));
        }
    }

    let sarif = serde_json::json!({
        "$schema": "https://json.schemastore.org/sarif-2.1.0.json",
        "version": "2.1.0",
        "runs": [{
            "tool": {
                "driver": {
                    "name": "pmat-comprehensive",
                    "version": env!("CARGO_PKG_VERSION"),
                    "informationUri": "https://github.com/paiml/paiml-mcp-agent-toolkit"
                }
            },
            "results": results
        }]
    });

    serde_json::to_string_pretty(&sarif).map_err(Into::into)
}