com.jetbrains.php.lang.PhpLangUtil Java Examples

The following examples show how to use com.jetbrains.php.lang.PhpLangUtil. 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: PhpDocUtil.java    From idea-php-toolbox with MIT License 6 votes vote down vote up
private static ArrayList<Method> getImplementedMethods(@Nullable PhpClass phpClass, @NotNull Method method, ArrayList<Method> implementedMethods) {
    if (phpClass == null) {
        return implementedMethods;
    }

    Method[] methods = phpClass.getOwnMethods();
    for (Method ownMethod : methods) {
        if (PhpLangUtil.equalsMethodNames(ownMethod.getName(), method.getName())) {
            implementedMethods.add(ownMethod);
        }
    }

    for(PhpClass interfaceClass: phpClass.getImplementedInterfaces()) {
        getImplementedMethods(interfaceClass, method, implementedMethods);
    }

    getImplementedMethods(phpClass.getSuperClass(), method, implementedMethods);

    return implementedMethods;
}
 
Example #2
Source File: PhpDocUtil.java    From idea-php-annotation-plugin with MIT License 6 votes vote down vote up
/**
 * Get class path on "use" path statement
 */
@Nullable
public static String getQualifiedName(@NotNull PsiElement psiElement, @NotNull String fqn) {

    PhpPsiElement scopeForUseOperator = PhpCodeInsightUtil.findScopeForUseOperator(psiElement);
    if (scopeForUseOperator == null) {
        return null;
    }

    PhpReference reference = PhpPsiUtil.getParentByCondition(psiElement, false, PhpReference.INSTANCEOF);
    String qualifiedName = PhpCodeInsightUtil.createQualifiedName(scopeForUseOperator, fqn, reference, false);
    if (!PhpLangUtil.isFqn(qualifiedName)) {
        return qualifiedName;
    }

    // @TODO: remove full fqn fallback
    if(qualifiedName.startsWith("\\")) {
        qualifiedName = qualifiedName.substring(1);
    }

    return qualifiedName;
}
 
Example #3
Source File: AnnotationInspectionUtil.java    From idea-php-annotation-plugin with MIT License 6 votes vote down vote up
@Nullable
public String getNamespace() {
    if (hasNamespace) {
        return this.namespace;
    }

    PhpPsiElement scope = PhpCodeInsightUtil.findScopeForUseOperator(psiElement);

    if (scope instanceof PhpNamespace) {
        String namespaceFqn = ((PhpNamespace) scope).getFQN();
        if (PhpLangUtil.isFqn(namespaceFqn) && !PhpLangUtil.isGlobalNamespaceFQN(namespaceFqn)) {
            hasNamespace = true;
            return this.namespace = namespaceFqn;
        }
    }

    return this.namespace = null;
}
 
Example #4
Source File: PhpDocUtil.java    From idea-php-toolbox with MIT License 6 votes vote down vote up
private static ArrayList<Method> getImplementedMethods(@Nullable PhpClass phpClass, @NotNull Method method, ArrayList<Method> implementedMethods) {
    if (phpClass == null) {
        return implementedMethods;
    }

    Method[] methods = phpClass.getOwnMethods();
    for (Method ownMethod : methods) {
        if (PhpLangUtil.equalsMethodNames(ownMethod.getName(), method.getName())) {
            implementedMethods.add(ownMethod);
        }
    }

    for(PhpClass interfaceClass: phpClass.getImplementedInterfaces()) {
        getImplementedMethods(interfaceClass, method, implementedMethods);
    }

    getImplementedMethods(phpClass.getSuperClass(), method, implementedMethods);

    return implementedMethods;
}
 
Example #5
Source File: PhpEntityClassCompletionProvider.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
public void addCompletions(@NotNull CompletionParameters parameters, ProcessingContext context, @NotNull CompletionResultSet resultSet) {

        if(!Symfony2ProjectComponent.isEnabled(parameters.getPosition())) {
            return;
        }

        PhpIndex phpIndex = PhpIndex.getInstance(parameters.getOriginalFile().getProject());
        Map<String, String> entityNamespaces = ServiceXmlParserFactory.getInstance(parameters.getOriginalFile().getProject(), EntityNamesServiceParser.class).getEntityNameMap();

        // copied from PhpCompletionUtil::addClassesInNamespace looks the official way to find classes in namespaces
        // its a really performance nightmare

        Collection<String> names = phpIndex.getAllClassNames(new CamelHumpMatcher(resultSet.getPrefixMatcher().getPrefix()));
        for (String name : names) {
            Collection<PhpClass> classes = phpIndex.getClassesByName(name);

            for(Map.Entry<String, String> entry: entityNamespaces.entrySet()) {
                String namespaceFqn = PhpLangUtil.toFQN(entry.getValue());
                Collection<PhpClass> filtered = PhpCompletionUtil.filterByNamespace(classes, namespaceFqn);
                for (PhpClass phpClass : filtered) {
                    resultSet.addElement(new PhpClassLookupElement(phpClass, true, PhpClassReferenceInsertHandler.getInstance()));
                }
            }
        }
    }
 
