pmat 2.93.1

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
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
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
//! Concrete implementations of defect analyzers for various quality issues.
//!
//! This module provides specific analyzers that implement the `DefectAnalyzer`
//! trait to detect different types of code quality issues. Each analyzer focuses
//! on a specific category of defects and can be composed to provide comprehensive
//! code quality assessment.
//!
//! # Analyzer Types
//!
//! - **`ComplexityDefectAnalyzer`**: Detects overly complex code using TDG metrics
//! - **`DeadCodeDefectAnalyzer`**: Identifies unreachable and unused code
//! - **`DuplicateDefectAnalyzer`**: Finds duplicated code blocks and patterns
//! - **`PerformanceDefectAnalyzer`**: Detects performance anti-patterns
//! - **`SecurityDefectAnalyzer`**: Identifies potential security vulnerabilities
//! - **`TechnicalDebtAnalyzer`**: Tracks self-admitted technical debt (SATD)
//!
//! # Severity Mapping
//!
//! Each analyzer maps its findings to standardized severity levels:
//! - **Critical**: Immediate action required (security, crashes)
//! - **High**: Should be fixed soon (performance, maintainability)
//! - **Medium**: Should be scheduled for fixing
//! - **Low**: Nice to fix, low priority
//!
//! # Example
//!
//! ```ignore
//! use pmat::services::defect_analyzers::{ComplexityDefectAnalyzer, ComplexityConfig};
//! use pmat::services::defect_analyzer::DefectAnalyzer;
//! use std::path::Path;
//!
//! # async fn example() -> Result<(), Box<dyn std::error::Error>> {
//! let analyzer = ComplexityDefectAnalyzer;
//! let config = ComplexityConfig {
//!     cyclomatic_threshold: 20,
//!     cognitive_threshold: 30,
//!     nesting_threshold: 5,
//! };
//!
//! let defects = analyzer.analyze(Path::new("src/"), config).await?;
//!
//! // Group defects by severity
//! let critical_count = defects.iter()
//!     .filter(|d| d.severity == Severity::Critical)
//!     .count();
//!     
//! println!("Found {} critical complexity issues", critical_count);
//! # Ok(())
//! # }
//! ```ignore

use crate::models::defect_report::{Defect, DefectCategory, Severity};
use crate::models::tdg::{TDGScore, TDGSeverity};
use crate::services::{
    big_o_analyzer::FunctionComplexity,
    dead_code_analyzer::{DeadCodeItem, UnreachableBlock},
    defect_analyzer::{AnalyzerConfig, DefectAnalyzer},
    duplicate_detector::{CloneGroup, CloneType},
    satd_detector::{DebtCategory, TechnicalDebt},
};
use anyhow::Result;
use async_trait::async_trait;
use std::collections::HashMap;
use std::path::{Path, PathBuf};

/// Helper function to discover source files
async fn discover_source_files(path: &Path) -> Result<Vec<PathBuf>> {
    use walkdir::WalkDir;

    let mut files = Vec::new();
    let extensions = ["rs", "js", "ts", "py", "java", "cpp", "c", "go"];

    for entry in WalkDir::new(path)
        .follow_links(false)
        .into_iter()
        .filter_map(std::result::Result::ok)
        .filter(|e| e.file_type().is_file())
    {
        let path = entry.path();
        if let Some(ext) = path.extension().and_then(|e| e.to_str()) {
            if extensions.contains(&ext) {
                files.push(path.to_path_buf());
            }
        }
    }

    Ok(files)
}

/// Adapter for complexity analysis (TDG-based)
pub struct ComplexityDefectAnalyzer;

#[async_trait]
impl DefectAnalyzer for ComplexityDefectAnalyzer {
    type Config = ComplexityConfig;

