Java Code Examples for com.jetbrains.php.PhpIndex#getBySignature()

The following examples show how to use com.jetbrains.php.PhpIndex#getBySignature() . 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: CodeInsightFixtureTestCase.java    From silex-idea-plugin with MIT License 6 votes vote down vote up
protected void assertPhpReferenceSignatureEquals(LanguageFileType languageFileType, @NotNull Class aClass, String configureByText, String typeSignature) {
    myFixture.configureByText(languageFileType, configureByText);
    PsiElement psiElement = myFixture.getFile().findElementAt(myFixture.getCaretOffset());

    psiElement = PsiTreeUtil.getParentOfType(psiElement, aClass);

    if (!(psiElement instanceof PhpReference)) {
        fail("Element is not PhpReference.");
    }

    PhpIndex phpIndex = PhpIndex.getInstance(myFixture.getProject());
    Collection<? extends PhpNamedElement> collection = phpIndex.getBySignature(((PhpReference)psiElement).getSignature(), null, 0);
    assertNotEmpty(collection);

    for (String type : collection.iterator().next().getType().getTypes()) {
        if (type.equals(typeSignature)) {
            return;
        }
    }

    fail("Can't find type: "+typeSignature+", found:"+collection.iterator().next().getType().toString());
}
 
Example 2
Source File: PhpTypeProviderUtil.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
/**
 * We can have multiple types inside a TypeProvider; split them on "|" so that we dont get empty types
 *
 * #M#x#M#C\FooBar.get?doctrine.odm.mongodb.document_manager.getRepository|
 * #M#x#M#C\FooBar.get?doctrine.odm.mongodb.document_manager.getRepository
 */
@NotNull
public static Collection<? extends PhpNamedElement> getTypeSignature(@NotNull PhpIndex phpIndex, @NotNull String signature) {

    if (!signature.contains("|")) {
        return phpIndex.getBySignature(signature, null, 0);
    }

    Collection<PhpNamedElement> elements = new HashSet<>();
    for (String s : signature.split("\\|")) {
        elements.addAll(phpIndex.getBySignature(s, null, 0));
    }

    return elements;
}
 
Example 3
Source File: PhpTypeProviderUtil.java    From idea-php-generics-plugin with MIT License 5 votes vote down vote up
/**
 * we can also pipe php references signatures and resolve them here
 * overwrite parameter to get string value
 */
@Nullable
public static String getResolvedParameter(@NotNull PhpIndex phpIndex, @NotNull String parameter, @Nullable Set<String> visited, int depth) {

    // PHP 5.5 class constant: "Class\Foo::class"
    if(parameter.startsWith("#K#C")) {
        // PhpStorm9: #K#C\Class\Foo.class
        if(parameter.endsWith(".class")) {
            return StringUtils.stripStart(parameter.substring(4, parameter.length() - 6), "\\");
        }
    }

    // #K#C\Class\Foo.property
    // #K#C\Class\Foo.CONST
    if(parameter.startsWith("#")) {

        // get psi element from signature
        Collection<? extends PhpNamedElement> signTypes = phpIndex.getBySignature(parameter, visited, depth);
        if(signTypes.size() == 0) {
            return null;
        }

        // get string value
        parameter = GenericsUtil.getStringValue(signTypes.iterator().next());
        if(parameter == null) {
            return null;
        }

    }

    return parameter;
}
 
Example 4
Source File: PhpTypeProviderUtil.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
/**
 * we can also pipe php references signatures and resolve them here
 * overwrite parameter to get string value
 */
@Nullable
public static String getResolvedParameter(@NotNull PhpIndex phpIndex, @NotNull String parameter) {

    // PHP 5.5 class constant: "Class\Foo::class"
    if(parameter.startsWith("#K#C")) {
        // PhpStorm9: #K#C\Class\Foo.class
        if(parameter.endsWith(".class")) {
            return StringUtils.stripStart(parameter.substring(4, parameter.length() - 6), "\\");
        }
    }

    // #K#C\Class\Foo.property
    // #K#C\Class\Foo.CONST
    if(parameter.startsWith("#")) {

        // get psi element from signature
        Collection<? extends PhpNamedElement> signTypes = phpIndex.getBySignature(parameter, null, 0);
        if(signTypes.size() == 0) {
            return null;
        }

        // get string value
        parameter = PhpElementsUtil.getStringValue(signTypes.iterator().next());
        if(parameter == null) {
            return null;
        }

    }

    return parameter;
}
 
