Java Code Examples for com.jetbrains.php.lang.documentation.phpdoc.psi.tags.PhpDocTag#getName()

The following examples show how to use com.jetbrains.php.lang.documentation.phpdoc.psi.tags.PhpDocTag#getName() . 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
@Nullable
public static PhpClass getAnnotationReference(PhpDocTag phpDocTag, final Map<String, String> useImports) {

    String tagName = phpDocTag.getName();
    if(tagName.startsWith("@")) {
        tagName = tagName.substring(1);
    }

    String className = tagName;
    String subNamespaceName = "";
    if(className.contains("\\")) {
        className = className.substring(0, className.indexOf("\\"));
        subNamespaceName = tagName.substring(className.length());
    }

    if(!useImports.containsKey(className)) {
        return null;
    }

    return PhpElementsUtil.getClass(phpDocTag.getProject(), useImports.get(className) + subNamespaceName);

}
 
Example 2
Source File: AnnotationUtil.java    From idea-php-annotation-plugin with MIT License 6 votes vote down vote up
@Nullable
public static PhpClass getAnnotationReference(@Nullable PhpDocTag phpDocTag) {
    if(phpDocTag == null) return null;

    // check annoation in current namespace
    String tagName = phpDocTag.getName();
    if(tagName.startsWith("@")) {
        tagName = tagName.substring(1);
    }

    PhpNamespace phpNamespace = PsiTreeUtil.getParentOfType(phpDocTag, PhpNamespace.class);
    if(phpNamespace != null) {
        String currentNsClass = phpNamespace.getFQN() + "\\" + tagName;
        PhpClass phpClass = PhpElementsUtil.getClass(phpDocTag.getProject(), currentNsClass);
        if(phpClass != null) {
            return phpClass;
        }
    }

    return getAnnotationReference(phpDocTag, getUseImportMap((PsiElement) phpDocTag));

}
 
Example 3
Source File: AnnotationBackportUtil.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
@Nullable
public static String getClassNameReference(PhpDocTag phpDocTag, Map<String, String> useImports) {

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

    String annotationName = phpDocTag.getName();
    if(StringUtils.isBlank(annotationName)) {
        return null;
    }

    if(annotationName.startsWith("@")) {
        annotationName = annotationName.substring(1);
    }

    String className = annotationName;
    String subNamespaceName = "";
    if(className.contains("\\")) {
        className = className.substring(0, className.indexOf("\\"));
        subNamespaceName = annotationName.substring(className.length());
    }

    if(!useImports.containsKey(className)) {
        return null;
    }

    // normalize name
    String annotationFqnName = useImports.get(className) + subNamespaceName;
    if(!annotationFqnName.startsWith("\\")) {
        annotationFqnName = "\\" + annotationFqnName;
    }

    return annotationFqnName;
}
 
Example 4
Source File: AnnotationDocBlockTagClassNotFoundInspection.java    From idea-php-annotation-plugin with MIT License 5 votes vote down vote up
private void visitAnnotationDocTag(@NotNull PhpDocTag phpDocTag, @NotNull ProblemsHolder holder, @NotNull AnnotationInspectionUtil.LazyNamespaceImportResolver lazyUseImporterCollector) {
    // Target for our inspection is DocTag name: @Foobar() => Foobar
    // This prevent highlighting the complete DocTag
    PsiElement firstChild = phpDocTag.getFirstChild();
    if (firstChild == null || firstChild.getNode().getElementType() != PhpDocElementTypes.DOC_TAG_NAME) {
        return;
    }

    String name = phpDocTag.getName();
    String tagName = StringUtils.stripStart(name, "@");

    // ignore "@test", but allow "@\test" to go through
    if (!tagName.startsWith("\\") && !Character.isUpperCase(tagName.codePointAt(0))) {
        return;
    }

    String clazz = AnnotationInspectionUtil.getClassFqnString(tagName, lazyUseImporterCollector);
    if (clazz == null) {
        return;
    }

    PhpClass classInterface = PhpElementsUtil.getClassInterface(phpDocTag.getProject(), clazz);
    if (classInterface == null) {
        holder.registerProblem(
            firstChild,
            MESSAGE,
            ProblemHighlightType.GENERIC_ERROR_OR_WARNING
        );
    }
}
 
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: PhpDocTagAnnotationRecursiveElementWalkingVisitor.java    From idea-php-annotation-plugin with MIT License 5 votes vote down vote up
@Nullable
private static String getClassNameReference(@NotNull PhpDocTag phpDocTag, @NotNull Map<String, String> useImports) {

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

    String annotationName = phpDocTag.getName();
    if(StringUtils.isBlank(annotationName)) {
        return null;
    }

    if(annotationName.startsWith("@")) {
        annotationName = annotationName.substring(1);
    }

    String className = annotationName;
    String subNamespaceName = "";
    if(className.contains("\\")) {
        className = className.substring(0, className.indexOf("\\"));
        subNamespaceName = annotationName.substring(className.length());
    }

    if(!useImports.containsKey(className)) {
        return null;
    }

    // normalize name
    String annotationFqnName = useImports.get(className) + subNamespaceName;
    if(!annotationFqnName.startsWith("\\")) {
        annotationFqnName = "\\" + annotationFqnName;
    }

    return annotationFqnName;
}
 
