コード例 #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
  private static void attachOnDefaultOptions(
      Project project, HashMap<String, String> defaultValues, String typeClass) {

    PsiElement setDefaultOptions =
        PhpElementsUtil.getPsiElementsBySignatureSingle(
            project, "#M#C\\" + typeClass + ".setDefaultOptions");
    if (setDefaultOptions == null) {
      return;
    }

    Collection<MethodReference> tests =
        PsiTreeUtil.findChildrenOfType(setDefaultOptions, MethodReference.class);
    for (MethodReference methodReference : tests) {
      // instance check
      // methodReference.getSignature().equals("#M#C\\Symfony\\Component\\OptionsResolver\\OptionsResolverInterface.setDefaults")
      if (PhpElementsUtil.isEqualMethodReferenceName(methodReference, "setDefaults")) {
        PsiElement[] parameters = methodReference.getParameters();
        if (parameters.length > 0 && parameters[0] instanceof ArrayCreationExpression) {
          for (String key :
              PhpElementsUtil.getArrayCreationKeys((ArrayCreationExpression) parameters[0])) {
            defaultValues.put(key, typeClass);
          }
        }
      }

      // support: parent::setDefaultOptions($resolver)
      // Symfony\Component\Form\Extension\Core\Type\FormType:setDefaultOptions
      if (PhpElementsUtil.isEqualMethodReferenceName(methodReference, "setDefaultOptions")
          && methodReference.getReferenceType() == PhpModifier.State.PARENT) {
        PsiElement parentMethod =
            PhpElementsUtil.getPsiElementsBySignatureSingle(
                project, methodReference.getSignature());
        if (parentMethod instanceof Method) {
          PhpClass phpClass = ((Method) parentMethod).getContainingClass();
          if (phpClass != null) {
            attachOnDefaultOptions(project, defaultValues, phpClass.getPresentableFQN());
          }
        }
      }
    }
  }