Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to do member level analysis? #30

Open
MingweiWilliamTang opened this issue Jul 25, 2017 · 5 comments
Open

How to do member level analysis? #30

MingweiWilliamTang opened this issue Jul 25, 2017 · 5 comments

Comments

@MingweiWilliamTang
Copy link

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.

@jjrdk
Copy link
Owner

jjrdk commented Jul 26, 2017

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

@MingweiWilliamTang
Copy link
Author

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.

@jjrdk
Copy link
Owner

jjrdk commented Jul 27, 2017

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

@MingweiWilliamTang
Copy link
Author

MingweiWilliamTang commented Jul 28, 2017

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.

@jjrdk
Copy link
Owner

jjrdk commented Aug 8, 2017

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants