Example #1
0
 public MemberMetric(
     string codeFile,
     AccessModifierKind accessModifier,
     IHalsteadMetrics halstead,
     int lineNumber,
     int linesOfCode,
     double maintainabilityIndex,
     int cyclomaticComplexity,
     string name,
     IEnumerable <ITypeCoupling> classCouplings,
     int numberOfParameters,
     int numberOfLocalVariables,
     int afferentCoupling,
     IMemberDocumentation documentation)
 {
     _halstead            = halstead;
     CodeFile             = codeFile;
     AccessModifier       = accessModifier;
     LineNumber           = lineNumber;
     LinesOfCode          = linesOfCode;
     MaintainabilityIndex = maintainabilityIndex;
     CyclomaticComplexity = cyclomaticComplexity;
     Name                   = name;
     ClassCouplings         = classCouplings;
     NumberOfParameters     = numberOfParameters;
     NumberOfLocalVariables = numberOfLocalVariables;
     AfferentCoupling       = afferentCoupling;
     Documentation          = documentation;
 }
Example #2
0
		static HalsteadMetrics()
		{
			GenericInstanceSetPropertyMetrics = new HalsteadMetrics(5, 3, 4, 3);
			GenericStaticSetPropertyMetrics = new HalsteadMetrics(4, 3, 3, 3);
			GenericInstanceGetPropertyMetrics = new HalsteadMetrics(3, 2, 3, 2);
			GenericStaticGetPropertyMetrics = new HalsteadMetrics(2, 1, 2, 1);
		}
Example #3
0
 static HalsteadMetrics()
 {
     GenericInstanceSetPropertyMetrics = new HalsteadMetrics(5, 3, 4, 3);
     GenericStaticSetPropertyMetrics   = new HalsteadMetrics(4, 3, 3, 3);
     GenericInstanceGetPropertyMetrics = new HalsteadMetrics(3, 2, 3, 2);
     GenericStaticGetPropertyMetrics   = new HalsteadMetrics(2, 1, 2, 1);
 }
        private MetricResult CalculateMaintainablityIndex(IHalsteadMetrics halsteadMetrics, MemberMetric metric)
        {
            double linesOfCode = metric.GetMetricsByName(MetricNames.LinesOfCode).Value.Value;
            double cyclomatic  = metric.GetMetricsByName(MetricNames.CyclomaticComplexity).Value.Value;
            double num1        = 1;

            if (linesOfCode == 0)
            {
                num1 = 100;
            }
            else
            {
                double?volume = halsteadMetrics.GetVolume();
                double num    = 1;
                if (volume.HasValue)
                {
                    num = Math.Log(volume.Value);
                }
                num1 = (171 - 5.2 * num - 0.23 * cyclomatic - 16.2 * Math.Log(linesOfCode)) * 100 / 171;
            }
            return(new MetricResult
            {
                Name = MetricNames.MaintainabilityIndex,
                Value = Math.Round(Math.Max(0, num1))
            });
        }
Example #5
0
		public MemberMetric(
			string codeFile,
			AccessModifierKind accessModifier,
			IHalsteadMetrics halstead,
			int lineNumber,
			int linesOfCode,
			double maintainabilityIndex,
			int cyclomaticComplexity,
			string name,
			IEnumerable<ITypeCoupling> classCouplings,
			int numberOfParameters,
			int numberOfLocalVariables,
			int afferentCoupling,
			IMemberDocumentation documentation)
		{
			_halstead = halstead;
			CodeFile = codeFile;
			AccessModifier = accessModifier;
			LineNumber = lineNumber;
			LinesOfCode = linesOfCode;
			MaintainabilityIndex = maintainabilityIndex;
			CyclomaticComplexity = cyclomaticComplexity;
			Name = name;
			ClassCouplings = classCouplings.AsArray();
			NumberOfParameters = numberOfParameters;
			NumberOfLocalVariables = numberOfLocalVariables;
			AfferentCoupling = afferentCoupling;
			Documentation = documentation;
		}
 private IEnumerable <MemberMetric> CalculateMemberMetrics(IEnumerable <MemberNode> nodes)
 {
     foreach (MemberNode memberNode in nodes)
     {
         SyntaxNode       syntaxNode       = memberNode.SyntaxNode;
         MemberMetricKind memberMetricKind = MemberMetricsCalculator.GetMemberMetricKind(memberNode);
         IHalsteadMetrics halsteadMetric   = (new HalsteadAnalyzer()).Calculate(memberNode);
         if (halsteadMetric == null)
         {
             continue;
         }
         MemberMetric memberMetric = new MemberMetric()
         {
             Name       = memberNode.DisplayName,
             CodeFile   = memberNode.CodeFile,
             LineNumber = memberNode.LineNumber,
             Halstead   = halsteadMetric,
             Kind       = memberMetricKind
         };
         MemberMetric memberMetric1 = memberMetric;
         foreach (Func <MemberNode, MetricResult> action in memberNodeActions)
         {
             memberMetric.AddMetricResult(action(memberNode));
         }
         foreach (Func <SyntaxNode, MetricResult> action in syntaxNodeActions)
         {
             memberMetric.AddMetricResult(action(syntaxNode));
         }
         memberMetric.AddMetricResult(CalculateMaintainablityIndex(halsteadMetric, memberMetric));
         //memberMetric.AddMetricResult(CalculateNumberOfDeliveredBugs(halsteadMetric));
         yield return(memberMetric);
     }
 }