    async fn analyze(&self, project_path: &Path, config: Self::Config) -> Result<Vec<Defect>> {
        let mut defects = Vec::new();
        let tdg_calculator = crate::services::tdg_calculator::TDGCalculator::new();

        // Analyze all source files
        let files = discover_source_files(project_path).await?;
        let scores = tdg_calculator.calculate_batch(files.clone()).await?;

        let mut index = 0;
        for (file_path, score) in files.into_iter().zip(scores.into_iter()) {
            if score.value > config.max_tdg_score {
                index += 1;
                defects.push(self.tdg_score_to_defect(file_path, score, index, &config));
            }
        }

        Ok(defects)
    }

    fn category(&self) -> DefectCategory {
        DefectCategory::Complexity
    }

    fn supports_incremental(&self) -> bool {
        true
    }
}

impl ComplexityDefectAnalyzer {
    fn tdg_score_to_defect(
        &self,
        file_path: PathBuf,
        score: TDGScore,
        index: usize,
        config: &ComplexityConfig,
    ) -> Defect {
        let severity = match score.severity {
            TDGSeverity::Critical => Severity::Critical,
            TDGSeverity::Warning => Severity::High,
            TDGSeverity::Normal => {
                if score.value > config.high_threshold {
                    Severity::Medium
                } else {
                    Severity::Low
                }
            }
        };

        let mut metrics = HashMap::new();
        metrics.insert("tdg_score".to_string(), score.value);
        metrics.insert("complexity_factor".to_string(), score.components.complexity);
        metrics.insert("churn_factor".to_string(), score.components.churn);
        metrics.insert("coupling_factor".to_string(), score.components.coupling);
        metrics.insert("domain_risk".to_string(), score.components.domain_risk);
        metrics.insert(
            "duplication_factor".to_string(),
            score.components.duplication,
        );
        metrics.insert("confidence".to_string(), score.confidence);

        Defect {
            id: format!("CPLX-{index:04}"),
            severity,
            category: DefectCategory::Complexity,
            file_path: file_path.clone(),
            line_start: 1, // TDG is file-level
            line_end: None,
            column_start: None,
            column_end: None,
            message: format!(
                "File has high complexity with TDG score of {:.2} (threshold: {:.1})",
                score.value, config.max_tdg_score
            ),
            rule_id: "tdg-complexity".to_string(),
            fix_suggestion: Some(self.generate_fix_suggestion(&score)),
            metrics,
        }
    }

    fn generate_fix_suggestion(&self, score: &TDGScore) -> String {
        let mut suggestions = Vec::new();

        if score.components.complexity > 0.7 {
            suggestions.push("reduce cyclomatic complexity by extracting methods");
        }
        if score.components.coupling > 0.7 {
            suggestions.push("reduce coupling by improving module boundaries");
        }
        if score.components.duplication > 0.5 {
            suggestions.push("eliminate code duplication");
        }

        if suggestions.is_empty() {
            "Consider refactoring to reduce overall complexity".to_string()
        } else {
            format!("Consider: {}", suggestions.join(", "))
        }
    }
}

#[derive(Clone)]
pub struct ComplexityConfig {
    pub max_tdg_score: f64,
    pub high_threshold: f64,
}

impl Default for ComplexityConfig {
    fn default() -> Self {
        Self {
            max_tdg_score: 2.0,
            high_threshold: 1.5,
        }
    }
}

impl AnalyzerConfig for ComplexityConfig {}

#[derive(Clone, Default)]
pub struct SATDConfig {
    pub include_test_files: bool,
}

impl AnalyzerConfig for SATDConfig {}

/// Adapter for SATD (Self-Admitted Technical Debt) detection
pub struct SATDDefectAnalyzer {
    detector: crate::services::satd_detector::SATDDetector,
}

impl SATDDefectAnalyzer {
    #[must_use] 
    pub fn new() -> Self {
        Self {
            detector: crate::services::satd_detector::SATDDetector::new(),
        }
    }
}

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

#[async_trait]
impl DefectAnalyzer for SATDDefectAnalyzer {
    type Config = SATDConfig;

    async fn analyze(&self, project_path: &Path, config: Self::Config) -> Result<Vec<Defect>> {
        let mut defects = Vec::new();

        // Analyze the project directory
        let result = self
            .detector
            .analyze_project(project_path, config.include_test_files)
            .await?;

        for (index, debt) in result.items.iter().enumerate() {
            defects.push(self.technical_debt_to_defect(debt, index + 1));
        }

        Ok(defects)
    }

