Java Code Examples for fr.adrienbrault.idea.symfony2plugin.util.MethodMatcher#MethodMatchParameter

The following examples show how to use fr.adrienbrault.idea.symfony2plugin.util.MethodMatcher#MethodMatchParameter . 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: ConfigUtil.java    From idea-php-shopware-plugin with MIT License 6 votes vote down vote up
/**
 * 
 */
@Nullable
public static String getNamespaceFromConfigValueParameter(@NotNull StringLiteralExpression parent) {
    MethodMatcher.MethodMatchParameter match = MethodMatcher.getMatchedSignatureWithDepth(parent, ShopwarePhpCompletion.CONFIG_NAMESPACE, 1);
    if(match == null) {
        return null;
    }

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

    PsiElement[] funcParameters = ((ParameterList) parameterList).getParameters();
    if(funcParameters.length == 0 || !(funcParameters[0] instanceof StringLiteralExpression)) {
        return null;
    }

    String namespace = ((StringLiteralExpression) funcParameters[0]).getContents();
    if(StringUtils.isBlank(namespace)) {
        return null;
    }

    return namespace;
}
 
Example 2
Source File: QueryBuilderGotoDeclarationHandler.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
private void attachPropertyGoto(StringLiteralExpression psiElement, List<PsiElement> targets) {

        MethodMatcher.MethodMatchParameter methodMatchParameter = MatcherUtil.matchPropertyField(psiElement);
        if(methodMatchParameter == null) {
            return;
        }

        QueryBuilderMethodReferenceParser qb = QueryBuilderCompletionContributor.getQueryBuilderParser(methodMatchParameter.getMethodReference());
        if(qb == null) {
            return;
        }

        QueryBuilderScopeContext collect = qb.collect();
        String propertyContent = psiElement.getContents();

        for(Map.Entry<String, QueryBuilderPropertyAlias> entry: collect.getPropertyAliasMap().entrySet()) {
            if(entry.getKey().equals(propertyContent)) {
                targets.addAll(entry.getValue().getPsiTargets());
            }
        }


    }
 
Example 3
Source File: QueryBuilderGotoDeclarationHandler.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
private void attachFromIndexGoto(StringLiteralExpression psiElement, List<PsiElement> targets) {

        MethodMatcher.MethodMatchParameter methodMatchParameter = new MethodMatcher.StringParameterMatcher(psiElement, 2)
            .withSignature("\\Doctrine\\ORM\\QueryBuilder", "from")
            .match();

        if(methodMatchParameter == null) {
            return;
        }

        QueryBuilderMethodReferenceParser qb = QueryBuilderCompletionContributor.getQueryBuilderParser(methodMatchParameter.getMethodReference());
        if(qb == null) {
            return;
        }

        QueryBuilderScopeContext collect = qb.collect();
        String propertyContent = psiElement.getContents();
        for(Map.Entry<String, QueryBuilderPropertyAlias> entry: collect.getPropertyAliasMap().entrySet()) {
            if(entry.getKey().equals(propertyContent)) {
                targets.addAll(entry.getValue().getPsiTargets());
            }
        }

    }
 
Example 4
Source File: DoctrineDbalQbGotoCompletionRegistrar.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
private boolean isTableNameRegistrar(PsiElement context) {

        MethodMatcher.MethodMatchParameter methodMatchParameter = new MethodMatcher.StringParameterRecursiveMatcher(context, 0)
            .withSignature("Doctrine\\DBAL\\Query\\QueryBuilder", "update")
            .withSignature("Doctrine\\DBAL\\Query\\QueryBuilder", "insert")
            .withSignature("Doctrine\\DBAL\\Query\\QueryBuilder", "from")
            .withSignature("Doctrine\\DBAL\\Query\\QueryBuilder", "delete")
            .withSignature("Doctrine\\DBAL\\Connection", "insert")
            .withSignature("Doctrine\\DBAL\\Connection", "update")
            .match();

        if(methodMatchParameter != null) {
            return true;
        }

        methodMatchParameter = new MethodMatcher.StringParameterRecursiveMatcher(context, 1)
            .withSignature("Doctrine\\DBAL\\Query\\QueryBuilder", "innerJoin")
            .withSignature("Doctrine\\DBAL\\Query\\QueryBuilder", "leftJoin")
            .withSignature("Doctrine\\DBAL\\Query\\QueryBuilder", "join")
            .withSignature("Doctrine\\DBAL\\Query\\QueryBuilder", "rightJoin")
            .match();

        return methodMatchParameter != null;

    }
 
