Inheritance: Microsoft.CodeAnalysis.SyntaxWalker
		public IEnumerable<IHalsteadMetrics> Calculate(SyntaxNode root)
		{
			var analyzer = new HalsteadAnalyzer();
			var childNodes = root.ChildNodes().AsArray();

			var types = childNodes.Where(n => n.IsKind(SyntaxKind.ClassDeclaration) || n.IsKind(SyntaxKind.StructDeclaration))
				.AsArray();
			var methods = types.SelectMany(n => n.ChildNodes().Where(_isMethod));
			var getProperties = types.SelectMany(n => n.ChildNodes().Where(IsGetProperty));
			var setProperties = types.SelectMany(n => n.ChildNodes().Where(IsSetProperty));
			var looseMethods = childNodes.Where(_isMethod);
			var looseGetProperties = childNodes.Where(IsGetProperty);
			var looseSetProperties = childNodes.Where(IsSetProperty);
			var members = methods.Concat(getProperties)
								 .Concat(setProperties)
								 .Concat(looseMethods)
								 .Concat(looseGetProperties)
								 .Concat(looseSetProperties)
								 .AsArray();
			if (members.Any())
			{
				return members.Select(analyzer.Calculate);
			}

			var statements = childNodes.Length == 0
				? root.DescendantNodesAndTokens().Select(x => SyntaxFactory.ParseStatement(x.ToFullString(), 0, new CSharpParseOptions(kind: SourceCodeKind.Script, preprocessorSymbols: new string[0])))
				: childNodes.Select(x => SyntaxFactory.ParseStatement(x.ToFullString(), 0, new CSharpParseOptions(kind: SourceCodeKind.Script, preprocessorSymbols: new string[0])));

			var fakeMethod = SyntaxFactory.MethodDeclaration(SyntaxFactory.PredefinedType(SyntaxFactory.Token(SyntaxKind.VoidKeyword)), "fake")
				.WithBody(SyntaxFactory.Block(statements));
			return new[]
				   {
					   analyzer.Calculate(fakeMethod)
				   };
		}
        private IMemberMetric CalculateMemberMetricSlim(SyntaxNode syntaxNode)
        {
            var       analyzer               = new HalsteadAnalyzer();
            var       halsteadMetrics        = analyzer.Calculate(syntaxNode);
            var       memberName             = _nameResolver.TryResolveMemberSignatureString(syntaxNode);
            var       source                 = Enumerable.Empty <ITypeCoupling>();
            var       complexity             = CalculateCyclomaticComplexity(syntaxNode);
            var       linesOfCode            = CalculateLinesOfCode(syntaxNode);
            const int NumberOfParameters     = 0;
            const int NumberOfLocalVariables = 0;
            var       maintainabilityIndex   = CalculateMaintainablityIndex(complexity, linesOfCode, halsteadMetrics);
            const int AfferentCoupling       = 0;
            const int LineNumber             = 0;
            var       filePath               = string.Empty;
            var       accessModifier         = GetAccessModifier(syntaxNode);

            return(new MemberMetric(filePath,
                                    accessModifier,
                                    halsteadMetrics,
                                    LineNumber,
                                    linesOfCode,
                                    maintainabilityIndex,
                                    complexity,
                                    memberName,
                                    source.AsArray(),
                                    NumberOfParameters,
                                    NumberOfLocalVariables,
                                    AfferentCoupling,
                                    null));
        }