    fn category(&self) -> DefectCategory {
        DefectCategory::TechnicalDebt
    }

    fn supports_incremental(&self) -> bool {
        true
    }
}

impl SATDDefectAnalyzer {
    fn technical_debt_to_defect(&self, debt: &TechnicalDebt, index: usize) -> Defect {
        let severity = match debt.severity {
            crate::services::satd_detector::Severity::Critical => Severity::Critical,
            crate::services::satd_detector::Severity::High => Severity::High,
            crate::services::satd_detector::Severity::Medium => Severity::Medium,
            crate::services::satd_detector::Severity::Low => Severity::Low,
        };

        let mut metrics = HashMap::new();
        metrics.insert(
            "debt_category".to_string(),
            debt.category.to_string().parse::<f64>().unwrap_or(0.0),
        );

        Defect {
            id: format!("SATD-{index:04}"),
            severity,
            category: DefectCategory::TechnicalDebt,
            file_path: debt.file.clone(),
            line_start: debt.line,
            line_end: None,
            column_start: Some(debt.column),
            column_end: None,
            message: format!("{}: {}", debt.category, debt.text),
            rule_id: format!("satd-{}", debt.category.to_string().to_lowercase()),
            fix_suggestion: Some(self.get_debt_fix_suggestion(&debt.category)),
            metrics,
        }
    }

    fn get_debt_fix_suggestion(&self, category: &DebtCategory) -> String {
        match category {
            DebtCategory::Design => "Refactor to improve design and architecture",
            DebtCategory::Defect => "Fix the known defect or bug",
            DebtCategory::Requirement => "Complete the missing requirement implementation",
            DebtCategory::Test => "Add proper test coverage",
            DebtCategory::Performance => "Optimize the performance issue",
            DebtCategory::Security => "Address the security vulnerability",
        }
        .to_string()
    }
}

/// Adapter for dead code detection
pub struct DeadCodeDefectAnalyzer;

impl DeadCodeDefectAnalyzer {
    #[must_use] 
    pub fn new() -> Self {
        Self {}
    }
}

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

#[derive(Clone)]
pub struct DeadCodeConfig {
    pub min_confidence: f64,
}

impl Default for DeadCodeConfig {
    fn default() -> Self {
        Self {
            min_confidence: 0.7,
        }
    }
}

impl AnalyzerConfig for DeadCodeConfig {}

#[async_trait]
impl DefectAnalyzer for DeadCodeDefectAnalyzer {
    type Config = DeadCodeConfig;

    async fn analyze(&self, project_path: &Path, config: Self::Config) -> Result<Vec<Defect>> {
        let mut defects = Vec::new();

        // Build dependency graph
        let project = crate::services::project_analyzer::Project::new(project_path)?;
        let dag = project.build_dependency_graph().await?;
        let mut analyzer = crate::services::dead_code_analyzer::DeadCodeAnalyzer::new(1000);
        let report = analyzer.analyze_dependency_graph(&dag);

        // Convert dead functions
        for (index, item) in report.dead_functions.iter().enumerate() {
            if f64::from(item.confidence) >= config.min_confidence {
                defects.push(self.dead_code_item_to_defect(item, "DEAD-FN", index + 1));
            }
        }

        // Convert dead classes
        for (index, item) in report.dead_classes.iter().enumerate() {
            if f64::from(item.confidence) >= config.min_confidence {
                defects.push(self.dead_code_item_to_defect(item, "DEAD-CLS", index + 1));
            }
        }

        // Convert unreachable code
        for (index, block) in report.unreachable_code.iter().enumerate() {
            defects.push(self.unreachable_block_to_defect(block, index + 1));
        }

        Ok(defects)
    }

    fn category(&self) -> DefectCategory {
        DefectCategory::DeadCode
    }

    fn supports_incremental(&self) -> bool {
        false // Requires full graph analysis
    }
}

