/**
   * @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.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;
    }
    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;
  }
  private static boolean checkGlobalVariable(Variable locateNode) {
    assert locateNode.getType() == ASTNode.VARIABLE;

    if (locateNode.getParent().getType() == ASTNode.STATIC_FIELD_ACCESS) {
      return false;
    }
    return locateNode.getParent().getType() == ASTNode.GLOBAL_STATEMENT // Global
        // $a
        // case
        || (locateNode.getEnclosingBodyNode() != null
            && locateNode.getEnclosingBodyNode().getType() == ASTNode.PROGRAM); // $a
    // declared
    // in
    // global
    // scope
    // case
  }
  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;
  }
 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);
     }
   }
 }
  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;
  }
 private static final boolean isThisVariable(Variable variable) {
   return (variable.isDollared()
       && variable.getName() instanceof Identifier
       && THIS.equalsIgnoreCase(((Identifier) variable.getName()).getName()));
 }