diff --git a/src/main/java/com/cflint/BugInfo.java b/src/main/java/com/cflint/BugInfo.java index 91e6ecdf3..5891b3ef6 100644 --- a/src/main/java/com/cflint/BugInfo.java +++ b/src/main/java/com/cflint/BugInfo.java @@ -1,5 +1,6 @@ package com.cflint; +import java.io.ObjectInputStream.GetField; import java.util.List; import com.cflint.config.CFLintPluginInfo.PluginInfoRule.PluginMessage; @@ -7,6 +8,7 @@ import cfml.parsing.cfscript.CFExpression; import cfml.parsing.cfscript.script.CFParsedStatement; +import javolution.context.Context; import net.htmlparser.jericho.Element; public class BugInfo implements Comparable { @@ -167,6 +169,8 @@ private void doMessageText(final Element elem) { String message = notNull(bugInfo.getMessage()); final String variable = notNull(bugInfo.getVariable()); message = message.replace("${variable}", variable); + message = message.replace("${functionName}", bugInfo.function); + if (message.contains("{tag}") && elem != null) { message = message.replaceAll("\\$\\{tag\\}", notNull(elem.getName())); } diff --git a/src/main/java/com/cflint/plugins/core/SimpleComplexityChecker.java b/src/main/java/com/cflint/plugins/core/SimpleComplexityChecker.java index ba2268cad..97f279e90 100644 --- a/src/main/java/com/cflint/plugins/core/SimpleComplexityChecker.java +++ b/src/main/java/com/cflint/plugins/core/SimpleComplexityChecker.java @@ -41,9 +41,9 @@ public void expression(final CFScriptStatement expression, final Context context complexity = 0; alreadyTooComplex = false; } else if (expression == null) { - bugs.add(new BugInfo.BugInfoBuilder().setLine(context.startLine()).setMessageCode("PARSE_NOTHING") - .setSeverity("WARNING").setFilename(context.getFilename()).setFunction(context.getFunctionName()) - .setMessage("Nothing to parse").build()); +// bugs.add(new BugInfo.BugInfoBuilder().setLine(context.startLine()).setMessageCode("PARSE_NOTHING") +// .setSeverity("WARNING").setFilename(context.getFilename()).setFunction(context.getFunctionName()) +// .setMessage("Nothing to parse").build()); } // Not using instanceof to avoid double counting else if (expression.getClass().equals(CFIfStatement.class) || expression.getClass().equals(CFForStatement.class) @@ -90,11 +90,7 @@ protected void checkComplexity(final String name, final int lineNo, final Contex if (!alreadyTooComplex && complexity > threshold) { alreadyTooComplex = true; - bugs.add(new BugInfo.BugInfoBuilder().setLine(lineNo).setMessageCode("FUNCTION_TOO_COMPLEX") - .setSeverity(severity) - .setFilename(context.getFilename()).setFunction(context.getFunctionName()).setMessage("Function " - + name + " is too complex. Consider breaking the function into smaller functions.") - .build()); + context.addMessage("FUNCTION_TOO_COMPLEX", null); } } diff --git a/src/main/resources/cflint.definition.json b/src/main/resources/cflint.definition.json index 33c4626be..37770bfc3 100644 --- a/src/main/resources/cflint.definition.json +++ b/src/main/resources/cflint.definition.json @@ -418,6 +418,7 @@ "className": "SimpleComplexityChecker", "message": [ { + "messageText": "Function ${functionName} is too complex. Consider breaking the function into smaller functions.", "code": "FUNCTION_TOO_COMPLEX", "severity": "WARNING" } diff --git a/src/main/resources/cflint.definition.xml b/src/main/resources/cflint.definition.xml index 6c260b050..d2d704b2e 100644 --- a/src/main/resources/cflint.definition.xml +++ b/src/main/resources/cflint.definition.xml @@ -186,6 +186,7 @@ + Function ${functionName} is too complex. Consider breaking the function into smaller functions. WARNING diff --git a/src/test/resources/com/cflint/tests/Complexity/.cflintrc.xml b/src/test/resources/com/cflint/tests/Complexity/.cflintrc.xml new file mode 100644 index 000000000..71aeb8aad --- /dev/null +++ b/src/test/resources/com/cflint/tests/Complexity/.cflintrc.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/src/test/resources/com/cflint/tests/Complexity/ifelse.cfc b/src/test/resources/com/cflint/tests/Complexity/ifelse.cfc new file mode 100644 index 000000000..22bf275ed --- /dev/null +++ b/src/test/resources/com/cflint/tests/Complexity/ifelse.cfc @@ -0,0 +1,9 @@ +component { + public void function foo() { + if (something) { + doSomething(); + } else { + doSomethingElse(); + } + } +} \ No newline at end of file diff --git a/src/test/resources/com/cflint/tests/Complexity/ifelse.expected.txt b/src/test/resources/com/cflint/tests/Complexity/ifelse.expected.txt new file mode 100644 index 000000000..8878e547a --- /dev/null +++ b/src/test/resources/com/cflint/tests/Complexity/ifelse.expected.txt @@ -0,0 +1 @@ +[ ] \ No newline at end of file diff --git a/src/test/resources/com/cflint/tests/Complexity/ifelseComplex.cfc b/src/test/resources/com/cflint/tests/Complexity/ifelseComplex.cfc new file mode 100644 index 000000000..0ecfdff1b --- /dev/null +++ b/src/test/resources/com/cflint/tests/Complexity/ifelseComplex.cfc @@ -0,0 +1,29 @@ +component { + public void function foo() { + if (something) { + doSomething(); + } else { + if (something) { + if (something) { + if (something) { + if (something) { + if (something) { + if (something) { + if (something) { + if (something) { + if (something) { + if (something) { + doSomethingElse(); + } + } + } + } + } + } + } + } + } + } + } + } +} \ No newline at end of file diff --git a/src/test/resources/com/cflint/tests/Complexity/ifelseComplex.expected.txt b/src/test/resources/com/cflint/tests/Complexity/ifelseComplex.expected.txt new file mode 100644 index 000000000..cd3f33bd7 --- /dev/null +++ b/src/test/resources/com/cflint/tests/Complexity/ifelseComplex.expected.txt @@ -0,0 +1,17 @@ +[ { + "severity" : "WARNING", + "id" : "FUNCTION_TOO_COMPLEX", + "message" : "FUNCTION_TOO_COMPLEX", + "category" : "CFLINT", + "abbrev" : "FT", + "locations" : [ { + "file" : "src/test/resources/com/cflint/tests/Complexity/ifelseComplex.cfc", + "fileName" : "ifelseComplex.cfc", + "function" : "foo", + "column" : "21", + "line" : "15", + "message" : "Function foo is too complex. Consider breaking the function into smaller functions.", + "variable" : "", + "expression" : "if(something ) {/ndoSomethingElse();/n/n }" + } ] +} ] \ No newline at end of file