]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-1931: Create a SQUID rule : total class complexity should not exceed a pre...
authorGodin <mandrikov@gmail.com>
Tue, 23 Nov 2010 20:36:24 +0000 (20:36 +0000)
committerGodin <mandrikov@gmail.com>
Tue, 23 Nov 2010 20:36:24 +0000 (20:36 +0000)
plugins/sonar-squid-java-plugin/src/main/java/org/sonar/java/squid/check/ClassComplexityCheck.java
plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/squid/check/ClassComplexityCheckTest.java

index c529996cd1992e1b9d736ef9e126cf80d0bf8782..50892d7fe20657770535b6112fe0ece2f5c25852 100644 (file)
 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;
   }
 
 }
index 24f7f02f261f8f527a198ffac700c7a63a3f1209..b1c00950233856f906b1e7ce17d22967936324d4 100644 (file)
@@ -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"));