Exemplo n.º 3
0
        private IMemberMetric CalculateMemberMetricSlim(SyntaxNode syntaxNode)
        {
            var analyzer             = new HalsteadAnalyzer();
            var halsteadMetrics      = analyzer.Calculate(syntaxNode);
            var memberName           = _nameResolver.TryResolveMemberSignatureString(syntaxNode);
            var complexity           = CalculateCyclomaticComplexity(syntaxNode);
            var linesOfCode          = CalculateLinesOfCode(syntaxNode);
            var maintainabilityIndex = CalculateMaintainablityIndex(complexity, linesOfCode, halsteadMetrics);
            var location             = syntaxNode.GetLocation();
            var lineNumber           = location.GetLineSpan().StartLinePosition.Line;
            var filePath             = location.SourceTree == null ? string.Empty : location.SourceTree.FilePath;
            var accessModifier       = GetAccessModifier(syntaxNode);

            return(new MemberMetric(
                       filePath,
                       accessModifier,
                       halsteadMetrics,
                       lineNumber,
                       linesOfCode,
                       maintainabilityIndex,
                       complexity,
                       memberName,
                       new ITypeCoupling[0],
                       0,
                       0,
                       0,
                       null));
        }
Exemplo n.º 4
0
        private async Task <IMemberMetric> CalculateMemberMetric(SyntaxNode syntaxNode)
        {
            var analyzer               = new HalsteadAnalyzer();
            var halsteadMetrics        = analyzer.Calculate(syntaxNode);
            var memberName             = _nameResolver.TryResolveMemberSignatureString(syntaxNode);
            var source                 = CalculateClassCoupling(syntaxNode);
            var complexity             = CalculateCyclomaticComplexity(syntaxNode);
            var linesOfCode            = CalculateLinesOfCode(syntaxNode);
            var sourceLinesOfCode      = CalculateSourceLinesOfCode(syntaxNode);
            var numberOfParameters     = CalculateNumberOfParameters(syntaxNode);
            var numberOfLocalVariables = CalculateNumberOfLocalVariables(syntaxNode);
            var maintainabilityIndex   = CalculateMaintainablityIndex(complexity, linesOfCode, halsteadMetrics);
            var afferentCoupling       = await CalculateAfferentCoupling(syntaxNode).ConfigureAwait(false);

            var location   = syntaxNode.GetLocation();
            var lineNumber = location.GetLineSpan().StartLinePosition.Line;
            var filePath   = location.SourceTree == null ? string.Empty : location.SourceTree.FilePath;

            filePath = filePath.GetPathRelativeTo(_rootFolder);
            var accessModifier = GetAccessModifier(syntaxNode);
            IMemberDocumentation documentation = null;

            if (syntaxNode.SyntaxTree == Model.SyntaxTree)
            {
                var symbol = Model.GetDeclaredSymbol(syntaxNode);
                documentation = await _documentationFactory.Create(symbol, CancellationToken.None);
            }

            return(new MemberMetric(filePath,
                                    accessModifier,
                                    halsteadMetrics,
                                    lineNumber,
                                    linesOfCode,
                                    sourceLinesOfCode,
                                    maintainabilityIndex,
                                    complexity,
                                    memberName,
                                    source.AsArray(),
                                    numberOfParameters,
                                    numberOfLocalVariables,
                                    afferentCoupling,
                                    documentation));
        }
        public IEnumerable <IHalsteadMetrics> Calculate(SyntaxNode root)
        {
            var analyzer   = new HalsteadAnalyzer();
            var childNodes = root.ChildNodes().AsArray();

            var types = childNodes.Where(n => n.IsKind(SyntaxKind.ClassDeclaration) || n.IsKind(SyntaxKind.StructDeclaration))
                        .AsArray();
            var methods            = types.SelectMany(n => n.ChildNodes().Where(_isMethod));
            var getProperties      = types.SelectMany(n => n.ChildNodes().Where(IsGetProperty));
            var setProperties      = types.SelectMany(n => n.ChildNodes().Where(IsSetProperty));
            var looseMethods       = childNodes.Where(_isMethod);
            var looseGetProperties = childNodes.Where(IsGetProperty);
            var looseSetProperties = childNodes.Where(IsSetProperty);
            var members            = methods.Concat(getProperties)
                                     .Concat(setProperties)
                                     .Concat(looseMethods)
                                     .Concat(looseGetProperties)
                                     .Concat(looseSetProperties)
                                     .AsArray();

            if (members.Any())
            {
                return(members.Select(analyzer.Calculate));
            }

            var statements = childNodes.Length == 0
                                ? root.DescendantNodesAndTokens().Select(x => SyntaxFactory.ParseStatement(x.ToFullString(), 0, new CSharpParseOptions(kind: SourceCodeKind.Script, preprocessorSymbols: new string[0])))
                                : childNodes.Select(x => SyntaxFactory.ParseStatement(x.ToFullString(), 0, new CSharpParseOptions(kind: SourceCodeKind.Script, preprocessorSymbols: new string[0])));

            var fakeMethod = SyntaxFactory.MethodDeclaration(SyntaxFactory.PredefinedType(SyntaxFactory.Token(SyntaxKind.VoidKeyword)), "fake")
                             .WithBody(SyntaxFactory.Block(statements));

            return(new[]
            {
                analyzer.Calculate(fakeMethod)
            });
        }
