6
6
import com .intellij .psi .util .PsiTreeUtil ;
7
7
import com .jetbrains .php .PhpIndex ;
8
8
import com .jetbrains .php .lang .parser .PhpElementTypes ;
9
- import com .jetbrains .php .lang .psi .elements .ArrayCreationExpression ;
10
- import com .jetbrains .php .lang .psi .elements .ClassConstantReference ;
11
- import com .jetbrains .php .lang .psi .elements .MethodReference ;
12
- import com .jetbrains .php .lang .psi .elements .PhpClass ;
9
+ import com .jetbrains .php .lang .psi .elements .*;
13
10
import fr .adrienbrault .idea .symfony2plugin .doctrine .EntityHelper ;
14
11
import fr .adrienbrault .idea .symfony2plugin .doctrine .dict .DoctrineModelField ;
15
12
import fr .adrienbrault .idea .symfony2plugin .doctrine .querybuilder .detector .FormQueryBuilderRepositoryDetector ;
@@ -61,15 +58,13 @@ public QueryBuilderScopeContext collect() {
61
58
}
62
59
63
60
for (MethodReference methodReference : methodReferences ) {
64
-
65
61
String name = methodReference .getName ();
66
62
if (name != null ) {
67
63
collectParameter (qb , methodReference , name );
68
64
collectJoins (qb , methodReference , name );
69
65
collectSelects (qb , methodReference , name );
70
66
collectSelectInForm (qb , methodReference , name );
71
67
}
72
-
73
68
}
74
69
75
70
// first tableMap entry is root, we add several initial data
@@ -184,25 +179,30 @@ private void collectParameter(QueryBuilderScopeContext qb, MethodReference metho
184
179
185
180
}
186
181
187
-
188
182
/**
183
+ * Extract model from context
189
184
*
190
- * @param methodReferences
191
- * @return
185
+ * ```
186
+ * getRepository('Foo')->createQueryBuilder('test')
187
+ * ```
188
+ *
189
+ * ```
190
+ * public function __construct(RegistryInterface $registry)
191
+ * {
192
+ * parent::__construct($registry, Entity::class);
193
+ * }
194
+ * ```
192
195
*/
193
196
@ NotNull
194
197
private Map <String , String > findRootDefinition (@ NotNull Collection <MethodReference > methodReferences ) {
195
-
196
- Map <String , String > roots = new HashMap <>();
197
-
198
198
if (methodReferences .size () == 0 ) {
199
- return roots ;
199
+ return Collections . emptyMap () ;
200
200
}
201
201
202
+ Map <String , String > roots = new HashMap <>();
202
203
String rootAlias = null ;
203
204
String repository = null ;
204
205
205
-
206
206
for (MethodReference methodReference : methodReferences ) {
207
207
String methodReferenceName = methodReference .getName ();
208
208
@@ -259,11 +259,38 @@ private Map<String, String> findRootDefinition(@NotNull Collection<MethodReferen
259
259
roots .put (repository , rootAlias );
260
260
}
261
261
262
+ // public function __construct(RegistryInterface $registry)
263
+ // parent::__construct($registry, Entity::class);
264
+ if (rootAlias != null && repository == null ) {
265
+ MethodReference methodReference = methodReferences .iterator ().next ();
266
+ PhpClass phpClass = PsiTreeUtil .getParentOfType (methodReference , PhpClass .class );
267
+
268
+ if (phpClass != null && PhpElementsUtil .isInstanceOf (phpClass , "\\ Doctrine\\ Bundle\\ DoctrineBundle\\ Repository\\ ServiceEntityRepository" )) {
269
+ Method constructor = phpClass .getConstructor ();
270
+ if (constructor != null ) {
271
+ for (MethodReference reference : PsiTreeUtil .findChildrenOfType (constructor , MethodReference .class )) {
272
+ if ("__construct" .equals (reference .getName ())) {
273
+ PsiElement [] parameters = reference .getParameters ();
274
+ if (parameters .length > 1 ) {
275
+ PsiElement parameter = parameters [1 ];
276
+ String stringValue = PhpElementsUtil .getStringValue (parameter );
277
+ if (stringValue != null ) {
278
+ roots .put (stringValue , rootAlias );
279
+ return roots ;
280
+ }
281
+ }
282
+ }
283
+ }
284
+ }
285
+ }
286
+ }
287
+
262
288
// we found a alias but not a repository name, so try a scope search if we are inside repository class
263
289
// class implements \Doctrine\Common\Persistence\ObjectRepository, so search for model name of "repositoryClass"
264
290
if (rootAlias != null && repository == null ) {
265
291
MethodReference methodReference = methodReferences .iterator ().next ();
266
292
PhpClass phpClass = PsiTreeUtil .getParentOfType (methodReference , PhpClass .class );
293
+
267
294
if (
268
295
phpClass != null &&
269
296
(
@@ -280,7 +307,6 @@ private Map<String, String> findRootDefinition(@NotNull Collection<MethodReferen
280
307
}
281
308
}
282
309
}
283
-
284
310
}
285
311
286
312
// search on PhpTypeProvider
0 commit comments