Java Code Examples for fr.adrienbrault.idea.symfony2plugin.util.PhpElementsUtil#getClassesInterface()

The following examples show how to use fr.adrienbrault.idea.symfony2plugin.util.PhpElementsUtil#getClassesInterface() . 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: DoctrineTypeGotoCompletionRegistrar.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
@NotNull
@Override
public Collection<LookupElement> getLookupElements() {
    String modelNameInScope = DoctrineMetadataUtil.findModelNameInScope(getElement());
    if(modelNameInScope == null) {
        return Collections.emptyList();
    }

    Set<String> unique = new HashSet<>();

    Collection<LookupElement> lookupElements = new ArrayList<>();
    for (PhpClass phpClass : PhpElementsUtil.getClassesInterface(getProject(), modelNameInScope)) {
        for (Field field : phpClass.getFields()) {
            if(field.isConstant() || unique.contains(field.getName())) {
                continue;
            }

            String name = field.getName();
            unique.add(name);
            lookupElements.add(LookupElementBuilder.create(name).withIcon(field.getIcon()).withTypeText(phpClass.getPresentableFQN(), true));
        }
    }

    return lookupElements;
}
 
Example 2
Source File: DoctrineTypeGotoCompletionRegistrar.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
@NotNull
@Override
public Collection<PsiElement> getPsiTargets(PsiElement element) {

    final String elementText = getElementText(element);
    if(elementText == null) {
        return Collections.emptyList();
    }

    String modelNameInScope = DoctrineMetadataUtil.findModelNameInScope(getElement());
    if(modelNameInScope == null) {
        return Collections.emptyList();
    }

    final Collection<PsiElement> psiElements = new ArrayList<>();

    for (PhpClass phpClass : PhpElementsUtil.getClassesInterface(getProject(), modelNameInScope)) {
        psiElements.addAll(ContainerUtil.filter(phpClass.getFields(), field ->
            !field.isConstant() && elementText.equals(field.getName()))
        );
    }

    return psiElements;
}
 
Example 3
Source File: ConsoleHelperGotoCompletionRegistrar.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
@NotNull
@Override
public Collection<PsiElement> getPsiTargets(PsiElement psiElement) {

    PsiElement element = psiElement.getParent();
    if(!(element instanceof StringLiteralExpression)) {
        return Collections.emptyList();
    }

    String value = ((StringLiteralExpression) element).getContents();
    if(StringUtils.isBlank(value)) {
        return Collections.emptyList();
    }

    Map<String, String> commandHelper = CommandUtil.getCommandHelper(getProject());
    if(!commandHelper.containsKey(value)) {
        return Collections.emptyList();
    }

    return new HashSet<>(PhpElementsUtil.getClassesInterface(getProject(), commandHelper.get(value)));
}
 
Example 4
Source File: TwigTemplateGoToDeclarationHandler.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
/**
 * Extract class from inline variables
 *
 * {# @var \AppBundle\Entity\Foo variable #}
 * {# @var variable \AppBundle\Entity\Foo #}
 */
@NotNull
private Collection<PhpClass> getVarClassGoto(@NotNull PsiElement psiElement) {
    String comment = psiElement.getText();

    if(StringUtils.isBlank(comment)) {
        return Collections.emptyList();
    }

    for(String pattern: TwigTypeResolveUtil.DOC_TYPE_PATTERN_SINGLE) {
        Matcher matcher = Pattern.compile(pattern).matcher(comment);
        if (matcher.find()) {
            String className = matcher.group("class");
            if(StringUtils.isNotBlank(className)) {
                return PhpElementsUtil.getClassesInterface(psiElement.getProject(), className);
            }
        }
    }

    return Collections.emptyList();
}
 
Example 5
Source File: ServiceLineMarkerProvider.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
/**
 * Constraints in same namespace and validateBy service name
 */
private void validatorClassMarker(PsiElement psiElement, Collection<LineMarkerInfo> results) {
    PsiElement phpClassContext = psiElement.getContext();
    if(!(phpClassContext instanceof PhpClass) || !PhpElementsUtil.isInstanceOf((PhpClass) phpClassContext, "\\Symfony\\Component\\Validator\\Constraint")) {
        return;
    }

    // class in same namespace
    String className = ((PhpClass) phpClassContext).getFQN() + "Validator";
    Collection<PhpClass> phpClasses = new ArrayList<>(PhpElementsUtil.getClassesInterface(psiElement.getProject(), className));

    // @TODO: validateBy alias

    if(phpClasses.size() == 0) {
        return;
    }

    NavigationGutterIconBuilder<PsiElement> builder = NavigationGutterIconBuilder.create(Symfony2Icons.SYMFONY_LINE_MARKER).
        setTargets(phpClasses).
        setTooltipText("Navigate to validator");

    results.add(builder.createLineMarkerInfo(psiElement));
}
 
Example 6
Source File: ServiceLineMarkerProvider.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
/**
 * "FooValidator" back to "Foo" constraint
 */
private void constraintValidatorClassMarker(PsiElement psiElement, Collection<LineMarkerInfo> results) {
    PsiElement phpClass = psiElement.getContext();
    if(!(phpClass instanceof PhpClass) || !PhpElementsUtil.isInstanceOf((PhpClass) phpClass, "Symfony\\Component\\Validator\\ConstraintValidatorInterface")) {
        return;
    }

    String fqn = ((PhpClass) phpClass).getFQN();
    if(!fqn.endsWith("Validator")) {
        return;
    }

    Collection<PhpClass> phpClasses = new ArrayList<>(
        PhpElementsUtil.getClassesInterface(psiElement.getProject(), fqn.substring(0, fqn.length() - "Validator".length()))
    );

    if(phpClasses.size() == 0) {
        return;
    }

    NavigationGutterIconBuilder<PsiElement> builder = NavigationGutterIconBuilder.create(Symfony2Icons.SYMFONY_LINE_MARKER).
        setTargets(phpClasses).
        setTooltipText("Navigate to constraint");

    results.add(builder.createLineMarkerInfo(psiElement));
}
 
Example 7
Source File: DoctrineMetadataUtil.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
@NotNull
public static Collection<PhpClass> getClassInsideScope(@NotNull PsiElement psiElement, @NotNull String originValue) {
    Collection<PhpClass> classesInterface = new ArrayList<>();
    String modelNameInScope = DoctrineMetadataUtil.findModelNameInScope(psiElement);
    if(modelNameInScope != null) {
        PhpClass classInsideNamespaceScope = PhpElementsUtil.getClassInsideNamespaceScope(psiElement.getProject(), modelNameInScope, originValue);
        if(classInsideNamespaceScope != null) {
            classesInterface = Collections.singletonList(classInsideNamespaceScope);
        }
    } else {
        classesInterface = PhpElementsUtil.getClassesInterface(psiElement.getProject(), originValue);
    }
    // @TODO: multi classes
    return classesInterface;
}
 
Example 8
Source File: YamlLineMarkerProvider.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
private void attachEntityClass(@NotNull Collection<LineMarkerInfo> lineMarkerInfos, @NotNull PsiElement psiElement) {
    if(psiElement.getNode().getElementType() != YAMLTokenTypes.SCALAR_KEY) {
        return;
    }

    PsiElement yamlKeyValue = psiElement.getParent();
    if(!(yamlKeyValue instanceof YAMLKeyValue)) {
        return;
    }

    if(yamlKeyValue.getParent() instanceof YAMLMapping && yamlKeyValue.getParent().getParent() instanceof YAMLDocument) {
        PsiFile containingFile;
        try {
            containingFile = yamlKeyValue.getContainingFile();
        } catch (PsiInvalidElementAccessException e) {
            return;
        }

        String fileName = containingFile.getName();
        if(isMetadataFile(fileName)) {
            String keyText = ((YAMLKeyValue) yamlKeyValue).getKeyText();
            if(StringUtils.isNotBlank(keyText)) {
                Collection<PhpClass> phpClasses = PhpElementsUtil.getClassesInterface(psiElement.getProject(), keyText);
                if(phpClasses.size() > 0) {
                    NavigationGutterIconBuilder<PsiElement> builder = NavigationGutterIconBuilder.create(Symfony2Icons.DOCTRINE_LINE_MARKER).
                        setTargets(phpClasses).
                        setTooltipText("Navigate to class");

                    lineMarkerInfos.add(builder.createLineMarkerInfo(psiElement));
                }
            }
        }
    }
}
 
