ArchiMetrics icon indicating copy to clipboard operation
ArchiMetrics copied to clipboard

How to do member level analysis?

Open MingweiWilliamTang opened this issue 7 years ago • 5 comments

The example you provide shows how to do project analysis. I'm wondering how to the member level analysis using Archimetrics. I noticed some membermetriccalculator functions, but it seems that they are defined as private and cannot be used directly.

Thank you.

MingweiWilliamTang avatar Jul 25 '17 23:07 MingweiWilliamTang

You should be able to pass in the member as a syntax tree to the CodeMetricsCalculator. See here - https://github.com/jjrdk/ArchiMetrics/blob/master/src/ArchiMetrics.Analysis/CodeMetricsCalculator.cs#L59

jjrdk avatar Jul 26 '17 12:07 jjrdk

Thanks for your help. Currently my code is ` var msWorkspace = Microsoft.CodeAnalysis.MSBuild.MSBuildWorkspace.Create(); var project = msWorkspace.OpenProjectAsync(Path).Result; var metricsCalculator = new CodeMetricsCalculator();

        var Trees = from doc in project.Documents
                    select doc.GetSyntaxTreeAsync().Result;
       
        var calculateTasks = metricsCalculator.Calculate(Trees);
        var metrics = (await Task.WhenAll(calculateTasks)).SelectMany(nm => nm);
        foreach (var metric in metrics) { 
            Console.WriteLine("{0} => {1}", metric.Name, metric.CyclomaticComplexity);
            Console.WriteLine("{0} => {1}", metric.Name, metric.DepthOfInheritance);    
        }
        ` 

It give me error s like System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values.`

May I ask what is the correct way to do that?

Thanks.

MingweiWilliamTang avatar Jul 27 '17 00:07 MingweiWilliamTang

It's impossible to say from the code snippet you have posted. You don't even tell me which line fails or what the source of the error is.

Have a look at the unit test for the code snippet calculator: https://github.com/jjrdk/ArchiMetrics/blob/master/tests/ArchiMetrics.Analysis.Tests/CodeMetricsCalculatorTests.cs#L63

jjrdk avatar Jul 27 '17 20:07 jjrdk

It works. I'm wondering if I want to to code complexity analysis at semantic level, what assembly should be put in as second parameter in CodeMetricsCalculator.Calculate(). Suppose I what to analyze a syntax tree tree1 of a file f1 in project project, here's my start code, what should I do next?

var msWorkspace = Microsoft.CodeAnalysis.MSBuild.MSBuildWorkspace.Create();
var project = msWorkspace.OpenProjectAsync(Path).Result;
var metricsCalculator = new CodeMetricsCalculator();
var tree1 = project.Documents.ElementAt(0).GetSyntaxTreeAsync().Result;
var ProjectCompile = var project.GetCompilationAsync().Result;

Thank you.

MingweiWilliamTang avatar Jul 28 '17 17:07 MingweiWilliamTang

The second parameter takes in all the assembly dependencies that are needed to correctly analyze the code. Since code snippets don't specify their assembly dependencies themselves, you will have to do it. Otherwise the parser will not understand which types you are using.

Can you not simply pass the syntax tree to the metrics calculator along with any references and get a result?

jjrdk avatar Aug 08 '17 11:08 jjrdk