com.jetbrains.php.lang.documentation.phpdoc.psi.PhpDocComment Java Examples

The following examples show how to use com.jetbrains.php.lang.documentation.phpdoc.psi.PhpDocComment. 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: AnnotationBackportUtil.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
public static boolean hasReference(@Nullable PhpDocComment docComment, String... className) {
    if(docComment == null) return false;

    Map<String, String> uses = AnnotationBackportUtil.getUseImportMap(docComment);

    for(PhpDocTag phpDocTag: PsiTreeUtil.findChildrenOfAnyType(docComment, PhpDocTag.class)) {
        if(!AnnotationBackportUtil.NON_ANNOTATION_TAGS.contains(phpDocTag.getName())) {
            PhpClass annotationReference = AnnotationBackportUtil.getAnnotationReference(phpDocTag, uses);
            if(annotationReference != null && PhpElementsUtil.isEqualClassName(annotationReference, className)) {
                return true;
            }
        }

    }

    return false;
}
 
Example #2
Source File: PhpElementsUtil.java    From idea-php-annotation-plugin with MIT License 6 votes vote down vote up
@Nullable
public static String getClassDeprecatedMessage(@NotNull PhpClass phpClass) {
    if (phpClass.isDeprecated()) {
        PhpDocComment docComment = phpClass.getDocComment();
        if (docComment != null) {
            for (PhpDocTag deprecatedTag : docComment.getTagElementsByName("@deprecated")) {
                String tagValue = deprecatedTag.getTagValue();
                if (StringUtils.isNotBlank(tagValue)) {
                    return StringUtils.abbreviate("Deprecated: " + tagValue, 100);
                }
            }
        }
    }

    return null;
}
 
Example #3
Source File: AnnotationUtil.java    From idea-php-annotation-plugin with MIT License 6 votes vote down vote up
@Nullable
public static PhpDocCommentAnnotation getPhpDocCommentAnnotationContainer(@Nullable PhpDocComment phpDocComment) {
    if(phpDocComment == null) {
        return null;
    }

    Map<String, String> uses = AnnotationUtil.getUseImportMap((PsiElement) phpDocComment);

    Map<String, PhpDocTagAnnotation> annotationRefsMap = new HashMap<>();
    for(PhpDocTag phpDocTag: PsiTreeUtil.findChildrenOfType(phpDocComment, PhpDocTag.class)) {
        if(!AnnotationUtil.isBlockedAnnotationTag(phpDocTag.getName())) {
            PhpClass annotationClass = AnnotationUtil.getAnnotationReference(phpDocTag, uses);
            if(annotationClass != null) {
                annotationRefsMap.put(annotationClass.getPresentableFQN(), new PhpDocTagAnnotation(annotationClass, phpDocTag));
            }
        }

    }

    return new PhpDocCommentAnnotation(annotationRefsMap, phpDocComment);
}
 
Example #4
Source File: AnnotationCompletionContributor.java    From idea-php-annotation-plugin with MIT License 6 votes vote down vote up
@Override
protected void addCompletions(@NotNull CompletionParameters completionParameters, @NotNull ProcessingContext processingContext, @NotNull CompletionResultSet completionResultSet) {
    PsiElement psiElement = completionParameters.getOriginalPosition();
    if(psiElement == null) {
        return;
    }

    PhpDocComment parentOfType = PsiTreeUtil.getParentOfType(psiElement, PhpDocComment.class);
    if(parentOfType == null) {
        return;
    }

    AnnotationTarget annotationTarget = PhpElementsUtil.findAnnotationTarget(parentOfType);
    if(annotationTarget == null) {
        return;
    }

    Map<String, String> importMap = AnnotationUtil.getUseImportMap((PsiElement) parentOfType);

    Project project = completionParameters.getPosition().getProject();
    attachLookupElements(project, importMap , annotationTarget, completionResultSet);

}
 
