Java Code Examples for fr.adrienbrault.idea.symfony2plugin.util.PhpElementsUtil#isMethodReferenceInstanceOf()

The following examples show how to use fr.adrienbrault.idea.symfony2plugin.util.PhpElementsUtil#isMethodReferenceInstanceOf() . 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: ServiceDeprecatedClassesInspection.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
@Override
public void visitElement(PsiElement element) {
    MethodReference methodReference = PsiElementUtils.getMethodReferenceWithFirstStringParameter(element);
    if (methodReference == null || !PhpElementsUtil.isMethodReferenceInstanceOf(methodReference, ServiceContainerUtil.SERVICE_GET_SIGNATURES)) {
        super.visitElement(element);
        return;
    }

    PsiElement psiElement = element.getParent();
    if(!(psiElement instanceof StringLiteralExpression)) {
        super.visitElement(element);
        return;
    }

    String contents = ((StringLiteralExpression) psiElement).getContents();
    if(StringUtils.isNotBlank(contents)) {
        this.problemRegistrar.attachDeprecatedProblem(element, contents, holder);
        this.problemRegistrar.attachServiceDeprecatedProblem(element, contents, holder);
    }

    super.visitElement(element);
}
 
Example 2
Source File: FormOptionGotoCompletionRegistrar.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
@Nullable
@Override
public GotoCompletionProvider getProvider(@NotNull PsiElement psiElement) {
    if (!Symfony2ProjectComponent.isEnabled(psiElement)) {
        return null;
    }

    ArrayCreationExpression arrayCreationExpression = PhpElementsUtil.getCompletableArrayCreationElement(psiElement.getParent());
    if(arrayCreationExpression != null) {
        PsiElement parameterList = arrayCreationExpression.getParent();
        if (parameterList instanceof ParameterList) {
            PsiElement context = parameterList.getContext();
            if(context instanceof MethodReference) {
                ParameterBag currentIndex = PsiElementUtils.getCurrentParameterIndex(arrayCreationExpression);
                if(currentIndex != null && currentIndex.getIndex() == 2) {
                    if (PhpElementsUtil.isMethodReferenceInstanceOf((MethodReference) context, FormUtil.PHP_FORM_BUILDER_SIGNATURES)) {
                        return getMatchingOption((ParameterList) parameterList, psiElement);
                    }
                }
            }
        }
    }

    return null;
}
 
Example 3
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 4
Source File: ConstantEnumCompletionContributor.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
private void attachLookup(CompletionResultSet completionResultSet, MethodReference psiElement, ConstantEnumCompletionProvider enumProvider) {

        // we allow string values
        if(enumProvider.getEnumConstantFilter().getInstance() == null || enumProvider.getEnumConstantFilter().getField() == null) {
            return;
        }

        if(!PhpElementsUtil.isMethodReferenceInstanceOf(psiElement, enumProvider.getCallToSignature())) {
            return;
        }

        PhpClass phpClass = PhpElementsUtil.getClassInterface(psiElement.getProject(), enumProvider.getEnumConstantFilter().getInstance());
        if(phpClass == null) {
            return;
        }

        for(Field field: phpClass.getFields()) {
            if(field.isConstant() && field.getName().startsWith(enumProvider.getEnumConstantFilter().getField())) {
                completionResultSet.addElement(new PhpConstantFieldPhpLookupElement(field));
            }
        }

    }
 
Example 5
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 6
Source File: PhpTemplateMissingInspection.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
@Nullable
private String getTemplateNameIfMissing(@NotNull PsiElement psiElement) {
    MethodReference methodReference = PsiElementUtils.getMethodReferenceWithFirstStringParameter(psiElement);
    if (methodReference == null || !PhpElementsUtil.isMethodReferenceInstanceOf(methodReference, SymfonyPhpReferenceContributor.TEMPLATE_SIGNATURES)) {
        return null;
    }

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

    String templateName = PhpElementsUtil.getFirstArgumentStringValue(methodReference);
    if(templateName == null || StringUtils.isBlank(templateName)) {
        return null;
    }

    if(TwigUtil.getTemplateFiles(psiElement.getProject(), templateName).size() > 0) {
        return null;
    }

    return templateName;
}
 
Example 7
Source File: PhpTranslationDomainInspection.java    From idea-php-symfony2-plugin with MIT License 6 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;
    }

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

    ParameterBag currentIndex = PsiElementUtils.getCurrentParameterIndex(psiElement);
    if(currentIndex != null && currentIndex.getIndex() == domainParameter) {
        annotateTranslationDomain((StringLiteralExpression) psiElement, holder);
    }
}
 
Example 8
Source File: FormTypeAsClassConstantInspection.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
@Override
public void visitElement(PsiElement element) {
    if(!(element instanceof MethodReference) ||
        !ArrayUtil.contains(((MethodReference) element).getName(), "add", "create") ||
        !PhpElementsUtil.isMethodReferenceInstanceOf((MethodReference) element, "Symfony\\Component\\Form\\FormBuilderInterface")
        ) {

        super.visitElement(element);
        return;
    }

    PsiElement[] parameters = ((MethodReference) element).getParameters();
    if(parameters.length < 2) {
        super.visitElement(element);
        return;
    }

    if(!(parameters[1] instanceof StringLiteralExpression) || ((StringLiteralExpression) parameters[1]).getContents().contains("\\")) {
        super.visitElement(element);
        return;
    }

    holder.registerProblem(
        parameters[1],
        MESSAGE,
        ProblemHighlightType.WEAK_WARNING, new MyLocalQuickFix(parameters[1])
    );

    super.visitElement(element);
}
 
Example 9
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 10
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;
}
 
Example 11
Source File: ParameterLanguageInjector.java    From idea-php-symfony2-plugin with MIT License 4 votes vote down vote up
@Override
public void getLanguagesToInject(@NotNull MultiHostRegistrar registrar, @NotNull PsiElement element) {
    if (!(element instanceof StringLiteralExpression) || !((PsiLanguageInjectionHost) element).isValidHost()) {
        return;
    }
    if (!Symfony2ProjectComponent.isEnabled(element.getProject())) {
        return;
    }

    final StringLiteralExpressionImpl expr = (StringLiteralExpressionImpl) element;

    PsiElement parent = expr.getParent();

    final boolean isParameter = parent instanceof ParameterList && expr.getPrevPsiSibling() == null; // 1st parameter
    final boolean isAssignment = parent instanceof AssignmentExpression;

    if (!isParameter && !isAssignment) {
        return;
    }

    if (isParameter)  {
        parent = parent.getParent();
    }

    for (MethodLanguageInjection languageInjection : LANGUAGE_INJECTIONS) {
        // $crawler->filter('...')
        // $em->createQuery('...')
        // JsonResponse::fromJsonString('...')
        if (parent instanceof MethodReference) {
            if (PhpElementsUtil.isMethodReferenceInstanceOf((MethodReference) parent, languageInjection.getSignatures())) {
                injectLanguage(registrar, expr, languageInjection);
                return;
            }
        }
        // $dql = "...";
        else if (parent instanceof AssignmentExpression) {
            Language language = languageInjection.getLanguage();
            if (language != null && LANGUAGE_ID_DQL.equals(language.getID())) {
                PhpPsiElement variable = ((AssignmentExpression) parent).getVariable();
                if (variable instanceof Variable) {
                    if (DQL_VARIABLE_NAME.equals(variable.getName())) {
                        injectLanguage(registrar, expr, languageInjection);
                        return;
                    }
                }
            }
        }
    }

}
 
Example 12
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);
            }
        }
    }
}