Example #7
0
        private bool CalculateGenericPropertyMetrics(MemberNode node)
        {
            PropertyDeclarationSyntax syntaxNode = node.SyntaxNode as PropertyDeclarationSyntax;
            PropertyDeclarationSyntax propertyDeclarationSyntax = syntaxNode;

            if (syntaxNode != null)
            {
                bool flag = ((IEnumerable <SyntaxToken>)propertyDeclarationSyntax.Modifiers).Any <SyntaxToken>((SyntaxToken x) => x.ValueText == "static");
                if (MemberBodySelector.FindBody(node) == null)
                {
                    switch (node.Kind)
                    {
                    case MemberKind.GetProperty:
                    {
                        this.metrics = (flag ? HalsteadMetrics.GenericStaticGetPropertyMetrics : HalsteadMetrics.GenericInstanceGetPropertyMetrics);
                        return(true);
                    }

                    case MemberKind.SetProperty:
                    {
                        this.metrics = (flag ? HalsteadMetrics.GenericStaticSetPropertyMetrics : HalsteadMetrics.GenericInstanceSetPropertyMetrics);
                        return(true);
                    }
                    }
                }
            }
            return(false);
        }
 private MetricResult CalculateNumberOfDeliveredBugs(IHalsteadMetrics halsteadMetrics)
 {
     return(new MetricResult
     {
         Name = MetricNames.NumberOfDeliverdBugs,
         Value = halsteadMetrics.GetBugs()
     });
 }
		private static double CalculateMaintainablityIndex(double cyclomaticComplexity, double linesOfCode, IHalsteadMetrics halsteadMetrics)
		{
			if (linesOfCode.Equals(0.0) || halsteadMetrics.NumberOfOperands.Equals(0) || halsteadMetrics.NumberOfOperators.Equals(0))
			{
				return 100.0;
			}

			var num = Math.Log(halsteadMetrics.GetVolume());
			var mi = ((171 - (5.2 * num) - (0.23 * cyclomaticComplexity) - (16.2 * Math.Log(linesOfCode))) * 100) / 171;

			return Math.Max(0.0, mi);
		}
Example #10
0
		public void VisitBlock(BlockSyntax node)
		{
			var tokens = node.DescendantTokens().ToList();
			var dictionary = ParseTokens(tokens, Operands.All);
			var dictionary2 = ParseTokens(tokens, Operators.All);
			var metrics = new HalsteadMetrics(
				numOperands: dictionary.Values.Sum(x => x.Count), 
				numUniqueOperands: dictionary.Values.SelectMany(x => x).Distinct().Count(), 
				numOperators: dictionary2.Values.Sum(x => x.Count), 
				numUniqueOperators: dictionary2.Values.SelectMany(x => x).Distinct().Count());
			_metrics = metrics;
		}
Example #11
0
		public IHalsteadMetrics Merge(IHalsteadMetrics other)
		{
			if (other == null)
			{
				return this;
			}

			return new HalsteadMetrics(
				NumberOfOperands + other.NumberOfOperands, 
				NumberOfOperators + other.NumberOfOperators, 
				NumberOfUniqueOperands + other.NumberOfUniqueOperands, 
				NumberOfUniqueOperators + other.NumberOfUniqueOperators);
		}