Example #5
Source File: ColumnNameCompletionProvider.java    From idea-php-annotation-plugin with MIT License 6 votes vote down vote up
@Override
public void getPropertyValueCompletions(AnnotationPropertyParameter annotationPropertyParameter, AnnotationCompletionProviderParameter completionParameter) {

    String propertyName = annotationPropertyParameter.getPropertyName();
    if(propertyName == null) {
        return;
    }

    if(propertyName.equals("name") && PhpLangUtil.equalsClassNames(annotationPropertyParameter.getPhpClass().getPresentableFQN(), "Doctrine\\ORM\\Mapping\\Column")) {
        PhpDocComment phpDocComment = PsiTreeUtil.getParentOfType(annotationPropertyParameter.getElement(), PhpDocComment.class);
        if(phpDocComment != null) {
            PhpPsiElement classField = phpDocComment.getNextPsiSibling();
            if(classField != null && classField.getNode().getElementType() == PhpElementTypes.CLASS_FIELDS) {
                Field field = PsiTreeUtil.getChildOfType(classField, Field.class);
                if(field != null) {
                    String name = field.getName();
                    if(StringUtils.isNotBlank(name)) {
                        completionParameter.getResult().addElement(LookupElementBuilder.create(underscore(name)));
                    }
                }
            }
        }
    }

}
 
Example #6
Source File: AnnotationBackportUtil.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
public static PhpDocTag getReference(@Nullable PhpDocComment docComment, String className) {
    if(docComment == null) return null;

    Map<String, String> uses = AnnotationBackportUtil.getUseImportMap(docComment);

    for(PhpDocTag phpDocTag: PsiTreeUtil.findChildrenOfAnyType(docComment, PhpDocTag.class)) {
        if(AnnotationBackportUtil.NON_ANNOTATION_TAGS.contains(phpDocTag.getName())) {
            continue;
        }

        PhpClass annotationReference = AnnotationBackportUtil.getAnnotationReference(phpDocTag, uses);
        if(annotationReference != null && PhpElementsUtil.isEqualClassName(annotationReference, className)) {
            return phpDocTag;
        }
    }

    return null;
}
 
Example #7
Source File: AnnotationBackportUtil.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
@NotNull
public static Map<String, String> getUseImportMap(@NotNull PhpDocComment phpDocComment) {

    // search for use alias in local file
    final Map<String, String> useImports = new HashMap<>();

    PhpPsiElement scope = PhpCodeInsightUtil.findScopeForUseOperator(phpDocComment);
    if(scope == null) {
        return useImports;
    }

    for (PhpUseList phpUseList : PhpCodeInsightUtil.collectImports(scope)) {
        for (PhpUse phpUse : phpUseList.getDeclarations()) {
            String alias = phpUse.getAliasName();
            if (alias != null) {
                useImports.put(alias, phpUse.getFQN());
            } else {
                useImports.put(phpUse.getName(), phpUse.getFQN());
            }
        }
    }

    return useImports;
}
 
Example #8
Source File: TwigUtil.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
/**
 * Get templates on "@Template()" and on method attached to given PhpDocTag
 */
@NotNull
public static Map<String, Collection<PsiElement>> getTemplateAnnotationFilesWithSiblingMethod(@NotNull PhpDocTag phpDocTag) {
    Map<String, Collection<PsiElement>> targets = new HashMap<>();

    // template on direct PhpDocTag
    Pair<String, Collection<PsiElement>> templateAnnotationFiles = TwigUtil.getTemplateAnnotationFiles(phpDocTag);
    if(templateAnnotationFiles != null) {
        targets.put(templateAnnotationFiles.getFirst(), templateAnnotationFiles.getSecond());
    }

    // templates on "Method" of PhpDocTag
    PhpDocComment phpDocComment = PsiTreeUtil.getParentOfType(phpDocTag, PhpDocComment.class);
    if(phpDocComment != null) {
        PsiElement method = phpDocComment.getNextPsiSibling();
        if(method instanceof Method) {
            for (String name : TwigUtil.getControllerMethodShortcut((Method) method)) {
                targets.put(name, new HashSet<>(getTemplatePsiElements(method.getProject(), name)));
            }
        }
    }

    return targets;
}
 
Example #9
Source File: GenericsUtil.java    From idea-php-generics-plugin with MIT License 6 votes vote down vote up
/**
 * - "@return array{optional?: string, bar: int}"
 * - "@return array{foo: string, bar: int}"
 * - "@psalm-param array{foo: string, bar: int}"
 */