Example 7
Source File: AnnotationMissingUseInspection.java    From idea-php-annotation-plugin with MIT License 4 votes vote down vote up
private void visitAnnotationDocTag(@NotNull PhpDocTag phpDocTag, @NotNull ProblemsHolder holder, @NotNull AnnotationInspectionUtil.LazyNamespaceImportResolver lazyNamespaceImportResolver) {
    // Target for our inspection is DocTag name: @Foobar() => Foobar
    // This prevent highlighting the complete DocTag
    PsiElement firstChild = phpDocTag.getFirstChild();
    if (firstChild == null || firstChild.getNode().getElementType() != PhpDocElementTypes.DOC_TAG_NAME) {
        return;
    }

    String name = phpDocTag.getName();
    String tagName = StringUtils.stripStart(name, "@");

    // ignore "@\Foo" absolute FQN ones
    if (tagName.startsWith("\\")) {
        return;
    }

    String[] split = tagName.split("\\\\");

    Map<String, String> useImportMap = lazyNamespaceImportResolver.getImports();
    if (useImportMap.containsKey(split[0])) {
        return;
    }

    PhpClass annotationReference = AnnotationUtil.getAnnotationReference(phpDocTag);
    if (annotationReference != null) {
        return;
    }

    Map<String, String> phpClasses = AnnotationUtil.getPossibleImportClasses(phpDocTag);
    if (phpClasses.size() > 0) {
        Collection<Pair<String, String>> collect = phpClasses.entrySet().stream()
            .map(entry -> Pair.create(entry.getKey(), entry.getValue()))
            .collect(Collectors.toList());

        holder.registerProblem(
            firstChild,
            MESSAGE,
            ProblemHighlightType.GENERIC_ERROR_OR_WARNING,
            new ImportUseForAnnotationQuickFix(phpDocTag, collect)
        );
    }
}
 
Example 8
Source File: AnnotationUtil.java    From idea-php-annotation-plugin with MIT License 4 votes vote down vote up
@Nullable
public static PhpClass getAnnotationReference(PhpDocTag phpDocTag, final Map<String, String> useImports) {

    String tagName = phpDocTag.getName();
    if(tagName.startsWith("@")) {
        tagName = tagName.substring(1);
    }

    String className = tagName;
    String subNamespaceName = "";
    if(className.contains("\\")) {
        className = className.substring(0, className.indexOf("\\"));
        subNamespaceName = tagName.substring(className.length());
    }

    if(!useImports.containsKey(className)) {

        // allow full classes on annotations #17 eg: @Doctrine\ORM\Mapping\PostPersist()
        PhpClass phpClass = PhpElementsUtil.getClass(phpDocTag.getProject(), tagName);
        if(phpClass != null && isAnnotationClass(phpClass)) {
            return phpClass;
        }

        // global namespace support
        AnnotationGlobalNamespacesLoaderParameter parameter = null;
        for (PhpAnnotationGlobalNamespacesLoader loader : EXTENSION_POINT_GLOBAL_NAMESPACES.getExtensions()) {
            if(parameter == null) {
                parameter = new AnnotationGlobalNamespacesLoaderParameter(phpDocTag.getProject());
            }

            for (String ns : loader.getGlobalNamespaces(parameter)) {
                PhpClass globalPhpClass = PhpElementsUtil.getClassInterface(phpDocTag.getProject(), ns + "\\" + className);
                if(globalPhpClass != null) {
                    return globalPhpClass;
                }
            }
        }

        return null;
    }

    return PhpElementsUtil.getClass(phpDocTag.getProject(), useImports.get(className) + subNamespaceName);

}
 
Example 9
Source File: AnnotationUtil.java    From idea-php-annotation-plugin with MIT License 4 votes vote down vote up
public static Map<String, String> getPossibleImportClasses(@NotNull PhpDocTag phpDocTag) {
    String className = phpDocTag.getName();
    if (className.startsWith("@")) {
        className = className.substring(1);
    }

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

    // "@Test\Bar" can be ignored here
    // "@Bar" We only search for a direct class
    if (!className.contains("\\")) {
        for (PhpClass annotationClass: AnnotationUtil.getAnnotationsClasses(phpDocTag.getProject())) {
            if (annotationClass.getName().equals(className)) {
                phpClasses.put(annotationClass.getFQN(), null);
            }
        }
    }

    // "@ORM\Entity" is search for configured alias which can also provide a valid "use" path
    if (!className.startsWith("\\")) {
        String[] split = className.split("\\\\");
        if (split.length > 1) {
            String aliasStart = split[0];

            for (UseAliasOption useAliasOption : AnnotationUtil.getActiveImportsAliasesFromSettings()) {
                String alias = useAliasOption.getAlias();
                if (!aliasStart.equals(alias)) {
                    continue;
                }

                String clazz = useAliasOption.getClassName() + "\\" + StringUtils.join(Arrays.copyOfRange(split, 1, split.length), "\\");
                PhpClass phpClass = PhpElementsUtil.getClassInterface(phpDocTag.getProject(), clazz);

                if (phpClass != null && isAnnotationClass(phpClass)) {
                    // user input for class name
                    phpClasses.put("\\" + StringUtils.stripStart(useAliasOption.getClassName(), "\\"), alias);
                }
            }
        }
    }

    return phpClasses;
}