fr.adrienbrault.idea.symfony2plugin.util.PhpTypeProviderUtil Java Examples

The following examples show how to use fr.adrienbrault.idea.symfony2plugin.util.PhpTypeProviderUtil. 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: ObjectRepositoryTypeProvider.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
@Nullable
@Override
public PhpType getType(PsiElement e) {
    if (!Settings.getInstance(e.getProject()).pluginEnabled) {
        return null;
    }

    if(!(e instanceof MethodReference) || !PhpElementsUtil.isMethodWithFirstStringOrFieldReference(e, "getRepository")) {
        return null;
    }


    String refSignature = ((MethodReference)e).getSignature();
    if(StringUtil.isEmpty(refSignature)) {
        return null;
    }

    String signature = PhpTypeProviderUtil.getReferenceSignatureByFirstParameter((MethodReference) e, TRIM_KEY);
    return signature == null ? null : new PhpType().add("#" + this.getKey() + signature);
}
 
Example #2
Source File: PhpTypeProviderUtilTest.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
/**
 * @see PhpTypeProviderUtil#getResolvedParameter
 */
public void testGetTypeSignature() {
    Function<PhpNamedElement, String> func = phpNamedElement ->
        phpNamedElement instanceof Method ? ((Method) phpNamedElement).getContainingClass().getFQN() : null;

    ArrayList<? extends PhpNamedElement> typeSignature = new ArrayList<PhpNamedElement>(PhpTypeProviderUtil.getTypeSignature(
        PhpIndex.getInstance(getProject()),
        "#M#C\\Doctrine\\Common\\Persistence\\ObjectManager.getRepository|#M#C\\Doctrine\\Common\\Persistence\\ObjectFoo.getRepository"
    ));

    assertContainsElements(ContainerUtil.map(typeSignature, func), "\\Doctrine\\Common\\Persistence\\ObjectManager", "\\Doctrine\\Common\\Persistence\\ObjectFoo");

    typeSignature = new ArrayList<PhpNamedElement>(PhpTypeProviderUtil.getTypeSignature(
        PhpIndex.getInstance(getProject()),
        "#M#C\\Doctrine\\Common\\Persistence\\ObjectManager.getRepository"
    ));
    assertContainsElements(ContainerUtil.map(typeSignature, func), "\\Doctrine\\Common\\Persistence\\ObjectManager");
}
 
Example #3
Source File: DicTypeProvider.java    From idea-php-laravel-plugin with MIT License 5 votes vote down vote up
@Nullable
@Override
public PhpType getType(PsiElement psiElement) {
    if (!LaravelSettings.getInstance(psiElement.getProject()).pluginEnabled) {
        return null;
    }

    // container calls are only on "get" methods
    if(psiElement instanceof FunctionReference && "app".equals(((FunctionReference) psiElement).getName())) {
        PsiElement[] parameters = ((FunctionReference) psiElement).getParameters();
        if(parameters.length > 0 && parameters[0] instanceof StringLiteralExpression) {
            String contents = ((StringLiteralExpression) parameters[0]).getContents();
            if(StringUtils.isNotBlank(contents)) {
                return new PhpType().add(
                    "#" + this.getKey() + ((FunctionReference) psiElement).getSignature() + TRIM_KEY + contents
                );
            }
        }
    }

    // container calls are only on "get" methods
    if(psiElement instanceof MethodReference && PhpElementsUtil.isMethodWithFirstStringOrFieldReference(psiElement, "make")) {
        String referenceSignature = PhpTypeProviderUtil.getReferenceSignature((MethodReference) psiElement, TRIM_KEY);
        if(referenceSignature != null) {
            return new PhpType().add("#" + this.getKey() + referenceSignature);
        }
    }

    return null;
}
 
Example #4
Source File: ShopwareApiResourcesTypeProvider.java    From idea-php-shopware-plugin with MIT License 5 votes vote down vote up
@Nullable
@Override
public PhpType getType(PsiElement e) {
    if (!Settings.getInstance(e.getProject()).pluginEnabled) {
        return null;
    }

    // container calls are only on "get" methods
    if(!(e instanceof MethodReference) || !PhpElementsUtil.isMethodWithFirstStringOrFieldReference(e, "getResource")) {
        return null;
    }

    String signature = PhpTypeProviderUtil.getReferenceSignatureByFirstParameter((MethodReference) e, TRIM_KEY);
    return signature == null ? null : new PhpType().add("#" + this.getKey() + signature);
}
 