@NotNull
public static Collection<ParameterArrayType> getReturnArrayTypes(@NotNull PhpNamedElement phpNamedElement) {
    PhpDocComment docComment = phpNamedElement.getDocComment();
    if (docComment == null) {
        return Collections.emptyList();
    }

    Collection<ParameterArrayType> types = new ArrayList<>();

    // workaround for invalid tags lexer on PhpStorm side
    for (PhpDocTag phpDocTag : getTagElementsByNameForAllFrameworks(docComment, "return")) {
        String text = phpDocTag.getText();
        Matcher arrayElementsMatcher = Pattern.compile("array\\s*\\{(.*)}\\s*", Pattern.MULTILINE).matcher(text);
        if (arrayElementsMatcher.find()) {
            String group = arrayElementsMatcher.group(1);
            types.addAll(GenericsUtil.getParameterArrayTypes(group, phpDocTag));
        }
    }

    return types;
}
 
Example #10
Source File: GenericsUtil.java    From idea-php-generics-plugin with MIT License 6 votes vote down vote up
@NotNull
private static Map<String, String> getUseImportMap(@Nullable PhpDocComment phpDocComment) {
    if(phpDocComment == null) {
        return Collections.emptyMap();
    }

    PhpPsiElement scope = PhpCodeInsightUtil.findScopeForUseOperator(phpDocComment);
    if(scope == null) {
        return Collections.emptyMap();
    }

    Map<String, String> useImports = new HashMap<>();

    for (PhpUseList phpUseList : PhpCodeInsightUtil.collectImports(scope)) {
        for(PhpUse phpUse : phpUseList.getDeclarations()) {
            String alias = phpUse.getAliasName();
            if (alias != null) {
                useImports.put(alias, phpUse.getFQN());
            } else {
                useImports.put(phpUse.getName(), phpUse.getFQN());
            }
        }
    }

    return useImports;
}
 
Example #11
Source File: GenericsUtilTest.java    From idea-php-generics-plugin with MIT License 6 votes vote down vote up
public void testThatParamArrayElementsAreExtracted() {
    PhpDocComment phpDocComment = PhpPsiElementFactory.createPhpPsiFromText(getProject(), PhpDocComment.class, "" +
        "/**\n" +
        "* @psalm-param array{foo: Foo, ?bar: int | string} $foobar\n" +
        "*/\n" +
        "function test($foobar) {}\n"
    );

    PhpDocTag[] tagElementsByName = phpDocComment.getTagElementsByName("@psalm-param");
    String tagValue = tagElementsByName[0].getTagValue();

    Collection<ParameterArrayType> vars = GenericsUtil.getParameterArrayTypes(tagValue, "foobar", tagElementsByName[0]);

    ParameterArrayType foo = Objects.requireNonNull(vars).stream().filter(parameterArrayType -> parameterArrayType.getKey().equalsIgnoreCase("foo")).findFirst().get();
    assertFalse(foo.isOptional());
    assertContainsElements(Collections.singletonList("Foo"), foo.getValues());

    ParameterArrayType foobar = Objects.requireNonNull(vars).stream().filter(parameterArrayType -> parameterArrayType.getKey().equalsIgnoreCase("bar")).findFirst().get();
    assertTrue(foobar.isOptional());
    assertContainsElements(Arrays.asList("string", "int"), foobar.getValues());
}
 
Example #12
Source File: GenericsUtil.java    From idea-php-generics-plugin with MIT License 5 votes vote down vote up
public static boolean isGenericsClass(@NotNull PhpClass phpClass) {
    PhpDocComment phpDocComment = phpClass.getDocComment();
    if(phpDocComment != null) {
        // "@template T"
        // "@psalm-template Foo"
        for (PhpDocTag phpDocTag : getTagElementsByNameForAllFrameworks(phpDocComment, "template")) {
            String tagValue = phpDocTag.getTagValue();
            if (StringUtils.isNotBlank(tagValue) && tagValue.matches("\\w+")) {
                return true;
            }
        }
    }

    return false;
}
 
Example #13
Source File: GenericsUtil.java    From idea-php-generics-plugin with MIT License 5 votes vote down vote up
@NotNull
public static PhpDocTag[] getTagElementsByNameForAllFrameworks(@NotNull PhpDocComment phpDocComment, @NotNull String parameterName) {
    return Stream.of(
        phpDocComment.getTagElementsByName("@psalm-" + parameterName),
        phpDocComment.getTagElementsByName("@" + parameterName),
        phpDocComment.getTagElementsByName("@phpstan-" + parameterName)
    ).flatMap(Stream::of).toArray(PhpDocTag[]::new);
}
 
