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

The following examples show how to use fr.adrienbrault.idea.symfony2plugin.util.PsiElementUtils#getChildrenOfType() . 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: DoctrineUtil.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
/**
 * Extract text: @Entity(repositoryClass="foo")
 */
@Nullable
public static String getAnnotationRepositoryClass(@NotNull PhpDocTag phpDocTag, @NotNull PhpClass phpClass) {
    PsiElement phpDocAttributeList = PsiElementUtils.getChildrenOfType(phpDocTag, PlatformPatterns.psiElement(PhpDocElementTypes.phpDocAttributeList));
    if(phpDocAttributeList == null) {
        return null;
    }

    // @TODO: use annotation plugin
    // repositoryClass="Foobar"
    String text = phpDocAttributeList.getText();

    String repositoryClass = EntityHelper.resolveDoctrineLikePropertyClass(
        phpClass,
        text,
        "repositoryClass",
        aVoid -> AnnotationUtil.getUseImportMap(phpDocTag)
    );

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

    return StringUtils.stripStart(repositoryClass, "\\");
}
 
Example 2
Source File: YamlCompletionContributor.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
private static void addYamlClassMethods(@Nullable PsiElement psiElement, CompletionResultSet completionResultSet, String classTag) {

        if(psiElement == null) {
            return;
        }

        YAMLKeyValue classKeyValue = PsiElementUtils.getChildrenOfType(psiElement, PlatformPatterns.psiElement(YAMLKeyValue.class).withName(classTag));
        if(classKeyValue == null) {
            return;
        }

        PhpClass phpClass = ServiceUtil.getResolvedClassDefinition(psiElement.getProject(), classKeyValue.getValueText());
        if(phpClass != null) {
            PhpElementsUtil.addClassPublicMethodCompletion(completionResultSet, phpClass);
        }
    }
 
Example 3
Source File: XmlServiceArgumentInspection.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
private void visitService(XmlTag xmlTag, @NotNull ProblemsHolder holder) {
    if(!ServiceActionUtil.isValidXmlParameterInspectionService(xmlTag)) {
        return;
    }

    final List<String> args = ServiceActionUtil.getXmlMissingArgumentTypes(xmlTag, false, getLazyServiceCollector(xmlTag));
    if (args.size() == 0) {
        return;
    }

    PsiElement childrenOfType = PsiElementUtils.getChildrenOfType(xmlTag, PlatformPatterns.psiElement(XmlTokenType.XML_NAME));
    if(childrenOfType == null) {
        return;
    }

    holder.registerProblem(childrenOfType, "Missing argument", ProblemHighlightType.GENERIC_ERROR_OR_WARNING, new AddServiceXmlArgumentLocalQuickFix(args));
}
 
Example 4
Source File: YamlHelper.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
@Nullable
public static YAMLKeyValue getYamlKeyValue(@Nullable PsiElement yamlCompoundValue, String keyName, boolean ignoreCase) {
    if (!(yamlCompoundValue instanceof YAMLMapping)) {
        return null;
    }

    if (!ignoreCase) {
        return ((YAMLMapping) yamlCompoundValue).getKeyValueByKey(keyName);
    }
    
    YAMLKeyValue classKeyValue;
    classKeyValue = PsiElementUtils.getChildrenOfType(yamlCompoundValue, PlatformPatterns.psiElement(YAMLKeyValue.class).withName(PlatformPatterns.string().oneOfIgnoreCase(keyName)));

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

    return classKeyValue;
}
 
Example 5
Source File: ShopwareBoostrapInspection.java    From idea-php-shopware-plugin with MIT License 5 votes vote down vote up
@NotNull
@Override
public PsiElementVisitor buildVisitor(final @NotNull ProblemsHolder holder, boolean isOnTheFly) {
    PsiFile psiFile = holder.getFile();
    if(!ShopwareProjectComponent.isValidForProject(psiFile)) {
        return super.buildVisitor(holder, isOnTheFly);
    }

    if(!"Bootstrap.php".equals(psiFile.getName())) {
        return super.buildVisitor(holder, isOnTheFly);
    }

    return new PsiElementVisitor() {
        @Override
        public void visitElement(PsiElement element) {
            if(element instanceof Method) {
                String methodName = ((Method) element).getName();
                if(INSTALL_METHODS.contains(((Method) element).getName())) {
                    if(PsiTreeUtil.collectElementsOfType(element, PhpReturn.class).size() == 0) {
                        PsiElement psiElement = PsiElementUtils.getChildrenOfType(element, PlatformPatterns.psiElement(PhpTokenTypes.IDENTIFIER).withText(methodName));
                        if(psiElement != null) {
                            holder.registerProblem(psiElement, "Shopware need return statement", ProblemHighlightType.GENERIC_ERROR);
                        }
                    }
                }

            }

            super.visitElement(element);
        }
    };
}
 
Example 6
Source File: FormOptionsUtil.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
private static void getMethodVars(Set<String> stringSet, Method method) {
    Collection<FieldReference> fieldReferences = PsiTreeUtil.collectElementsOfType(method, FieldReference.class);
    for(FieldReference fieldReference: fieldReferences) {
        PsiElement psiVar = PsiElementUtils.getChildrenOfType(fieldReference, PlatformPatterns.psiElement().withText("vars"));
        if(psiVar != null) {
            getFormViewVarsAttachKeys(stringSet, fieldReference);
        }

    }
}
 
