package com.cflint.plugins.core;

import net.htmlparser.jericho.Element;
import cfml.parsing.cfscript.script.CFFuncDeclStatement;
import cfml.parsing.cfscript.script.CFFunctionParameter;
import cfml.parsing.cfscript.script.CFScriptStatement;

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

public class ArgTypeChecker extends CFLintScannerAdapter {
	final String severity = "WARNING";

	@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();

			for (final CFFunctionParameter argument : function.getFormals()) {
				final String name = argument.getName();
				final String variableType = argument.getType();

				if (variableType == null) {
					bugs.add(new BugInfo.BugInfoBuilder().setLine(begLine).setMessageCode("ARG_TYPE_MISSING")
						.setSeverity(severity).setFilename(context.getFilename())
						.setMessage("Argument " + name + " is missing a type.")
						.build());
				}
				else if (variableType.equals("any")) {
					bugs.add(new BugInfo.BugInfoBuilder().setLine(begLine).setMessageCode("ARG_TYPE_ANY")
						.setSeverity(severity).setFilename(context.getFilename())
						.setMessage("Argument " + name + " is any. Please change to be the correct type.")
						.build());
				}
			}
		}
	}

	@Override
	public void element(final Element element, final Context context, final BugList bugs) {
		if (element.getName().equals("cfargument")) {
			final String name = element.getAttributeValue("name");
			final String variableType = element.getAttributeValue("type");
			int begLine = element.getSource().getRow(element.getBegin());

			if (variableType == null) {
				bugs.add(new BugInfo.BugInfoBuilder().setLine(begLine).setMessageCode("ARG_TYPE_MISSING")
					.setSeverity(severity).setFilename(context.getFilename())
					.setMessage("Argument " + name + " is missing a type.")
					.build());
			}
			else if (variableType.equals("any")) {
				bugs.add(new BugInfo.BugInfoBuilder().setLine(begLine).setMessageCode("ARG_TYPE_ANY")
					.setSeverity(severity).setFilename(context.getFilename())
					.setMessage("Argument " + name + " is any. Please change to be the correct type.")
					.build());
			}
		}
	}

}

@@ -1136,33 +1136,72 @@
 t.getFilename())
+.setFunction(context.getFunctionName())
 %0A
-
 %09%09%09%09%09%09.setMessag
@@ -1238,32 +1238,57 @@
 ssing a type.%22)%0A
+%09%09%09%09%09%09.setVariable(name)%0A
 %09%09%09%09%09%09.build());
@@ -1487,24 +1487,63 @@
 tFilename())
+.setFunction(context.getFunctionName())
 %0A%09%09%09%09%09%09.setM
@@ -1616,24 +1616,49 @@
 ect type.%22)%0A
+%09%09%09%09%09%09.setVariable(name)%0A
 %09%09%09%09%09%09.build
@@ -2198,32 +2198,71 @@
 t.getFilename())
+.setFunction(context.getFunctionName())
 %0A%09%09%09%09%09.setMessag
@@ -2299,32 +2299,56 @@
 ssing a type.%22)%0A
+%09%09%09%09%09.setVariable(name)%0A
 %09%09%09%09%09.build());%0A
@@ -2543,24 +2543,69 @@
 Filename())%0A
+%09%09%09%09%09.setFunction(context.getFunctionName())%0A
 %09%09%09%09%09.setMes
@@ -2676,24 +2676,48 @@
 ect type.%22)%0A
+%09%09%09%09%09.setVariable(name)%0A
 %09%09%09%09%09.build(

add function name and variable name to bug info