Java Code Examples for fr.adrienbrault.idea.symfony2plugin.util.PsiElementUtils#getMethodParameterPsiElementAt()

The following examples show how to use fr.adrienbrault.idea.symfony2plugin.util.PsiElementUtils#getMethodParameterPsiElementAt() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FormOptionGotoCompletionRegistrar.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
private GotoCompletionProvider getMatchingOption(ParameterList parameterList, @NotNull PsiElement psiElement) {
    // form name can be a string alias; also resolve on constants, properties, ...
    PsiElement psiElementAt = PsiElementUtils.getMethodParameterPsiElementAt(parameterList, 1);

    Set<String> formTypeNames = new HashSet<>();
    if(psiElementAt != null) {
        PhpClass phpClass = FormUtil.getFormTypeClassOnParameter(psiElementAt);
        if(phpClass != null) {
            formTypeNames.add(phpClass.getFQN());
        }
    }

    // fallback to form
    if(formTypeNames.size() == 0) {
        formTypeNames.add("form"); // old Symfony systems
        formTypeNames.add("Symfony\\Component\\Form\\Extension\\Core\\Type\\FormType");
    }

    return new FormReferenceCompletionProvider(psiElement, formTypeNames);
}
 
Example 2
Source File: FormFieldResolver.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
private static void attachFormFields(@Nullable MethodReference methodReference, @NotNull Collection<TwigTypeContainer> targets) {
    if(methodReference != null && PhpElementsUtil.isMethodReferenceInstanceOf(
        methodReference,
        new MethodMatcher.CallToSignature("\\Symfony\\Bundle\\FrameworkBundle\\Controller\\Controller", "createForm"),
        new MethodMatcher.CallToSignature("\\Symfony\\Bundle\\FrameworkBundle\\Controller\\ControllerTrait", "createForm"),
        new MethodMatcher.CallToSignature("\\Symfony\\Bundle\\FrameworkBundle\\Controller\\AbstractController", "createForm")
    )) {
        PsiElement formType = PsiElementUtils.getMethodParameterPsiElementAt(methodReference, 0);
        if(formType != null) {
            PhpClass phpClass = FormUtil.getFormTypeClassOnParameter(formType);

            if(phpClass == null) {
                return;
            }

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

            targets.addAll(getTwigTypeContainer(method));
        }
    }
}
 
Example 3
Source File: FormFieldResolver.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
private static List<TwigTypeContainer> getTwigTypeContainer(Method method) {
    MethodReference[] formBuilderTypes = FormUtil.getFormBuilderTypes(method);
    List<TwigTypeContainer> twigTypeContainers = new ArrayList<>();

    for(MethodReference methodReference: formBuilderTypes) {

        String fieldName = PsiElementUtils.getMethodParameterAt(methodReference, 0);
        PsiElement psiElement = PsiElementUtils.getMethodParameterPsiElementAt(methodReference, 1);
        TwigTypeContainer twigTypeContainer = new TwigTypeContainer(fieldName);

        // find form field type
        if(psiElement != null) {
            PhpClass phpClass = FormUtil.getFormTypeClassOnParameter(psiElement);
            if(phpClass != null) {
                twigTypeContainer.withDataHolder(new FormDataHolder(phpClass));
            }
        }

        twigTypeContainers.add(twigTypeContainer);
    }

    return twigTypeContainers;
}
 
Example 4
Source File: QueryBuilderMethodReferenceParser.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
private void collectSelectInForm(QueryBuilderScopeContext qb, MethodReference methodReference, String name) {

        // $qb->from('foo', 'select')
        if(!"from".equals(name)) {
            return;
        }

        PsiElement psiElement = PsiElementUtils.getMethodParameterPsiElementAt(methodReference, 1);
        String literalValue = PhpElementsUtil.getStringValue(psiElement);
        if(literalValue != null) {
            qb.addSelect(literalValue);
        }

    }
 
Example 5
Source File: QueryBuilderMethodReferenceParser.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
private void collectSelects(QueryBuilderScopeContext qb, MethodReference methodReference, String name) {

        if(!Arrays.asList("select", "addSelect").contains(name)) {
            return;
        }

        // $qb->select('foo')
        // $qb->select('foo', 'foo1')
        for(PsiElement parameter: methodReference.getParameters()) {
            String literalValue = PhpElementsUtil.getStringValue(parameter);
            if(literalValue != null) {
                qb.addSelect(literalValue);
            }
        }

        // $qb->select(array('foo', 'bar', 'accessoryDetail'))
        PsiElement psiElement = PsiElementUtils.getMethodParameterPsiElementAt(methodReference, 0);
        if(psiElement instanceof ArrayCreationExpression) {
            for(PsiElement arrayValue: PsiElementUtils.getChildrenOfTypeAsList(psiElement, PlatformPatterns.psiElement(PhpElementTypes.ARRAY_VALUE))) {
                if(arrayValue.getChildren().length == 1) {
                    String arrayValueString = PhpElementsUtil.getStringValue(arrayValue.getChildren()[0]);
                    if(arrayValueString != null) {
                        qb.addSelect(arrayValueString);
                    }
                }
            }
        }

    }
 
Example 6
Source File: FormUtil.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
private static PhpClass getFormTypeClass(@Nullable MethodReference calledMethodReference) {
    if(calledMethodReference == null || !PhpElementsUtil.isMethodReferenceInstanceOf(calledMethodReference, "\\Symfony\\Component\\Form\\FormFactory", "create")) {
        return null;
    }

    // $form = "$this->createForm("new Type()", $entity)";
    PsiElement formType = PsiElementUtils.getMethodParameterPsiElementAt(calledMethodReference, 0);
    if(formType == null) {
        return null;
    }

    return getFormTypeClassOnParameter(formType);
}
 
Example 7
Source File: PhpMethodVariableResolveUtil.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
/**
 *  search for variables which are possible accessible inside rendered twig template
 */
@NotNull
private static List<PsiElement> collectPossibleTemplateArrays(@NotNull Function method) {

    List<PsiElement> collectedTemplateVariables = new ArrayList<>();

    // Annotation controller
    // @TODO: check for phpdoc tag
    for(PhpReturn phpReturn : PsiTreeUtil.findChildrenOfType(method, PhpReturn.class)) {
        PhpPsiElement returnPsiElement = phpReturn.getFirstPsiChild();

        // @TODO: think of support all types here
        // return $template
        // return array('foo' => $var)
        if(returnPsiElement instanceof Variable || returnPsiElement instanceof ArrayCreationExpression) {
            collectedTemplateVariables.add(returnPsiElement);
        }

    }

    // twig render calls:
    // $twig->render('foo', $vars);
    Set<FunctionReference> references = new HashSet<>();
    visitRenderTemplateFunctions(method, triple -> {
        FunctionReference functionScope = triple.getThird();
        if (functionScope != null) {
            references.add(functionScope);
        }
    });

    for(FunctionReference methodReference : references) {
        PsiElement templateParameter = PsiElementUtils.getMethodParameterPsiElementAt((methodReference).getParameterList(), 1);
        if(templateParameter != null) {
            collectedTemplateVariables.add(templateParameter);
        }
    }

    return collectedTemplateVariables;
}
 
Example 8
Source File: PhpTranslationKeyInspection.java    From idea-php-symfony2-plugin with MIT License 4 votes vote down vote up
private void invoke(@NotNull ProblemsHolder holder, @NotNull PsiElement psiElement) {
    if (!(psiElement instanceof StringLiteralExpression) || !(psiElement.getContext() instanceof ParameterList)) {
        return;
    }

    ParameterList parameterList = (ParameterList) psiElement.getContext();
    PsiElement methodReference = parameterList.getContext();
    if (!(methodReference instanceof MethodReference)) {
        return;
    }

    if (!PhpElementsUtil.isMethodReferenceInstanceOf((MethodReference) methodReference, TranslationUtil.PHP_TRANSLATION_SIGNATURES)) {
        return;
    }

    ParameterBag currentIndex = PsiElementUtils.getCurrentParameterIndex(psiElement);
    if(currentIndex == null || currentIndex.getIndex() != 0) {
        return;
    }

    int domainParameter = 2;
    if("transChoice".equals(((MethodReference) methodReference).getName())) {
        domainParameter = 3;
    }

    PsiElement domainElement = PsiElementUtils.getMethodParameterPsiElementAt(parameterList, domainParameter);

    if(domainElement == null) {
        // no domain found; fallback to default domain
        annotateTranslationKey((StringLiteralExpression) psiElement, "messages", holder);
    } else {
        // resolve string in parameter
        PsiElement[] parameters = parameterList.getParameters();
        if(parameters.length >= domainParameter) {
            String domain = PhpElementsUtil.getStringValue(parameters[domainParameter]);
            if(domain != null) {
                annotateTranslationKey((StringLiteralExpression) psiElement, domain, holder);
            }
        }
    }
}