コード例 #1
0
  /** $this->vars['test'] $view->vars = array_replace($view->vars, array(...)); */
  private static void getFormViewVarsAttachKeys(
      Set<String> stringSet, FieldReference fieldReference) {

    // $this->vars['test']
    PsiElement context = fieldReference.getContext();
    if (context instanceof ArrayAccessExpression) {
      ArrayIndex arrayIndex = PsiTreeUtil.findChildOfType(context, ArrayIndex.class);
      if (arrayIndex != null) {
        PsiElement psiElement = arrayIndex.getFirstChild();
        if (psiElement instanceof StringLiteralExpression) {
          String contents = ((StringLiteralExpression) psiElement).getContents();
          if (StringUtils.isNotBlank(contents)) {
            stringSet.add(contents);
          }
        }
      }
    }

    // array_replace($view->vars, array(...))
    if (context instanceof ParameterList) {
      PsiElement functionReference = context.getContext();
      if (functionReference instanceof FunctionReference
          && "array_replace".equals(((FunctionReference) functionReference).getName())) {
        PsiElement[] psiElements = ((ParameterList) context).getParameters();
        if (psiElements.length > 1) {
          if (psiElements[1] instanceof ArrayCreationExpression) {
            stringSet.addAll(
                PhpElementsUtil.getArrayCreationKeys((ArrayCreationExpression) psiElements[1]));
          }
        }
      }
    }
  }
コード例 #2
0
  /**
   * Find a string return value of a method context "function() { return 'foo'}" First match wins
   */
  @Nullable
  public static String getMethodReturnAsString(
      @NotNull PhpClass phpClass, @NotNull String methodName) {

    Method method = phpClass.findMethodByName(methodName);
    if (method == null) {
      return null;
    }

    final Set<String> values = new HashSet<String>();
    method.acceptChildren(
        new PsiRecursiveElementWalkingVisitor() {
          @Override
          public void visitElement(PsiElement element) {

            if (PhpElementsUtil.getMethodReturnPattern().accepts(element)) {
              String value = PhpElementsUtil.getStringValue(element);
              if (value != null && StringUtils.isNotBlank(value)) {
                values.add(value);
              }
            }

            super.visitElement(element);
          }
        });

    if (values.size() == 0) {
      return null;
    }

    // we support only first item
    return values.iterator().next();
  }