Skip to content

Fix doctrine repository find() methods not always returning the correct type hints #1434

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.jetbrains.php.lang.psi.elements.StringLiteralExpression;
import com.jetbrains.php.lang.psi.resolve.types.PhpType;
import com.jetbrains.php.lang.psi.resolve.types.PhpTypeProvider3;
import com.jetbrains.php.lang.psi.resolve.types.PhpTypeProvider4;
import fr.adrienbrault.idea.symfony2plugin.Settings;
import fr.adrienbrault.idea.symfony2plugin.extension.MethodSignatureTypeProviderExtension;
import fr.adrienbrault.idea.symfony2plugin.extension.MethodSignatureTypeProviderParameter;
Expand All @@ -27,7 +28,7 @@
* @author Adrien Brault <[email protected]>
* @author Daniel Espendiller <[email protected]>
*/
public class MethodSignatureTypeProvider implements PhpTypeProvider3 {
public class MethodSignatureTypeProvider implements PhpTypeProvider4 {

final static char TRIM_KEY = '\u0181';
private static final ExtensionPointName<MethodSignatureTypeProviderExtension> EXTENSIONS = new ExtensionPointName<>("fr.adrienbrault.idea.symfony2plugin.extension.MethodSignatureTypeProviderExtension");
Expand Down Expand Up @@ -120,6 +121,11 @@ private Collection<MethodSignatureSetting> getSignatureSetting(String methodName
return matchedSignatures;
}

@Override
public PhpType complete(String expression, Project project) {
return null;
}

@Override
public Collection<? extends PhpNamedElement> getBySignature(String expression, Set<String> visited, int depth, Project project) {
// get back our original call
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.jetbrains.php.lang.psi.elements.PhpNamedElement;
import com.jetbrains.php.lang.psi.resolve.types.PhpType;
import com.jetbrains.php.lang.psi.resolve.types.PhpTypeProvider3;
import com.jetbrains.php.lang.psi.resolve.types.PhpTypeProvider4;
import fr.adrienbrault.idea.symfony2plugin.Settings;
import fr.adrienbrault.idea.symfony2plugin.dic.container.util.ServiceContainerUtil;
import fr.adrienbrault.idea.symfony2plugin.stubs.ContainerCollectionResolver;
Expand All @@ -24,7 +25,7 @@
* @author Adrien Brault <[email protected]>
* @author Daniel Espendiller <[email protected]>
*/
public class SymfonyContainerTypeProvider implements PhpTypeProvider3 {
public class SymfonyContainerTypeProvider implements PhpTypeProvider4 {
private static char TRIM_KEY = '\u0182';

@Override
Expand All @@ -48,6 +49,11 @@ public PhpType getType(PsiElement e) {
return signature == null ? null : new PhpType().add("#" + this.getKey() + signature);
}

@Override
public PhpType complete(String expression, Project project) {
return null;
}

@Override
public Collection<? extends PhpNamedElement> getBySignature(String expression, Set<String> visited, int depth, Project project) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.jetbrains.php.lang.psi.elements.PhpNamedElement;
import com.jetbrains.php.lang.psi.resolve.types.PhpType;
import com.jetbrains.php.lang.psi.resolve.types.PhpTypeProvider3;
import com.jetbrains.php.lang.psi.resolve.types.PhpTypeProvider4;
import fr.adrienbrault.idea.symfony2plugin.Settings;
import fr.adrienbrault.idea.symfony2plugin.util.PhpElementsUtil;
import fr.adrienbrault.idea.symfony2plugin.util.PhpTypeProviderUtil;
Expand All @@ -24,7 +25,7 @@
*
* @author Daniel Espendiller <[email protected]>
*/
public class ObjectManagerFindTypeProvider implements PhpTypeProvider3 {
public class ObjectManagerFindTypeProvider implements PhpTypeProvider4 {

final static char TRIM_KEY = '\u0183';

Expand Down Expand Up @@ -62,6 +63,11 @@ public PhpType getType(PsiElement e) {
return null;
}

@Override
public PhpType complete(String expression, Project project) {
return null;
}

@Override
public Collection<? extends PhpNamedElement> getBySignature(String expression, Set<String> visited, int depth, Project project) {
// get back our original call
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@
import com.jetbrains.php.lang.psi.elements.MethodReference;
import com.jetbrains.php.lang.psi.elements.PhpClass;
import com.jetbrains.php.lang.psi.elements.PhpNamedElement;
import com.jetbrains.php.lang.psi.elements.impl.MethodImpl;
import com.jetbrains.php.lang.psi.resolve.types.PhpType;
import com.jetbrains.php.lang.psi.resolve.types.PhpTypeProvider3;
import com.jetbrains.php.lang.psi.resolve.types.PhpTypeProvider4;
import fr.adrienbrault.idea.symfony2plugin.Settings;
import fr.adrienbrault.idea.symfony2plugin.util.MethodMatcher;
import fr.adrienbrault.idea.symfony2plugin.util.PhpElementsUtil;
Expand All @@ -26,7 +27,7 @@
/**
* @author Daniel Espendiller <[email protected]>
*/
public class ObjectRepositoryResultTypeProvider implements PhpTypeProvider3 {
public class ObjectRepositoryResultTypeProvider implements PhpTypeProvider4 {
private static MethodMatcher.CallToSignature[] FIND_SIGNATURES = new MethodMatcher.CallToSignature[] {
new MethodMatcher.CallToSignature("\\Doctrine\\Common\\Persistence\\ObjectRepository", "find"),
new MethodMatcher.CallToSignature("\\Doctrine\\Common\\Persistence\\ObjectRepository", "findOneBy"),
Expand Down Expand Up @@ -102,7 +103,14 @@ public PhpType getType(PsiElement e) {

repositorySignature = repositorySignature.substring(1, nextMethodCall);

return new PhpType().add("#" + this.getKey() + refSignature + TRIM_KEY + repositorySignature);
String signature = "#" + this.getKey() + refSignature.substring(0, refSignature.indexOf("|")) + TRIM_KEY + repositorySignature;

return new PhpType().add(signature);
}

@Override
public PhpType complete(String expression, Project project) {
return null;
}

@Override
Expand Down Expand Up @@ -142,8 +150,10 @@ public Collection<? extends PhpNamedElement> getBySignature(String expression, S

String name = method.getName();
if(name.equals("findAll") || name.equals("findBy")) {
method.getType().add(phpClass.getFQN() + "[]");
return phpNamedElementCollections;
Method m = new MethodImpl(method.getNode());
m.getType().add(phpClass.getFQN() + "[]");

return PhpTypeProviderUtil.mergeSignatureResults(phpNamedElementCollections, m);
}

return PhpTypeProviderUtil.mergeSignatureResults(phpNamedElementCollections, phpClass);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import com.jetbrains.php.lang.psi.elements.PhpClass;
import com.jetbrains.php.lang.psi.elements.PhpNamedElement;
import com.jetbrains.php.lang.psi.resolve.types.PhpType;
import com.jetbrains.php.lang.psi.resolve.types.PhpTypeProvider3;
import com.jetbrains.php.lang.psi.resolve.types.PhpTypeProvider4;
import fr.adrienbrault.idea.symfony2plugin.Settings;
import fr.adrienbrault.idea.symfony2plugin.util.MethodMatcher;
import fr.adrienbrault.idea.symfony2plugin.util.PhpElementsUtil;
Expand All @@ -23,7 +23,7 @@
/**
* @author Daniel Espendiller <[email protected]>
*/
public class ObjectRepositoryTypeProvider implements PhpTypeProvider3 {
public class ObjectRepositoryTypeProvider implements PhpTypeProvider4 {
private static MethodMatcher.CallToSignature[] GET_REPOSITORIES_SIGNATURES = new MethodMatcher.CallToSignature[] {
new MethodMatcher.CallToSignature("\\Doctrine\\Common\\Persistence\\ManagerRegistry", "getRepository"),
new MethodMatcher.CallToSignature("\\Doctrine\\Common\\Persistence\\ObjectManager", "getRepository"),
Expand Down Expand Up @@ -59,6 +59,11 @@ public PhpType getType(PsiElement e) {
return signature == null ? null : new PhpType().add("#" + this.getKey() + signature);
}

@Override
public PhpType complete(String expression, Project project) {
return null;
}

@Override
public Collection<? extends PhpNamedElement> getBySignature(String expression, Set<String> visited, int depth, Project project) {
// get back our original call
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.jetbrains.php.lang.psi.elements.*;
import com.jetbrains.php.lang.psi.resolve.types.PhpType;
import com.jetbrains.php.lang.psi.resolve.types.PhpTypeProvider3;
import com.jetbrains.php.lang.psi.resolve.types.PhpTypeProvider4;
import fr.adrienbrault.idea.symfony2plugin.Settings;
import org.apache.commons.lang.StringUtils;
import org.jetbrains.annotations.Nullable;
Expand All @@ -17,7 +18,7 @@
/**
* @author Daniel Espendiller <[email protected]>
*/
public class EventDispatcherTypeProvider implements PhpTypeProvider3 {
public class EventDispatcherTypeProvider implements PhpTypeProvider4 {

private static char TRIM_KEY = '\u0197';

Expand Down Expand Up @@ -76,6 +77,11 @@ public PhpType getType(PsiElement e) {
return new PhpType().add("#" + this.getKey() + refSignature + TRIM_KEY + signature);
}

@Override
public PhpType complete(String expression, Project project) {
return null;
}

@Override
public Collection<? extends PhpNamedElement> getBySignature(String expression, Set<String> visited, int depth, Project project) {
// get back our original call
Expand Down
12 changes: 6 additions & 6 deletions src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,12 @@
<idea-version since-build="182.0"/>

<extensions defaultExtensionNs="com.jetbrains.php">
<typeProvider3 implementation="fr.adrienbrault.idea.symfony2plugin.dic.SymfonyContainerTypeProvider"/>
<typeProvider3 implementation="fr.adrienbrault.idea.symfony2plugin.util.EventDispatcherTypeProvider"/>
<typeProvider3 implementation="fr.adrienbrault.idea.symfony2plugin.doctrine.ObjectRepositoryTypeProvider"/>
<typeProvider3 implementation="fr.adrienbrault.idea.symfony2plugin.doctrine.ObjectRepositoryResultTypeProvider"/>
<typeProvider3 implementation="fr.adrienbrault.idea.symfony2plugin.doctrine.ObjectManagerFindTypeProvider"/>
<typeProvider3 implementation="fr.adrienbrault.idea.symfony2plugin.assistant.signature.MethodSignatureTypeProvider"/>
<typeProvider4 implementation="fr.adrienbrault.idea.symfony2plugin.dic.SymfonyContainerTypeProvider"/>
<typeProvider4 implementation="fr.adrienbrault.idea.symfony2plugin.util.EventDispatcherTypeProvider"/>
<typeProvider4 implementation="fr.adrienbrault.idea.symfony2plugin.doctrine.ObjectRepositoryTypeProvider"/>
<typeProvider4 implementation="fr.adrienbrault.idea.symfony2plugin.doctrine.ObjectRepositoryResultTypeProvider"/>
<typeProvider4 implementation="fr.adrienbrault.idea.symfony2plugin.doctrine.ObjectManagerFindTypeProvider"/>
<typeProvider4 implementation="fr.adrienbrault.idea.symfony2plugin.assistant.signature.MethodSignatureTypeProvider"/>
<libraryRoot id="symfony_meta" path="/symfony-meta/" runtime="false"/>
<libraryRoot id="doctrine_meta" path="/doctrine-meta/" runtime="false"/>
</extensions>
Expand Down