| | | 1 | | using System.Collections.Generic; |
| | | 2 | | using System.Linq; |
| | | 3 | | using DirectSight.Parser.Analysis; |
| | | 4 | | |
| | | 5 | | namespace DirectSight.CodeAnalysis; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// Analyses the method metrics for risk hotspots based on exceeded thresholds. |
| | | 9 | | /// </summary> |
| | | 10 | | internal class RiskHotspotsAnalyzer |
| | | 11 | | { |
| | | 12 | | /// <summary> |
| | | 13 | | /// The thresholds of the various metrics. |
| | | 14 | | /// </summary> |
| | 2 | 15 | | private readonly Dictionary<string, decimal> thresholdsByMetricName = new() |
| | 2 | 16 | | { |
| | 2 | 17 | | ["Cyclomatic complexity"] = 15, |
| | 2 | 18 | | ["NPath complexity"] = 200, |
| | 2 | 19 | | ["Crap Score"] = 30 |
| | 2 | 20 | | }; |
| | | 21 | | |
| | | 22 | | /// <summary> |
| | | 23 | | /// Initializes a new instance of the <see cref="RiskHotspotsAnalyzer"/> class. |
| | | 24 | | /// </summary> |
| | 6 | 25 | | public RiskHotspotsAnalyzer() {} |
| | | 26 | | |
| | | 27 | | /// <summary> |
| | | 28 | | /// Performs a risk hotspot analysis on the given assemblies. |
| | | 29 | | /// </summary> |
| | | 30 | | /// <param name="assemblies">The assemlies to analyze.</param> |
| | | 31 | | /// <returns>The risk hotspot analysis result.</returns> |
| | | 32 | | public RiskHotspotAnalysisResult PerformRiskHotspotAnalysis(IEnumerable<Assembly> assemblies) |
| | 2 | 33 | | { |
| | 2 | 34 | | var riskHotspots = new List<RiskHotspot>(); |
| | | 35 | | |
| | 2 | 36 | | bool codeCodeQualityMetricsAvailable = false; |
| | | 37 | | |
| | 10 | 38 | | foreach (var assembly in assemblies) |
| | 2 | 39 | | { |
| | 78 | 40 | | foreach (var clazz in assembly.Classes) |
| | 36 | 41 | | { |
| | 36 | 42 | | int fileIndex = 0; |
| | | 43 | | |
| | 180 | 44 | | foreach (var file in clazz.Files) |
| | 36 | 45 | | { |
| | 252 | 46 | | foreach (var methodMetric in file.MethodMetrics) |
| | 72 | 47 | | { |
| | 72 | 48 | | var codeCodeQualityMetrics = methodMetric.Metrics |
| | 504 | 49 | | .Where(m => m.MetricType == MetricType.CodeQuality); |
| | | 50 | | |
| | 72 | 51 | | codeCodeQualityMetricsAvailable |= codeCodeQualityMetrics.Any(); |
| | | 52 | | |
| | 72 | 53 | | var statusMetrics = codeCodeQualityMetrics |
| | 216 | 54 | | .Select(m => new MetricStatus(m, this.thresholdsByMetricName.TryGetValue(m.Name, out var thr |
| | 72 | 55 | | .ToArray(); |
| | | 56 | | |
| | 288 | 57 | | if (statusMetrics.Any(m => m.Exceeded)) |
| | 0 | 58 | | { |
| | 0 | 59 | | riskHotspots.Add(new RiskHotspot(assembly, clazz, methodMetric, statusMetrics, fileIndex)); |
| | 0 | 60 | | } |
| | 72 | 61 | | } |
| | | 62 | | |
| | 36 | 63 | | fileIndex++; |
| | 36 | 64 | | } |
| | 36 | 65 | | } |
| | 2 | 66 | | } |
| | | 67 | | |
| | 2 | 68 | | var result = new RiskHotspotAnalysisResult( |
| | 0 | 69 | | [.. riskHotspots.OrderByDescending(r => r.StatusMetrics.Where(m => m.Exceeded).Max(m => m.Metric.Value))], |
| | 2 | 70 | | codeCodeQualityMetricsAvailable); |
| | | 71 | | |
| | 2 | 72 | | return result; |
| | 2 | 73 | | } |
| | | 74 | | } |