diff --git a/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/metrics/impl/visitors/CognitiveComplexityVisitor.java b/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/metrics/impl/visitors/CognitiveComplexityVisitor.java index 00dbf63f62..afb412874e 100644 --- a/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/metrics/impl/visitors/CognitiveComplexityVisitor.java +++ b/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/metrics/impl/visitors/CognitiveComplexityVisitor.java @@ -11,7 +11,9 @@ public class CognitiveComplexityVisitor extends ApexParserVisitorAdapter { public static class State { private int complexity = 0; private int nestingLevel = 0; + private BooleanOp currentBooleanOperation = null; + private String methodName = null; public double getComplexity() { return complexity; @@ -44,6 +46,16 @@ public class CognitiveComplexityVisitor extends ApexParserVisitorAdapter { void decreaseNestingLevel() { nestingLevel--; } + + void methodCall(String methodCalledName) { + if (methodCalledName.equals(methodName)) { + structureComplexity(); + } + } + + void setMethodName(String name) { + methodName = name; + } } @Override @@ -185,4 +197,18 @@ public class CognitiveComplexityVisitor extends ApexParserVisitorAdapter { return data; } + + @Override + public Object visit(ASTMethod node, Object data) { + State state = (State) data; + state.setMethodName(node.getNode().getMethodInfo().getCanonicalName()); + return super.visit(node, data); + } + + @Override + public Object visit(ASTMethodCallExpression node, Object data) { + State state = (State) data; + state.methodCall(node.getNode().getMethodName()); + return super.visit(node, data); + } } diff --git a/pmd-apex/src/test/resources/net/sourceforge/pmd/lang/apex/metrics/impl/xml/CognitiveComplexityTest.xml b/pmd-apex/src/test/resources/net/sourceforge/pmd/lang/apex/metrics/impl/xml/CognitiveComplexityTest.xml index ef225a7c0f..6b0df4594c 100644 --- a/pmd-apex/src/test/resources/net/sourceforge/pmd/lang/apex/metrics/impl/xml/CognitiveComplexityTest.xml +++ b/pmd-apex/src/test/resources/net/sourceforge/pmd/lang/apex/metrics/impl/xml/CognitiveComplexityTest.xml @@ -338,4 +338,25 @@ ]]> + + + Recursion bumps complexity value + 1 + + 'c__Foo#foo(Integer)' has value 3. + + + + + \ No newline at end of file