Example 5
Source File: MatcherUtil.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
@Nullable
public static MethodMatcher.MethodMatchParameter matchPropertyField(PsiElement psiElement) {

    if (!Symfony2ProjectComponent.isEnabled(psiElement)) {
        return null;
    }

    MethodMatcher.MethodMatchParameter methodMatchParameter = new MethodMatcher.StringParameterAnyMatcher(psiElement)
        .withSignature(SELECT_FIELDS)
        .match();

    if(methodMatchParameter == null) {
        methodMatchParameter = new MethodMatcher.StringParameterAnyMatcher(psiElement)
            .withSignature(SELECT_FIELDS_VARIADIC)
            .match();
    }

    if(methodMatchParameter == null) {
        methodMatchParameter = new MethodMatcher.ArrayParameterMatcher(psiElement, 0)
            .withSignature(SELECT_FIELDS)
            .match();
    }

    return methodMatchParameter;
}
 
Example 6
Source File: PhpGoToHandler.java    From idea-php-shopware-plugin with MIT License 5 votes vote down vote up
private void attachNamespaceNavigation(@NotNull PsiElement psiElement, @NotNull List<PsiElement> psiElements) {
    PsiElement parent = psiElement.getParent();
    if(!(parent instanceof StringLiteralExpression)) {
        return;
    }

    String contents = ((StringLiteralExpression) parent).getContents();
    if(StringUtils.isBlank(contents)) {
        return;
    }

    MethodMatcher.MethodMatchParameter match = MethodMatcher.getMatchedSignatureWithDepth(parent, ShopwarePhpCompletion.CONFIG_NAMESPACE);
    if(match == null) {
        return;
    }

    ConfigUtil.visitNamespace(psiElement.getProject(), pair -> {
        if(contents.equalsIgnoreCase(pair.getFirst())) {
            PhpClass phpClass = pair.getSecond();
            PsiElement target = phpClass;

            // PhpClass or "Resources/config.xml" as target
            PsiDirectory pluginDir = phpClass.getContainingFile().getParent();
            if(pluginDir != null) {
                VirtualFile resources = VfsUtil.findRelativeFile(pluginDir.getVirtualFile(), "Resources", "config.xml");
                if(resources != null) {
                    PsiFile file = PsiManager.getInstance(phpClass.getProject()).findFile(resources);
                    if(file instanceof XmlFile) {
                        target = file;
                    }
                }
            }

            psiElements.add(target);
        }
    });
}
 
Example 7
Source File: QueryBuilderGotoDeclarationHandler.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
private void attachJoinGoto(StringLiteralExpression psiElement, List<PsiElement> targets) {

        MethodMatcher.MethodMatchParameter methodMatchParameter = MatcherUtil.matchJoin(psiElement);
        if(methodMatchParameter == null) {
            return;
        }

        QueryBuilderMethodReferenceParser qb = QueryBuilderCompletionContributor.getQueryBuilderParser(methodMatchParameter.getMethodReference());
        if(qb == null) {
            return;
        }

        String[] joinSplit = StringUtils.split(psiElement.getContents(), ".");
        if(joinSplit.length != 2) {
            return;
        }

        QueryBuilderScopeContext collect = qb.collect();
        if(!collect.getRelationMap().containsKey(joinSplit[0])) {
            return;
        }

        List<QueryBuilderRelation> relations = collect.getRelationMap().get(joinSplit[0]);
        for(QueryBuilderRelation relation: relations) {
            if(joinSplit[1].equals(relation.getFieldName()) && relation.getTargetEntity() != null) {
                PhpClass phpClass = PhpElementsUtil.getClassInterface(psiElement.getProject(), relation.getTargetEntity());
                if(phpClass != null) {
                    targets.add(phpClass);
                }

            }
        }


    }
 