Example #6
Source File: AnnotationBackportUtil.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
/**
 * Get class path on "use" path statement
 */
@Nullable
public static String getQualifiedName(@NotNull PsiElement psiElement, @NotNull String fqn) {

    PhpPsiElement scopeForUseOperator = PhpCodeInsightUtil.findScopeForUseOperator(psiElement);
    if (scopeForUseOperator == null) {
        return null;
    }

    PhpReference reference = PhpPsiUtil.getParentByCondition(psiElement, false, PhpReference.INSTANCEOF);
    String qualifiedName = PhpCodeInsightUtil.createQualifiedName(scopeForUseOperator, fqn, reference, false);
    if (!PhpLangUtil.isFqn(qualifiedName)) {
        return qualifiedName;
    }

    // @TODO: remove full fqn fallback
    if(qualifiedName.startsWith("\\")) {
        qualifiedName = qualifiedName.substring(1);
    }

    return qualifiedName;
}
 
Example #7
Source File: PhpElementsUtil.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
private static ArrayList<Method> getImplementedMethods(@Nullable PhpClass phpClass, @NotNull Method method, ArrayList<Method> implementedMethods) {
    if (phpClass == null) {
        return implementedMethods;
    }

    Method[] methods = phpClass.getOwnMethods();
    for (Method ownMethod : methods) {
        if (PhpLangUtil.equalsMethodNames(ownMethod.getName(), method.getName())) {
            implementedMethods.add(ownMethod);
        }
    }

    for(PhpClass interfaceClass: phpClass.getImplementedInterfaces()) {
        getImplementedMethods(interfaceClass, method, implementedMethods);
    }

    getImplementedMethods(phpClass.getSuperClass(), method, implementedMethods);

    return implementedMethods;
}
 
Example #8
Source File: DoctrineAnnotationStaticCompletionProvider.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("type") && PhpLangUtil.equalsClassNames(annotationPropertyParameter.getPhpClass().getPresentableFQN(), "Doctrine\\ORM\\Mapping\\Column")) {
        completionParameter.getResult().addAllElements(DoctrineUtil.getTypes(annotationPropertyParameter.getProject()));
    }

    if(propertyName.equals("onDelete") && PhpLangUtil.equalsClassNames(annotationPropertyParameter.getPhpClass().getPresentableFQN(), "Doctrine\\ORM\\Mapping\\JoinColumn")) {
        for(String s: Arrays.asList("CASCADE", "SET NULL")) {
            completionParameter.getResult().addElement(LookupElementBuilder.create(s));
        }
    }

}
 
Example #9
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 #10
Source File: DoctrineAnnotationTypeProvider.java    From idea-php-annotation-plugin with MIT License 6 votes vote down vote up
@Nullable
@Override
public PsiReference[] getPropertyReferences(AnnotationPropertyParameter annotationPropertyParameter, PhpAnnotationReferenceProviderParameter referencesByElementParameter) {

    if(annotationPropertyParameter.getType() != AnnotationPropertyParameter.Type.PROPERTY_VALUE) {
        return null;
    }

    String propertyName = annotationPropertyParameter.getPropertyName();
    if(propertyName == null || !propertyName.equals("repositoryClass")) {
        return null;
    }

    String presentableFQN = annotationPropertyParameter.getPhpClass().getPresentableFQN();
    if(!PhpLangUtil.equalsClassNames("Doctrine\\ORM\\Mapping\\Entity", presentableFQN)) {
        return null;
    }

    return new PsiReference[] {
        new DoctrineRepositoryReference((StringLiteralExpression) annotationPropertyParameter.getElement())
    };

}
 
Example #11
Source File: SymfonyCompletionProvider.java    From idea-php-annotation-plugin with MIT License 5 votes vote down vote up
@Override
public void getPropertyValueCompletions(AnnotationPropertyParameter parameter, AnnotationCompletionProviderParameter completion) {
    if(parameter.getType() != AnnotationPropertyParameter.Type.PROPERTY_ARRAY) {
        return;
    }

    if("methods".equals(parameter.getPropertyName()) && PhpLangUtil.equalsClassNames(StringUtils.stripStart(parameter.getPhpClass().getFQN(), "\\"), "Symfony\\Component\\Routing\\Annotation\\Route")) {
        for (String s : new String[]{"HEAD", "GET", "POST", "PUT", "PATCH", "DELETE", "PURGE", "OPTIONS", "TRACE", "CONNECT"}) {
            completion.getResult().addElement(LookupElementBuilder.create(s));
        }
    }
}
 
