16
16
import fr .adrienbrault .idea .symfony2plugin .util .PhpTypeProviderUtil ;
17
17
import org .jetbrains .annotations .NotNull ;
18
18
import org .jetbrains .annotations .Nullable ;
19
- import java .util .Arrays ;
20
- import java .util .Collection ;
21
- import java .util .HashSet ;
22
- import java .util .Set ;
19
+
20
+ import java .util .*;
23
21
import java .util .stream .Collectors ;
24
22
25
23
/**
@@ -63,7 +61,8 @@ public PhpType getType(PsiElement e) {
63
61
}
64
62
65
63
String methodRefName = methodRef .getName ();
66
- if (null == methodRefName || !Arrays .asList (new String [] {"find" , "findOneBy" , "findAll" , "findBy" }).contains (methodRefName )) {
64
+
65
+ if (null == methodRefName || (!Arrays .asList (new String [] {"find" , "findAll" }).contains (methodRefName ) && !methodRefName .startsWith ("findOneBy" ) && !methodRefName .startsWith ("findBy" ))) {
67
66
return null ;
68
67
}
69
68
@@ -109,7 +108,7 @@ public PhpType complete(String s, Project project) {
109
108
110
109
PhpIndex phpIndex = PhpIndex .getInstance (project );
111
110
112
- Collection <? extends PhpNamedElement > typeSignature = PhpTypeProviderUtil . getTypeSignature (phpIndex , originalSignature );
111
+ Collection <? extends PhpNamedElement > typeSignature = getTypeSignatureMagic (phpIndex , originalSignature );
113
112
114
113
// ->getRepository(SecondaryMarket::class)->findAll() => "findAll", but only if its a instance of this method;
115
114
// so non Doctrine method are already filtered
@@ -124,7 +123,7 @@ public PhpType complete(String s, Project project) {
124
123
PhpType phpType = new PhpType ();
125
124
126
125
resolveMethods .stream ()
127
- .map (name -> name .equals ("findAll" ) || name .equals ("findBy" ) ? phpClass .getFQN () + "[]" : phpClass .getFQN ())
126
+ .map (name -> name .equals ("findAll" ) || name .startsWith ("findBy" ) ? phpClass .getFQN () + "[]" : phpClass .getFQN ())
128
127
.collect (Collectors .toSet ())
129
128
.forEach (phpType ::add );
130
129
@@ -147,4 +146,33 @@ private Collection<Method> getObjectRepositoryCall(Collection<? extends PhpNamed
147
146
148
147
return methods ;
149
148
}
149
+
150
+ /**
151
+ * We can have multiple types inside a TypeProvider; split them on "|" so that we dont get empty types
152
+ *
153
+ * #M#x#M#C\FooBar.get?doctrine.odm.mongodb.document_manager.getRepository|
154
+ * #M#x#M#C\FooBar.get?doctrine.odm.mongodb.document_manager.getRepository
155
+ */
156
+ @ NotNull
157
+ private static Collection <? extends PhpNamedElement > getTypeSignatureMagic (@ NotNull PhpIndex phpIndex , @ NotNull String signature ) {
158
+ // magic method resolving; we need to have the ObjectRepository method which does not exists for magic methods, so strip it
159
+ // #M#x#M#C\FooBar.get?doctrine.odm.mongodb.document_manager.findByName => findBy
160
+ // #M#x#M#C\FooBar.get?doctrine.odm.mongodb.document_manager.findOneBy => findOne
161
+ Collection <PhpNamedElement > elements = new HashSet <>();
162
+ for (String s : signature .split ("\\ |" )) {
163
+ int i = s .lastIndexOf ("." );
164
+ if (i > 0 ) {
165
+ String substring = s .substring (i + 1 );
166
+ if (substring .startsWith ("findOneBy" )) {
167
+ s = s .substring (0 , i + 1 ) + "findOneBy" ;
168
+ } else if (substring .startsWith ("findBy" )) {
169
+ s = s .substring (0 , i + 1 ) + "findBy" ;
170
+ }
171
+ }
172
+
173
+ elements .addAll (phpIndex .getBySignature (s , null , 0 ));
174
+ }
175
+
176
+ return elements ;
177
+ }
150
178
}
0 commit comments