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

The following examples show how to use fr.adrienbrault.idea.symfony2plugin.util.PsiElementUtils#getParameterIndexValue() . 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: FormGotoCompletionRegistrar.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
@Nullable
private GotoCompletionProvider createTranslationGotoCompletion(@NotNull PsiElement psiElement, @NotNull PsiElement arrayCreation) {
    int parameterIndexValue = PsiElementUtils.getParameterIndexValue(arrayCreation);
    if(parameterIndexValue != 2) {
        return null;
    }

    PsiElement parameterList = arrayCreation.getParent();
    if(parameterList instanceof ParameterList) {
        PsiElement methodReference = parameterList.getParent();
        if(methodReference instanceof MethodReference) {
            if(PhpElementsUtil.isMethodReferenceInstanceOf((MethodReference) methodReference, "\\Symfony\\Component\\Form\\FormBuilderInterface", "add") ||
                PhpElementsUtil.isMethodReferenceInstanceOf((MethodReference) methodReference, "\\Symfony\\Component\\Form\\FormBuilderInterface", "create")
                ) {
                return new TranslationGotoCompletionProvider(psiElement, extractTranslationDomainFromScope((ArrayCreationExpression) arrayCreation));
            }
        }
    }

    return null;
}
 
Example 2
Source File: PhpConfigReferenceContributor.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
private static boolean phpStringLiteralExpressionClassReference(String signature, int index, PsiElement psiElement) {

        if (!(psiElement.getContext() instanceof ParameterList)) {
            return false;
        }

        ParameterList parameterList = (ParameterList) psiElement.getContext();
        if (parameterList == null || !(parameterList.getContext() instanceof NewExpression)) {
            return false;
        }

        if(PsiElementUtils.getParameterIndexValue(psiElement) != index) {
            return false;
        }

        NewExpression newExpression = (NewExpression) parameterList.getContext();
        ClassReference classReference = newExpression.getClassReference();
        if(classReference == null) {
            return false;
        }

        return classReference.getSignature().equals("#C" + signature);
    }
 
Example 3
Source File: VoterUtil.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
private static void visitAttributeForeach(@NotNull Method method, @NotNull Consumer<Pair<String, PsiElement>> consumer) {
    Parameter[] parameters = method.getParameters();
    if(parameters.length < 3) {
        return;
    }

    for (Variable variable : PhpElementsUtil.getVariablesInScope(method, parameters[2])) {
        // foreach ($attributes as $attribute)
        PsiElement psiElement = PsiTreeUtil.nextVisibleLeaf(variable);
        if(psiElement != null && psiElement.getNode().getElementType() == PhpTokenTypes.kwAS) {
            PsiElement parent = variable.getParent();
            if(!(parent instanceof ForeachStatement)) {
                continue;
            }

            PhpPsiElement variableDecl = variable.getNextPsiSibling();
            if(variableDecl instanceof Variable) {
                for (Variable variable1 : PhpElementsUtil.getVariablesInScope(parent, (Variable) variableDecl)) {
                    visitVariable(variable1, consumer);
                }
            }
        }

        // in_array('foobar', $attributes)
        PsiElement parameterList = variable.getParent();
        if(parameterList instanceof ParameterList && PsiElementUtils.getParameterIndexValue(variable) == 1) {
            PsiElement functionCall = parameterList.getParent();
            if(functionCall instanceof FunctionReference && "in_array".equalsIgnoreCase(((FunctionReference) functionCall).getName())) {
                PsiElement[] functionParameter = ((ParameterList) parameterList).getParameters();
                if(functionParameter.length > 0) {
                    String stringValue = PhpElementsUtil.getStringValue(functionParameter[0]);
                    if(stringValue != null && StringUtils.isNotBlank(stringValue)) {
                        consumer.accept(Pair.create(stringValue, functionParameter[0]));
                    }
                }
            }
        }
    }
}
 
Example 4
Source File: ArrayValueWithKeyAndNewExpressionMatcher.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
@Nullable
private static Pair<NewExpressionCall, NewExpression> matchesMethodCall(@NotNull ArrayCreationExpression arrayCreationExpression, @NotNull NewExpressionCall[] classes) {

    // get parameter, we less then 0 we are not inside method reference
    int parameterIndex = PsiElementUtils.getParameterIndexValue(arrayCreationExpression);
    if(parameterIndex < 0) {
        return null;
    }

    // filter index
    List<NewExpressionCall> filter = ContainerUtil.filter(classes, expressionCall ->
        expressionCall.getIndex() == parameterIndex
    );

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

    PsiElement parameterList = arrayCreationExpression.getParent();
    if(!(parameterList instanceof ParameterList)) {
        return null;
    }

    PsiElement newExpression = parameterList.getParent();
    if (!(newExpression instanceof NewExpression)) {
        return null;
    }

    for (NewExpressionCall aClass : filter) {
        if(PhpElementsUtil.getNewExpressionPhpClassWithInstance((NewExpression) newExpression, aClass.getClazz()) == null) {
            continue;
        }

        return Pair.create(aClass, (NewExpression) newExpression);
    }

    return null;
}
 
Example 5
Source File: ArrayValueWithKeyAndMethodMatcher.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
@Nullable
private static Pair<PhpMethodReferenceCall, MethodReference> matchesMethodCall(@NotNull PsiElement psiFirstMethodReferenceChild, @NotNull PhpMethodReferenceCall[] methodCalls) {

    // get parameter, we less then 0 we are not inside method reference
    int parameterIndex = PsiElementUtils.getParameterIndexValue(psiFirstMethodReferenceChild);
    if(parameterIndex < 0) {
        return null;
    }

    PsiElement parameterList = psiFirstMethodReferenceChild.getParent();
    if(!(parameterList instanceof ParameterList)) {
        return null;
    }

    PsiElement methodReference = parameterList.getParent();
    if (!(methodReference instanceof MethodReference)) {
        return null;
    }

    for (PhpMethodReferenceCall methodCall : methodCalls) {
        if(parameterIndex != methodCall.getIndex() ||
            !ArrayUtils.contains(methodCall.getMethods(), ((MethodReference) methodReference).getName()) ||
            !PhpElementsUtil.isMethodReferenceInstanceOf((MethodReference) methodReference, methodCall.getClassName())
            )
        {
            continue;
        }

        return Pair.create(methodCall, (MethodReference) methodReference);
    }

    return null;
}