Exemplo n.º 6
0
		private IMemberMetric CalculateMemberMetricSlim(SyntaxNode syntaxNode)
		{
			var analyzer = new HalsteadAnalyzer();
			var halsteadMetrics = analyzer.Calculate(syntaxNode);
			var memberName = _nameResolver.TryResolveMemberSignatureString(syntaxNode);
			var complexity = CalculateCyclomaticComplexity(syntaxNode);
			var linesOfCode = CalculateLinesOfCode(syntaxNode);
			var maintainabilityIndex = CalculateMaintainablityIndex(complexity, linesOfCode, halsteadMetrics);
			var location = syntaxNode.GetLocation();
			var lineNumber = location.GetLineSpan().StartLinePosition.Line;
			var filePath = location.SourceTree == null ? string.Empty : location.SourceTree.FilePath;
			var accessModifier = GetAccessModifier(syntaxNode);
			return new MemberMetric(
				filePath,
				accessModifier,
				halsteadMetrics,
				lineNumber,
				linesOfCode,
				maintainabilityIndex,
				complexity,
				memberName,
				new ITypeCoupling[0],
				0,
				0,
				0,
				null);
		}
Exemplo n.º 7
0
		private async Task<IMemberMetric> CalculateMemberMetric(SyntaxNode syntaxNode)
		{
			var analyzer = new HalsteadAnalyzer();
			var halsteadMetrics = analyzer.Calculate(syntaxNode);
			var memberName = _nameResolver.TryResolveMemberSignatureString(syntaxNode);
			var source = CalculateClassCoupling(syntaxNode);
			var complexity = CalculateCyclomaticComplexity(syntaxNode);
			var linesOfCode = CalculateLinesOfCode(syntaxNode);
			var numberOfParameters = CalculateNumberOfParameters(syntaxNode);
			var numberOfLocalVariables = CalculateNumberOfLocalVariables(syntaxNode);
			var maintainabilityIndex = CalculateMaintainablityIndex(complexity, linesOfCode, halsteadMetrics);
			var afferentCoupling = await CalculateAfferentCoupling(syntaxNode).ConfigureAwait(false);
			var location = syntaxNode.GetLocation();
			var lineNumber = location.GetLineSpan().StartLinePosition.Line;
			var filePath = location.SourceTree == null ? string.Empty : location.SourceTree.FilePath;
			var accessModifier = GetAccessModifier(syntaxNode);
			IMemberDocumentation documentation = null;
			
			if (syntaxNode.SyntaxTree == Model.SyntaxTree)
			{
				var symbol = Model.GetDeclaredSymbol(syntaxNode);
				documentation = await _documentationFactory.Create(symbol, CancellationToken.None);
			}

			return new MemberMetric(
				filePath,
				accessModifier,
				halsteadMetrics,
				lineNumber,
				linesOfCode,
				maintainabilityIndex,
				complexity,
				memberName,
				source.AsArray(),
				numberOfParameters,
				numberOfLocalVariables,
				afferentCoupling,
				documentation);
		}