diff options
author | Godin <mandrikov@gmail.com> | 2010-11-23 20:27:50 +0000 |
---|---|---|
committer | Godin <mandrikov@gmail.com> | 2010-11-23 20:27:50 +0000 |
commit | 334c4356cf894c8db0358ff662633c4189a42e6c (patch) | |
tree | 6b9d423952d50ce44db704385a52ce3fbab1d30a /plugins/sonar-squid-java-plugin/src | |
parent | 97c4395b62302005d8f4f7fce5d21d3d068e0631 (diff) | |
download | sonarqube-334c4356cf894c8db0358ff662633c4189a42e6c.tar.gz sonarqube-334c4356cf894c8db0358ff662633c4189a42e6c.zip |
SONAR-1845: Create a new Sonar rule to check method complexity
Diffstat (limited to 'plugins/sonar-squid-java-plugin/src')
2 files changed, 19 insertions, 9 deletions
diff --git a/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/java/squid/check/MethodComplexityCheck.java b/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/java/squid/check/MethodComplexityCheck.java index 27361d34deb..8e527e8a302 100644 --- a/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/java/squid/check/MethodComplexityCheck.java +++ b/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/java/squid/check/MethodComplexityCheck.java @@ -1,31 +1,41 @@ package org.sonar.java.squid.check; import org.sonar.check.IsoCategory; +import org.sonar.check.Priority; import org.sonar.check.Rule; import org.sonar.check.RuleProperty; import org.sonar.squid.api.CheckMessage; import org.sonar.squid.api.SourceMethod; import org.sonar.squid.measures.Metric; -@Rule(key = "MethodComplexityCheck", name = "MethodComplexityCheck", isoCategory = IsoCategory.Maintainability) +@Rule(key = "MethodCyclomaticComplexity", name = "Avoid too complex method", isoCategory = IsoCategory.Maintainability, + priority = Priority.MAJOR, description = "<p>The Cyclomatic Complexity is measured by the number of (&&, ||) operators " + + "and (if, while, do, for, ?:, catch, switch, case, return, throw) statements in the body of a constructor, " + + "method, static initializer, or instance initializer. " + + "The minimun Cyclomatic Complexity of a method is 1 and the last return stament, if exists, is not taken into account. " + + "The more complex is a method, the more possible different paths through the source code exist. " + + "Generally 1-4 is considered good, 5-7 ok, 8-10 consider re-factoring, and 11+ re-factor now. " + + "Indeed above 10, it's pretty difficult to be able to think about all possible paths when maintaining the source code, " + + "so the risk of regression increases exponentially.</p>") public class MethodComplexityCheck extends SquidCheck { - @RuleProperty(description = "Threshold.") - private Integer threshold; + @RuleProperty(description = "Maximum complexity allowed.", defaultValue = "10") + private Integer max; @Override public void visitMethod(SourceMethod sourceMethod) { int complexity = sourceMethod.getInt(Metric.COMPLEXITY); - if (complexity > threshold) { - CheckMessage message = new CheckMessage(this, "Method complexity exceeds " + threshold + "."); + if (complexity > max) { + CheckMessage message = new CheckMessage(this, "The Cyclomatic Complexity of this method is " + complexity + " which is greater than " + + max + " authorized."); message.setLine(sourceMethod.getStartAtLine()); - message.setCost(complexity - threshold); + message.setCost(complexity - max); getSourceFile(sourceMethod).log(message); } } - public void setThreshold(int threshold) { - this.threshold = threshold; + public void setMax(int max) { + this.max = max; } } diff --git a/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/squid/check/MethodComplexityCheckTest.java b/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/squid/check/MethodComplexityCheckTest.java index 5533ca8e18d..6f59bc473d7 100644 --- a/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/squid/check/MethodComplexityCheckTest.java +++ b/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/squid/check/MethodComplexityCheckTest.java @@ -21,7 +21,7 @@ public class MethodComplexityCheckTest { public void setUp() { squid = new Squid(new JavaSquidConfiguration()); MethodComplexityCheck check = new MethodComplexityCheck(); - check.setThreshold(5); + check.setMax(5); squid.registerVisitor(check); JavaAstScanner scanner = squid.register(JavaAstScanner.class); scanner.scanFile(getFile("/metrics/branches/ComplexBranches.java")); |