Some documentation about Wmc and Cyclo

This commit is contained in:
oowekyala
2017-07-17 14:54:16 +02:00
parent 2a53d2e532
commit 026b03dfb2
4 changed files with 21 additions and 11 deletions

View File

@ -22,7 +22,7 @@ import net.sourceforge.pmd.lang.java.oom.metrics.visitors.StandardCycloVisitor;
* block is given by {@code CYCLO = e - n + 2p} [2]. In practice it can be calculated by counting control flow
* statements following the standard rules given below.
*
* <p>The standard version of the metric complies with McCabe's original definition:
* <p>The standard version of the metric complies with McCabe's original definition [3]:
*
* <ul>
* <li>+1 for every control flow statement ({@code if, case, catch, throw, do, while, for, break, continue}) and
@ -34,11 +34,15 @@ import net.sourceforge.pmd.lang.java.oom.metrics.visitors.StandardCycloVisitor;
* control flow statement in itself.
* </ul>
*
* <p>Version {@link Version#IGNORE_BOOLEAN_PATHS}: Boolean operators are not counted, which means that empty
* <p>Version {@link CycloVersion#IGNORE_BOOLEAN_PATHS}: Boolean operators are not counted, which means that empty
* fall-through cases in {@code switch} statements are not counted as well.
*
* <p>References: <ul> <li> [1] Lanza, Object-Oriented Metrics in Practice, 2005. <li> [2] McCabe, A Complexity Measure,
* in Proceedings of the 2nd ICSE (1976). </ul>
* <p>References:
* <ul>
* <li> [1] Lanza, Object-Oriented Metrics in Practice, 2005.
* <li> [2] McCabe, A Complexity Measure, in Proceedings of the 2nd ICSE (1976).
* <li> [3] <a href="https://docs.sonarqube.org/display/SONAR/Metrics+-+Complexity">Sonarqube online documentation</a>
* </ul>
*
* @author Clément Fournier
* @since June 2017
@ -48,7 +52,7 @@ public final class CycloMetric {
// TODO:cf Cyclo should develop factorized boolean operators to count them
/** Variants of CYCLO. */
public enum Version implements MetricVersion {
public enum CycloVersion implements MetricVersion {
/** Do not count the paths in boolean expressions as decision points. */
IGNORE_BOOLEAN_PATHS
}
@ -58,7 +62,7 @@ public final class CycloMetric {
@Override
public double computeFor(ASTMethodOrConstructorDeclaration node, MetricVersion version) {
JavaParserVisitor visitor = (CycloMetric.Version.IGNORE_BOOLEAN_PATHS.equals(version))
JavaParserVisitor visitor = (CycloVersion.IGNORE_BOOLEAN_PATHS.equals(version))
? new CycloPathUnawareOperationVisitor()
: new StandardCycloVisitor();

View File

@ -9,10 +9,14 @@ import net.sourceforge.pmd.lang.java.oom.Metrics;
import net.sourceforge.pmd.lang.java.oom.api.MetricVersion;
import net.sourceforge.pmd.lang.java.oom.api.OperationMetricKey;
import net.sourceforge.pmd.lang.java.oom.api.ResultOption;
import net.sourceforge.pmd.lang.java.oom.metrics.CycloMetric.CycloVersion;
/**
* Weighed Method Count. It is the sum of the statical complexity of all operations of a class. We use
* the standard version of {@link CycloMetric} to quantify the complexity of a metric. [1]
* {@link CycloMetric} to quantify the complexity of a metric. [1]
*
* <p>The versions that can be used of this metric are defined in {@link CycloVersion}. They have the effect of using
* that version of Cyclo to calculate Wmc.
*
* <p>[1] Lanza. Object-Oriented Metrics in Practice.
*
@ -23,6 +27,7 @@ public final class WmcMetric extends AbstractClassMetric {
@Override
public double computeFor(ASTAnyTypeDeclaration node, MetricVersion version) {
return Metrics.get(OperationMetricKey.CYCLO, node, ResultOption.SUM);
return Metrics.get(OperationMetricKey.CYCLO, node, version, ResultOption.SUM);
}
}

View File

@ -15,7 +15,7 @@ import net.sourceforge.pmd.lang.java.oom.api.Metric.Version;
import net.sourceforge.pmd.lang.java.oom.api.MetricVersion;
import net.sourceforge.pmd.lang.java.oom.api.OperationMetricKey;
import net.sourceforge.pmd.lang.java.oom.api.ResultOption;
import net.sourceforge.pmd.lang.java.oom.metrics.CycloMetric;
import net.sourceforge.pmd.lang.java.oom.metrics.CycloMetric.CycloVersion;
import net.sourceforge.pmd.lang.java.rule.AbstractJavaMetricsRule;
import net.sourceforge.pmd.lang.rule.properties.BooleanProperty;
import net.sourceforge.pmd.lang.rule.properties.EnumeratedProperty;
@ -40,7 +40,7 @@ public class CyclomaticComplexityRule extends AbstractJavaMetricsRule {
private static final String[] VERSION_LABELS = {"standard", "ignoreBooleanPaths"};
private static final MetricVersion[] CYCLO_VERSIONS = {Metric.Version.STANDARD, CycloMetric.Version.IGNORE_BOOLEAN_PATHS};
private static final MetricVersion[] CYCLO_VERSIONS = {Metric.Version.STANDARD, CycloVersion.IGNORE_BOOLEAN_PATHS};
private static final EnumeratedProperty<MetricVersion> CYCLO_VERSION_DESCRIPTOR = new EnumeratedProperty<>(
"cycloVersion", "Choose a variant of Cyclo or the standard",

View File

@ -8,6 +8,7 @@ import net.sourceforge.pmd.lang.java.oom.api.ClassMetricKey;
import net.sourceforge.pmd.lang.java.oom.api.Metric;
import net.sourceforge.pmd.lang.java.oom.api.MetricVersion;
import net.sourceforge.pmd.lang.java.oom.api.OperationMetricKey;
import net.sourceforge.pmd.lang.java.oom.metrics.CycloMetric.CycloVersion;
/**
* Tests standard cyclo.
@ -36,6 +37,6 @@ public class CycloTestRule extends AbstractMetricTestRule {
@Override
protected MetricVersion[] versionValues() {
return new MetricVersion[] {Metric.Version.STANDARD, CycloMetric.Version.IGNORE_BOOLEAN_PATHS};
return new MetricVersion[] {Metric.Version.STANDARD, CycloVersion.IGNORE_BOOLEAN_PATHS};
}
}