howmany 3.0.0

A blazingly fast, intelligent code analysis tool with parallel processing, caching, and beautiful visualizations
Documentation
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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
use crate::core::stats::AggregatedStats;
use crate::core::types::{CodeStats, FileStats};
use crate::utils::errors::Result;
use chrono::Utc;
use serde_json::Value;
use serde_sarif::sarif::{
    ArtifactLocation, Location, Message, MultiformatMessageString, PhysicalLocation, Region,
    ReportingDescriptor, Result as SarifResult, ResultKind, ResultLevel, Run, RunAutomationDetails,
    Sarif, Tool, ToolComponent,
};

pub struct SarifConverter {
    tool_name: String,
    tool_version: String,
}

impl SarifConverter {
    pub fn new() -> Self {
        Self {
            tool_name: "howmany".to_string(),
            tool_version: env!("CARGO_PKG_VERSION").to_string(),
        }
    }

    /// Get the tool name
    pub fn tool_name(&self) -> &str {
        &self.tool_name
    }

    /// Get the tool version
    pub fn tool_version(&self) -> &str {
        &self.tool_version
    }

    /// Convert CodeStats and file analysis to SARIF format
    pub fn convert_basic_analysis(
        &self,
        stats: &CodeStats,
        individual_files: &[(String, FileStats)],
    ) -> Result<Sarif> {
        let mut results = Vec::new();

        // Create results for each file with potential issues
        for (file_path, file_stats) in individual_files {
            // Generate code quality results
            if let Some(quality_results) = self.analyze_file_quality(file_path, file_stats) {
                results.extend(quality_results);
            }
        }

        // Add project-level summary results
        results.extend(self.create_project_summary_results(stats));

        self.create_sarif_log(results)
    }

    /// Convert comprehensive AggregatedStats to SARIF format
    pub fn convert_comprehensive_analysis(
        &self,
        aggregated_stats: &AggregatedStats,
        individual_files: &[(String, FileStats)],
    ) -> Result<Sarif> {
        let mut results = Vec::new();

        // Generate detailed complexity and quality results
        for (file_path, file_stats) in individual_files {
            // Complexity analysis results
            if let Some(complexity_results) =
                self.analyze_file_complexity(file_path, file_stats, aggregated_stats)
            {
                results.extend(complexity_results);
            }

            // Quality metrics results
            if let Some(quality_results) =
                self.analyze_comprehensive_quality(file_path, file_stats, aggregated_stats)
            {
                results.extend(quality_results);
            }
        }

        // Add comprehensive project-level results
        results.extend(self.create_comprehensive_project_results(aggregated_stats));

        self.create_sarif_log(results)
    }

    /// Analyze individual file for basic quality issues
    fn analyze_file_quality(
        &self,
        file_path: &str,
        file_stats: &FileStats,
    ) -> Option<Vec<SarifResult>> {
        let mut results = Vec::new();

        // Large file warning
        if file_stats.total_lines > 1000 {
            results.push(self.create_result(
                "HM001",
                "Large File",
                &format!(
                    "File has {} lines, consider breaking it into smaller modules",
                    file_stats.total_lines
                ),
                "warning",
                file_path,
                None,
            ));
        }

        // Low documentation ratio
        let doc_ratio = if file_stats.total_lines > 0 {
            (file_stats.doc_lines + file_stats.comment_lines) as f64 / file_stats.total_lines as f64
        } else {
            0.0
        };

        if doc_ratio < 0.1 && file_stats.code_lines > 50 {
            results.push(self.create_result(
                "HM002",
                "Low Documentation",
                &format!("Documentation ratio is {:.1}%, consider adding more comments and documentation", doc_ratio * 100.0),
                "info",
                file_path,
                None,
            ));
        }

        // Empty or very small files
        if file_stats.code_lines == 0 {
            results.push(self.create_result(
                "HM003",
                "Empty File",
                "File contains no code lines, consider removing if unused",
                "note",
                file_path,
                None,
            ));
        }

        if results.is_empty() {
            None
        } else {
            Some(results)
        }
    }

