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 3ae4d16a94..03b1b5bb4c 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 @@ -122,4 +122,15 @@ public class CognitiveComplexityVisitor extends ApexParserVisitorAdapter { return data; } + + @Override + public Object visit(ASTTernaryExpression node, Object data) { + State state = (State) data; + + state.increaseNestingLevel(); + super.visit(node, data); + state.decreaseNestingLevel(); + + 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 2f2410aede..300c7d1450 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 @@ -271,4 +271,23 @@ ]]> + + + Ternary operators cause nesting + 1 + + 'c__Foo#foo(Integer)' has value 3. + + + 0 ? // +2 + 1 : 0; + } + } + ]]> + + \ No newline at end of file