Example #12
Source File: EmbeddedClassCompletionProvider.java    From idea-php-annotation-plugin with MIT License 5 votes vote down vote up
@Override
boolean supports(AnnotationPropertyParameter parameter) {
    return
        parameter.getType() == AnnotationPropertyParameter.Type.PROPERTY_VALUE &&
        "class".equals(parameter.getPropertyName()) &&
        PhpLangUtil.equalsClassNames(StringUtils.stripStart(parameter.getPhpClass().getFQN(), "\\"), "Doctrine\\ORM\\Mapping\\Embedded");
}
 
Example #13
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 #14
Source File: DoctrineCustomIdGenerator.java    From idea-php-annotation-plugin with MIT License 5 votes vote down vote up
@Override
boolean supports(AnnotationPropertyParameter parameter) {
    return
        parameter.getType() == AnnotationPropertyParameter.Type.PROPERTY_VALUE &&
        "class".equals(parameter.getPropertyName()) &&
        PhpLangUtil.equalsClassNames(StringUtils.stripStart(parameter.getPhpClass().getFQN(), "\\"), "Doctrine\\ORM\\Mapping\\CustomIdGenerator");
}
 
Example #15
Source File: PhpMetaUtil.java    From idea-php-advanced-autocomplete with MIT License 4 votes vote down vote up
private static boolean metaFunctionWithName(FunctionReference reference, String name) {
    return PhpLangUtil.equalsClassNames(reference.getName(), name) && META_NAMESPACE_PREFIX.equals(reference.getNamespaceName());
}
 
Example #16
Source File: DoctrineOrmRepositoryIntention.java    From idea-php-annotation-plugin with MIT License 4 votes vote down vote up
@Override
public void invoke(@NotNull Project project, Editor editor, @NotNull PsiElement element) throws IncorrectOperationException {

    PhpClass phpClass = getScopedPhpClass(element);
    if(phpClass == null) {
        return;
    }

    String fileName = phpClass.getName() + "Repository.php";
    String namespace = DoctrineUtil.trimBlackSlashes(phpClass.getNamespaceName());
    PsiDirectory dir = phpClass.getContainingFile().getContainingDirectory();

    PsiDirectory repositoryDir = dir;
    if (dir.getParentDirectory() != null) {
        PsiDirectory checkDir = dir.getParentDirectory().findSubdirectory("Repository");

        if (dir.findFile(fileName) == null && checkDir != null) {
            repositoryDir = checkDir;
        }
    }

    String repoClass = phpClass.getPresentableFQN() + "Repository";
    PhpClass repoPhpClass = PhpElementsUtil.getClass(project, repoClass);

    if (repoPhpClass == null && dir != repositoryDir) {
        // Entity/../Repository/
        namespace = phpClass.getNamespaceName();
        namespace = namespace.substring(0, namespace.lastIndexOf("\\"));
        namespace = namespace.substring(0, namespace.lastIndexOf("\\"));
        namespace = namespace + "\\Repository";
        namespace = DoctrineUtil.trimBlackSlashes(namespace);

        repoClass = namespace + "\\" + phpClass.getName() + "Repository";
        repoClass = PhpLangUtil.toPresentableFQN(repoClass);

        repoPhpClass = PhpElementsUtil.getClass(project, repoClass);
    }

    if(repoPhpClass == null) {
        if(repositoryDir.findFile(fileName) == null) {
            final FileTemplate fileTemplate = FileTemplateManager.getInstance(project).getTemplate("Doctrine Entity Repository");
            final Properties defaultProperties = FileTemplateManager.getInstance(project).getDefaultProperties();

            Properties properties = new Properties(defaultProperties);

            properties.setProperty("NAMESPACE", namespace);
            properties.setProperty("NAME", phpClass.getName() + "Repository");

            properties.setProperty("ENTITY_NAMESPACE", DoctrineUtil.trimBlackSlashes(phpClass.getNamespaceName()));
            properties.setProperty("ENTITY_NAME", phpClass.getName());

            try {
                PsiElement newElement = FileTemplateUtil.createFromTemplate(fileTemplate, fileName, properties, repositoryDir);

                new OpenFileDescriptor(project, newElement.getContainingFile().getVirtualFile(), 0).navigate(true);
            } catch (Exception e) {
                return;
            }
        } else {
            if (!ApplicationManager.getApplication().isHeadlessEnvironment()) {
                HintManager.getInstance().showErrorHint(editor, "Repository already exists ");
            }
        }
    }

    PhpDocTagAnnotation ormEntityPhpDocBlock = DoctrineUtil.getOrmEntityPhpDocBlock(phpClass);
    if(ormEntityPhpDocBlock != null) {
        PhpDocTag phpDocTag = ormEntityPhpDocBlock.getPhpDocTag();
        PhpPsiElement firstPsiChild = phpDocTag.getFirstPsiChild();
        insertAttribute(editor, repoClass, phpDocTag, firstPsiChild);
    }

}
 
