All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.cflint.plugins.core.TooManyArgumentsChecker Maven / Gradle / Ivy

Go to download

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.

There is a newer version: 1.5.0
Show newest version
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.script.CFFuncDeclStatement;
import cfml.parsing.cfscript.script.CFScriptStatement;
import net.htmlparser.jericho.Element;

public class TooManyArgumentsChecker extends CFLintScannerAdapter {
	final String severity = "WARNING";
	final int ARGUMENT_THRESHOLD = 10;

	protected int argumentCount = 0;
	protected int functionLine = 0;

	@Override
	public void expression(final CFScriptStatement expression, final Context context, final BugList bugs) {
		if (expression instanceof CFFuncDeclStatement) {
			final CFFuncDeclStatement function = (CFFuncDeclStatement) expression;
			final int begLine = function.getLine();
			final int noArguments = function.getFormals().size();

			checkNumberArguments(noArguments, begLine, context, bugs);
		}
	}

	@Override
	public void element(final Element element, final Context context, final BugList bugs) {
		if (element.getName().equals("cffunction")) {
			functionLine = element.getSource().getRow(element.getBegin());
			argumentCount = 0;
		} else if (element.getName().equals("cfargument")) {
			argumentCount++;
		}
		// No easy way of detecting end tag so assumes functions will contain
		// some code
		// otherwise the argument count will be off by one
		else if (!element.getName().equals("!---") && argumentCount > 0) {
			checkNumberArguments(argumentCount, functionLine, context, bugs);
			argumentCount = 0;
			functionLine = 0;
		}
	}

	protected void checkNumberArguments(final int argumentCount, final int atLine, final Context context,
			final BugList bugs) {
		final String argumentThreshold = getParameter("maximum");
		int threshold = ARGUMENT_THRESHOLD;

		if (argumentThreshold != null) {
			threshold = Integer.parseInt(argumentThreshold);
		}

		if (argumentCount > threshold) {
			bugs.add(new BugInfo.BugInfoBuilder().setLine(atLine).setMessageCode("EXCESSIVE_ARGUMENTS")
					.setSeverity(severity).setFilename(context.getFilename()).setFunction(context.getFunctionName())
					.setMessage("Function " + context.getFunctionName() + " has too many arguments. Has"
							+ Integer.toString(argumentCount) + " arguments, should be less than "
							+ Integer.toString(threshold) + ".")
					.build());
		}
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy