/** * @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 instanceof Identifier) { final Identifier id = (Identifier) node; if (name.equals(id.getName()) && id.getParent().getType() == type) { exists = true; } } return true; }
public boolean apply(ASTNode node) { // stops when found - that's the reason to use ApplyAll if (exists) return false; if (node.getType() == ASTNode.CLASS_DECLARATION) { // do nothing never catch method names } else if (node.getType() == ASTNode.FUNCTION_DECLARATION) { FunctionDeclaration functionDeclaration = (FunctionDeclaration) node; Identifier identifier = functionDeclaration.getFunctionName(); if (identifier.getName().equalsIgnoreCase(name)) { exists = true; } } return true; }
public boolean apply(ASTNode node) { // stops when found - that's the reason to use ApplyAll if (exists) return false; if (node.getType() == ASTNode.VARIABLE) { Variable variable = (Variable) node; if (variable.isDollared()) { assert variable.getName().getType() == ASTNode.IDENTIFIER; Identifier identifier = (Identifier) variable.getName(); if (identifier.getName().equals(name)) { exists = true; } } } return true; }
/** * @param targetIdentifier * @param variable * @param isGlobal * @param globalStatement * @return true is the */ private static boolean checkGlobal( Identifier targetIdentifier, final GlobalStatement globalStatement) { final List<Variable> variables = globalStatement.variables(); for (final Variable current : variables) { // if the variable is reflection (eg. global $$var) skip if (current.getName().getType() == ASTNode.IDENTIFIER) { Identifier id = (Identifier) current.getName(); // variables are case sensative if (id.getName().equals(targetIdentifier.getName())) { return true; } } } 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; }
/** * @param locateNode * @return true if the given path indicates a function */ private static boolean isFunction(ASTNode locateNode) { assert locateNode != null; ASTNode parent = null; Identifier targetIdentifier = null; if (locateNode.getType() == ASTNode.FUNCTION_DECLARATION) { parent = ((FunctionDeclaration) locateNode); targetIdentifier = ((FunctionDeclaration) locateNode).getFunctionName(); } else if (locateNode instanceof Identifier && !"define".equals(((Identifier) locateNode).getName())) { // $NON-NLS-1$ targetIdentifier = (Identifier) locateNode; parent = targetIdentifier.getParent(); if (parent != null && parent.getType() == ASTNode.NAMESPACE_NAME) { parent = targetIdentifier.getParent().getParent(); } } else { return false; } // check if it is a function if (parent == null || parent.getType() != ASTNode.FUNCTION_DECLARATION && parent.getType() != ASTNode.FUNCTION_NAME || parent.getParent().getType() == ASTNode.METHOD_DECLARATION || parent.getType() == ASTNode.FULLY_QUALIFIED_TRAIT_METHOD_REFERENCE) { return false; } if (parent instanceof TraitAlias) { return false; } // check if it is a method final int type = parent.getParent().getType(); if (type == ASTNode.FUNCTION_INVOCATION) { final int parentParentType = parent.getParent().getParent().getType(); if (parentParentType == ASTNode.METHOD_DECLARATION || parentParentType == ASTNode.STATIC_METHOD_INVOCATION) { return false; } } else if (type == ASTNode.METHOD_DECLARATION) { return false; } return true; }
public boolean apply(ASTNode node) { // stops when found - that's the reason to use ApplyAll if (exists) return false; if (node.getType() == ASTNode.CLASS_DECLARATION || node.getType() == ASTNode.FUNCTION_DECLARATION) { isGlobalScope = false; node.childrenAccept(this); isGlobalScope = true; return false; } else if (node instanceof Identifier) { Identifier identifier = (Identifier) node; if (identifier.getParent().getType() == ASTNode.VARIABLE) { Variable variable = (Variable) identifier.getParent(); if (variable.isDollared() && isGlobalScope && name.equals(identifier.getName())) { exists = true; } } } else if (node.getType() == ASTNode.GLOBAL_STATEMENT) { GlobalStatement globalStatement = (GlobalStatement) node; final List<Variable> variables = globalStatement.variables(); for (final Variable variable : variables) { final Expression variableName = variable.getName(); if (variable.isDollared() && variableName instanceof Identifier) { Identifier identifier = (Identifier) variableName; if (name.equals(identifier.getName())) { exists = true; } } } } return true; }
private static boolean checkGlobalIdentifier(ASTNode locateNode) { // check if it is a GLOBALS['a'] direction if (locateNode.getType() == ASTNode.SCALAR) { Scalar scalar = (Scalar) locateNode; return checkGLOBALS(scalar); } // check if it is an identifier if (locateNode.getType() != ASTNode.IDENTIFIER) { return false; } final Identifier targetIdentifier = ((Identifier) locateNode); ASTNode parent = locateNode.getParent(); if (parent == null || parent.getType() != ASTNode.VARIABLE) { return false; } final Variable variable = (Variable) parent; // if it is not a dollared variable - it is not a global one if (!variable.isDollared() || variable.getParent().getType() == ASTNode.FIELD_DECLARATION) { return false; } // ignore static memeber call if (parent.getParent().getType() == ASTNode.STATIC_FIELD_ACCESS) { final StaticFieldAccess staticFieldAccess = (StaticFieldAccess) parent.getParent(); if (staticFieldAccess.getMember() == variable) { return false; } } if (parent.getParent().getLocationInParent() == FieldsDeclaration.FIELDS_PROPERTY) { return false; } // check if declared global in function while (parent != null) { // if the variable was used inside a function if (parent.getType() == ASTNode.FUNCTION_DECLARATION) { // global declaration detection final int end = parent.getEnd(); class GlobalSeacher extends ApplyAll { public int offset = end; public boolean apply(ASTNode node) { if (offset != end) { return false; } if (node.getType() == ASTNode.GLOBAL_STATEMENT) { GlobalStatement globalStatement = (GlobalStatement) node; if (checkGlobal(targetIdentifier, globalStatement)) { offset = globalStatement.getStart(); } } return true; } } GlobalSeacher searchGlobal = new GlobalSeacher(); parent.accept(searchGlobal); return searchGlobal.offset <= targetIdentifier.getStart(); } parent = parent.getParent(); } 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$ }