    /// Analyze file complexity using AggregatedStats
    fn analyze_file_complexity(
        &self,
        file_path: &str,
        _file_stats: &FileStats,
        aggregated_stats: &AggregatedStats,
    ) -> Option<Vec<SarifResult>> {
        let mut results = Vec::new();

        // Get file extension for complexity analysis
        let extension = std::path::Path::new(file_path)
            .extension()
            .and_then(|ext| ext.to_str())
            .unwrap_or("unknown");

        // Check if we have complexity data for this extension
        if let Some(complexity_data) = aggregated_stats
            .complexity
            .complexity_by_extension
            .get(extension)
        {
            // High complexity warning
            if complexity_data.cyclomatic_complexity > 15.0 {
                results.push(self.create_result(
                    "HM101",
                    "High Complexity",
                    &format!(
                        "Average cyclomatic complexity is {:.1}, consider refactoring",
                        complexity_data.cyclomatic_complexity
                    ),
                    "warning",
                    file_path,
                    None,
                ));
            }

            // Cognitive complexity issues
            if complexity_data.cognitive_complexity > 20.0 {
                results.push(self.create_result(
                    "HM102",
                    "High Cognitive Complexity",
                    &format!(
                        "Cognitive complexity is {:.1}, may be difficult to understand",
                        complexity_data.cognitive_complexity
                    ),
                    "warning",
                    file_path,
                    None,
                ));
            }

            // Deep nesting warning
            if complexity_data.max_nesting_depth > 5 {
                results.push(self.create_result(
                    "HM103",
                    "Deep Nesting",
                    &format!(
                        "Maximum nesting depth is {}, consider extracting nested logic",
                        complexity_data.max_nesting_depth
                    ),
                    "info",
                    file_path,
                    None,
                ));
            }
        }

        if results.is_empty() {
            None
        } else {
            Some(results)
        }
    }

    /// Analyze comprehensive quality metrics
    fn analyze_comprehensive_quality(
        &self,
        file_path: &str,
        _file_stats: &FileStats,
        aggregated_stats: &AggregatedStats,
    ) -> Option<Vec<SarifResult>> {
        let mut results = Vec::new();

        // Maintainability issues
        if aggregated_stats
            .complexity
            .quality_metrics
            .maintainability_index
            < 50.0
        {
            results.push(self.create_result(
                "HM201",
                "Low Maintainability",
                &format!("Maintainability index is {:.1}/100, consider refactoring", 
                    aggregated_stats.complexity.quality_metrics.maintainability_index),
                "warning",
                file_path,
                None,
            ));
        }

        // Code health issues
        if aggregated_stats
            .complexity
            .quality_metrics
            .code_health_score
            < 60.0
        {
            results.push(self.create_result(
                "HM202",
                "Poor Code Health",
                &format!("Code health score is {:.1}/100, review code quality practices", 
                    aggregated_stats.complexity.quality_metrics.code_health_score),
                "info",
                file_path,
                None,
            ));
        }

        if results.is_empty() {
            None
        } else {
            Some(results)
        }
    }

    /// Create project-level summary results
    fn create_project_summary_results(&self, stats: &CodeStats) -> Vec<SarifResult> {
        let mut results = Vec::new();

        // Project size summary
        results.push(self.create_result(
            "HM000",
            "Project Summary",
            &format!(
                "Project contains {} files with {} total lines ({} code, {} comments)",
                stats.total_files,
                stats.total_lines,
                stats.total_code_lines,
                stats.total_comment_lines
            ),
            "note",
            "project://summary",
            None,
        ));

        // Large project warning
        if stats.total_files > 1000 {
            results.push(self.create_result(
                "HM301",
                "Large Project",
                &format!(
                    "Project has {} files, consider modularization strategies",
                    stats.total_files
                ),
                "info",
                "project://summary",
                None,
            ));
        }

        results
    }

    /// Create comprehensive project-level results
    fn create_comprehensive_project_results(
        &self,
        aggregated_stats: &AggregatedStats,
    ) -> Vec<SarifResult> {
        let mut results = Vec::new();

        // Comprehensive project summary
        results.push(self.create_result(
            "HM000",
            "Comprehensive Project Summary",
            &format!("Project: {} files, {} functions, {:.1} avg complexity, {:.1}/100 quality score", 
                aggregated_stats.basic.total_files,
                aggregated_stats.complexity.function_count,
                aggregated_stats.complexity.cyclomatic_complexity,
                aggregated_stats.complexity.quality_metrics.code_health_score),
            "note",
            "project://summary",
            None,
        ));

        // Technical debt indicators
        if aggregated_stats
            .complexity
            .quality_metrics
            .code_health_score
            < 70.0
        {
            results.push(self.create_result(
                "HM401",
                "Technical Debt Alert",
                &format!("Overall code health is {:.1}/100. Consider prioritizing refactoring efforts", 
                    aggregated_stats.complexity.quality_metrics.code_health_score),
                "warning",
                "project://technical-debt",
                None,
            ));
        }

        // Quality assessment insights
        results.push(self.create_result(
            "HM402",
            "Quality Assessment Insights",
            &format!("Overall code quality: {:.1}/100 (Maintainability: {:.1}/100, Code Health: {:.1}/100)", 
                aggregated_stats.ratios.quality_metrics.overall_quality_score,
                aggregated_stats.complexity.quality_metrics.maintainability_index,
                aggregated_stats.complexity.quality_metrics.code_health_score),
            "note",
            "project://quality-insights",
            None,
        ));

        results
    }

