Any boolean expression causes increases in cognative complexity
This commit is contained in:
2 files changed
+79
No files matched your search
+37
@@ -1,5 +1,7 @@
|
||||
package net.sourceforge.pmd.lang.apex.metrics.impl.visitors;
|
||||
|
||||
import apex.jorje.data.ast.BooleanOp;
|
||||
import apex.jorje.data.ast.PrefixOp;
|
||||
import net.sourceforge.pmd.lang.apex.ast.*;
|
||||
|
||||
/**
|
||||
@@ -9,6 +11,7 @@ public class CognitiveComplexityVisitor extends ApexParserVisitorAdapter {
|
||||
public static class State {
|
||||
private int complexity = 0;
|
||||
private int nestingLevel = 0;
|
||||
private BooleanOp currentBooleanOperation = null;
|
||||
|
||||
public double getComplexity() {
|
||||
return complexity;
|
||||
@@ -22,6 +25,16 @@ public class CognitiveComplexityVisitor extends ApexParserVisitorAdapter {
|
||||
complexity += nestingLevel;
|
||||
}
|
||||
|
||||
void booleanOperation(BooleanOp op) {
|
||||
if (currentBooleanOperation != op) {
|
||||
if (op != null) {
|
||||
structureComplexity();
|
||||
}
|
||||
|
||||
currentBooleanOperation = op;
|
||||
}
|
||||
}
|
||||
|
||||
void increaseNestingLevel() {
|
||||
structureComplexity();
|
||||
nestingComplexity();
|
||||
@@ -133,4 +146,28 @@ public class CognitiveComplexityVisitor extends ApexParserVisitorAdapter {
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object visit(ASTBooleanExpression node, Object data) {
|
||||
State state = (State) data;
|
||||
|
||||
BooleanOp op = node.getNode().getOp();
|
||||
if (op == BooleanOp.AND || op == BooleanOp.OR) {
|
||||
state.booleanOperation(op);
|
||||
}
|
||||
|
||||
return super.visit(node, data);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object visit(ASTPrefixExpression node, Object data) {
|
||||
State state = (State) data;
|
||||
|
||||
PrefixOp op = node.getNode().getOp();
|
||||
if (op == PrefixOp.NOT) {
|
||||
state.booleanOperation(null);
|
||||
}
|
||||
|
||||
return super.visit(node, data);
|
||||
}
|
||||
}
|
||||
+42
@@ -290,4 +290,46 @@
|
||||
]]>
|
||||
</code>
|
||||
</test-code>
|
||||
|
||||
<test-code>
|
||||
<description>Boolean operators</description>
|
||||
<expected-problems>5</expected-problems>
|
||||
<expected-messages>
|
||||
<message>'c__Foo#a(Integer)' has value 1.</message>
|
||||
<message>'c__Foo#b(Integer)' has value 1.</message>
|
||||
<message>'c__Foo#c(Integer)' has value 1.</message>
|
||||
<message>'c__Foo#d(Boolean,Boolean,Boolean,Boolean,Boolean,Boolean)' has value 3.</message>
|
||||
<message>'c__Foo#e(Boolean,Boolean,Boolean)' has value 2.</message>
|
||||
</expected-messages>
|
||||
<code>
|
||||
<![CDATA[
|
||||
class Foo {
|
||||
Boolean a(Integer n) {
|
||||
return n > 0 && n > 1; // +1
|
||||
}
|
||||
|
||||
Boolean b(Integer n) {
|
||||
return n > 0 && n > 1 && n > 2; // +1
|
||||
}
|
||||
|
||||
Boolean c(Integer n) {
|
||||
return n > 0 || n < 0; // +1
|
||||
}
|
||||
|
||||
Boolean d(Boolean a, Boolean b, Boolean c, Boolean d, Boolean e, Boolean f) {
|
||||
return (a
|
||||
&& b && c) // +1
|
||||
|| (d || e) // +1
|
||||
&& f; // +1
|
||||
}
|
||||
|
||||
Boolean e(Boolean a, Boolean b, Boolean c) {
|
||||
return a
|
||||
&& // +1
|
||||
!(b && c); // +1
|
||||
}
|
||||
}
|
||||
]]>
|
||||
</code>
|
||||
</test-code>
|
||||
</test-data>
|
||||
Reference in new issue
Block a user