<commit_before>package com.cflint.plugins.core;

import java.util.Arrays;
import java.util.Map;

import ro.fortsoft.pf4j.Extension;
import net.htmlparser.jericho.Element;
import cfml.parsing.cfscript.CFExpression;
import cfml.parsing.cfscript.CFFunctionExpression;
import cfml.parsing.cfscript.script.CFCompoundStatement;
import cfml.parsing.cfscript.script.CFFuncDeclStatement;
import cfml.parsing.cfscript.script.CFScriptStatement;

import com.cflint.BugInfo;
import com.cflint.BugList;
import com.cflint.plugins.CFLintScannerAdapter;
import com.cflint.plugins.Context;

@Extension
public class FunctionLengthChecker extends CFLintScannerAdapter {
	final int LENGTH_THRESHOLD = 50;
	final String severity = "INFO";

	public void expression(final CFExpression expression, final Context context, final BugList bugs) {
	}

	public void expression(final CFScriptStatement expression, final Context context, final BugList bugs) {
		if (expression instanceof CFFuncDeclStatement) {
			CFFuncDeclStatement function = (CFFuncDeclStatement) expression;
			String decompile = function.Decompile(1);
			final int begLine = function.getLine();
			String[] lines = decompile.split("\\n");
			CFCompoundStatement body = (CFCompoundStatement) function.getBody();

			checkSize(context, begLine, lines.length, bugs);
		}
	}

	public void element(final Element element, final Context context, final BugList bugs) {
		String elementName = element.getName();
		if (elementName.equals("cffunction")) {
			//this includes whitespace-change it
			int begLine = element.getSource().getRow(element.getBegin());
			//int endLine = element.getSource().getRow(element.getEnd()); 
			int total = element.getAllStartTags().size();

			checkSize(context, begLine, total, bugs);
		}
	}

	protected void checkSize(Context context, int atLine, int linesLength, BugList bugs) {
		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())
					.setMessage("Function is " + linesLength + " lines. Should be less than " + LENGTH_THRESHOLD + " lines.")
					.build());
		}
	}
}<commit_msg>Added length parameter to function length checker. Fixed up function length message.<commit_after>package com.cflint.plugins.core;

import java.util.Arrays;
import java.util.Map;

import ro.fortsoft.pf4j.Extension;
import net.htmlparser.jericho.Element;
import cfml.parsing.cfscript.CFExpression;
import cfml.parsing.cfscript.CFFunctionExpression;
import cfml.parsing.cfscript.script.CFCompoundStatement;
import cfml.parsing.cfscript.script.CFFuncDeclStatement;
import cfml.parsing.cfscript.script.CFScriptStatement;

import com.cflint.BugInfo;
import com.cflint.BugList;
import com.cflint.plugins.CFLintScannerAdapter;
import com.cflint.plugins.Context;

@Extension
public class FunctionLengthChecker extends CFLintScannerAdapter {
	final int LENGTH_THRESHOLD = 100;
	final String severity = "INFO";

	public void expression(final CFExpression expression, final Context context, final BugList bugs) {
	}

	public void expression(final CFScriptStatement expression, final Context context, final BugList bugs) {
		if (expression instanceof CFFuncDeclStatement) {
			CFFuncDeclStatement function = (CFFuncDeclStatement) expression;
			String decompile = function.Decompile(1);
			final int begLine = function.getLine();
			String[] lines = decompile.split("\\n");
			CFCompoundStatement body = (CFCompoundStatement) function.getBody();

			checkSize(context, begLine, lines.length, bugs);
		}
	}

	public void element(final Element element, final Context context, final BugList bugs) {
		String elementName = element.getName();

		if (elementName.equals("cffunction")) {
			//this includes whitespace-change it
			int begLine = element.getSource().getRow(element.getBegin());
			//int endLine = element.getSource().getRow(element.getEnd()); 
			int total = element.getAllStartTags().size();

			checkSize(context, begLine, total, bugs);
		}
	}

	protected void checkSize(Context context, int atLine, int linesLength, BugList bugs) {
		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())
					.setMessage("Function is " + Integer.toString(linesLength) + " lines. Should be less than " + Integer.toString(length) + " lines.")
					.build());
		}
	}
}
{'fraction_non_alphanumeric': 0.10804072304176189, 'fraction_numerical': 0.0018699355911074174, 'mean_word_length': 5.260078023407022, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6960499', 'n_tokens_mistral': 1562, 'n_tokens_neox': 1437, 'n_words': 333}