impl DeadCodeDefectAnalyzer {
    fn dead_code_item_to_defect(&self, item: &DeadCodeItem, prefix: &str, index: usize) -> Defect {
        let severity = if item.confidence > 0.9 {
            Severity::High
        } else if item.confidence > 0.7 {
            Severity::Medium
        } else {
            Severity::Low
        };

        let mut metrics = HashMap::new();
        metrics.insert("confidence".to_string(), f64::from(item.confidence));

        Defect {
            id: format!("{prefix}-{index:04}"),
            severity,
            category: DefectCategory::DeadCode,
            file_path: PathBuf::from(&item.file_path),
            line_start: item.line_number,
            line_end: None,
            column_start: None,
            column_end: None,
            message: format!(
                "Dead {}: '{}' is never used (confidence: {:.0}%)",
                format!("{:?}", item.dead_type).to_lowercase(),
                item.name,
                item.confidence * 100.0
            ),
            rule_id: format!("dead-{}", format!("{:?}", item.dead_type).to_lowercase()),
            fix_suggestion: Some(format!(
                "Remove unused {} '{}'",
                format!("{:?}", item.dead_type).to_lowercase(),
                item.name
            )),
            metrics,
        }
    }

    fn unreachable_block_to_defect(&self, block: &UnreachableBlock, index: usize) -> Defect {
        let mut metrics = HashMap::new();
        metrics.insert(
            "lines".to_string(),
            f64::from(block.end_line - block.start_line + 1),
        );

        Defect {
            id: format!("UNREACH-{index:04}"),
            severity: Severity::High,
            category: DefectCategory::DeadCode,
            file_path: PathBuf::from(&block.file_path),
            line_start: block.start_line,
            line_end: Some(block.end_line),
            column_start: None,
            column_end: None,
            message: format!(
                "Unreachable code block ({} lines) - {}",
                block.end_line - block.start_line + 1,
                block.reason
            ),
            rule_id: "unreachable-code".to_string(),
            fix_suggestion: Some("Remove unreachable code".to_string()),
            metrics,
        }
    }
}

/// Adapter for code duplication detection
pub struct DuplicationDefectAnalyzer {
    detector: crate::services::duplicate_detector::DuplicateDetectionEngine,
}

impl DuplicationDefectAnalyzer {
    #[must_use] 
    pub fn new() -> Self {
        let config = crate::services::duplicate_detector::DuplicateDetectionConfig::default();
        Self {
            detector: crate::services::duplicate_detector::DuplicateDetectionEngine::new(config),
        }
    }
}

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

#[derive(Clone)]
pub struct DuplicationConfig {
    pub min_similarity: f64,
}

impl Default for DuplicationConfig {
    fn default() -> Self {
        Self {
            min_similarity: 0.8,
        }
    }
}

impl AnalyzerConfig for DuplicationConfig {}

#[async_trait]
impl DefectAnalyzer for DuplicationDefectAnalyzer {
    type Config = DuplicationConfig;

    async fn analyze(&self, project_path: &Path, config: Self::Config) -> Result<Vec<Defect>> {
        let mut defects = Vec::new();
        let files = discover_source_files(project_path).await?;

        // Prepare file contents
        let mut file_contents = Vec::new();
        for file in files {
            if let Ok(content) = tokio::fs::read_to_string(&file).await {
                let language = self.detect_language(&file);
                file_contents.push((file.clone(), content, language));
            }
        }

        let report = self.detector.detect_duplicates(&file_contents)?;

        // Convert clone groups to defects
        for (group_index, group) in report.groups.iter().enumerate() {
            if group.average_similarity >= config.min_similarity {
                for (instance_index, instance) in group.fragments.iter().enumerate() {
                    defects.push(self.clone_to_defect(
                        group,
                        instance,
                        group_index * 100 + instance_index + 1,
                    ));
                }
            }
        }

        Ok(defects)
    }

    fn category(&self) -> DefectCategory {
        DefectCategory::Duplication
    }

    fn supports_incremental(&self) -> bool {
        false // Requires full codebase analysis
    }
}

impl DuplicationDefectAnalyzer {
    fn detect_language(&self, path: &Path) -> crate::services::duplicate_detector::Language {
        use crate::services::duplicate_detector::Language;

        match path.extension().and_then(|e| e.to_str()) {
            Some("rs") => Language::Rust,
            Some("ts") => Language::TypeScript,
            Some("js") => Language::JavaScript,
            Some("py") => Language::Python,
            Some("c") => Language::C,
            Some("cpp" | "cc") => Language::Cpp,
            Some("kt") => Language::Kotlin,
            _ => Language::Rust, // Default
        }
    }

    fn clone_to_defect(
        &self,
        group: &CloneGroup,
        instance: &crate::services::duplicate_detector::CloneInstance,
        index: usize,
    ) -> Defect {
        let severity = match &group.clone_type {
            CloneType::Type1 { .. } if group.total_lines > 50 => Severity::High,
            CloneType::Type1 { .. } => Severity::Medium,
            CloneType::Type2 { .. } if group.total_lines > 30 => Severity::Medium,
            CloneType::Type2 { .. } => Severity::Low,
            CloneType::Type3 { .. } => Severity::Low,
        };

        let mut metrics = HashMap::new();
        metrics.insert(
            "similarity".to_string(),
            instance.similarity_to_representative,
        );
        metrics.insert("total_lines".to_string(), group.total_lines as f64);
        metrics.insert("group_size".to_string(), group.fragments.len() as f64);

        Defect {
            id: format!("DUP-{index:04}"),
            severity,
            category: DefectCategory::Duplication,
            file_path: instance.file.clone(),
            line_start: instance.start_line as u32,
            line_end: Some(instance.end_line as u32),
            column_start: None,
            column_end: None,
            message: format!(
                "{:?} code clone ({} lines, {:.0}% similarity) in group {}",
                group.clone_type,
                instance.end_line - instance.start_line + 1,
                instance.similarity_to_representative * 100.0,
                group.id
            ),
            rule_id: format!("duplication-{:?}", group.clone_type).to_lowercase(),
            fix_suggestion: Some(
                "Extract duplicated code into a shared function or module".to_string(),
            ),
            metrics,
        }
    }
}

/// Adapter for performance analysis (Big-O)
pub struct PerformanceDefectAnalyzer {
    analyzer: crate::services::big_o_analyzer::BigOAnalyzer,
}

impl PerformanceDefectAnalyzer {
    #[must_use] 
    pub fn new() -> Self {
        Self {
            analyzer: crate::services::big_o_analyzer::BigOAnalyzer::new(),
        }
    }
}

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

#[derive(Clone, Default)]
pub struct PerformanceConfig {
    pub include_nlogn: bool,
}

impl AnalyzerConfig for PerformanceConfig {}

#[async_trait]
impl DefectAnalyzer for PerformanceDefectAnalyzer {
    type Config = PerformanceConfig;

    async fn analyze(&self, project_path: &Path, config: Self::Config) -> Result<Vec<Defect>> {
        let mut defects = Vec::new();
        let analysis_config = crate::services::big_o_analyzer::BigOAnalysisConfig {
            project_path: project_path.to_path_buf(),
            include_patterns: vec![],
            exclude_patterns: vec![],
            confidence_threshold: 50,
            analyze_space_complexity: true,
        };
        let report = self.analyzer.analyze(analysis_config).await?;

        for (index, func) in report.high_complexity_functions.iter().enumerate() {
            if self.is_problematic_complexity(&func.time_complexity, &config) {
                defects.push(self.function_complexity_to_defect(func, index + 1));
            }
        }

        Ok(defects)
    }

    fn category(&self) -> DefectCategory {
        DefectCategory::Performance
    }

    fn supports_incremental(&self) -> bool {
        true
    }
}

