コード例 #1
0
  public static List<Variable> getVariableReferencesInScope(
      final Variable variable, final boolean includeSelf) {

    final List<Variable> variables = new ArrayList<Variable>();

    Variable variableDecl = null;
    if (!variable.isDeclaration()) {
      PsiElement psiElement = variable.resolve();
      if (psiElement instanceof Variable) {
        variableDecl = (Variable) psiElement;
      }
    } else {
      variableDecl = variable;
    }

    if (variableDecl == null) {
      return variables;
    }

    Method method = PsiTreeUtil.getParentOfType(variable, Method.class);

    PhpPsiUtil.hasReferencesInSearchScope(
        method.getUseScope(),
        variableDecl,
        new CommonProcessors.FindProcessor<PsiReference>() {
          @Override
          protected boolean accept(PsiReference psiReference) {

            PsiElement variableRef = psiReference.getElement();
            if (variableRef instanceof Variable) {
              if (includeSelf) {
                variables.add((Variable) variableRef);
              } else {
                if (!variableRef.equals(variable)) {
                  variables.add((Variable) variableRef);
                }
              }
            }

            return false;
          }
        });

    return variables;
  }
コード例 #2
0
  /** Reads annotations and metadata and produces a cacheable map */
  private Map<String, Collection<String>> parseTypes(Project project) {
    Map<String, Collection<String>> map =
        new THashMap<String, Collection<String>>(); // parent -> known-key[]
    Collection<Variable> variables = getVariables(project, "CONFIG_KEYS");
    for (Variable variable : variables) {
      if (!NAMESPACE_NAME.equals(variable.getNamespaceName())) continue;
      PsiElement parent = variable.getParent();

      if (parent instanceof AssignmentExpression) {
        PhpPsiElement value = ((AssignmentExpression) parent).getValue();
        if (value instanceof ArrayCreationExpression) {
          Iterable<ArrayHashElement> elements = ((ArrayCreationExpression) value).getHashElements();
          parseTypes2(map, elements, "");
        }
      }
    }
    return map;
  }
コード例 #3
0
  /**
   * Find first variable declaration in parent scope of a given variable:
   *
   * <p>function() { $event = new FooEvent(); dispatch('foo', $event); }
   */
  @Nullable
  public static String getFirstVariableTypeInScope(@NotNull Variable variable) {

    // parent search scope, eg Method else fallback to a grouped statement
    PsiElement searchScope = PsiTreeUtil.getParentOfType(variable, Function.class);
    if (searchScope == null) {
      searchScope = PsiTreeUtil.getParentOfType(variable, GroupStatement.class);
    }

    if (searchScope == null) {
      return null;
    }

    final String name = variable.getName();
    if (name == null) {
      return null;
    }

    final String[] result = {null};
    searchScope.acceptChildren(
        new PsiRecursiveElementVisitor() {
          @Override
          public void visitElement(PsiElement element) {
            if (element instanceof Variable && name.equals(((Variable) element).getName())) {
              PsiElement assignmentExpression = element.getParent();
              if (assignmentExpression instanceof AssignmentExpression) {
                PhpPsiElement value = ((AssignmentExpression) assignmentExpression).getValue();
                if (value instanceof NewExpression) {
                  ClassReference classReference = ((NewExpression) value).getClassReference();
                  if (classReference != null) {
                    String classSignature = classReference.getFQN();
                    if (StringUtils.isNotBlank(classSignature)) {
                      result[0] = classSignature;
                    }
                  }
                }
              }
            }

            super.visitElement(element);
          }
        });

    return result[0];
  }