Example 8
Source File: QueryBuilderGotoDeclarationHandler.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
private void attachExprGoto(StringLiteralExpression psiElement, List<PsiElement> targets) {


        MethodMatcher.MethodMatchParameter methodMatchParameter = new MethodMatcher.StringParameterMatcher(psiElement, 0)
            .withSignature(QueryBuilderCompletionContributor.EXPR)
            .match();

        if(methodMatchParameter == null) {
            return;
        }

        // simple resolve query inline instance usage
        // $qb->expr()->in('')
        MethodReference methodReference = methodMatchParameter.getMethodReference();
        PsiElement methodReferenceChild = methodReference.getFirstChild();
        if(!(methodReferenceChild instanceof MethodReference)) {
            return;
        }
        QueryBuilderMethodReferenceParser qb = QueryBuilderCompletionContributor.getQueryBuilderParser((MethodReference) methodReferenceChild);
        if(qb == null) {
            return;
        }

        String propertyContent = psiElement.getContents();
        QueryBuilderScopeContext collect = qb.collect();
        for(Map.Entry<String, QueryBuilderPropertyAlias> entry: collect.getPropertyAliasMap().entrySet()) {
            if(entry.getKey().equals(propertyContent)) {
                targets.addAll(entry.getValue().getPsiTargets());
            }
        }
    }
 
Example 9
Source File: MatcherUtil.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
@Nullable
public static MethodMatcher.MethodMatchParameter matchJoin(PsiElement psiElement) {
    return new MethodMatcher.StringParameterMatcher(psiElement, 0)
        .withSignature("\\Doctrine\\ORM\\QueryBuilder", "join")
        .withSignature("\\Doctrine\\ORM\\QueryBuilder", "leftJoin")
        .withSignature("\\Doctrine\\ORM\\QueryBuilder", "rightJoin")
        .withSignature("\\Doctrine\\ORM\\QueryBuilder", "innerJoin")
        .match();
}
 
Example 10
Source File: PhpRouteMissingInspection.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
private void invoke(@NotNull String routeName, @NotNull final PsiElement element, @NotNull ProblemsHolder holder) {
    MethodMatcher.MethodMatchParameter methodMatchParameter = new MethodMatcher.StringParameterMatcher(element, 0)
        .withSignature(PhpRouteReferenceContributor.GENERATOR_SIGNATURES)
        .match();

    if(methodMatchParameter == null) {
        return;
    }

    Collection<Route> route = RouteHelper.getRoute(element.getProject(), routeName);
    if(route.size() == 0) {
        holder.registerProblem(element, "Missing Route", ProblemHighlightType.GENERIC_ERROR_OR_WARNING);
    }
}
 