Example 5
Source File: PhpTypeProviderUtil.java    From idea-php-toolbox with MIT License 5 votes vote down vote up
/**
 * we can also pipe php references signatures and resolve them here
 * overwrite parameter to get string value
 */
@Nullable
public static String getResolvedParameter(PhpIndex phpIndex, String parameter) {

    // PHP 5.5 class constant: "Class\Foo::class"
    if(parameter.startsWith("#K#C")) {
        // PhpStorm9: #K#C\Class\Foo.class
        if(parameter.endsWith(".class")) {
            return StringUtils.stripStart(parameter.substring(4, parameter.length() - 6), "\\");
        }
    }

    // #K#C\Class\Foo.property
    // #K#C\Class\Foo.CONST
    if(parameter.startsWith("#")) {

        // get psi element from signature
        Collection<? extends PhpNamedElement> signTypes = phpIndex.getBySignature(parameter, null, 0);
        if(signTypes.size() == 0) {
            return null;
        }

        // get string value
        parameter = PhpElementsUtil.getStringValue(signTypes.iterator().next());
        if(parameter == null) {
            return null;
        }

    }

    return parameter;
}
 
Example 6
Source File: PhpTypeProviderUtil.java    From idea-php-laravel-plugin with MIT License 5 votes vote down vote up
/**
 * We can have multiple types inside a TypeProvider; split them on "|" so that we dont get empty types
 *
 * #M#x#M#C\FooBar.get?doctrine.odm.mongodb.document_manager.getRepository|
 * #M#x#M#C\FooBar.get?doctrine.odm.mongodb.document_manager.getRepository
 */
@NotNull
public static Collection<? extends PhpNamedElement> getTypeSignature(@NotNull PhpIndex phpIndex, @NotNull String signature) {

    if (!signature.contains("|")) {
        return phpIndex.getBySignature(signature, null, 0);
    }

    Collection<PhpNamedElement> elements = new ArrayList<PhpNamedElement>();
    for (String s : signature.split("\\|")) {
        elements.addAll(phpIndex.getBySignature(s, null, 0));
    }

    return elements;
}
 
Example 7
Source File: PhpTypeProviderUtil.java    From idea-php-laravel-plugin with MIT License 5 votes vote down vote up
/**
 * we can also pipe php references signatures and resolve them here
 * overwrite parameter to get string value
 */
@Nullable
public static String getResolvedParameter(@NotNull PhpIndex phpIndex, @NotNull String parameter) {

    // PHP 5.5 class constant: "Class\Foo::class"
    if(parameter.startsWith("#K#C")) {
        // PhpStorm9: #K#C\Class\Foo.class
        if(parameter.endsWith(".class")) {
            return parameter.substring(4, parameter.length() - 6);
        }

        // PhpStorm8: #K#C\Class\Foo.
        // workaround since signature has empty type
        if(parameter.endsWith(".")) {
            return parameter.substring(4, parameter.length() - 1);
        }
    }

    // #K#C\Class\Foo.property
    // #K#C\Class\Foo.CONST
    if(parameter.startsWith("#")) {

        // get psi element from signature
        Collection<? extends PhpNamedElement> signTypes = phpIndex.getBySignature(parameter, null, 0);
        if(signTypes.size() == 0) {
            return null;
        }

        // get string value
        parameter = PhpElementsUtil.getStringValue(signTypes.iterator().next());
        if(parameter == null) {
            return null;
        }

    }

    return parameter;
}
 
