示例#1
0
        public INamespaceMetric CalculateFrom(NamespaceDeclarationSyntaxInfo namespaceNode, IEnumerable <ITypeMetric> metrics)
        {
            const string   DocumentationTypeName = "NamespaceDoc";
            var            typeMetrics           = metrics.AsArray();
            var            documentationType     = typeMetrics.FirstOrDefault(x => x.Name == DocumentationTypeName);
            IDocumentation documentation         = null;

            if (documentationType != null)
            {
                documentation = documentationType.Documentation;
                typeMetrics   = typeMetrics.Where(x => x.Name != DocumentationTypeName).AsArray();
            }

            var linesOfCode = typeMetrics.Sum(x => x.LinesOfCode);
            var source      = typeMetrics.SelectMany(x => x.ClassCouplings)
                              .GroupBy(x => x.ToString())
                              .Select(x => new TypeCoupling(x.First().TypeName, x.First().Namespace, x.First().Assembly, x.SelectMany(y => y.UsedMethods), x.SelectMany(y => y.UsedProperties), x.SelectMany(y => y.UsedEvents)))
                              .Where(x => x.Namespace != namespaceNode.Name)
                              .OrderBy(x => x.Assembly + x.Namespace + x.TypeName)
                              .AsArray();
            var maintainabilitySource = typeMetrics.Select(x => new Tuple <int, double>(x.LinesOfCode, x.MaintainabilityIndex)).AsArray();
            var maintainabilityIndex  = linesOfCode > 0 && maintainabilitySource.Any() ? maintainabilitySource.Sum(x => x.Item1 * x.Item2) / linesOfCode : 100.0;
            var cyclomaticComplexity  = typeMetrics.Sum(x => x.CyclomaticComplexity);
            var depthOfInheritance    = typeMetrics.Any() ? typeMetrics.Max(x => x.DepthOfInheritance) : 0;

            return(new NamespaceMetric(
                       maintainabilityIndex,
                       cyclomaticComplexity,
                       linesOfCode,
                       source,
                       depthOfInheritance,
                       namespaceNode.Name,
                       typeMetrics,
                       documentation));
        }
		public INamespaceMetric CalculateFrom(NamespaceDeclarationSyntaxInfo namespaceNode, IEnumerable<ITypeMetric> metrics)
		{
			const string DocumentationTypeName = "NamespaceDoc";
			var typeMetrics = metrics.AsArray();
			var documentationType = typeMetrics.FirstOrDefault(x => x.Name == DocumentationTypeName);
			IDocumentation documentation = null;
			if (documentationType != null)
			{
				documentation = documentationType.Documentation;
				typeMetrics = typeMetrics.Where(x => x.Name != DocumentationTypeName).AsArray();
			}

			var linesOfCode = typeMetrics.Sum(x => x.LinesOfCode);
			var source = typeMetrics.SelectMany(x => x.ClassCouplings)
						  .GroupBy(x => x.ToString())
						  .Select(x => new TypeCoupling(x.First().TypeName, x.First().Namespace, x.First().Assembly, x.SelectMany(y => y.UsedMethods), x.SelectMany(y => y.UsedProperties), x.SelectMany(y => y.UsedEvents)))
						  .Where(x => x.Namespace != namespaceNode.Name)
						  .OrderBy(x => x.Assembly + x.Namespace + x.TypeName)
						  .AsArray();
			var maintainabilitySource = typeMetrics.Select(x => new Tuple<int, double>(x.LinesOfCode, x.MaintainabilityIndex)).AsArray();
			var maintainabilityIndex = linesOfCode > 0 && maintainabilitySource.Any() ? maintainabilitySource.Sum(x => x.Item1 * x.Item2) / linesOfCode : 100.0;
			var cyclomaticComplexity = typeMetrics.Sum(x => x.CyclomaticComplexity);
			var depthOfInheritance = typeMetrics.Any() ? typeMetrics.Max(x => x.DepthOfInheritance) : 0;
			return new NamespaceMetric(
				maintainabilityIndex,
				cyclomaticComplexity,
				linesOfCode,
				source,
				depthOfInheritance,
				namespaceNode.Name,
				typeMetrics,
				documentation);
		}
		private static async Task<Tuple<Compilation, SemanticModel, SyntaxTree, NamespaceDeclarationSyntaxInfo>> VerifyCompilation(Compilation compilation, NamespaceDeclarationSyntaxInfo namespaceNode)
		{
			SemanticModel semanticModel;
			var tree = namespaceNode.Syntax.SyntaxTree;
			if (tree == null)
			{
				var compilationUnit = SyntaxFactory.CompilationUnit()
					.WithMembers(SyntaxFactory.List(new[] { (MemberDeclarationSyntax)namespaceNode.Syntax }));
				var cu = CSharpSyntaxTree.Create(compilationUnit);
				var root = await cu.GetRootAsync().ConfigureAwait(false);
				namespaceNode.Syntax = root.ChildNodes().First();
				var newCompilation = compilation.AddSyntaxTrees(cu);
				semanticModel = newCompilation.GetSemanticModel(cu);
				return new Tuple<Compilation, SemanticModel, SyntaxTree, NamespaceDeclarationSyntaxInfo>(newCompilation, semanticModel, cu, namespaceNode);
			}

			var result = AddToCompilation(compilation, tree);
			compilation = result.Item1;
			tree = result.Item2;
			semanticModel = compilation.GetSemanticModel(tree);
			return new Tuple<Compilation, SemanticModel, SyntaxTree, NamespaceDeclarationSyntaxInfo>(compilation, semanticModel, tree, namespaceNode);
		}