From 71d2dd5063bfaa2d5d034e92b41ba62e68c4aa9d Mon Sep 17 00:00:00 2001 From: Nicolas Bihan Date: Fri, 19 Apr 2019 10:26:43 -0500 Subject: [PATCH] Add complexity calculation --- pom.xml | 2 +- .../plugin/coldfusion/ColdFusionSensor.java | 56 ++++++++++++++++++- .../com/wellsky/ColdfusionSensorTest.java | 6 +- src/test/resources/EpisodeClaim.cfc | 17 ++++++ src/test/resources/testmetrics1.cfm | 6 ++ 5 files changed, 82 insertions(+), 5 deletions(-) diff --git a/pom.xml b/pom.xml index 4221eca..b1e529c 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ com.stepstone.sonar.plugin sonar-coldfusion-plugin sonar-plugin - 1.7.0-SNAPSHOT + 1.7.1-SNAPSHOT SonarQube Coldfusion Analyzer Enables scanning of ColdFusion source files diff --git a/src/main/java/com/stepstone/sonar/plugin/coldfusion/ColdFusionSensor.java b/src/main/java/com/stepstone/sonar/plugin/coldfusion/ColdFusionSensor.java index 61c73d2..ceb98df 100644 --- a/src/main/java/com/stepstone/sonar/plugin/coldfusion/ColdFusionSensor.java +++ b/src/main/java/com/stepstone/sonar/plugin/coldfusion/ColdFusionSensor.java @@ -42,6 +42,8 @@ import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; +import java.util.regex.Matcher; +import java.util.regex.Pattern; public class ColdFusionSensor implements Sensor { @@ -116,7 +118,7 @@ private void measureProcessor(SensorContext context) { for (InputFile inputFile : fs.inputFiles(fs.predicates().hasLanguage(ColdFusionPlugin.LANGUAGE_KEY))) { Callable callableTask = () -> { try { - metricsLinesCounter(inputFile, context); + metricsCounter(inputFile, context); return 1; } catch (IOException e) { return 0; @@ -138,16 +140,19 @@ private void measureProcessor(SensorContext context) { //Very basic and naive line of code counter for Coldfusion //Might count a line of code as comment - private void metricsLinesCounter(InputFile inputFile, SensorContext context) throws IOException { + private void metricsCounter(InputFile inputFile, SensorContext context) throws IOException { String currentLine; int commentLines = 0; int blankLines = 0; int lines = 0; + int complexity = 1; + try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputFile.inputStream()))) { if (inputFile.inputStream() != null) { while ((currentLine = reader.readLine()) != null) { lines++; + if (currentLine.contains("")) { @@ -161,14 +166,59 @@ private void metricsLinesCounter(InputFile inputFile, SensorContext context) thr } } else if (currentLine.trim().isEmpty()) { blankLines++; + continue; } + + complexity = getLineComplexity(currentLine, complexity); } } } + int linesOfCode = lines-blankLines-commentLines; + // every 100 lines of code add 1 to the content's complexity + complexity = complexity + (linesOfCode / 100); + context.newMeasure().forMetric(CoreMetrics.COMMENT_LINES).on(inputFile).withValue(commentLines).save(); - context.newMeasure().forMetric(CoreMetrics.NCLOC).on(inputFile).withValue(lines-blankLines-commentLines).save(); + context.newMeasure().forMetric(CoreMetrics.NCLOC).on(inputFile).withValue(linesOfCode).save(); context.newMeasure().forMetric(CoreMetrics.LINES).on(inputFile).withValue(lines).save(); + context.newMeasure().forMetric(CoreMetrics.COMPLEXITY).on(inputFile).withValue(complexity).save(); } + private int getLineComplexity(String currentLine, int complexity) { + int mcCabeComplexity =0; + int lineByLineComplexity = 0; + int lineByLineComplexityIncrement = 4; + int thisLineComplexityAdd = 0; + int thisLineComplexitySubtract = 0; + + // SCORE INCREMENTS + mcCabeComplexity += countRegexOccurrences(currentLine, "( measureNloc = context.measure(o.key(),CoreMetrics.NCLOC.key()); Measure measureComment = context.measure(o.key(),CoreMetrics.COMMENT_LINES.key()); + Measure measureComplexity = context.measure(o.key(),CoreMetrics.COMPLEXITY.key()); nloc+=measureNloc.value(); comments+=measureComment.value(); + complexity+=measureComplexity.value(); } - assertThat(nloc).isEqualTo(36); + assertThat(nloc).isEqualTo(56); assertThat(comments).isEqualTo(9); + assertThat(complexity).isEqualTo(10); } diff --git a/src/test/resources/EpisodeClaim.cfc b/src/test/resources/EpisodeClaim.cfc index c8795cd..948ec53 100644 --- a/src/test/resources/EpisodeClaim.cfc +++ b/src/test/resources/EpisodeClaim.cfc @@ -28,6 +28,23 @@ + + + if (local.InfusionTherapy is 1) + { + M0250 = M0250 + 14; + } + if (local.ParentalNutrition is 1) + { + M0250 = M0250 + 20; + } + if (local.EnteralNutrition is 1) + { + M0250 = M0250 + 24; + } + + + diff --git a/src/test/resources/testmetrics1.cfm b/src/test/resources/testmetrics1.cfm index 58a94da..a9890d4 100644 --- a/src/test/resources/testmetrics1.cfm +++ b/src/test/resources/testmetrics1.cfm @@ -8,3 +8,9 @@ the world to see. + + + Nico! + + the world to see. +