impl PerformanceDefectAnalyzer {
    fn is_problematic_complexity(
        &self,
        complexity: &crate::models::complexity_bound::ComplexityBound,
        config: &PerformanceConfig,
    ) -> bool {
        use crate::models::complexity_bound::BigOClass;

        matches!(
            complexity.class,
            BigOClass::Quadratic | BigOClass::Cubic | BigOClass::Exponential | BigOClass::Factorial
        ) || (config.include_nlogn && matches!(complexity.class, BigOClass::Linearithmic))
    }

    fn function_complexity_to_defect(&self, func: &FunctionComplexity, index: usize) -> Defect {
        use crate::models::complexity_bound::BigOClass;

        let severity = match func.time_complexity.class {
            BigOClass::Exponential | BigOClass::Factorial => Severity::Critical,
            BigOClass::Cubic => Severity::High,
            BigOClass::Quadratic => Severity::Medium,
            _ => Severity::Low,
        };

        let mut metrics = HashMap::new();
        metrics.insert(
            "time_complexity_class".to_string(),
            f64::from(func.time_complexity.class as u8),
        );
        metrics.insert(
            "space_complexity_class".to_string(),
            f64::from(func.space_complexity.class as u8),
        );
        metrics.insert("confidence".to_string(), f64::from(func.confidence));

        Defect {
            id: format!("PERF-{index:04}"),
            severity,
            category: DefectCategory::Performance,
            file_path: func.file_path.clone(),
            line_start: func.line_number as u32,
            line_end: None,
            column_start: None,
            column_end: None,
            message: format!(
                "Function '{}' has high time complexity: {}",
                func.function_name,
                func.time_complexity.notation()
            ),
            rule_id: "high-complexity".to_string(),
            fix_suggestion: Some(self.generate_performance_suggestion(&func.time_complexity)),
            metrics,
        }
    }

    fn generate_performance_suggestion(
        &self,
        complexity: &crate::models::complexity_bound::ComplexityBound,
    ) -> String {
        use crate::models::complexity_bound::BigOClass;

        match complexity.class {
            BigOClass::Quadratic => {
                "Consider using a more efficient algorithm or data structure to reduce quadratic complexity"
            }
            BigOClass::Cubic => {
                "Cubic complexity is rarely acceptable; consider algorithmic improvements"
            }
            BigOClass::Exponential => {
                "Exponential complexity should be avoided; consider dynamic programming or approximation"
            }
            BigOClass::Factorial => {
                "Factorial complexity is unacceptable for most use cases; fundamental algorithm redesign needed"
            }
            _ => {
                "Review algorithm efficiency and consider optimization"
            }
        }
        .to_string()
    }
}

/// Adapter for architecture issues detection
pub struct ArchitectureDefectAnalyzer;

impl ArchitectureDefectAnalyzer {
    #[must_use] 
    pub fn new() -> Self {
        Self {}
    }
}

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

#[derive(Clone)]
pub struct ArchitectureConfig {
    pub max_coupling: usize,
}

impl Default for ArchitectureConfig {
    fn default() -> Self {
        Self { max_coupling: 10 }
    }
}

impl AnalyzerConfig for ArchitectureConfig {}

#[async_trait]
impl DefectAnalyzer for ArchitectureDefectAnalyzer {
    type Config = ArchitectureConfig;

    async fn analyze(&self, project_path: &Path, _config: Self::Config) -> Result<Vec<Defect>> {
        let defects = Vec::new();

        // Build dependency graph
        let project = crate::services::project_analyzer::Project::new(project_path)?;
        let _dag = project.build_dependency_graph().await?;

        // Analyze architecture issues
        // This is a placeholder for actual architecture analysis

        Ok(defects)
    }

    fn category(&self) -> DefectCategory {
        DefectCategory::Architecture
    }

    fn supports_incremental(&self) -> bool {
        false
    }
}

#[cfg(test)]
mod property_tests {
    use proptest::prelude::*;

    proptest! {
        #[test]
        fn basic_property_stability(_input in ".*") {
            // Basic property test for coverage
            prop_assert!(true);
        }

        #[test]
        fn module_consistency_check(_x in 0u32..1000) {
            // Module consistency verification
            prop_assert!(_x < 1001);
        }
    }
}