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

SONARJAVA-2113 Feed cognititve complexity metric #1385

Merged
merged 4 commits into from
Apr 20, 2017
Merged

Conversation

m-g-sonar
Copy link

No description provided.

if (shouldAnalyzeMethod(semanticModel, methodTree)) {
CognitiveComplexityVisitor visitor = new CognitiveComplexityVisitor();
methodTree.accept(visitor);
return new JavaFileScannerContext.CognitiveComplexity(visitor.complexity, visitor.locations);
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}

private boolean isSonarLintContext() {
return sensorContext.getSonarQubeVersion().isGreaterThanOrEqual(Version.create(6, 0)) && sensorContext.runtime().getProduct() == SonarProduct.SONARLINT;
}

private boolean isGreaterThanOrEqualToSonarQube6_3() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is written isSQGreaterThan62 in sonarComponents, I would use the same naming.

* Cognitive complexity and associated locations
*/
class CognitiveComplexity {
public static final CognitiveComplexity EMPTY = new CognitiveComplexity(0, Collections.emptyList());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be private.

@@ -64,20 +66,49 @@ private CognitiveComplexityVisitor() {
complexity = 0;
nesting = 1;
ignoreNesting = false;
flow = new ArrayList<>();
locations = new ArrayList<>();
ignored = new HashSet<>();
}

public static JavaFileScannerContext.CognitiveComplexity methodComplexity(@Nullable SemanticModel semanticModel, MethodTree methodTree) {
if (shouldAnalyzeMethod(semanticModel, methodTree)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am pretty sure we can get rid of the semanticModel parameters in most of the methods here... this is only used to be null tested... we can maybe get rid of this reference alltogether in that class.

if (shouldAnalyzeMethod(methodTree)) {
CognitiveComplexityVisitor visitor = new CognitiveComplexityVisitor();
methodTree.accept(visitor);
return new JavaFileScannerContext.CognitiveComplexity(visitor.complexity, visitor.locations);
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

@saberduck saberduck left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I didn't miss anything, it seems to be possible to avoid extending JavaFileScannerContext interface by using CognitiveComplexityVisitor directly.



private static boolean shouldAnalyzeMethod(MethodTree methodTree) {
return methodTree.block() != null && ((ClassTree) methodTree.parent()).simpleName() != null && !isWithinLocalClass(methodTree);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor: simpleName() != null could be extracted to isAnonymousClass method, so the intent of the test is more obvious

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@@ -111,12 +112,20 @@ public void scanFile(JavaFileScannerContext context) {

RangeDistributionBuilder fileComplexityDistribution = new RangeDistributionBuilder(LIMITS_COMPLEXITY_FILES);
saveMetricOnFile(CoreMetrics.FILE_COMPLEXITY_DISTRIBUTION, fileComplexityDistribution.add(fileComplexity).build());

if (isSonarQubeGreaterThanOrEqualTo63()) {
saveMetricOnFile(CoreMetrics.COGNITIVE_COMPLEXITY, context.compilationUnitCognitiveComplexity());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

context.getTree() provides access to CompilationUnitTree, so CognitiveComplexityVisitor.cognitiveComplexity can be used directly, thus avoiding the need for new method in the JavaScannerContext interface

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

CognitiveComplexityVisitor complexityVisitor = new CognitiveComplexityVisitor();
method.accept(complexityVisitor);
int total = complexityVisitor.complexity;
JavaFileScannerContext.CognitiveComplexity result = context.cognitiveComplexity(method);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CognitiveComplexityVisitor.methodComplexity can be used directly, so method on JavaScannerContext can be removed

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@@ -111,12 +112,20 @@ public void scanFile(JavaFileScannerContext context) {

RangeDistributionBuilder fileComplexityDistribution = new RangeDistributionBuilder(LIMITS_COMPLEXITY_FILES);
saveMetricOnFile(CoreMetrics.FILE_COMPLEXITY_DISTRIBUTION, fileComplexityDistribution.add(fileComplexity).build());

if (isSonarQubeGreaterThanOrEqualTo63()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems that this version checking is needed in multiple places in the code, maybe it is worth to have single utility class handling these checks.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe, but clearly way out of scope of this ticket.

@m-g-sonar m-g-sonar dismissed benzonico’s stale review April 19, 2017 16:45

Implementation updated.

@m-g-sonar m-g-sonar merged commit 88bcd69 into master Apr 20, 2017
@m-g-sonar m-g-sonar deleted the SONARJAVA-2113 branch April 20, 2017 08:11
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

Successfully merging this pull request may close these issues.

4 participants