Example 8
Source File: CodeInsightFixtureTestCase.java    From silex-idea-plugin with MIT License 5 votes vote down vote up
protected void assertTypeEquals(LanguageFileType languageFileType, @NotNull Class aClass, String configureByText, String phpClassType) {
    myFixture.configureByText(languageFileType, configureByText);
    PsiElement psiElement = myFixture.getFile().findElementAt(myFixture.getCaretOffset());

    psiElement = PsiTreeUtil.getParentOfType(psiElement, aClass);

    if (!(psiElement instanceof PhpReference)) {
        fail("Element is not PhpReference.");
    }

    PhpIndex phpIndex = PhpIndex.getInstance(myFixture.getProject());
    Collection<? extends PhpNamedElement> collection = phpIndex.getBySignature(((PhpReference)psiElement).getSignature(), null, 0);
    assertNotEmpty(collection);

    String types = "";

    for (String type : collection.iterator().next().getType().getTypes()) {
        Collection<? extends PhpNamedElement> col = phpIndex.getBySignature(type, null, 0);
        if (col.size() == 0) {
            continue;
        }

        for (String classType : col.iterator().next().getType().getTypes()) {
            types = types + classType + '|';
            if (classType.equals(phpClassType)) {
                return;
            }
        }
    }

    fail("Can't find type: "+phpClassType+", found:"+types);
}
 
Example 9
Source File: Utils.java    From silex-idea-plugin with MIT License 5 votes vote down vote up
public static String getResolvedParameter(PhpIndex phpIndex, String parameter) {

        // PHP 5.5 class constant: "Class\Foo::class"
        if(parameter.startsWith("#K#C")) {
            // PhpStorm9: #K#C\Class\Foo.class
            if(parameter.endsWith(".class")) {
                return parameter.substring(5, parameter.length() - 6);
            }

            // PhpStorm8: #K#C\Class\Foo.
            // workaround since signature has empty type
            if(parameter.endsWith(".")) {
                return parameter.substring(5, parameter.length() - 1);
            }
        }

        // #P#C\Class\Foo.property
        // #K#C\Class\Foo.CONST
        if(parameter.startsWith("#")) {

            Collection<? extends PhpNamedElement> signTypes = phpIndex.getBySignature(parameter);
            if(signTypes.size() == 0) {
                return "";
            }

            parameter = Utils.getStringValue(signTypes.iterator().next());
            if(parameter == null) {
                return "";
            }
        }

        return parameter;
    }
 
Example 10
Source File: PimplePhpTypeProvider.java    From silex-idea-plugin with MIT License 5 votes vote down vote up
@Override
public Collection<? extends PhpNamedElement> getBySignature(String expression, Project project) {

    PhpIndex phpIndex = PhpIndex.getInstance(project);
    Signature signature = new Signature(expression);

    // try to resolve service type
    if(ProjectComponent.isEnabled(project) && signature.hasParameter()) {
        ArrayList<String> parameters = new ArrayList<String>();
        if (Utils.findPimpleContainer(phpIndex, expression, parameters)) {
            return phpIndex.getClassesByFQN(getClassNameFromParameters(phpIndex, project, parameters));
        }
    }

    // if it's not a service try to get original type
    Collection<? extends PhpNamedElement> collection = phpIndex.getBySignature(signature.base, null, 0);
    if (collection.size() == 0) {
        return Collections.emptySet();
    }

    // original type can be array (#C\ClassType[]) resolve to proper value type
    PhpNamedElement element = collection.iterator().next();

    for (String type : element.getType().getTypes()) {
        if (type.endsWith("[]")) {
            Collection<? extends PhpNamedElement> result = phpIndex.getClassesByFQN(type.substring(0, type.length() - 2));
            if (result.size() != 0) {
                return result;
            }
        }
    }

    return collection;
}
 
Example 11
Source File: PhpTypeProviderUtil.java    From idea-php-toolbox with MIT License 5 votes vote down vote up
/**
 * we can also pipe php references signatures and resolve them here
 * overwrite parameter to get string value
 */