Example #14
Source File: TemplateAnnotationIndex.java    From idea-php-generics-plugin with MIT License 5 votes vote down vote up
private void visitPhpClass(@NotNull PhpClass phpClass) {
    String fqn = phpClass.getFQN();
    if(!fqn.startsWith("\\")) {
        fqn = "\\" + fqn;
    }

    PhpDocComment phpDocComment = phpClass.getDocComment();
    if (phpDocComment != null) {
        Map<String, String> useImportMap = null;
        for (PhpDocTag phpDocTag : GenericsUtil.getTagElementsByNameForAllFrameworks(phpDocComment, "extends")) {
            String tagValue = phpDocTag.getTagValue();

            Matcher matcher = CLASS_EXTENDS_MATCHER.matcher(tagValue);
            if (!matcher.find()) {
                continue;
            }

            String extendsClass = matcher.group(1);
            String type = matcher.group(2);

            // init the imports scope; to be only loaded once
            if (useImportMap == null) {
                useImportMap = AnnotationUtil.getUseImportMap(phpDocComment);
            }

            // resolve the class name based on the scope of namespace and use statement
            // eg: "@template BarAlias\MyContainer<Bar\Foobar>" we need global namespaces starting with "\"
            extendsClass = GenericsUtil.getFqnClassNameFromScope(fqn, extendsClass, useImportMap);
            type = GenericsUtil.getFqnClassNameFromScope(fqn, type, useImportMap);

            map.put(fqn, new TemplateAnnotationUsage(
                fqn,
                TemplateAnnotationUsage.Type.EXTENDS,
                0,
                extendsClass + "::" + type
            ));
        }
    }

}
 
Example #15
Source File: AnnotationBackportUtil.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
@Nullable
public static Method getMethodScope(@NotNull PhpDocTag phpDocTag) {
    PhpDocComment parentOfType = PsiTreeUtil.getParentOfType(phpDocTag, PhpDocComment.class);
    if(parentOfType == null) {
        return null;
    }

    PhpPsiElement method = parentOfType.getNextPsiSibling();
    if(!(method instanceof Method)) {
        return null;
    }

    return (Method) method;
}
 
Example #16
Source File: AnnotationBackportUtil.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
/**
 * Generate a full FQN class name out of a given short class name with respecting current namespace and use scope
 *
 * - "Foobar" needs to have its use statement attached
 * - No use statement match its on the same namespace as the class
 *
 * TODO: find a core function for this
 *
 * @param shortClassName Foobar
 */
@NotNull
public static String getFqnClassNameFromScope(@NotNull PhpClass phpClass, @NotNull String shortClassName) {
    // @TODO: better scope? we dont need a doc comment
    PhpDocComment docComment = phpClass.getDocComment();
    if (docComment == null) {
        return shortClassName;
    }

    return getFqnClassNameFromScope(phpClass, shortClassName, AnnotationUtil.getUseImportMap(docComment));
}
 
Example #17
Source File: DoctrineUtil.java    From idea-php-annotation-plugin with MIT License 5 votes vote down vote up
public static boolean isOrmColumnProperty(@NotNull Field field) {
    PhpDocComment docComment = field.getDocComment();
    if(docComment == null) {
        return false;
    }

    PhpDocCommentAnnotation container = AnnotationUtil.getPhpDocCommentAnnotationContainer(docComment);
    return container != null
        && (container.getPhpDocBlock("Doctrine\\ORM\\Mapping\\Column") != null || container.getPhpDocBlock("Doctrine\\ORM\\Mapping\\JoinColumn") != null);
}
 
Example #18
Source File: DoctrineUtil.java    From idea-php-annotation-plugin with MIT License 5 votes vote down vote up
@Nullable
public static PhpDocTagAnnotation getOrmEntityPhpDocBlock(@NotNull PhpClass phpClass) {
    PhpDocComment docComment = phpClass.getDocComment();
    if(docComment == null) {
        return null;
    }

    PhpDocCommentAnnotation container = AnnotationUtil.getPhpDocCommentAnnotationContainer(docComment);
    if(container == null) {
        return null;
    }

    return container.getPhpDocBlock("Doctrine\\ORM\\Mapping\\Entity");
}
 
