- versions used (SonarQube, Scanner, language analyzer)
sonar maven plugin 3.7.0.1746
sonarqube 8.4.2.36762 - minimal code sample to reproduce (with analysis parameter, and potential instructions to compile).
import java.util.Map;
import java.util.function.Function;
import java.util.function.UnaryOperator;
public class Complexity {
private static final Function<String, String> NO_OP = UnaryOperator.identity();
private Map<String, Function<String, String>> functions;
public Complexity() {
setFunctions();
}
public Function<String, String> getFunction(String key) {
return functions.getOrDefault(key, NO_OP);
}
private void setFunctions() {
functions = Map.ofEntries(
Map.entry("key1", v -> {
if ("test".equals(v)) {
return "the test";
} else {
return v + "test";
}
}),
Map.entry("key2", v -> {
if ("test2".equals(v)) {
return "the 2nd test";
} else {
return v + " 2nd test";
}
}),
Map.entry("key3", v -> {
if ("test3".equals(v)) {
return "the 3rd test";
} else {
return v + " 3rd test";
}
}),
Map.entry("key4", v -> {
if ("test4".equals(v)) {
return "the 4th test";
} else {
return v + " 4th test";
}
}),
Map.entry("key5", v -> {
if ("test4".equals(v)) {
return "the 5th test";
} else {
return v + " 5th test";
}
}),
Map.entry("key6", v -> {
if ("test6".equals(v)) {
return "the 6th test";
} else {
return v + " 6th test";
}
}),
Map.entry("key7", v -> {
if ("test5".equals(v)) {
return "the 7th test";
} else {
return v + " 7th test";
}
}),
Map.entry("key8", v -> {
if ("test8".equals(v)) {
return "the 8th test";
} else {
return v + " 8th test";
}
}),
Map.entry("key9", v -> {
if ("test9".equals(v)) {
return "the 9th test";
} else {
return v + " 9th test";
}
}),
Map.entry("key10", v -> {
if ("test10".equals(v)) {
return "the 10th test";
} else {
return v + " 10th test";
}
})
);
}
}
In the above example I am getting a Cognitive complexity of 30 since it is adding up all if/else statements, even though the context of each if/else is within the lambda expression and not part of the setFunctions
method flow. Is this a false positive, or am I missing some part of how Cognitive complexity works?
Thank you very much for your time.