@Nullable
public static String getResolvedParameter(PhpIndex phpIndex, String parameter) {

    // PHP 5.5 class constant: "Class\Foo::class"
    if(parameter.startsWith("#K#C")) {
        // PhpStorm9: #K#C\Class\Foo.class
        if(parameter.endsWith(".class")) {
            return StringUtils.stripStart(parameter.substring(4, parameter.length() - 6), "\\");
        }
    }

    // #K#C\Class\Foo.property
    // #K#C\Class\Foo.CONST
    if(parameter.startsWith("#")) {

        // get psi element from signature
        Collection<? extends PhpNamedElement> signTypes = phpIndex.getBySignature(parameter, null, 0);
        if(signTypes.size() == 0) {
            return null;
        }

        // get string value
        parameter = PhpElementsUtil.getStringValue(signTypes.iterator().next());
        if(parameter == null) {
            return null;
        }

    }

    return parameter;
}
 
Example 12
Source File: PhpTypeProviderUtil.java    From idea-php-generics-plugin with MIT License 5 votes vote down vote up
/**
 * We can have multiple types inside a TypeProvider; split them on "|" so that we dont get empty types
 *
 * #M#x#M#C\FooBar.get?doctrine.odm.mongodb.document_manager.getRepository|
 * #M#x#M#C\FooBar.get?doctrine.odm.mongodb.document_manager.getRepository
 */
@NotNull
public static Collection<? extends PhpNamedElement> getTypeSignature(@NotNull PhpIndex phpIndex, @NotNull String signature) {

    if (!signature.contains("|")) {
        return phpIndex.getBySignature(signature, null, 0);
    }

    Collection<PhpNamedElement> elements = new ArrayList<>();
    for (String s : signature.split("\\|")) {
        elements.addAll(phpIndex.getBySignature(s, null, 0));
    }

    return elements;
}
 
Example 13
Source File: Utils.java    From silex-idea-plugin with MIT License 4 votes vote down vote up
private static Boolean findPimpleContainer(PhpIndex phpIndex, String expression, ArrayList<String> parameters, int depth) {

        if (++depth > 5) {
            return false;
        }

        Signature signature = new Signature(expression);
        Collection<? extends PhpNamedElement> collection;

        if (expression.startsWith("#")) {
            collection = phpIndex.getBySignature(signature.base, null, 0);
        } else {
            collection = phpIndex.getClassesByFQN(signature.base);
        }

        if (collection.size() == 0) {
            return false;
        }

        PhpNamedElement element = collection.iterator().next();

        if (element instanceof PhpClass) {
            if (Utils.isPimpleContainerClass((PhpClass) element)) {
                if (signature.hasParameter()) {
                    parameters.add(signature.parameter);
                }
                return true;
            }
        }

        for (String type : element.getType().getTypes()) {

            if (findPimpleContainer(phpIndex, type, parameters, depth)) {
                if (signature.hasParameter()) {
                    parameters.add(signature.parameter);
                }
                return true;
            }
        }

        return false;
    }
 
Example 14
Source File: ShopwareApiResourcesTypeProvider.java    From idea-php-shopware-plugin with MIT License 4 votes vote down vote up
@Override
public Collection<? extends PhpNamedElement> getBySignature(String expression, Set<String> visited, int depth, Project project) {

    // get back our original call
    // since phpstorm 7.1.2 we need to validate this
    int endIndex = expression.lastIndexOf(TRIM_KEY);
    if(endIndex == -1) {
        return Collections.emptySet();
    }

    String originalSignature = expression.substring(0, endIndex);
    String parameter = expression.substring(endIndex + 1);

    // search for called method
    PhpIndex phpIndex = PhpIndex.getInstance(project);
    Collection<? extends PhpNamedElement> phpNamedElementCollections = phpIndex.getBySignature(originalSignature, null, 0);
    if(phpNamedElementCollections.size() == 0) {
        return Collections.emptySet();
    }

    // get first matched item
    PhpNamedElement phpNamedElement = phpNamedElementCollections.iterator().next();
    if(!(phpNamedElement instanceof Method)) {
        return Collections.emptySet();
    }

    parameter = PhpTypeProviderUtil.getResolvedParameter(phpIndex, parameter);
    if(parameter == null) {
        return Collections.emptySet();
    }

    // finally search the classes
    if(PhpElementsUtil.isMethodInstanceOf((Method) phpNamedElement, "\\Shopware\\Components\\Api\\Manager", "getResource")) {
        PhpClass phpClass = ShopwareUtil.getResourceClass(project, parameter);
        if(phpClass != null) {
            return Collections.singletonList(phpClass);
        }
    }

    return phpNamedElementCollections;
}
 