Example 11
Source File: ModelsControllerRelatedGotoCollector.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
@Override
public void collectGotoRelatedItems(ControllerActionGotoRelatedCollectorParameter parameter) {

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

    for(PsiElement psiElement: parameter.getParameterLists()) {
        MethodMatcher.MethodMatchParameter matchedSignature = MethodMatcher.getMatchedSignatureWithDepth(psiElement, SymfonyPhpReferenceContributor.REPOSITORY_SIGNATURES);
        if (matchedSignature != null) {
            String resolveString = PhpElementsUtil.getStringValue(psiElement);
            if(resolveString != null)  {
                for(PsiElement templateTarget: EntityHelper.getModelPsiTargets(parameter.getProject(), resolveString)) {

                    if(!uniqueTargets.contains(templateTarget)) {

                        uniqueTargets.add(templateTarget);
                        // we can provide targets to model config and direct class targets
                        if(templateTarget instanceof PsiFile) {
                            parameter.add(new RelatedPopupGotoLineMarker.PopupGotoRelatedItem(templateTarget, resolveString).withIcon(templateTarget.getIcon(0), Symfony2Icons.DOCTRINE_LINE_MARKER));
                        } else {
                            // @TODO: we can resolve for model types and provide icons, but not for now
                            parameter.add(new RelatedPopupGotoLineMarker.PopupGotoRelatedItem(templateTarget, resolveString).withIcon(Symfony2Icons.DOCTRINE, Symfony2Icons.DOCTRINE_LINE_MARKER));
                        }
                    }

                }
            }
        }
    }

}
 
Example 12
Source File: TranslationPlaceholderGotoCompletionRegistrar.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
@Nullable
@Override
public GotoCompletionProvider getProvider(@NotNull PsiElement psiElement) {
    PsiElement context = psiElement.getContext();
    if (!(context instanceof StringLiteralExpression)) {
        return null;
    }

    MethodMatcher.MethodMatchParameter match = new MethodMatcher.ArrayParameterMatcher(context, placeHolderParameter)
        .withSignature("Symfony\\Component\\Translation\\TranslatorInterface", method)
        .withSignature("Symfony\\Contracts\\Translation\\TranslatorInterface", method)
        .match();

    if (match == null) {
        return null;
    }

    PsiElement[] parameters = match.getMethodReference().getParameters();
    String key = PhpElementsUtil.getStringValue(parameters[0]);
    if(key == null) {
        return null;
    }

    String domain = "messages";
    if(parameters.length > domainParameter) {
        domain = PhpElementsUtil.getStringValue(parameters[domainParameter]);
        if(domain == null) {
            return null;
        }
    }

    return new MyTranslationPlaceholderGotoCompletionProvider(psiElement, key, domain);
}
 
Example 13
Source File: LazySubscriberReferenceProvider.java    From idea-php-shopware-plugin with MIT License 4 votes vote down vote up
@Nullable
@Override
public PsiElement[] getGotoDeclarationTargets(@Nullable PsiElement psiElement, int i, Editor editor) {
    if(psiElement == null || !ShopwareProjectComponent.isValidForProject(psiElement)) {
        return new PsiElement[0];
    }

    PsiElement context = psiElement.getContext();
    if(!(context instanceof StringLiteralExpression)) {
        return new PsiElement[0];
    }

    String hookNameContent = null;

    ArrayCreationExpression arrayCreationExpression = PhpElementsUtil.getCompletableArrayCreationElement(context);
    if(arrayCreationExpression != null) {

        PsiElement returnStatement = arrayCreationExpression.getParent();
        if(returnStatement instanceof PhpReturn) {
            Method method = PsiTreeUtil.getParentOfType(returnStatement, Method.class);
            if(method != null) {
                if("getSubscribedEvents".equals(method.getName())) {
                    PhpClass phpClass = method.getContainingClass();
                    if(phpClass != null && PhpElementsUtil.isInstanceOf(phpClass, "\\Enlight\\Event\\SubscriberInterface")) {
                        hookNameContent = ((StringLiteralExpression) context).getContents();
                    }
                }
            }
        }

    } else  {

        MethodMatcher.MethodMatchParameter match = new MethodMatcher.StringParameterMatcher(context, 0)
            .withSignature("\\Shopware_Components_Plugin_Bootstrap", "subscribeEvent")
            .match();

        if(match == null) {
            return new PsiElement[0];
        }

        hookNameContent = ((StringLiteralExpression) context).getContents();
    }

    if(hookNameContent == null) {
        return new PsiElement[0];
    }

    return getHookTargets(psiElement.getProject(), hookNameContent);
}