Example 9
Source File: TwigTypeResolveUtil.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
private static Set<String> resolveTwigMethodName(Project project, Collection<String> previousElement, String typeName) {

        Set<String> types = new HashSet<>();

        for(String prevClass: previousElement) {
            for (PhpClass phpClass : PhpElementsUtil.getClassesInterface(project, prevClass)) {
                for(PhpNamedElement target : getTwigPhpNameTargets(phpClass, typeName)) {
                    types.addAll(target.getType().getTypes());
                }
            }
        }

        return types;
    }
 
Example 10
Source File: RouteFormLineMarkerProvider.java    From idea-php-drupal-symfony2-bridge with MIT License 4 votes vote down vote up
private void collectRouteInlineClasses(@NotNull Collection<LineMarkerInfo> results, @NotNull Project project, @NotNull PsiElement psiElement) {

        if(!(YamlElementPatternHelper.getSingleLineScalarKey("_form").accepts(psiElement) ||
            YamlElementPatternHelper.getSingleLineScalarKey("_entity_form").accepts(psiElement))
            ) {
            return;
        }

        PsiElement yamlScalar = psiElement.getParent();
        if(!(yamlScalar instanceof YAMLScalar)) {
            return;
        }

        String textValue = ((YAMLScalar) yamlScalar).getTextValue();

        Collection<PhpClass> classesInterface = new ArrayList<>(PhpElementsUtil.getClassesInterface(project, textValue));
        classesInterface.addAll(IndexUtil.getFormClassForId(project, textValue));

        if(classesInterface.size() == 0) {
            return;
        }

        PsiElement yamlKeyValue = yamlScalar.getParent();
        if(!(yamlKeyValue instanceof YAMLKeyValue)) {
            return;
        }

        YAMLMapping parentMapping = ((YAMLKeyValue) yamlKeyValue).getParentMapping();
        if(parentMapping == null) {
            return;
        }

        PsiElement parent = parentMapping.getParent();
        if(!(parent instanceof YAMLKeyValue)) {
            return;
        }

        String keyText = ((YAMLKeyValue) parent).getKeyText();

        if(!"defaults".equals(keyText)) {
            return;
        }

        YAMLMapping parentMapping1 = ((YAMLKeyValue) parent).getParentMapping();
        if(parentMapping1 == null) {
            return;
        }

        PsiElement parent1 = parentMapping1.getParent();
        if(!(parent1 instanceof YAMLKeyValue)) {
            return;
        }

        NavigationGutterIconBuilder<PsiElement> builder = NavigationGutterIconBuilder.create(Symfony2Icons.FORM_TYPE_LINE_MARKER).
            setTargets(classesInterface).
            setTooltipText("Navigate to form");

        results.add(builder.createLineMarkerInfo(parent1));
    }
 