Example 15
Source File: PhpToolboxTypeProvider.java    From idea-php-toolbox with MIT License 4 votes vote down vote up
@Override
public Collection<? extends PhpNamedElement> getBySignature(String expression, Set<String> visited, int depth, Project project) {

    // get back our original call
    // since phpstorm 7.1.2 we need to validate this
    int endIndex = expression.indexOf(String.valueOf(TRIM_KEY));
    if(endIndex == -1) {
        return Collections.emptySet();
    }

    String originalSignature = expression.substring(0, endIndex);
    String parametersSignature = expression.substring(endIndex + 1);

    // search for called method
    PhpIndex phpIndex = PhpIndex.getInstance(project);
    Collection<? extends PhpNamedElement> phpNamedElements = phpIndex.getBySignature(originalSignature, null, 0);
    if(phpNamedElements.size() == 0) {
        return Collections.emptySet();
    }

    // get first matched item
    PhpNamedElement phpNamedElement = phpNamedElements.iterator().next();
    if(!(phpNamedElement instanceof Function)) {
        return phpNamedElements;
    }

    Map<Integer, String> parameters = getParameters(parametersSignature, phpIndex);
    if (parameters.isEmpty()) {
        return phpNamedElements;
    }

    Map<String, Collection<JsonRawLookupElement>> providerMap = ExtensionProviderUtil.getProviders(
        project,
        ApplicationManager.getApplication().getComponent(PhpToolboxApplicationService.class)
    );

    Set<Pair<String, Integer>> providers = getProviderNames(project, (Function) phpNamedElement);

    Collection<PhpNamedElement> elements = new HashSet<>();
    elements.addAll(phpNamedElements);

    for (Pair<String, Integer> providerPair : providers) {
        String providerName = providerPair.first;
        Integer index = providerPair.second;

        String parameter = parameters.get(index);
        if (parameter == null) {
            continue;
        }

        PhpToolboxProviderInterface provider = ExtensionProviderUtil.getProvider(project, providerName);
        if(!(provider instanceof PhpToolboxTypeProviderInterface)) {
            continue;
        }

        PhpToolboxTypeProviderArguments args = new PhpToolboxTypeProviderArguments(
            project,
            parameter,
            providerMap.containsKey(providerName) ? providerMap.get(providerName) : Collections.emptyList()
        );

        Collection<PhpNamedElement> items = ((PhpToolboxTypeProviderInterface) provider).resolveParameter(args);
        if(items != null && items.size() > 0) {
            elements.addAll(items);
        }
    }

    return elements;
}
 
Example 16
Source File: ObjectRepositoryResultTypeProvider.java    From idea-php-symfony2-plugin with MIT License 4 votes vote down vote up
/**
 * We can have multiple types inside a TypeProvider; split them on "|" so that we dont get empty types
 *
 * #M#x#M#C\FooBar.get?doctrine.odm.mongodb.document_manager.getRepository|
 * #M#x#M#C\FooBar.get?doctrine.odm.mongodb.document_manager.getRepository
 */