Example #12
0
        public IHalsteadMetrics Merge(IHalsteadMetrics other)
        {
            if (other == null)
            {
                return(this);
            }

            return(new HalsteadMetrics(
                       NumberOfOperands + other.NumberOfOperands,
                       NumberOfOperators + other.NumberOfOperators,
                       NumberOfUniqueOperands + other.NumberOfUniqueOperands,
                       NumberOfUniqueOperators + other.NumberOfUniqueOperators));
        }
        public void VisitBlock(BlockSyntax node)
        {
            var tokens      = node.DescendantTokens().ToList();
            var dictionary  = ParseTokens(tokens, Operands.All);
            var dictionary2 = ParseTokens(tokens, Operators.All);
            var metrics     = new HalsteadMetrics(
                numOperands: dictionary.Values.Sum(x => x.Count),
                numUniqueOperands: dictionary.Values.SelectMany(x => x).Distinct().Count(),
                numOperators: dictionary2.Values.Sum(x => x.Count),
                numUniqueOperators: dictionary2.Values.SelectMany(x => x).Distinct().Count());

            _metrics = metrics;
        }
Example #14
0
        public override void VisitBlock(BlockSyntax node)
        {
            base.VisitBlock(node);
            IEnumerable <SyntaxToken> list = node.DescendantTokens(null, false).ToList <SyntaxToken>();
            IDictionary <SyntaxKind, IList <string> > syntaxKinds  = HalsteadAnalyzer.ParseTokens(list, HalsteadOperands.All);
            IDictionary <SyntaxKind, IList <string> > syntaxKinds1 = HalsteadAnalyzer.ParseTokens(list, HalsteadOperators.All);
            HalsteadMetrics halsteadMetric = new HalsteadMetrics()
            {
                NumOperands        = syntaxKinds.Values.SelectMany <IList <string>, string>((IList <string> x) => x).Count <string>(),
                NumUniqueOperands  = syntaxKinds.Values.SelectMany <IList <string>, string>((IList <string> x) => x).Distinct <string>().Count <string>(),
                NumOperators       = syntaxKinds1.Values.SelectMany <IList <string>, string>((IList <string> x) => x).Count <string>(),
                NumUniqueOperators = syntaxKinds1.Values.SelectMany <IList <string>, string>((IList <string> x) => x).Distinct <string>().Count <string>()
            };

            this.metrics = halsteadMetric;
        }
Example #15
0
        static HalsteadMetrics()
        {
            HalsteadMetrics halsteadMetric = new HalsteadMetrics()
            {
                NumOperands        = 5,
                NumOperators       = 3,
                NumUniqueOperands  = 4,
                NumUniqueOperators = 3
            };

            HalsteadMetrics.GenericInstanceSetPropertyMetrics = halsteadMetric;
            HalsteadMetrics halsteadMetric1 = new HalsteadMetrics()
            {
                NumOperands        = 4,
                NumOperators       = 3,
                NumUniqueOperands  = 3,
                NumUniqueOperators = 3
            };

            HalsteadMetrics.GenericStaticSetPropertyMetrics = halsteadMetric1;
            HalsteadMetrics halsteadMetric2 = new HalsteadMetrics()
            {
                NumOperands        = 3,
                NumOperators       = 2,
                NumUniqueOperands  = 3,
                NumUniqueOperators = 2
            };

            HalsteadMetrics.GenericInstanceGetPropertyMetrics = halsteadMetric2;
            HalsteadMetrics halsteadMetric3 = new HalsteadMetrics()
            {
                NumOperands        = 2,
                NumOperators       = 1,
                NumUniqueOperands  = 2,
                NumUniqueOperators = 1
            };

            HalsteadMetrics.GenericStaticGetPropertyMetrics = halsteadMetric3;
        }
        private static double CalculateMaintainablityIndex(double cyclomaticComplexity, double linesOfCode, IHalsteadMetrics halsteadMetrics)
        {
            if (linesOfCode.Equals(0.0) || halsteadMetrics.NumberOfOperands.Equals(0) || halsteadMetrics.NumberOfOperators.Equals(0))
            {
                return(100.0);
            }

            var num = Math.Log(halsteadMetrics.GetVolume());
            var mi  = ((171 - (5.2 * num) - (0.23 * cyclomaticComplexity) - (16.2 * Math.Log(linesOfCode))) * 100) / 171;

            return(Math.Max(0.0, mi));
        }