Example 11
Source File: DoctrinePhpMappingDriver.java    From idea-php-symfony2-plugin with MIT License 4 votes vote down vote up
@Override
public DoctrineMetadataModel getMetadata(@NotNull DoctrineMappingDriverArguments args) {
    PsiFile psiFile = args.getPsiFile();
    if(!(psiFile instanceof PhpFile)) {
        return null;
    }

    Collection<DoctrineModelField> fields = new ArrayList<>();
    DoctrineMetadataModel model = new DoctrineMetadataModel(fields);

    for (PhpClass phpClass : PhpElementsUtil.getClassesInterface(args.getProject(), args.getClassName())) {
        // remove duplicate code
        // @TODO: fr.adrienbrault.idea.symfony2plugin.doctrine.EntityHelper.getModelFields()
        PhpDocComment docComment = phpClass.getDocComment();
        if(docComment == null) {
            continue;
        }

        // Doctrine ORM
        // @TODO: external split
        if(AnnotationBackportUtil.hasReference(docComment, "\\Doctrine\\ORM\\Mapping\\Entity", "\\TYPO3\\Flow\\Annotations\\Entity")) {

            // @TODO: reuse annotations plugin
            PhpDocTag phpDocTag = AnnotationBackportUtil.getReference(docComment, "\\Doctrine\\ORM\\Mapping\\Table");
            if(phpDocTag != null) {
                Matcher matcher = Pattern.compile("name[\\s]*=[\\s]*[\"|']([\\w_\\\\]+)[\"|']").matcher(phpDocTag.getText());
                if (matcher.find()) {
                    model.setTable(matcher.group(1));
                }
            }

            Map<String, Map<String, String>> maps = new HashMap<>();
            for(Field field: phpClass.getFields()) {
                if (field.isConstant()) {
                    continue;
                }

                if (!AnnotationBackportUtil.hasReference(field.getDocComment(), EntityHelper.ANNOTATION_FIELDS)) {
                    continue;
                }

                // context change is case of "trait" or extends
                PhpClass containingClass = field.getContainingClass();
                if (containingClass == null) {
                    continue;
                }

                // collect import context based on the class name
                String fqn = containingClass.getFQN();
                if (!maps.containsKey(fqn)) {
                    maps.put(fqn, AnnotationUtil.getUseImportMap(field.getDocComment()));
                }

                DoctrineModelField modelField = new DoctrineModelField(field.getName());
                EntityHelper.attachAnnotationInformation(containingClass, field, modelField.addTarget(field), maps.get(fqn));
                fields.add(modelField);
            }
        }
    }

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

    return model;
}
 
Example 12
Source File: YamlGoToDeclarationHandler.java    From idea-php-symfony2-plugin with MIT License 4 votes vote down vote up
@NotNull
private Collection<PsiElement> getClassGoto(@NotNull PsiElement psiElement) {
    String text = PsiElementUtils.trimQuote(psiElement.getText());
    return new ArrayList<>(PhpElementsUtil.getClassesInterface(psiElement.getProject(), text));
}
 
Example 13
Source File: ControllerIndex.java    From idea-php-symfony2-plugin with MIT License 4 votes vote down vote up
@NotNull
public Collection<Method> resolveShortcutName(@NotNull String controllerName) {
    String[] split = controllerName.split(":");

    // normalize: "FooBundle:Apple/Bar:foo" => FooBundle:Apple\Bar:foo
    // support: "FooBundle:Apple\Bar:foo" => FooBundle:Apple\Bar:foo\bar
    if(split.length == 3) {
        // normalize incoming path "/" => "\" this are PHP namespace but both supported
        split[1] = split[1].replaceAll("/+", "\\\\").replaceAll("\\\\+", "\\\\");
        split[2] = split[2].replaceAll("/+", "\\\\").replaceAll("\\\\+", "\\\\");

        Collection<Method> methods = new HashSet<>();
        for (SymfonyBundle symfonyBundle : new SymfonyBundleUtil(project).getBundles()) {
            // Bundle matched "AppBundle"
            if(split[0].equalsIgnoreCase(symfonyBundle.getName())) {
                String namespace = split[1] + "\\" + split[2];

                // last element is our method name
                int lastBackslash = namespace.lastIndexOf("\\");
                if(lastBackslash > 0) {
                    String methodName = namespace.substring(lastBackslash + 1);

                    // AppBundle/Controller/FooController
                    String className = symfonyBundle.getNamespaceName() + "Controller\\" + namespace.substring(0, lastBackslash) + "Controller";

                    for (PhpClass phpClass : PhpElementsUtil.getClassesInterface(project, className)) {

                        // cleanup action to support "fooAction" and "foo" methods
                        if(methodName.endsWith("Action")) {
                            methodName = methodName.substring(0, methodName.length() - "Action".length());
                        }

                        // find method
                        for (String string : new String[] {methodName, methodName + "Action"}) {
                            Method methodByName = phpClass.findMethodByName(string);
                            if(methodByName != null) {
                                methods.add(methodByName);
                            }
                        }
                    }
                }
            }
        }

        return methods;
    }

    ControllerAction controllerAction = new ControllerIndex(project).getControllerActionOnService(controllerName);
    if(controllerAction != null) {
        return Collections.singletonList(controllerAction.getMethod());
    }

    return Collections.emptyList();
}