Ejemplo n.º 1
0
    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;
    }
Ejemplo n.º 2
0
  /**
   * @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;
  }
Ejemplo n.º 3
0
  private static boolean isLocalVariable(ASTNode locateNode) {
    assert locateNode != null;
    Variable parent = null;
    // check if it is an identifier
    if (locateNode instanceof Identifier
        && ((Identifier) locateNode).getParent() instanceof Variable) {
      parent = (Variable) ((Identifier) locateNode).getParent();
    } else if (locateNode.getType() == ASTNode.VARIABLE) {
      parent = (Variable) locateNode;
    } else {
      return false;
    }

    // check for not variables / or $this / or field declaration
    if (!parent.isDollared()
        || isThisVariable(parent)
        || parent.getType() == ASTNode.FIELD_DECLARATION) {
      return false;
    }

    // check for static variables
    if (parent.isDollared()
        && parent.getParent() != null
        && parent.getParent().getType() == ASTNode.STATIC_FIELD_ACCESS) {
      return false;
    }

    // check for static array variables
    if (parent.isDollared()
        && parent.getParent() != null
        && parent.getParent().getType() == ASTNode.ARRAY_ACCESS
        && parent != ((ArrayAccess) parent.getParent()).getIndex()
        && parent.getParent().getParent().getType() == ASTNode.STATIC_FIELD_ACCESS) {
      return false;
    }
    ASTNode node = parent;
    while (node != null) {
      final int type = node.getType();
      if (type == ASTNode.FUNCTION_DECLARATION) {
        return true;
      }
      node = node.getParent();
    }
    return false;
  }
Ejemplo n.º 4
0
 public void endVisit(Variable variable) {
   if (variable.getParent().getType() != ASTNode.FIELD_ACCESS
       || (variable.getParent().getType() == ASTNode.FIELD_ACCESS
           && ((FieldAccess) variable.getParent()).getDispatcher() == variable)) {
     Expression varName = variable.getName();
     if (params.contains(((Identifier) varName).getName())
         && varName instanceof Identifier
         && variable.isDollared()
         && variable.getParent().getType() != ASTNode.STATIC_FIELD_ACCESS) {
       highlight(variable);
     }
   }
 }
Ejemplo n.º 5
0
    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;
    }
Ejemplo n.º 6
0
  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;
  }
Ejemplo n.º 7
0
 private static final boolean isThisVariable(Variable variable) {
   return (variable.isDollared()
       && variable.getName() instanceof Identifier
       && THIS.equalsIgnoreCase(((Identifier) variable.getName()).getName()));
 }