Skip to content

Commit

Permalink
Update the POM file
Browse files Browse the repository at this point in the history
Update UnusedArgumentChecker.java to look at the function
Update test to look fro arguments in cfqueryparam
  • Loading branch information
msp1kpj committed Feb 11, 2017
1 parent b86c2d5 commit f1ff75b
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 7 deletions.
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,11 @@
<artifactId>jackson-core</artifactId>
<version>2.8.6</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.6</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-module-jaxb-annotations</artifactId>
Expand Down
27 changes: 20 additions & 7 deletions src/main/java/com/cflint/plugins/core/UnusedArgumentChecker.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,15 @@ public class UnusedArgumentChecker extends CFLintScannerAdapter {

@Override
public void element(final Element element, final Context context, final BugList bugs) {
if (element.getName().equals("cfargument")) {
if (element.getName().equals("cfargument")) {
final String name = element.getAttributeValue("name") != null
? element.getAttributeValue("name").toLowerCase() : "";
methodArguments.put(name, false);
setArgumentLineNo(name, context.startLine());
final String code = element.getParentElement().toString();
if(isUsed(code, name)){
methodArguments.put(name, true);
}
}
}

Expand Down Expand Up @@ -57,10 +61,10 @@ protected void setArgumentLineNo(final String argument, final Integer lineNo) {
}

protected void useIdentifier(final CFFullVarExpression fullVarExpression) {
if (fullVarExpression.getExpressions().size() > 0) {
if (fullVarExpression.getExpressions().size() > 0) {
final CFExpression identifier1 = fullVarExpression.getExpressions().get(0);
if (identifier1 instanceof CFIdentifier) {
if ("arguments".equalsIgnoreCase(((CFIdentifier) identifier1).getName())
if ("arguments".equalsIgnoreCase(((CFIdentifier) identifier1).getName())
&& fullVarExpression.getExpressions().size() > 1) {
final CFExpression identifier2 = fullVarExpression.getExpressions().get(1);
if (identifier2 instanceof CFIdentifier) {
Expand All @@ -74,15 +78,15 @@ protected void useIdentifier(final CFFullVarExpression fullVarExpression) {
}

protected void useIdentifier(final CFIdentifier identifier) {
final String name = identifier.getName().toLowerCase();
final String name = identifier.getName().toLowerCase();
if (methodArguments.get(name) != null) {
methodArguments.put(name, true);
}
}

@Override
public void expression(final CFExpression expression, final Context context, final BugList bugs) {
if (expression instanceof CFFullVarExpression) {
if (expression instanceof CFFullVarExpression) {
useIdentifier((CFFullVarExpression) expression);
} else if (expression instanceof CFIdentifier) {
useIdentifier((CFIdentifier) expression);
Expand All @@ -98,13 +102,22 @@ public void startFunction(final Context context, final BugList bugs) {
@Override
public void endFunction(final Context context, final BugList bugs) {
// sort by line number
for (final Map.Entry<String, Boolean> method : methodArguments.entrySet()) {
for (final Map.Entry<String, Boolean> method : methodArguments.entrySet()) {
final String name = method.getKey();
final Boolean used = method.getValue();
if (!used) {
final String name = method.getKey();
context.addMessage("UNUSED_METHOD_ARGUMENT", name, argumentLineNo.get(name));
}
}
}

private boolean isUsed(String content, final String name) {
boolean isUsed = false;
content = content.replace(" ", "").replace("'", "\"").toLowerCase();
boolean structKeyCheck = (content.contains("arguments[\"" + name + "\"]"));
boolean isDefinedCheck = (content.contains("arguments." + name));
isUsed = structKeyCheck || isDefinedCheck;
return isUsed;
}

}
20 changes: 20 additions & 0 deletions src/test/java/com/cflint/TestUnusedArgumentChecker.java
Original file line number Diff line number Diff line change
Expand Up @@ -248,5 +248,25 @@ public void testArgumentsUsedInTagWithArgumentsScope() throws ParseException, IO
final Map<String, List<BugInfo>> result = cfBugs.getBugs().getBugList();
assertEquals(0, result.size());
}

@Test
public void testArgumentsUsedInQuerygWithArgumentsScope() throws ParseException, IOException {
final String tagSrc = "<cfcomponent>\r\n"
+ "<cffunction name=\"getProduct\">\r\n"
+ "<cfargument name=\"productid\">\r\n"
+ "<cfargument name=\"b\">\r\n"
+ "<cfset var qryProduct = \"\" />\r\n"
+ "<cfquery name=\"qryProduct\">\r\n"
+ "SET @productId = <cfqueryparam value=\"#arguments.productid#\" cfsqltype=\"cf_sql_integer\" />\r\n"
+ "SELECT @productId as productId\r\n"
+ "</cfquery>\r\n"
+ "<cfreturn arguments.a + arguments.b>\r\n"
+ "</cffunction>\r\n"
+ "</cfcomponent>\r\n";

cfBugs.process(tagSrc, "test");
final Map<String, List<BugInfo>> result = cfBugs.getBugs().getBugList();
assertEquals(0, result.size());
}

}

0 comments on commit f1ff75b

Please sign in to comment.