Example 7
Source File: EventAnnotationStubIndex.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
private String findClassInstance(@NotNull PhpDocComment phpDocComment, @NotNull PhpDocTag phpDocTag) {
    PsiElement phpDocAttributeList = PsiElementUtils.getChildrenOfType(phpDocTag, PlatformPatterns.psiElement(PhpDocElementTypes.phpDocAttributeList));
    if(phpDocAttributeList instanceof PhpPsiElement) {
        PsiElement childrenOfType = PsiElementUtils.getChildrenOfType(phpDocAttributeList, PlatformPatterns.psiElement(PhpDocElementTypes.phpDocString));
        if(childrenOfType instanceof StringLiteralExpression) {
            String contents = StringUtils.stripStart(((StringLiteralExpression) childrenOfType).getContents(), "\\");
            if(StringUtils.isNotBlank(contents) && contents.length() < 350) {
                return contents;
            }
        }
    }

    return EventDispatcherUtil.extractEventClassInstance(phpDocComment.getText());
}
 
Example 8
Source File: AnnotationRouteElementWalkingVisitor.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
@Nullable
private String getClassRoutePattern(@NotNull PhpDocTag phpDocTag) {
    PhpClass phpClass = PsiTreeUtil.getParentOfType(phpDocTag, PhpClass.class);
    if(phpClass == null) {
        return null;
    }

    PhpDocComment docComment = phpClass.getDocComment();
    for (PhpDocTag docTag : PsiTreeUtil.getChildrenOfTypeAsList(docComment, PhpDocTag.class)) {
        String classNameReference = AnnotationBackportUtil.getClassNameReference(docTag, this.fileImports);
        if(classNameReference == null) {
            continue;
        }

        if(!RouteHelper.isRouteClassAnnotation(classNameReference)) {
            continue;
        }

        PsiElement docAttr = PsiElementUtils.getChildrenOfType(docTag, PlatformPatterns.psiElement(PhpDocElementTypes.phpDocAttributeList));
        if(!(docAttr instanceof PhpPsiElement)) {
            continue;
        }

        PhpPsiElement firstPsiChild = ((PhpPsiElement) docAttr).getFirstPsiChild();
        if(!(firstPsiChild instanceof StringLiteralExpression)) {
            continue;
        }

        String contents = ((StringLiteralExpression) firstPsiChild).getContents();
        if(StringUtils.isNotBlank(contents)) {
            return contents;
        }
    }

    return null;
}
 
Example 9
Source File: AnnotationRouteElementWalkingVisitor.java    From idea-php-symfony2-plugin with MIT License 4 votes vote down vote up
private void visitPhpDocTag(@NotNull PhpDocTag phpDocTag) {

        // "@var" and user non related tags dont need an action
        if(AnnotationBackportUtil.NON_ANNOTATION_TAGS.contains(phpDocTag.getName())) {
            return;
        }

        // init file imports
        if(this.fileImports == null) {
            this.fileImports = AnnotationBackportUtil.getUseImportMap(phpDocTag);
        }

        if(this.fileImports.size() == 0) {
            return;
        }

        String annotationFqnName = AnnotationBackportUtil.getClassNameReference(phpDocTag, this.fileImports);
        if(annotationFqnName == null || !RouteHelper.isRouteClassAnnotation(annotationFqnName)) {
            return;
        }

        PsiElement phpDocAttributeList = PsiElementUtils.getChildrenOfType(phpDocTag, PlatformPatterns.psiElement(PhpDocElementTypes.phpDocAttributeList));
        if(!(phpDocAttributeList instanceof PhpPsiElement)) {
            return;
        }

        String routeName = AnnotationUtil.getPropertyValue(phpDocTag, "name");
        if(routeName == null) {
            routeName = AnnotationBackportUtil.getRouteByMethod(phpDocTag);
        }

        if(routeName != null && StringUtils.isNotBlank(routeName)) {
            // prepend route name on PhpClass scope
            String routeNamePrefix = getRouteNamePrefix(phpDocTag);
            if(routeNamePrefix != null) {
                routeName = routeNamePrefix + routeName;
            }

            StubIndexedRoute route = new StubIndexedRoute(routeName);

            String path = "";

            // extract class path @Route("/foo") => "/foo" for prefixing upcoming methods
            String classPath = getClassRoutePattern(phpDocTag);
            if(classPath != null) {
                path += classPath;
            }

            // extract method path @Route("/foo") => "/foo"
            PhpPsiElement firstPsiChild = ((PhpPsiElement) phpDocAttributeList).getFirstPsiChild();
            if(firstPsiChild instanceof StringLiteralExpression) {
                String contents = ((StringLiteralExpression) firstPsiChild).getContents();
                if(StringUtils.isNotBlank(contents)) {
                    path += contents;
                }
            }

            if (path.length() > 0) {
                route.setPath(path);
            }

            route.setController(getController(phpDocTag));

            // @Method(...)
            extractMethods(phpDocTag, route);

            map.put(routeName, route);
        }
    }