From 0231350dea7b8235a9309b24d15088ab59eea09f Mon Sep 17 00:00:00 2001 From: Gwilym Kuiper Date: Tue, 18 Feb 2020 16:59:12 +0000 Subject: [PATCH] Handle boolean expressions on different statements --- .../impl/visitors/CognitiveComplexityVisitor.java | 15 +++++++++++++++ .../metrics/impl/xml/CognitiveComplexityTest.xml | 8 +++++++- 2 files changed, 22 insertions(+), 1 deletion(-) 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 de14a62040..00dbf63f62 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 @@ -170,4 +170,19 @@ public class CognitiveComplexityVisitor extends ApexParserVisitorAdapter { return super.visit(node, data); } + + @Override + public Object visit(ASTBlockStatement node, Object data) { + State state = (State) data; + + for (ApexNode child : node.children()) { + child.jjtAccept(this, data); + + // This needs to happen because the current 'run' of boolean operations is terminated + // once we finish a statement. + state.booleanOperation(null); + } + + return 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 5784716ff1..ef225a7c0f 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 @@ -293,13 +293,14 @@ Boolean operators - 5 + 6 'c__Foo#a(Integer)' has value 1. 'c__Foo#b(Integer)' has value 1. 'c__Foo#c(Integer)' has value 1. 'c__Foo#d(Boolean,Boolean,Boolean,Boolean,Boolean,Boolean)' has value 3. 'c__Foo#e(Boolean,Boolean,Boolean)' has value 2. + 'c__Foo#f()' has value 2.