@NotNull
private static Collection<? extends PhpNamedElement> getTypeSignatureMagic(@NotNull PhpIndex phpIndex, @NotNull String signature) {
    // magic method resolving; we need to have the ObjectRepository method which does not exists for magic methods, so strip it
    // #M#x#M#C\FooBar.get?doctrine.odm.mongodb.document_manager.findByName => findBy
    // #M#x#M#C\FooBar.get?doctrine.odm.mongodb.document_manager.findOneBy => findOne
    Collection<PhpNamedElement> elements = new HashSet<>();
    for (String s : signature.split("\\|")) {
        int i = s.lastIndexOf(".");
        if (i > 0) {
            // method already exists in repository use it
            for (PhpNamedElement phpNamedElement : phpIndex.getBySignature(s, null, 0)) {
                if (phpNamedElement instanceof Method) {
                    if (MANAGED_FIND_METHOD.contains(phpNamedElement.getName())) {
                        continue;
                    }

                    // we got into the repository itself so stop here; was not overwritten by repository class
                    PhpClass containingClass = ((Method) phpNamedElement).getContainingClass();
                    if (PhpElementsUtil.isEqualClassName(containingClass, "\\Doctrine\\Persistence\\ObjectRepository") || PhpElementsUtil.isEqualClassName(containingClass, "\\Doctrine\\Common\\Persistence\\ObjectRepository")) {
                        continue;
                    }

                    return Collections.emptyList();
                }
            }

            // strip field name from the method name "findOneByName" => "findOneBy"
            String substring = s.substring(i + 1);
            if (substring.startsWith("findOneBy")) {
                s = s.substring(0, i + 1) + "findOneBy";
            } else if(substring.startsWith("findBy")) {
                s = s.substring(0, i + 1) + "findBy";
            }
        }

        elements.addAll(phpIndex.getBySignature(s, null, 0));
    }

    return elements;
}
 
Example 17
Source File: PhpToolboxTypeProvider.java    From idea-php-toolbox with MIT License 4 votes vote down vote up
@Override
public Collection<? extends PhpNamedElement> getBySignature(String expression, Set<String> visited, int depth, Project project) {

    // get back our original call
    // since phpstorm 7.1.2 we need to validate this
    int endIndex = expression.indexOf(String.valueOf(TRIM_KEY));
    if(endIndex == -1) {
        return Collections.emptySet();
    }

    String originalSignature = expression.substring(0, endIndex);
    String parametersSignature = expression.substring(endIndex + 1);

    // search for called method
    PhpIndex phpIndex = PhpIndex.getInstance(project);
    Collection<? extends PhpNamedElement> phpNamedElements = phpIndex.getBySignature(originalSignature, null, 0);
    if(phpNamedElements.size() == 0) {
        return Collections.emptySet();
    }

    // get first matched item
    PhpNamedElement phpNamedElement = phpNamedElements.iterator().next();
    if(!(phpNamedElement instanceof Function)) {
        return phpNamedElements;
    }

    Map<Integer, String> parameters = getParameters(parametersSignature, phpIndex);
    if (parameters.isEmpty()) {
        return phpNamedElements;
    }

    Map<String, Collection<JsonRawLookupElement>> providerMap = ExtensionProviderUtil.getProviders(
        project,
        ApplicationManager.getApplication().getComponent(PhpToolboxApplicationService.class)
    );

    Set<Pair<String, Integer>> providers = getProviderNames(project, (Function) phpNamedElement);

    Collection<PhpNamedElement> elements = new HashSet<>();
    elements.addAll(phpNamedElements);

    for (Pair<String, Integer> providerPair : providers) {
        String providerName = providerPair.first;
        Integer index = providerPair.second;

        String parameter = parameters.get(index);
        if (parameter == null) {
            continue;
        }

        PhpToolboxProviderInterface provider = ExtensionProviderUtil.getProvider(project, providerName);
        if(!(provider instanceof PhpToolboxTypeProviderInterface)) {
            continue;
        }

        PhpToolboxTypeProviderArguments args = new PhpToolboxTypeProviderArguments(
            project,
            parameter,
            providerMap.containsKey(providerName) ? providerMap.get(providerName) : Collections.emptyList()
        );

        Collection<PhpNamedElement> items = ((PhpToolboxTypeProviderInterface) provider).resolveParameter(args);
        if(items != null && items.size() > 0) {
            elements.addAll(items);
        }
    }

    return elements;
}