Example #19
Source File: DoctrineUtil.java    From idea-php-annotation-plugin with MIT License 5 votes vote down vote up
public static boolean repositoryClassExists(PhpDocTag phpDocTag)
{
    StringLiteralExpression repositoryClass = AnnotationUtil.getPropertyValueAsPsiElement(phpDocTag, "repositoryClass");
    if(repositoryClass == null || StringUtils.isBlank(repositoryClass.getContents())) {
        return false;
    }

    PhpClass phpClass = PhpElementsUtil.getClassInsideAnnotation(repositoryClass, repositoryClass.getContents());
    if(phpClass != null) {
        return true;
    }

    PhpDocComment phpDocComment = PsiTreeUtil.getParentOfType(repositoryClass, PhpDocComment.class);
    if(phpDocComment == null) {
        return false;
    }

    PhpPsiElement phpClassContext = phpDocComment.getNextPsiSibling();
    if(!(phpClassContext instanceof PhpClass)) {
        return false;
    }

    String ns = ((PhpClass) phpClassContext).getNamespaceName();
    String repoClass = repositoryClass.getContents();
    String targetClass;

    if(repoClass.startsWith("\\") || repoClass.startsWith(ns)) {
        targetClass = repoClass;
    } else {
        targetClass = ns + repoClass;
    }

    String classPath = PhpLangUtil.toPresentableFQN(targetClass);
    PhpClass repoPhpClass = PhpElementsUtil.getClass(phpClassContext.getProject(), classPath);

    return repoPhpClass != null;
}
 
Example #20
Source File: DoctrineClassGeneratorAction.java    From idea-php-annotation-plugin with MIT License 5 votes vote down vote up
@Override
protected boolean isValidForFile(@NotNull Project project, @NotNull Editor editor, @NotNull PsiFile file) {
    if (!(file instanceof PhpFile) || !DoctrineUtil.isDoctrineOrmInVendor(project)) {
        return false;
    }

    int offset = editor.getCaretModel().getOffset();
    if (offset <= 0) {
        return false;
    }

    PsiElement psiElement = file.findElementAt(offset);
    if (psiElement == null) {
        return false;
    }

    if (!PlatformPatterns.psiElement().inside(PhpClass.class).accepts(psiElement)) {
        return false;
    }

    PhpClass phpClass = PsiTreeUtil.getParentOfType(psiElement, PhpClass.class);
    if (phpClass == null) {
        return false;
    }

    PhpDocComment docComment = phpClass.getDocComment();
    if (docComment == null) {
        return true;
    }

    PhpDocCommentAnnotation container = AnnotationUtil.getPhpDocCommentAnnotationContainer(docComment);

    return container == null || container.getPhpDocBlock(supportedClass()) == null;
}
 
Example #21
Source File: DoctrineOrmRepositoryIntention.java    From idea-php-annotation-plugin with MIT License 5 votes vote down vote up
/**
 * Scope resolve for PhpClass:
 * "@ORM\Entity" or inside PhpClass
 */
@Nullable
private PhpClass getScopedPhpClass(PsiElement element) {

    // inside "@ORM\Entity"
    PsiElement parent = element.getParent();

    // inside "@ORM\Entity(<caret>)"
    if(parent.getNode().getElementType() == PhpDocElementTypes.phpDocAttributeList) {
        parent = parent.getParent();
    }

    if(parent instanceof PhpDocTag) {
        PhpDocTagAnnotation phpDocAnnotationContainer = AnnotationUtil.getPhpDocAnnotationContainer((PhpDocTag) parent);

        if(phpDocAnnotationContainer != null) {
            PhpClass phpClass = phpDocAnnotationContainer.getPhpClass();
            if("Doctrine\\ORM\\Mapping\\Entity".equals(phpClass.getPresentableFQN())) {
                PsiElement docTag = parent.getParent();
                if(docTag instanceof PhpDocComment) {
                    PhpPsiElement nextPsiSibling = ((PhpDocComment) docTag).getNextPsiSibling();
                    if(nextPsiSibling instanceof PhpClass) {
                        return (PhpClass) nextPsiSibling;
                    }
                }
            }
        }

        return null;
    }

    // and finally check PhpClass class scope
    return PsiTreeUtil.getParentOfType(element, PhpClass.class);
}
 
