public TypeMetric CalculateFrom(TypeDeclarationSyntaxInfo typeNode, MemberMetrics memberMetrics) { TypeDeclarationSyntax syntax = (TypeDeclarationSyntax)typeNode.Syntax; TypeMetricKind metricKind = TypeMetricsCalculator.GetMetricKind(syntax); LcomMetrics lcomMetric = this.CalculateLackOfCohesion(syntax); TypeMetric typeMetric = new TypeMetric() { Name = TypeMetricsCalculator.GetDisplayName(syntax), Kind = metricKind, MemberMetrics = memberMetrics }; List <MetricResult> metricResults = new List <MetricResult>(); foreach (MetricCalculator calculator in calculators) { typeMetric.AddMetricResult(calculator.Calculate <MemberMetrics, MemberMetric>(memberMetrics)); } typeMetric.AddMetricResult(new MetricResult() { Name = MetricNames.LackOfCohesionOfMethods, Value = lcomMetric.Lcom }); typeMetric.AddMetricResult(new MetricResult() { Name = MetricNames.LackOfCohesionOfMethodsHendersonSellers, Value = lcomMetric.LcomHendersonSellers }); typeMetric.AddMetricResult(new MetricResult() { Name = MetricNames.NumberOfFields, Value = lcomMetric.NumFields }); typeMetric.AddMetricResult(new MetricResult() { Name = MetricNames.NumberOfMethods, Value = lcomMetric.NumMethods }); return(typeMetric); }
public LcomMetrics Calculate(TypeDeclarationSyntax type) { SemanticModel semanticModel = this.model; CancellationToken cancellationToken = new CancellationToken(); this.typeSymbol = semanticModel.GetDeclaredSymbol(type, cancellationToken); SimpleMemberCollectorSyntaxWalker simpleMemberCollectorSyntaxWalker = new SimpleMemberCollectorSyntaxWalker(); IEnumerable <SyntaxNode> list = simpleMemberCollectorSyntaxWalker.GetMembers(this.model, type).ToList <SyntaxNode>(); this.FilterInstanceFieldDeclarations(list.OfType <FieldDeclarationSyntax>().ToList <FieldDeclarationSyntax>()); this.FilterInstanceAutoProperties(list.OfType <PropertyDeclarationSyntax>().ToList <PropertyDeclarationSyntax>()); this.Visit(type); double num = (double)this.numFieldAccesses.Values.Sum(); int num1 = checked (this.numMethods * this.numInstanceFields); double num2 = Math.Round(1 - num / (double)num1, 2); if (double.IsNaN(num2) || num2 < 0) { num2 = 0; } double num3 = ((double)this.numMethods - num / (double)this.numInstanceFields) / (double)(checked (this.numMethods - 1)); num3 = Math.Round(num3, 2); if (double.IsNaN(num3) || num2 < 0) { num3 = 0; } LcomMetrics lcomMetric = new LcomMetrics() { Lcom = num2, LcomHendersonSellers = num3, NumFields = this.numFields, NumMethods = this.numMethods }; return(lcomMetric); }