Java Code Examples for com.jetbrains.php.lang.psi.elements.PhpClass#findFieldByName()

The following examples show how to use com.jetbrains.php.lang.psi.elements.PhpClass#findFieldByName() . 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: AnnotationGoToDeclarationHandler.java    From idea-php-annotation-plugin with MIT License 6 votes vote down vote up
/**
 * Add static field targets for @Route(name=ClassName::<FOO>)
 *
 * @param psiElement DOC_IDENTIFIER after DOC_STATIC
 * @param targets Goto targets
 */
private void addStaticClassConstTargets(PsiElement psiElement, List<PsiElement> targets) {
    PhpClass phpClass = AnnotationUtil.getClassFromConstant(psiElement);
    if(phpClass != null) {
        String constName = psiElement.getText();

        if ("class".equals(constName)) {
            targets.add(phpClass);
        } else {
            Field field = phpClass.findFieldByName(constName, true);
            if(field != null) {
                targets.add(field);
            }
        }
    }
}
 
Example 2
Source File: TwigTemplateGoToDeclarationHandler.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
@NotNull
private Collection<PsiElement> getConstantGoto(@NotNull PsiElement psiElement) {
    Collection<PsiElement> targetPsiElements = new ArrayList<>();

    String contents = psiElement.getText();
    if(StringUtils.isBlank(contents)) {
        return targetPsiElements;
    }

    // global constant
    if(!contents.contains(":")) {
        targetPsiElements.addAll(PhpIndex.getInstance(psiElement.getProject()).getConstantsByName(contents));
        return targetPsiElements;
    }

    // resolve class constants
    String[] parts = contents.split("::");
    if(parts.length != 2) {
        return targetPsiElements;
    }

    PhpClass phpClass = PhpElementsUtil.getClassInterface(psiElement.getProject(), parts[0].replace("\\\\", "\\"));
    if(phpClass == null) {
        return targetPsiElements;
    }

    Field field = phpClass.findFieldByName(parts[1], true);
    if(field != null) {
        targetPsiElements.add(field);
    }

    return targetPsiElements;
}
 
Example 3
Source File: SymfonyUtil.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
@NotNull
public static Set<String> getVersions(@NotNull Project project) {
    Set<String> versions = new HashSet<>();

    for (PhpClass phpClass : PhpElementsUtil.getClassesInterface(project, "Symfony\\Component\\HttpKernel\\Kernel")) {
        Field versionField = phpClass.findFieldByName("VERSION", true);
        if(versionField == null) {
            continue;
        }

        PsiElement defaultValue = versionField.getDefaultValue();
        if(!(defaultValue instanceof StringLiteralExpression)) {
            continue;
        }

        String contents = ((StringLiteralExpression) defaultValue).getContents();
        if(isBlank(contents)) {
            continue;
        }

        // 3.2.0-DEV, 3.2.0-RC1
        contents = contents.toLowerCase().replaceAll("(.*)-([\\w]+)$", "$1");
        versions.add(contents);
    }

    return versions;
}
 
Example 4
Source File: AnnotationDocBlockConstantDeprecatedInspection.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 lazyUseImporterCollector) {
    for (PsiElement element : PsiTreeUtil.collectElements(phpDocTag, psiElement -> psiElement.getNode().getElementType() == PhpDocTokenTypes.DOC_STATIC)) {
        PsiElement nextSibling = element.getNextSibling();
        if (nextSibling == null || nextSibling.getNode().getElementType() != PhpDocTokenTypes.DOC_IDENTIFIER) {
            continue;
        }

        PsiElement prevSibling = element.getPrevSibling();
        if (prevSibling == null) {
            return;
        }

        String namespaceForDocIdentifier = PhpDocUtil.getNamespaceForDocIdentifier(prevSibling);
        if (namespaceForDocIdentifier == null) {
            return;
        }

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

        PhpClass phpClass = PhpElementsUtil.getClassInterface(phpDocTag.getProject(), clazz);
        if (phpClass == null) {
            return;
        }

        // ::class direct class access
        String text = nextSibling.getText();
        if ("class".equals(text)) {
            if (phpClass.isDeprecated()) {
                holder.registerProblem(
                    nextSibling,
                    MESSAGE,
                    ProblemHighlightType.LIKE_DEPRECATED
                );
            }

            return;
        }

        // ::CONST fetch the field
        Field fieldByName = phpClass.findFieldByName(text, true);
        if (fieldByName != null && fieldByName.isConstant() && fieldByName.isDeprecated()) {
            holder.registerProblem(
                nextSibling,
                MESSAGE,
                ProblemHighlightType.LIKE_DEPRECATED
            );
        }
    }
}