Example #22
Source File: AnnotationPattern.java    From idea-php-annotation-plugin with MIT License 5 votes vote down vote up
public static ElementPattern<PsiElement> getDocBlockTag() {
    return
        PlatformPatterns.or(
            PlatformPatterns.psiElement()
                .withSuperParent(1, PhpDocPsiElement.class)
                     .withParent(PhpDocComment.class)
            .withLanguage(PhpLanguage.INSTANCE)
        ,
            // all "@<caret>"
            PlatformPatterns.psiElement(PhpDocTokenTypes.DOC_TAG_NAME)
                .withLanguage(PhpLanguage.INSTANCE)
        );
}
 
Example #23
Source File: AnnotationUtil.java    From idea-php-annotation-plugin with MIT License 5 votes vote down vote up
public static boolean isAnnotationClass(@NotNull PhpClass phpClass) {
    PhpDocComment phpDocComment = phpClass.getDocComment();
    if(phpDocComment != null) {
        PhpDocTag[] annotationDocTags = phpDocComment.getTagElementsByName("@Annotation");
        return annotationDocTags.length > 0;
    }

    return false;
}
 
Example #24
Source File: GenericsUtil.java    From idea-php-generics-plugin with MIT License 5 votes vote down vote up
/**
 * Resolve the given parameter to find possible psalm docs recursively
 *
 * $foo->foo([])
 *
 * TODO: method search in recursion
 */
@NotNull
public static Collection<ParameterArrayType> getTypesForParameter(@NotNull PsiElement psiElement) {
    PsiElement parent = psiElement.getParent();

    if (parent instanceof ParameterList) {
        PsiElement functionReference = parent.getParent();
        if (functionReference instanceof FunctionReference) {
            PsiElement resolve = ((FunctionReference) functionReference).resolve();

            if (resolve instanceof Function) {
                Parameter[] functionParameters = ((Function) resolve).getParameters();

                int currentParameterIndex = PhpElementsUtil.getCurrentParameterIndex((ParameterList) parent, psiElement);
                if (currentParameterIndex >= 0 && functionParameters.length - 1 >= currentParameterIndex) {
                    String name = functionParameters[currentParameterIndex].getName();
                    PhpDocComment docComment = ((Function) resolve).getDocComment();

                    if (docComment != null) {
                        return GenericsUtil.getParameterArrayTypes(docComment, name);
                    }
                }
            }
        }
    }

    return Collections.emptyList();
}
 
Example #25
Source File: AnnotationUtil.java    From idea-php-annotation-plugin with MIT License 5 votes vote down vote up
public static boolean isAnnotationPhpDocTag(PhpDocTag phpDocTag) {
    PhpDocComment phpDocComment = PsiTreeUtil.getParentOfType(phpDocTag, PhpDocComment.class);
    if(phpDocComment == null) {
        return false;
    }

    PsiElement nextPsiElement = phpDocComment.getNextPsiSibling();
    if(nextPsiElement == null || !(nextPsiElement instanceof Method || nextPsiElement instanceof PhpClass || nextPsiElement.getNode().getElementType() == PhpElementTypes.CLASS_FIELDS)) {
        return false;
    }

    return true;
}
 
Example #26
Source File: AnnotationUtil.java    From idea-php-annotation-plugin with MIT License 5 votes vote down vote up
/**
 * Get attributes for @Foo("test", ATTR<caret>IBUTE)
 *
 * "@Attributes(@Attribute("accessControl", type="string"))"
 * "@Attributes({@Attribute("accessControl", type="string")})"
 * "class foo { public $foo; }"
 */
