-
Notifications
You must be signed in to change notification settings - Fork 688
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
Conversation
9c7f36c to
8719d2c
Compare
| if (shouldAnalyzeMethod(semanticModel, methodTree)) { | ||
| CognitiveComplexityVisitor visitor = new CognitiveComplexityVisitor(); | ||
| methodTree.accept(visitor); | ||
| return new JavaFileScannerContext.CognitiveComplexity(visitor.complexity, visitor.locations); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FP on S2440, see https://jira.sonarsource.com/browse/SONARJAVA-2098
| } | ||
|
|
||
| private boolean isSonarLintContext() { | ||
| return sensorContext.getSonarQubeVersion().isGreaterThanOrEqual(Version.create(6, 0)) && sensorContext.runtime().getProduct() == SonarProduct.SONARLINT; | ||
| } | ||
|
|
||
| private boolean isGreaterThanOrEqualToSonarQube6_3() { |
There was a problem hiding this comment.
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()); |
There was a problem hiding this comment.
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)) { | |||
There was a problem hiding this comment.
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.
8719d2c to
650ae35
Compare
| if (shouldAnalyzeMethod(methodTree)) { | ||
| CognitiveComplexityVisitor visitor = new CognitiveComplexityVisitor(); | ||
| methodTree.accept(visitor); | ||
| return new JavaFileScannerContext.CognitiveComplexity(visitor.complexity, visitor.locations); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FP on S2440, see https://jira.sonarsource.com/browse/SONARJAVA-2098
There was a problem hiding this 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); |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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()); | |||
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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()) { | |||
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
650ae35 to
626ffe0
Compare
No description provided.