    /// Create a SARIF result object
    fn create_result(
        &self,
        rule_id: &str,
        _rule_name: &str,
        message_text: &str,
        level: &str,
        file_path: &str,
        region: Option<Region>,
    ) -> SarifResult {
        let result_level = match level {
            "error" => ResultLevel::Error,
            "warning" => ResultLevel::Warning,
            "note" => ResultLevel::Note,
            "info" => ResultLevel::None, // Use None as fallback since Info doesn't exist
            _ => ResultLevel::None,
        };

        let result = SarifResult {
            rule_id: Some(rule_id.to_string()),
            rule_index: None,
            kind: Some(ResultKind::Review),
            level: Some(result_level),
            message: Message {
                text: Some(message_text.to_string()),
                markdown: None,
                id: None,
                arguments: None,
                properties: None,
            },
            locations: None,
            analysis_target: None,
            web_request: None,
            web_response: None,
            fingerprints: None,
            partial_fingerprints: None,
            code_flows: None,
            graphs: None,
            graph_traversals: None,
            stacks: None,
            related_locations: None,
            suppressions: None,
            baseline_state: None,
            attachments: None,
            work_item_uris: None,
            provenance: None,
            fixes: None,
            taxa: None,
            properties: None,
            // Add missing required fields
            correlation_guid: None,
            guid: None,
            hosted_viewer_uri: None,
            occurrence_count: None,
            rank: None,
            rule: None,
        };

        // Set location if it's a file-based result
        if !file_path.starts_with("project://") {
            let location = Location {
                id: None,
                physical_location: Some(PhysicalLocation {
                    artifact_location: Some(ArtifactLocation {
                        uri: Some(self.normalize_file_path(file_path)),
                        uri_base_id: None,
                        index: None,
                        description: None,
                        properties: None,
                    }),
                    region,
                    context_region: None,
                    address: None,
                    properties: None,
                }),
                logical_locations: None,
                message: None,
                annotations: None,
                relationships: None,
                properties: None,
            };

            let mut result_with_location = result;
            result_with_location.locations = Some(vec![location]);
            result_with_location
        } else {
            result
        }
    }

    /// Create the complete SARIF log structure
    fn create_sarif_log(&self, results: Vec<SarifResult>) -> Result<Sarif> {
        let sarif_log = Sarif {
            version: Value::String(super::SARIF_VERSION.to_string()),
            schema: Some(super::SARIF_SCHEMA.to_string()),
            runs: vec![self.create_run(results)],
            inline_external_properties: None,
            properties: None,
        };

        Ok(sarif_log)
    }

    /// Create a SARIF run
    fn create_run(&self, results: Vec<SarifResult>) -> Run {
        let driver = ToolComponent {
            guid: None,
            name: self.tool_name.clone(),
            organization: None,
            product: None,
            product_suite: None,
            short_description: None,
            full_description: None,
            full_name: Some("HowMany Code Analysis Tool".to_string()),
            version: Some(self.tool_version.clone()),
            semantic_version: Some(self.tool_version.clone()),
            dotted_quad_file_version: None,
            release_date_utc: None,
            download_uri: None,
            information_uri: Some("https://github.com/GriffinCanCode/howmany".to_string()),
            global_message_strings: None,
            notifications: None,
            rules: Some(self.create_rule_definitions()),
            taxa: None,
            locations: None,
            language: None,
            contents: None,
            is_comprehensive: None,
            localized_data_semantic_version: None,
            minimum_required_localized_data_semantic_version: None,
            associated_component: None,
            translation_metadata: None,
            supported_taxonomies: None,
            properties: None,
        };

        let tool = Tool {
            driver,
            extensions: None,
            properties: None,
        };

        let start_time = Utc::now();
        let automation_details = RunAutomationDetails {
            description: Some(Message {
                text: Some("HowMany code analysis run".to_string()),
                markdown: None,
                id: None,
                arguments: None,
                properties: None,
            }),
            id: Some(format!("howmany-{}", start_time.format("%Y%m%d-%H%M%S"))),
            guid: None,
            correlation_guid: None,
            properties: None,
        };

        Run {
            tool,
            invocations: None,
            conversion: None,
            language: None,
            version_control_provenance: None,
            original_uri_base_ids: None,
            results: Some(results),
            automation_details: Some(automation_details),
            run_aggregates: None,
            baseline_guid: None,
            redaction_tokens: None,
            default_encoding: None,
            default_source_language: None,
            newline_sequences: None,
            column_kind: None,
            external_property_file_references: None,
            thread_flow_locations: None,
            taxonomies: None,
            addresses: None,
            translations: None,
            policies: None,
            web_requests: None,
            web_responses: None,
            properties: None,
            // Add missing required fields for Run
            artifacts: None,
            graphs: None,
            logical_locations: None,
            special_locations: None,
        }
    }

    /// Create rule definitions for all howmany rules
    pub fn create_rule_definitions(&self) -> Vec<ReportingDescriptor> {
        vec![
            self.create_rule(
                "HM000",
                "Project Summary",
                "Provides an overview of project statistics",
            ),
            self.create_rule(
                "HM001",
                "Large File",
                "Detects files that may be too large and should be split",
            ),
            self.create_rule(
                "HM002",
                "Low Documentation",
                "Identifies files with insufficient documentation",
            ),
            self.create_rule("HM003", "Empty File", "Detects files with no code content"),
            self.create_rule(
                "HM101",
                "High Complexity",
                "Identifies functions or files with high cyclomatic complexity",
            ),
            self.create_rule(
                "HM102",
                "High Cognitive Complexity",
                "Detects code that may be difficult to understand",
            ),
            self.create_rule(
                "HM103",
                "Deep Nesting",
                "Identifies deeply nested code structures",
            ),
            self.create_rule(
                "HM201",
                "Low Maintainability",
                "Detects code with low maintainability scores",
            ),
            self.create_rule(
                "HM202",
                "Poor Code Health",
                "Identifies overall code health issues",
            ),
            self.create_rule(
                "HM301",
                "Large Project",
                "Warns about projects that may benefit from modularization",
            ),
            self.create_rule(
                "HM401",
                "Technical Debt Alert",
                "Highlights significant technical debt indicators",
            ),
            self.create_rule(
                "HM402",
                "Quality Assessment Insights",
                "Provides overall code quality insights",
            ),
        ]
    }

    /// Create a single rule definition
    fn create_rule(&self, id: &str, name: &str, description: &str) -> ReportingDescriptor {
        ReportingDescriptor {
            id: id.to_string(),
            deprecated_ids: None,
            deprecated_names: None,
            deprecated_guids: None,
            guid: None,
            name: Some(name.to_string()),
            short_description: Some(MultiformatMessageString {
                text: description.to_string(),
                markdown: None,
                properties: None,
            }),
            full_description: Some(MultiformatMessageString {
                text: self.get_rule_help_text(id),
                markdown: None,
                properties: None,
            }),
            message_strings: None,
            default_configuration: None,
            help_uri: Some(format!(
                "https://github.com/GriffinCanCode/howmany/blob/main/docs/rules/{}.md",
                id
            )),
            help: None,
            relationships: None,
            properties: None,
        }
    }

    /// Get detailed help text for rules
    fn get_rule_help_text(&self, rule_id: &str) -> String {
        match rule_id {
            "HM001" => "Large files can be difficult to maintain and understand. Consider breaking them into smaller, more focused modules.".to_string(),
            "HM002" => "Well-documented code is easier to maintain. Consider adding comments explaining complex logic and public APIs.".to_string(),
            "HM003" => "Empty files may indicate incomplete implementation or files that can be removed to clean up the codebase.".to_string(),
            "HM101" => "High cyclomatic complexity indicates code that may be difficult to test and maintain. Consider refactoring into smaller functions.".to_string(),
            "HM102" => "High cognitive complexity makes code harder to understand. Consider simplifying control flow and reducing nested conditions.".to_string(),
            "HM103" => "Deeply nested code is harder to read and maintain. Consider extracting nested logic into separate functions.".to_string(),
            "HM201" => "Low maintainability scores indicate code that may be expensive to modify. Focus on improving code structure and reducing complexity.".to_string(),
            "HM202" => "Poor code health affects long-term project sustainability. Review coding standards and consider refactoring efforts.".to_string(),
            "HM301" => "Large projects benefit from modular architecture. Consider organizing code into logical modules or packages.".to_string(),
            "HM401" => "Technical debt accumulation can slow development. Prioritize refactoring efforts to improve code quality.".to_string(),
            "HM402" => "Quality assessment insights provide an overall view of code quality metrics.".to_string(),
            _ => format!("Rule {} provides code quality insights from HowMany analysis.", rule_id),
        }
    }

    /// Normalize file paths for SARIF output
    pub fn normalize_file_path(&self, file_path: &str) -> String {
        // Convert to forward slashes and ensure proper URI format
        let normalized = file_path.replace('\\', "/");

        // If it's already a URI, return as-is
        if normalized.starts_with("http://")
            || normalized.starts_with("https://")
            || normalized.starts_with("file://")
        {
            normalized
        } else if normalized.starts_with('/') || std::path::Path::new(&normalized).is_absolute() {
            // Convert absolute paths to file URIs. A leading slash counts as
            // absolute even where the host disagrees: Windows calls `/x`
            // relative because it carries no drive letter, and a consumer
            // reading the SARIF should see the same URI either way.
            format!("file://{}", normalized)
        } else {
            // Relative paths are kept as-is
            normalized
        }
    }
}

impl Default for SarifConverter {
    fn default() -> Self {
        Self::new()
    }
}