aboutsummaryrefslogtreecommitdiffstats
path: root/plugins/sonar-squid-java-plugin
diff options
context:
space:
mode:
authorGodin <mandrikov@gmail.com>2010-11-23 20:36:24 +0000
committerGodin <mandrikov@gmail.com>2010-11-23 20:36:24 +0000
commit63e52abdc39b84f2f32281e4e2bc7ec791a86685 (patch)
tree9355744c9a2b325855d6b6fc2a408a945e1e4cc2 /plugins/sonar-squid-java-plugin
parent334c4356cf894c8db0358ff662633c4189a42e6c (diff)
downloadsonarqube-63e52abdc39b84f2f32281e4e2bc7ec791a86685.tar.gz
sonarqube-63e52abdc39b84f2f32281e4e2bc7ec791a86685.zip
SONAR-1931: Create a SQUID rule : total class complexity should not exceed a pre-defined threshold
Diffstat (limited to 'plugins/sonar-squid-java-plugin')
-rw-r--r--plugins/sonar-squid-java-plugin/src/main/java/org/sonar/java/squid/check/ClassComplexityCheck.java26
-rw-r--r--plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/squid/check/ClassComplexityCheckTest.java2
2 files changed, 19 insertions, 9 deletions
diff --git a/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/java/squid/check/ClassComplexityCheck.java b/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/java/squid/check/ClassComplexityCheck.java
index c529996cd19..50892d7fe20 100644
--- a/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/java/squid/check/ClassComplexityCheck.java
+++ b/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/java/squid/check/ClassComplexityCheck.java
@@ -21,31 +21,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.SourceClass;
import org.sonar.squid.measures.Metric;
-@Rule(key = "ClassComplexityCheck", name = "ClassComplexityCheck", isoCategory = IsoCategory.Maintainability)
+@Rule(key = "ClassCyclomaticComplexity", name = "Avoid too complex class", 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 class plus one for "
+ + "each constructor, method (but not getter/setter), static initializer, or instance initializer in the class. "
+ + "The last return stament in method, if exists, is not taken into account.</p>"
+ + "<p>Even when the Cyclomatic Complexity of a class is very high, this complexity might be well distributed among all methods. "
+ + "Nevertheless, most of the time, a very complex class is a class which breaks the "
+ + "<a href='http://en.wikipedia.org/wiki/Single_responsibility_principle'>Single Responsibility Principle</a> "
+ + "and which should be re-factored to be split in several classes.</p>")
public class ClassComplexityCheck extends SquidCheck {
- @RuleProperty(description = "Threshold.")
- private Integer threshold;
+ @RuleProperty(description = "Maximum complexity allowed.", defaultValue = "200")
+ private Integer max;
@Override
public void visitClass(SourceClass sourceClass) {
int complexity = sourceClass.getInt(Metric.COMPLEXITY);
- if (complexity > threshold) {
- CheckMessage message = new CheckMessage(this, "Class complexity exceeds " + threshold + ".");
+ if (complexity > max) {
+ CheckMessage message = new CheckMessage(this, "The Cyclomatic Complexity of this class is " + complexity + " which is greater than "
+ + max + " authorized.");
message.setLine(sourceClass.getStartAtLine());
- message.setCost(complexity - threshold);
+ message.setCost(complexity - max);
getSourceFile(sourceClass).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/ClassComplexityCheckTest.java b/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/squid/check/ClassComplexityCheckTest.java
index 24f7f02f261..b1c00950233 100644
--- a/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/squid/check/ClassComplexityCheckTest.java
+++ b/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/squid/check/ClassComplexityCheckTest.java
@@ -42,7 +42,7 @@ public class ClassComplexityCheckTest {
public void setUp() {
squid = new Squid(new JavaSquidConfiguration());
ClassComplexityCheck check = new ClassComplexityCheck();
- check.setThreshold(5);
+ check.setMax(5);
squid.registerVisitor(check);
JavaAstScanner scanner = squid.register(JavaAstScanner.class);
scanner.scanFile(getFile("/metrics/branches/NoBranches.java"));