public static void visitAttributes(@NotNull PhpClass phpClass, TripleFunction<String, String, PsiElement, Void> fn) {
    for (Field field : phpClass.getFields()) {
        if(field.getModifier().isPublic() && !field.isConstant()) {
            String type = null;
            for (String type2 : field.getType().filterNull().getTypes()) {
                if (PhpType.isPrimitiveType(type2)) {
                    type = StringUtils.stripStart(type2, "\\");
                }
            }

            fn.fun(field.getName(), type, field);
        }
    }

    PhpDocComment docComment = phpClass.getDocComment();
    if (docComment != null) {
        for (PhpDocTag phpDocTag : docComment.getTagElementsByName("@Attributes")) {
            for (PhpDocTag docTag : PsiTreeUtil.collectElementsOfType(phpDocTag, PhpDocTag.class)) {
                String name = docTag.getName();
                if (!"@Attribute".equals(name)) {
                    continue;
                }

                String defaultPropertyValue = AnnotationUtil.getDefaultPropertyValue(docTag);
                if (defaultPropertyValue == null || StringUtils.isBlank(defaultPropertyValue)) {
                    continue;
                }

                fn.fun(defaultPropertyValue, AnnotationUtil.getPropertyValue(docTag, "type"), docTag);
            }
        }
    }
}
 
Example #27
Source File: PhpElementsUtil.java    From idea-php-annotation-plugin with MIT License 5 votes vote down vote up
@Nullable
static public AnnotationTarget findAnnotationTarget(@Nullable PhpDocComment phpDocComment) {
    if(phpDocComment == null) {
        return null;
    }

    if(phpDocComment.getNextPsiSibling() instanceof Method) {
        return AnnotationTarget.METHOD;
    }

    if(PlatformPatterns.psiElement(PhpElementTypes.CLASS_FIELDS).accepts(phpDocComment.getNextPsiSibling())) {
        return AnnotationTarget.PROPERTY;
    }

    if(phpDocComment.getNextPsiSibling() instanceof PhpClass) {
        return AnnotationTarget.CLASS;
    }

    // workaround: if file has no use statements all is wrapped inside a group
    if(phpDocComment.getNextPsiSibling() instanceof GroupStatement) {
        PsiElement groupStatement =  phpDocComment.getNextPsiSibling();
        if(groupStatement != null && groupStatement.getFirstChild() instanceof PhpClass) {
            return AnnotationTarget.CLASS;
        }
    }

    return null;
}
 
Example #28
Source File: AnnotationBackportUtil.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
@NotNull
public static Map<String, String> getUseImportMap(@NotNull PhpDocTag phpDocTag) {
    PhpDocComment phpDoc = PsiTreeUtil.getParentOfType(phpDocTag, PhpDocComment.class);
    if(phpDoc == null) {
        return Collections.emptyMap();
    }

    return getUseImportMap(phpDoc);
}
 
Example #29
Source File: ExtbaseModelCollectionReturnTypeProvider.java    From idea-php-typo3-plugin with MIT License 5 votes vote down vote up
@Nullable
private static PhpType inferTypeFromClassMember(PhpClassMember classMember) {
    if (classMember == null) {
        return null;
    }

    PhpDocComment docComment = classMember.getDocComment();
    if (docComment == null) {
        return null;
    }

    PhpDocParamTag varTag = docComment.getVarTag();
    if (varTag == null) {
        return null;
    }

    String text = varTag.getText();
    if (!text.contains("ObjectStorage<")) {
        return null;
    }

    PhpType phpType = new PhpType();

    String pattern = "<(.*?)>";
    Pattern compiled = Pattern.compile(pattern);
    Matcher matcher = compiled.matcher(text);
    while (matcher.find()) {
        phpType.add(matcher.group(1) + "[]");
    }

    return phpType;
}
 
Example #30
Source File: ConfigEntityTypeAnnotationIndex.java    From idea-php-drupal-symfony2-bridge with MIT License 5 votes vote down vote up
@Override
public void visitElement(PsiElement element) {
    if(!(element instanceof PhpDocTag)) {
        super.visitElement(element);
        return;
    }

    String annotationName = StringUtils.stripStart(((PhpDocTag) element).getName(), "@");
    if(!"ConfigEntityType".equals(annotationName)) {
        super.visitElement(element);
        return;
    }

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

    PhpPsiElement phpClass = ((PhpDocComment) phpDocComment).getNextPsiSibling();
    if(!(phpClass instanceof PhpClass)) {
        super.visitElement(element);
        return;
    }

    String tagValue = element.getText();
    Matcher matcher = Pattern.compile("id\\s*=\\s*\"([\\w\\.-]+)\"").matcher(tagValue);
    if (matcher.find()) {
        map.put(matcher.group(1), StringUtils.stripStart(((PhpClass) phpClass).getFQN(), "\\"));
    }

    super.visitElement(element);
}