Example #5
Source File: ObjectManagerFindTypeProvider.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
@Nullable
@Override
public PhpType getType(PsiElement e) {
    if (!Settings.getInstance(e.getProject()).pluginEnabled) {
        return null;
    }

    if(!(e instanceof MethodReference) || !PhpElementsUtil.isMethodWithFirstStringOrFieldReference(e, "find")) {
        return null;
    }

    String refSignature = ((MethodReference)e).getSignature();
    if(StringUtil.isEmpty(refSignature)) {
        return null;
    }

    // we need the param key on getBySignature(), since we are already in the resolved method there attach it to signature
    // param can have dotted values split with \
    PsiElement[] parameters = ((MethodReference)e).getParameters();
    if (parameters.length >= 2) {
        String signature = PhpTypeProviderUtil.getReferenceSignatureByFirstParameter((MethodReference) e, TRIM_KEY);
        if(signature != null) {
            return new PhpType().add("#" + this.getKey() + signature);
        }
    }

    return null;
}
 
Example #6
Source File: SymfonyContainerTypeProvider.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
@Nullable
@Override
public PhpType getType(PsiElement e) {
    if (!Settings.getInstance(e.getProject()).pluginEnabled) {
        return null;
    }

    // container calls are only on "get" methods
    if(!(e instanceof MethodReference) || !PhpElementsUtil.isMethodWithFirstStringOrFieldReference(e, "get")) {
        return null;
    }

    String signature = PhpTypeProviderUtil.getReferenceSignatureByFirstParameter((MethodReference) e, TRIM_KEY);
    return signature == null ? null : new PhpType().add("#" + this.getKey() + signature);
}
 
Example #7
Source File: PhpTypeProviderUtilTest.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
/**
 * @see PhpTypeProviderUtil#mergeSignatureResults
 */
public void testMergeSignatureResults() {

    Collection<PhpNamedElement> phpNamedElements = new ArrayList<>();
    phpNamedElements.add(PhpElementsUtil.getClassMethod(getProject(), "PhpType\\Bar", "foo"));
    phpNamedElements.add(PhpElementsUtil.getClassMethod(getProject(), "PhpType\\Bar", "bar"));
    phpNamedElements.add(PhpElementsUtil.getClassMethod(getProject(), "PhpType\\Bar", "car"));

    Collection<? extends PhpNamedElement> elements = PhpTypeProviderUtil.mergeSignatureResults(phpNamedElements, PhpElementsUtil.getClass(getProject(), "\\PhpType\\Foo"));
    assertEquals(2, elements.size());
}
 
Example #8
Source File: PhpTypeProviderUtilTest.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
/**
 * @see PhpTypeProviderUtil#getReferenceSignatureByFirstParameter
 */
public void testGetReferenceSignature() {
    assertEquals("#F\\foo|foobar", PhpTypeProviderUtil.getReferenceSignatureByFirstParameter(
        PhpPsiElementFactory.createFunctionReference(getProject(), "<?php foo('foobar');"), "|".charAt(0))
    );

    assertEquals("#F\\foo|#K#C\\Foo.class", PhpTypeProviderUtil.getReferenceSignatureByFirstParameter(
        PhpPsiElementFactory.createFunctionReference(getProject(), "<?php class Foo {}; foo(Foo::class);"), "|".charAt(0))
    );

    assertEquals("#F\\foo|#P#C\\Foo.foo", PhpTypeProviderUtil.getReferenceSignatureByFirstParameter(
        PhpPsiElementFactory.createFunctionReference(getProject(), "<?php class Foo { private $foo = 'foobar' \n private function() { foo($this->foo); } }; "), "|".charAt(0))
    );
}
 
