/** * @param scalar * @return true if the scalar is defines as $GLOBALS call */ private static boolean checkGLOBALS(Scalar scalar) { final String stringValue = scalar.getStringValue(); if (scalar.getScalarType() != Scalar.TYPE_STRING || stringValue.length() < 3) { return false; } final char charAtZero = stringValue.charAt(0); final char charAtEnd = stringValue.charAt(stringValue.length() - 1); if (!detectString(charAtZero) || !detectString(charAtEnd)) { return false; } if (scalar.getParent().getType() == ASTNode.ARRAY_ACCESS) { ArrayAccess arrayAccess = (ArrayAccess) scalar.getParent(); final Expression variableName = arrayAccess.getName(); if (variableName.getType() == ASTNode.VARIABLE) { Variable var = (Variable) variableName; if (var.isDollared() && var.getName() instanceof Identifier) { final Identifier id = (Identifier) var.getName(); return id.getName().equals("_GLOBALS") // $NON-NLS-1$ || id.getName().equals("GLOBALS"); // $NON-NLS-1$ } } } return false; }
public boolean apply(ASTNode node) { // stops when found - that's the reason to use ApplyAll if (exists) return false; if (node.getType() == ASTNode.SCALAR) { final Scalar scalar = (Scalar) node; final String stringValue = scalar.getStringValue(); if (scalar.getScalarType() != Scalar.TYPE_STRING || stringValue == null) { return false; } final int length = stringValue.length() - 1; if (stringValue.charAt(0) != '"' && stringValue.charAt(length) != '"' && stringValue.equals(name)) { exists = true; } } else if (node.getType() == ASTNode.FUNCTION_INVOCATION) { FunctionInvocation functionInvocation = (FunctionInvocation) node; final Expression functionName = functionInvocation.getFunctionName().getName(); if (!(functionName instanceof Identifier)) { return false; } final Identifier identifier = (Identifier) functionName; final List<Expression> parameters = functionInvocation.parameters(); if (!"define".equalsIgnoreCase(identifier.getName()) || parameters == null || parameters.size() == 0) { // $NON-NLS-1$ return false; } final Expression expression = parameters.get(0); if (expression.getType() != ASTNode.SCALAR) { return false; } Scalar scalar = (Scalar) expression; final String stringValue = scalar.getStringValue(); if (stringValue.length() < 2 || stringValue.charAt(0) != '"') { return false; } exists = name.equals(stringValue.substring(1, stringValue.length() - 1)); } return true; }
/** * Identifies a constant use * * @param locateNode * @return */ private static boolean isConstant(ASTNode locateNode) { assert locateNode != null; Scalar scalar = null; // check if it is an identifier if (locateNode.getType() != ASTNode.SCALAR) { ASTNode parent = locateNode.getParent(); ASTNode node = null; // php 5.3, the parent is NamespaceName if (parent instanceof NamespaceName) { node = parent.getParent().getParent(); } else { // non-php 5.3 node = parent.getParent(); } // check if the node is 'define' if ((locateNode instanceof Identifier) && "define".equals(((Identifier) locateNode).getName()) // $NON-NLS-1$ && node instanceof FunctionInvocation) { FunctionInvocation inv = (FunctionInvocation) node; List<Expression> parameters = inv.parameters(); if (parameters != null && parameters.size() > 0) { Expression param = parameters.get(0); if (param instanceof Scalar) { scalar = (Scalar) param; } else { return false; } } } else { return false; } } else { scalar = (Scalar) locateNode; } // if it is not a dollared variable - it is not a global one if (scalar == null || scalar.getScalarType() != Scalar.TYPE_STRING || scalar.getStringValue() == null) { return false; } final int length = scalar.getStringValue().length() - 1; final char charAtBegining = scalar.getStringValue().charAt(0); final char charAtEnd = scalar.getStringValue().charAt(length); if (!detectString(charAtEnd) && !detectString(charAtBegining)) { return true; } // check if it is part of define ASTNode previous = locateNode.getParent(); if (previous instanceof NamespaceName) { previous = previous.getParent(); } if (previous.getType() == ASTNode.FUNCTION_NAME) { previous = previous.getParent(); } if (previous.getType() != ASTNode.FUNCTION_INVOCATION) { return false; } final FunctionInvocation functionInvocation = (FunctionInvocation) previous; if (!(functionInvocation.getFunctionName().getName() instanceof Identifier)) { return false; } final Identifier identifier = (Identifier) functionInvocation.getFunctionName().getName(); return "define".equalsIgnoreCase(identifier.getName()) || "constant".equalsIgnoreCase(identifier.getName()); // $NON-NLS-1$ //$NON-NLS-2$ }