Java Code Examples for com.jetbrains.php.lang.documentation.phpdoc.psi.PhpDocComment#getTagElementsByName()

The following examples show how to use com.jetbrains.php.lang.documentation.phpdoc.psi.PhpDocComment#getTagElementsByName() . 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: 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 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: ServiceDeprecatedClassesInspection.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
public void attachDeprecatedProblem(PsiElement element, String text, ProblemsHolder holder) {

            PhpClass phpClass = ServiceUtil.getResolvedClassDefinition(element.getProject(), text, getLazyServiceCollector(element.getProject()));
            if(phpClass == null) {
                return;
            }

            PhpDocComment docComment = phpClass.getDocComment();
            if(docComment != null && docComment.getTagElementsByName("@deprecated").length > 0) {
                holder.registerProblem(element, String.format("Class '%s' is deprecated", phpClass.getName()), ProblemHighlightType.LIKE_DEPRECATED);
            }

        }
 
Example 4
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 5
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 6
Source File: AnnotationUtil.java    From idea-php-annotation-plugin with MIT License 4 votes vote down vote up
/**
 * Provide a annotation class container. Allows easy access to @Target attributes
 * Array and single value supported: @Target("PROPERTY", "METHOD"), @Target("CLASS")
 */
@Nullable
public static PhpAnnotation getClassAnnotation(@NotNull PhpClass phpClass) {
    if(!AnnotationUtil.isAnnotationClass(phpClass)) {
        return null;
    }

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

    List<AnnotationTarget> targets = new ArrayList<>();

    PhpDocTag[] tagElementsByName = phpDocComment.getTagElementsByName("@Target");

    if(tagElementsByName.length > 0) {
        for (PhpDocTag phpDocTag : tagElementsByName) {
            // @Target("PROPERTY", "METHOD")
            // @Target("CLASS")
            // @Target("ALL")
            String text = phpDocTag.getText();
            Matcher matcher = Pattern.compile("\"(\\w+)\"").matcher(text);

            // regex matched; on invalid we at target to UNKNOWN condition
            boolean isMatched = false;

            // match enum value
            while (matcher.find()) {
                isMatched = true;
                try {
                    targets.add(AnnotationTarget.valueOf(matcher.group(1).toUpperCase()));
                } catch (IllegalArgumentException e) {
                    targets.add(AnnotationTarget.UNKNOWN);
                }
            }

            // regex failed provide UNKNOWN target
            if(!isMatched) {
                targets.add(AnnotationTarget.UNKNOWN);
            }
        }
    } else {
        // no target attribute so UNDEFINED target
        targets.add(AnnotationTarget.UNDEFINED);
    }

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

    return new PhpAnnotation(phpClass, targets);
}