Example #9
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 #10
Source File: ObjectRepositoryTypeProvider.java    From idea-php-symfony2-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
    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 = PhpTypeProviderUtil.getTypeSignature(phpIndex, originalSignature);
    if(phpNamedElementCollections.size() == 0) {
        return Collections.emptySet();
    }

    PhpNamedElement phpNamedElement = phpNamedElementCollections.iterator().next();
    if(!(phpNamedElement instanceof Method)) {
        return phpNamedElementCollections;
    }

    if (!PhpElementsUtil.isMethodInstanceOf((Method) phpNamedElement, GET_REPOSITORIES_SIGNATURES)) {
        return phpNamedElementCollections;
    }

    // we can also pipe php references signatures and resolve them here
    // overwrite parameter to get string value
    parameter = PhpTypeProviderUtil.getResolvedParameter(phpIndex, parameter);
    if(parameter == null) {
        return phpNamedElementCollections;
    }

    PhpClass phpClass = EntityHelper.getEntityRepositoryClass(project, parameter);
    if(phpClass == null) {
        // self add :)
        return phpNamedElementCollections;
    }

    return PhpTypeProviderUtil.mergeSignatureResults(phpNamedElementCollections, phpClass);
}
 
Example #11
Source File: ObjectManagerFindTypeProvider.java    From idea-php-symfony2-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
    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 = PhpTypeProviderUtil.getTypeSignature(phpIndex, originalSignature);
    if(phpNamedElementCollections.size() == 0) {
        return Collections.emptySet();
    }

    PhpNamedElement phpNamedElement = phpNamedElementCollections.iterator().next();
    if(!(phpNamedElement instanceof Method)) {
        return Collections.emptySet();
    }

    if (!(
        PhpElementsUtil.isMethodInstanceOf((Method) phpNamedElement, "\\Doctrine\\Common\\Persistence\\ObjectManager", "find") ||
        PhpElementsUtil.isMethodInstanceOf((Method) phpNamedElement, "\\Doctrine\\Persistence\\ObjectManager", "find")
    )) {
        return Collections.emptySet();
    }

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

    PhpClass phpClass = EntityHelper.resolveShortcutName(project, parameter);
    if(phpClass == null) {
        return Collections.emptySet();
    }

    return PhpTypeProviderUtil.mergeSignatureResults(phpNamedElementCollections, phpClass);
}
 
Example #12
Source File: ObjectRepositoryResultTypeProvider.java    From idea-php-symfony2-plugin with MIT License 4 votes vote down vote up
@Nullable
@Override
public PhpType complete(String s, Project project) {
    int endIndex = s.lastIndexOf(TRIM_KEY);
    if(endIndex == -1) {
        return null;
    }

    String originalSignature = s.substring(0, endIndex);
    String parameter = s.substring(endIndex + 1);
    parameter = PhpTypeProviderUtil.getResolvedParameter(PhpIndex.getInstance(project), parameter);
    if(parameter == null) {
        return null;
    }

    PhpClass phpClass = EntityHelper.resolveShortcutName(project, parameter);
    if(phpClass == null) {
        return null;
    }

    PhpIndex phpIndex = PhpIndex.getInstance(project);

    Collection<? extends PhpNamedElement> typeSignature = getTypeSignatureMagic(phpIndex, originalSignature);

    // ->getRepository(SecondaryMarket::class)->findAll() => "findAll", but only if its a instance of this method;
    // so non Doctrine method are already filtered
    Set<String> resolveMethods = getObjectRepositoryCall(typeSignature).stream()
        .map(PhpNamedElement::getName)
        .collect(Collectors.toSet());

    if (resolveMethods.isEmpty()) {
        return null;
    }

    PhpType phpType = new PhpType();

    resolveMethods.stream()
        .map(name -> name.equals("findAll") || name.startsWith("findBy") ? phpClass.getFQN() + "[]" : phpClass.getFQN())
        .collect(Collectors.toSet())
        .forEach(phpType::add);

    return phpType;
}
 
