com.cflint.plugins.core.FunctionLengthChecker Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of CFLint Show documentation
Show all versions of CFLint Show documentation
A static code analysis tool for ColdFusion (in the spirit of FindBugs and Lint). With CFLint, you are able to analyze your ColdFusion code base for code violations.
package com.cflint.plugins.core;
import com.cflint.BugInfo;
import com.cflint.BugList;
import com.cflint.plugins.CFLintScannerAdapter;
import com.cflint.plugins.Context;
import cfml.parsing.cfscript.CFExpression;
import cfml.parsing.cfscript.script.CFFuncDeclStatement;
import cfml.parsing.cfscript.script.CFScriptStatement;
import net.htmlparser.jericho.Element;
import ro.fortsoft.pf4j.Extension;
@Extension
public class FunctionLengthChecker extends CFLintScannerAdapter {
final int LENGTH_THRESHOLD = 100;
final String severity = "INFO";
@Override
public void expression(final CFExpression expression, final Context context, final BugList bugs) {
}
@Override
public void expression(final CFScriptStatement expression, final Context context, final BugList bugs) {
if (expression instanceof CFFuncDeclStatement) {
final CFFuncDeclStatement function = (CFFuncDeclStatement) expression;
final String decompile = function.Decompile(1);
final int begLine = function.getLine();
final String[] lines = decompile.split("\\n");
checkSize(context, begLine, lines.length, bugs);
}
}
@Override
public void element(final Element element, final Context context, final BugList bugs) {
final String elementName = element.getName();
if (elementName.equals("cffunction")) {
// this includes whitespace-change it
final int begLine = element.getSource().getRow(element.getBegin());
// int endLine = element.getSource().getRow(element.getEnd());
final int total = element.getAllStartTags().size();
checkSize(context, begLine, total, bugs);
}
}
protected void checkSize(final Context context, final int atLine, final int linesLength, final BugList bugs) {
final String lengthThreshold = getParameter("length");
int length = LENGTH_THRESHOLD;
if (lengthThreshold != null) {
length = Integer.parseInt(lengthThreshold);
}
if (linesLength > length) {
bugs.add(new BugInfo.BugInfoBuilder().setLine(atLine).setMessageCode("EXCESSIVE_FUNCTION_LENGTH")
.setSeverity(severity).setFilename(context.getFilename()).setFunction(context.getFunctionName())
.setMessage("Function " + context.getFunctionName() + " is " + Integer.toString(linesLength)
+ " lines. Should be less than " + Integer.toString(length) + " lines.")
.build());
}
}
}