Example #17
Source File: DoctrineTypeDeprecatedInspection.java    From idea-php-annotation-plugin with MIT License 4 votes vote down vote up
@Override
public void visitElement(PsiElement element) {
    if (!(element instanceof StringLiteralExpression) || element.getNode().getElementType() != PhpDocElementTypes.phpDocString) {
        super.visitElement(element);
        return;
    }

    String contents = ((StringLiteralExpression) element).getContents();
    if (StringUtils.isBlank(contents)) {
        super.visitElement(element);
        return;
    }

    PhpDocTag phpDocTag = PsiTreeUtil.getParentOfType(element, PhpDocTag.class);
    if (phpDocTag == null) {
        super.visitElement(element);
        return;
    }

    PsiElement propertyName = PhpElementsUtil.getPrevSiblingOfPatternMatch(element, PlatformPatterns.psiElement(PhpDocTokenTypes.DOC_IDENTIFIER));
    if(propertyName == null) {
        super.visitElement(element);
        return;
    }

    String text = propertyName.getText();
    if ("type".equalsIgnoreCase(text)) {
        PhpDocTagAnnotation phpDocAnnotationContainer = AnnotationUtil.getPhpDocAnnotationContainer(phpDocTag);
        if (phpDocAnnotationContainer != null) {
            PhpClass tagPhpClass = phpDocAnnotationContainer.getPhpClass();
            if (PhpLangUtil.equalsClassNames(tagPhpClass.getPresentableFQN(), "Doctrine\\ORM\\Mapping\\Column")) {
                for (PhpClass columnPhpClass : DoctrineUtil.getColumnTypesTargets(holder.getProject(), contents)) {
                    if (!columnPhpClass.isDeprecated()) {
                        continue;
                    }

                    String deprecationMessage = PhpElementsUtil.getClassDeprecatedMessage(columnPhpClass);

                    holder.registerProblem(
                        element,
                        "[Annotations] " + (deprecationMessage != null ? deprecationMessage : String.format("Field '%s' is deprecated", text)),
                        ProblemHighlightType.LIKE_DEPRECATED
                    );

                    break;
                }
            }
        }
    }

    super.visitElement(element);
}
 
Example #18
Source File: ContentEntityTypeAnnotation.java    From idea-php-drupal-symfony2-bridge with MIT License 4 votes vote down vote up
private boolean isSupported(@NotNull AnnotationPropertyParameter parameter) {
    return parameter.getType() != AnnotationPropertyParameter.Type.DEFAULT &&
        PhpLangUtil.equalsClassNames(StringUtils.stripStart(parameter.getPhpClass().getFQN(), "\\"), "Drupal\\Core\\Entity\\Annotation\\ContentEntityType");
}
 
Example #19
Source File: TranslationAnnotationReference.java    From idea-php-drupal-symfony2-bridge with MIT License 4 votes vote down vote up
private boolean isSupported(@NotNull AnnotationPropertyParameter parameter) {
    return parameter.getType() == AnnotationPropertyParameter.Type.DEFAULT &&
        PhpLangUtil.equalsClassNames(StringUtils.stripStart(parameter.getPhpClass().getFQN(), "\\"), "Drupal\\Core\\Annotation\\Translation");
}
 
Example #20
Source File: ConfigEntityTypeAnnotation.java    From idea-php-drupal-symfony2-bridge with MIT License 4 votes vote down vote up
private boolean isSupported(@NotNull AnnotationPropertyParameter parameter) {
    return parameter.getType() != AnnotationPropertyParameter.Type.DEFAULT &&
        PhpLangUtil.equalsClassNames(StringUtils.stripStart(parameter.getPhpClass().getFQN(), "\\"), "Drupal\\Core\\Entity\\Annotation\\ConfigEntityType");
}