コード例 #1
0
        public async Task <SolutionVersionWithMetrics> GetSolutionVersionWithMetrics()
        {
            using (var workspace = MSBuildWorkspace.Create())
            {
                workspace.WorkspaceFailed += (o, e) => Console.WriteLine(e.Diagnostic.Message);
                var solution = await workspace.OpenSolutionAsync(_solutionLocation, new ConsoleProgressReporter());

                var projects = solution.Projects.Where(x => x.FilePath.EndsWith(".csproj") && !x.Name.Contains("Tests"));

//                Project project = projects.First();
                SolutionVersionWithMetrics solutionVersionWithMetrics = new SolutionVersionWithMetrics();
                foreach (Project project in projects)
                {
                    Compilation       comp        = project.GetCompilationAsync().Result;
                    List <SyntaxTree> syntaxTrees = comp.SyntaxTrees.ToList();

                    Dictionary <INamedTypeSymbol, int> classExtensions = NumberOfChildren.GetClassExtensions(syntaxTrees, comp);
                    Dictionary <INamedTypeSymbol, int> classCouplings  = CouplingBetweenObjects.CalculateCouplings(syntaxTrees, comp);

                    foreach (var syntaxTree in syntaxTrees)
                    {
                        string location = syntaxTree.FilePath;
                        if (location.ToLower().Contains("test"))
                        {
                            continue;
                        }
                        SemanticModel semanticModel           = comp.GetSemanticModel(syntaxTree);
                        List <ClassDeclarationSyntax> classes = GetClassesFromRoot(syntaxTree.GetRoot());
                        string relativePath  = location.Replace(@"\", "/");
                        var    pathArr       = relativePath.Split('/');
                        string fileAndParent = $@"{pathArr[pathArr.Length-2]}/{pathArr[pathArr.Length-1]}";

                        foreach (ClassDeclarationSyntax classDecl in classes)
                        {
                            string className         = classDecl.Identifier.ValueText;
                            string classNameWithHash = $"{className}-{fileAndParent.GetHashCode().ToString()}";

                            if (solutionVersionWithMetrics.ClassProcessed(classNameWithHash))
                            {
                                continue;
                            }

                            ClassWithMetrics classMetricResults = GetMetricResults(classDecl, semanticModel, className, location, classExtensions, classCouplings);

                            if (className == "LocalizableStrings")
                            {
                                Console.WriteLine();
                            }

                            solutionVersionWithMetrics.AddClassWithMetrics(
                                classNameWithHash
                                , classMetricResults);
                        }
                    }
                }
                return(solutionVersionWithMetrics);
            }
        }
コード例 #2
0
        private static ClassWithMetrics GetMetricResults(ClassDeclarationSyntax classDecl, SemanticModel semanticModel, string className, string location, Dictionary <INamedTypeSymbol, int> classExtensions, Dictionary <INamedTypeSymbol, int> classCouplings)
        {
            ClassWithMetrics classMetricResults = new ClassWithMetrics(className, location);

            var lambdaMetrics = LambdaMetrics.GetValueList(classDecl, semanticModel);

            classMetricResults.AddMetric(Measure.LambdaCount, lambdaMetrics.LambdaCount);
            classMetricResults.AddMetric(Measure.LambdaFieldVariableUsageCount, lambdaMetrics.FieldVariableUsageCount);
            classMetricResults.AddMetric(Measure.LambdaLocalVariableUsageCount, lambdaMetrics.LocalVariableUsageCount);
            classMetricResults.AddMetric(Measure.LambdaSideEffectCount, lambdaMetrics.SideEffects);


            int sourceLinesOfCode = SourceLinesOfCode.GetCount(classDecl);

            classMetricResults.AddMetric(Measure.SourceLinesOfCode, sourceLinesOfCode);

            int commentDensity = CommentDensity.GetCount(classDecl, sourceLinesOfCode);

            classMetricResults.AddMetric(Measure.CommentDensity, commentDensity);

            int cyclomaticComplexity = CyclomaticComplexity.GetCount(classDecl);

            classMetricResults.AddMetric(Measure.CyclomaticComplexity, cyclomaticComplexity);

            int weightedMethodsPerClass = WeightedMethodsPerClass.GetCount(classDecl);

            classMetricResults.AddMetric(Measure.WeightedMethodsPerClass, weightedMethodsPerClass);

            int depthOfInheritanceTree = DepthOfInheritanceTree.GetCount(classDecl, semanticModel);

            classMetricResults.AddMetric(Measure.DepthOfInheritanceTree, depthOfInheritanceTree);

            int numberOfChildren = NumberOfChildren.GetCount(classDecl, semanticModel, classExtensions);

            classMetricResults.AddMetric(Measure.NumberOfChildren, numberOfChildren);

            int couplingBetweenObjects = CouplingBetweenObjects.GetCount(classDecl, semanticModel, classCouplings);

            classMetricResults.AddMetric(Measure.CouplingBetweenObjects, couplingBetweenObjects);

            int responseForAClass = ResponseForAClass.GetCount(classDecl);

            classMetricResults.AddMetric(Measure.ResponseForAClass, responseForAClass);

            int lackOfCohesionOfMethods = LackOfCohesionOfMethods.GetCount(classDecl, semanticModel);

            classMetricResults.AddMetric(Measure.LackOfCohesionOfMethods, lackOfCohesionOfMethods);

            int sourceLinesOfLambda = SourceLinesOfLambda.GetCount(classDecl);

            classMetricResults.AddMetric(Measure.SourceLinesOfLambda, sourceLinesOfLambda);

            int lambdaScore = (int)((double)sourceLinesOfLambda / sourceLinesOfCode * 100);

            classMetricResults.AddMetric(Measure.LambdaScore, lambdaScore);

            int unterminatedCollections = UnterminatedCollections.GetCount(classDecl, semanticModel);

            classMetricResults.AddMetric(Measure.UnterminatedCollections, unterminatedCollections);

            return(classMetricResults);
        }