From 026b03dfb2330e7118f77b1695cf14a70af73950 Mon Sep 17 00:00:00 2001 From: oowekyala Date: Mon, 17 Jul 2017 14:54:16 +0200 Subject: [PATCH] Some documentation about Wmc and Cyclo --- .../pmd/lang/java/oom/metrics/CycloMetric.java | 16 ++++++++++------ .../pmd/lang/java/oom/metrics/WmcMetric.java | 9 +++++++-- .../java/oom/rule/CyclomaticComplexityRule.java | 4 ++-- .../pmd/lang/java/oom/metrics/CycloTestRule.java | 3 ++- 4 files changed, 21 insertions(+), 11 deletions(-) diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/oom/metrics/CycloMetric.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/oom/metrics/CycloMetric.java index 842e08c2aa..5cc7d841fc 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/oom/metrics/CycloMetric.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/oom/metrics/CycloMetric.java @@ -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. * - *

The standard version of the metric complies with McCabe's original definition: + *

The standard version of the metric complies with McCabe's original definition [3]: * *

* - *

Version {@link Version#IGNORE_BOOLEAN_PATHS}: Boolean operators are not counted, which means that empty + *

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. * - *

References:

+ *

References: + *

* * @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(); diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/oom/metrics/WmcMetric.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/oom/metrics/WmcMetric.java index 7e36b8359d..c7b70a5e8c 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/oom/metrics/WmcMetric.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/oom/metrics/WmcMetric.java @@ -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] + * + *

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. * *

[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); } + } diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/oom/rule/CyclomaticComplexityRule.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/oom/rule/CyclomaticComplexityRule.java index 95f323475e..7183a099e0 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/oom/rule/CyclomaticComplexityRule.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/oom/rule/CyclomaticComplexityRule.java @@ -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 CYCLO_VERSION_DESCRIPTOR = new EnumeratedProperty<>( "cycloVersion", "Choose a variant of Cyclo or the standard", diff --git a/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/oom/metrics/CycloTestRule.java b/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/oom/metrics/CycloTestRule.java index 8e73c17ab0..2e419657bf 100644 --- a/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/oom/metrics/CycloTestRule.java +++ b/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/oom/metrics/CycloTestRule.java @@ -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}; } }