Example #13
Source File: MethodSignatureTypeProvider.java    From idea-php-symfony2-plugin with MIT License 4 votes vote down vote up
@Nullable
@Override
public PhpType getType(PsiElement e) {
    if (!Settings.getInstance(e.getProject()).pluginEnabled || !(e instanceof MethodReference)) {
        return null;
    }

    Collection<MethodSignatureSetting> signatures = getSignatureSettings(e);
    if(signatures.size() == 0) {
        return null;
    }

    MethodReference methodReference = (MethodReference) e;
    Collection<MethodSignatureSetting> matchedSignatures = getSignatureSetting(methodReference.getName(), signatures);
    if(matchedSignatures.size() == 0) {
        return null;
    }

    String refSignature = ((MethodReference)e).getSignature();
    if(StringUtil.isEmpty(refSignature)) {
        return null;
    }

    // we need the param key on getBySignature(), since we are already in the resolved method there attach it to signature
    // param can have dotted values split with \
    PsiElement[] parameters = ((MethodReference)e).getParameters();
    for(MethodSignatureSetting methodSignature: matchedSignatures) {
        if (parameters.length - 1 >= methodSignature.getIndexParameter()) {
            PsiElement parameter = parameters[methodSignature.getIndexParameter()];
            if ((parameter instanceof StringLiteralExpression)) {
                String param = ((StringLiteralExpression)parameter).getContents();
                if (StringUtil.isNotEmpty(param)) {
                    return new PhpType().add("#" + this.getKey() + refSignature + TRIM_KEY + param);
                }
            }

            String parameterSignature = PhpTypeProviderUtil.getReferenceSignatureByFirstParameter(methodReference, TRIM_KEY);
            if(parameterSignature != null) {
                return new PhpType().add("#" + this.getKey() + parameterSignature);
            }
        }
    }

    return null;
}
 
Example #14
Source File: MethodSignatureTypeProvider.java    From idea-php-symfony2-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
    int endIndex = expression.lastIndexOf(TRIM_KEY);
    if(endIndex == -1) {
        return null;
    }

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

    PhpIndex phpIndex = PhpIndex.getInstance(project);
    Collection<? extends PhpNamedElement> phpNamedElementCollections = PhpTypeProviderUtil.getTypeSignature(phpIndex, originalSignature);
    if(phpNamedElementCollections.size() == 0) {
        return null;
    }

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

    Collection<MethodSignatureSetting> signatures = getSignatureSettings(phpNamedElement);
    if(signatures.size() == 0) {
        return null;
    }

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

    Collection<PhpNamedElement> phpNamedElements = new ArrayList<>();

    for(MethodSignatureSetting matchedSignature: signatures) {
        for(PhpTypeSignatureInterface signatureTypeProvider: PhpTypeSignatureTypes.DEFAULT_PROVIDER) {
            if( signatureTypeProvider.getName().equals(matchedSignature.getReferenceProviderName()) && PhpElementsUtil.isMethodInstanceOf((Method) phpNamedElement, matchedSignature.getCallTo(), matchedSignature.getMethodName())) {
                Collection<? extends PhpNamedElement> namedElements = signatureTypeProvider.getByParameter(project, parameter);
                if(namedElements != null) {
                    phpNamedElements.addAll(namedElements);
                }
            }
        }
    }

    // not good but we need return any previous types: null clears all types
    return new ArrayList<>(phpNamedElements);
}
 
Example #15
Source File: SymfonyContainerTypeProvider.java    From idea-php-symfony2-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 = PhpTypeProviderUtil.getTypeSignature(phpIndex, originalSignature);
    if(phpNamedElementCollections.size() == 0) {
        return Collections.emptySet();
    }

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

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

    // finally search the classes
    if(PhpElementsUtil.isMethodInstanceOf((Method) phpNamedElement, ServiceContainerUtil.SERVICE_GET_SIGNATURES)) {
        ContainerService containerService = ContainerCollectionResolver.getService(project, parameter);

        if(containerService != null) {
            Collection<PhpNamedElement> phpClasses = new HashSet<>();

            for (String s : containerService.getClassNames()) {
                phpClasses.addAll(PhpIndex.getInstance(project).getAnyByFQN(s));
            }

            return phpClasses;
        }
    }

    return phpNamedElementCollections;
}
 
Example #16
Source File: PhpTypeProviderUtilTest.java    From idea-php-symfony2-plugin with MIT License 4 votes vote down vote up
/**
 * @see PhpTypeProviderUtil#getResolvedParameter
 */
public void testGetResolvedParameter() {
    assertEquals("Class\\Foo", PhpTypeProviderUtil.getResolvedParameter(PhpIndex.getInstance(getProject()), "#K#C\\Class\\Foo.class"));
}