-
-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathSymfony2InterfacesUtil.java
More file actions
386 lines (300 loc) · 14 KB
/
Copy pathSymfony2InterfacesUtil.java
File metadata and controls
386 lines (300 loc) · 14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
package de.espend.idea.php.toolbox.symfony;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiPolyVariantReference;
import com.intellij.psi.PsiReference;
import com.intellij.psi.ResolveResult;
import com.jetbrains.php.PhpIndex;
import com.jetbrains.php.lang.psi.elements.Method;
import com.jetbrains.php.lang.psi.elements.MethodReference;
import com.jetbrains.php.lang.psi.elements.PhpClass;
import com.jetbrains.php.lang.psi.elements.StringLiteralExpression;
import de.espend.idea.php.toolbox.symfony.utils.PhpElementsUtil;
import de.espend.idea.php.toolbox.symfony.util.MethodMatcher;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.*;
/**
* @author Adrien Brault <adrien.brault@gmail.com>
* @author Daniel Espendiller <daniel@espendiller.net>
*/
public class Symfony2InterfacesUtil {
public boolean isContainerGetCall(PsiElement e) {
return isCallTo(e, new Method[] {
getInterfaceMethod(e.getProject(), "\\Symfony\\Component\\DependencyInjection\\ContainerInterface", "get"),
getClassMethod(e.getProject(), "\\Symfony\\Bundle\\FrameworkBundle\\Controller\\Controller", "get"),
});
}
public boolean isContainerGetCall(Method e) {
return isCallTo(e, new Method[] {
getInterfaceMethod(e.getProject(), "\\Symfony\\Component\\DependencyInjection\\ContainerInterface", "get"),
getClassMethod(e.getProject(), "\\Symfony\\Bundle\\FrameworkBundle\\Controller\\Controller", "get"),
});
}
public boolean isTemplatingRenderCall(PsiElement e) {
return isCallTo(e, new Method[] {
getInterfaceMethod(e.getProject(), "\\Symfony\\Component\\Templating\\EngineInterface", "render"),
getInterfaceMethod(e.getProject(), "\\Symfony\\Component\\Templating\\StreamingEngineInterface", "stream"),
getInterfaceMethod(e.getProject(), "\\Symfony\\Bundle\\FrameworkBundle\\Templating\\EngineInterface", "renderResponse"),
getClassMethod(e.getProject(), "\\Symfony\\Bundle\\FrameworkBundle\\Controller\\Controller", "render"),
getClassMethod(e.getProject(), "\\Symfony\\Bundle\\FrameworkBundle\\Controller\\Controller", "renderView"),
getClassMethod(e.getProject(), "\\Symfony\\Bundle\\FrameworkBundle\\Controller\\Controller", "stream"),
});
}
public boolean isTranslatorCall(PsiElement e) {
return isCallTo(e, new Method[] {
getInterfaceMethod(e.getProject(), "\\Symfony\\Component\\Translation\\TranslatorInterface", "trans"),
getInterfaceMethod(e.getProject(), "\\Symfony\\Component\\Translation\\TranslatorInterface", "transChoice"),
});
}
public boolean isGetRepositoryCall(Method e) {
return isCallTo(e, new Method[] {
getInterfaceMethod(e.getProject(), "\\Doctrine\\Common\\Persistence\\ManagerRegistry", "getRepository"),
getInterfaceMethod(e.getProject(), "\\Doctrine\\Common\\Persistence\\ObjectManager", "getRepository"),
});
}
public boolean isObjectRepositoryCall(Method e) {
return isCallTo(e, new Method[] {
getInterfaceMethod(e.getProject(), "\\Doctrine\\Common\\Persistence\\ObjectRepository", "find"),
getInterfaceMethod(e.getProject(), "\\Doctrine\\Common\\Persistence\\ObjectRepository", "findOneBy"),
getInterfaceMethod(e.getProject(), "\\Doctrine\\Common\\Persistence\\ObjectRepository", "findAll"),
getInterfaceMethod(e.getProject(), "\\Doctrine\\Common\\Persistence\\ObjectRepository", "findBy"),
});
}
public boolean isFormBuilderFormTypeCall(PsiElement e) {
List<Method> methods = getCallToSignatureInterfaceMethods(e, getFormBuilderInterface());
return isCallTo(e, methods.toArray( new Method[methods.size()]));
}
protected boolean isCallTo(PsiElement e, Method expectedMethod) {
return isCallTo(e, new Method[] { expectedMethod });
}
protected boolean isCallTo(PsiElement e, Method[] expectedMethods) {
return isCallTo(e, expectedMethods, 1);
}
protected boolean isCallTo(Method e, Method[] expectedMethods) {
PhpClass methodClass = e.getContainingClass();
if(methodClass == null) {
return false;
}
for (Method expectedMethod : expectedMethods) {
// @TODO: its stuff from beginning times :)
if(expectedMethod == null) {
continue;
}
PhpClass containingClass = expectedMethod.getContainingClass();
if (containingClass != null && expectedMethod.getName().equalsIgnoreCase(e.getName()) && isInstanceOf(methodClass, containingClass)) {
return true;
}
}
return false;
}
protected boolean isCallTo(PsiElement e, Method[] expectedMethods, int deepness) {
if (!(e instanceof MethodReference)) {
return false;
}
MethodReference methodRef = (MethodReference) e;
// resolve is also called on invalid php code like "use <xxx>"
// so double check the method name before resolve the method
if(!isMatchingMethodName(methodRef, expectedMethods)) {
return false;
}
PsiReference psiReference = methodRef.getReference();
if (null == psiReference) {
return false;
}
Method[] multiResolvedMethod = getMultiResolvedMethod(psiReference);
if(multiResolvedMethod == null) {
return false;
}
for (Method method : multiResolvedMethod) {
PhpClass methodClass = method.getContainingClass();
if(methodClass == null) {
continue;
}
for (Method expectedMethod : expectedMethods) {
// @TODO: its stuff from beginning times :)
if(expectedMethod == null) {
continue;
}
PhpClass containingClass = expectedMethod.getContainingClass();
if (null != containingClass && expectedMethod.getName().equalsIgnoreCase(method.getName()) && isInstanceOf(methodClass, containingClass)) {
return true;
}
}
}
return false;
}
/**
* Single resolve doesnt work if we have non unique class names in project context,
* so try a multiResolve
*/
@Nullable
public static Method[] getMultiResolvedMethod(PsiReference psiReference) {
// class be unique in normal case, so try this first
PsiElement resolvedReference = psiReference.resolve();
if (resolvedReference instanceof Method) {
return new Method[] { (Method) resolvedReference };
}
// try multiResolve if class exists twice in project
if(psiReference instanceof PsiPolyVariantReference) {
Collection<Method> methods = new HashSet<>();
for(ResolveResult resolveResult : ((PsiPolyVariantReference) psiReference).multiResolve(false)) {
PsiElement element = resolveResult.getElement();
if(element instanceof Method) {
methods.add((Method) element);
}
}
if(methods.size() > 0) {
return methods.toArray(new Method[methods.size()]);
}
}
return null;
}
protected boolean isMatchingMethodName(MethodReference methodRef, Method[] expectedMethods) {
String methodRefName = methodRef.getName();
for (Method expectedMethod : Arrays.asList(expectedMethods)) {
if(expectedMethod == null) {
continue;
}
if(expectedMethod.getName().equalsIgnoreCase(methodRefName)) {
return true;
}
}
return false;
}
@Deprecated
@Nullable
public static String getFirstArgumentStringValue(MethodReference e) {
String stringValue = null;
PsiElement[] parameters = e.getParameters();
if (parameters.length > 0 && parameters[0] instanceof StringLiteralExpression) {
stringValue = ((StringLiteralExpression) parameters[0]).getContents();
}
return stringValue;
}
@Deprecated
@Nullable
protected Method getInterfaceMethod(Project project, String interfaceFQN, String methodName) {
Collection<PhpClass> interfaces = PhpIndex.getInstance(project).getInterfacesByFQN(interfaceFQN);
if (interfaces.size() < 1) {
return null;
}
return findClassMethodByName(interfaces.iterator().next(), methodName);
}
@Deprecated
@Nullable
protected Method getClassMethod(Project project, String classFQN, String methodName) {
return PhpElementsUtil.getClassMethod(project, classFQN, methodName);
}
@Nullable
protected Method findClassMethodByName(PhpClass phpClass, String methodName) {
return PhpElementsUtil.getClassMethod(phpClass, methodName);
}
protected boolean isImplementationOfInterface(PhpClass phpClass, PhpClass phpInterface) {
if (phpClass == phpInterface) {
return true;
}
for (PhpClass implementedInterface : phpClass.getImplementedInterfaces()) {
if (isImplementationOfInterface(implementedInterface, phpInterface)) {
return true;
}
}
if (null == phpClass.getSuperClass()) {
return false;
}
return isImplementationOfInterface(phpClass.getSuperClass(), phpInterface);
}
public boolean isInstanceOf(PhpClass subjectClass, String expectedClass) {
PhpClass instanceClass = PhpElementsUtil.getClassInterface(subjectClass.getProject(), expectedClass);
if(instanceClass == null) {
return false;
}
return isInstanceOf(subjectClass, instanceClass);
}
public boolean isInstanceOf(Project project, String subjectClass, String expectedClass) {
PhpClass subjectPhpClass = PhpElementsUtil.getClassInterface(project, subjectClass);
if(subjectPhpClass == null) {
return false;
}
return isInstanceOf(subjectPhpClass, expectedClass);
}
public boolean isInstanceOf(@NotNull PhpClass subjectClass, @NotNull PhpClass expectedClass) {
// we have equal class instance, on non multiple classes with same name fallback to namespace and classname
if (subjectClass == expectedClass || PhpElementsUtil.isEqualClassName(subjectClass, expectedClass)) {
return true;
}
if (expectedClass.isInterface()) {
return isImplementationOfInterface(subjectClass, expectedClass);
}
if (null == subjectClass.getSuperClass()) {
return false;
}
return isInstanceOf(subjectClass.getSuperClass(), expectedClass);
}
public boolean isCallTo(MethodReference e, String ClassInterfaceName, String methodName) {
return isCallTo((PsiElement) e, ClassInterfaceName, methodName);
}
/**
* @deprecated isCallTo with MethodReference
*/
@Deprecated
public boolean isCallTo(PsiElement e, String ClassInterfaceName, String methodName) {
// we need a full fqn name
if(ClassInterfaceName.contains("\\") && !ClassInterfaceName.startsWith("\\")) {
ClassInterfaceName = "\\" + ClassInterfaceName;
}
Method[] methods = getExpectedMethods(e.getProject(), ClassInterfaceName, methodName);
if(methods.length == 0) {
return false;
}
return isCallTo(e, methods);
}
@NotNull
private Method[] getExpectedMethods(@NotNull Project project, @NotNull String ClassInterfaceName, @NotNull String methodName) {
Set<Method> methods = new HashSet<>();
for (PhpClass phpClass : PhpIndex.getInstance(project).getAnyByFQN(ClassInterfaceName)) {
// handle constructor as string
if(methodName.equalsIgnoreCase("__construct")) {
Method constructor = phpClass.getConstructor();
if(constructor != null) {
methods.add(constructor);
}
continue;
}
Method method = phpClass.findMethodByName(methodName);
if(method != null) {
methods.add(method);
}
}
return methods.toArray(new Method[methods.size()]);
}
public boolean isCallTo(Method e, String ClassInterfaceName, String methodName) {
// we need a full fqn name
if(ClassInterfaceName.contains("\\") && !ClassInterfaceName.startsWith("\\")) {
ClassInterfaceName = "\\" + ClassInterfaceName;
}
Method[] methods = getExpectedMethods(e.getProject(), ClassInterfaceName, methodName);
if(methods.length == 0) {
return false;
}
return isCallTo(e, methods);
}
private List<Method> getCallToSignatureInterfaceMethods(PsiElement e, Collection<MethodMatcher.CallToSignature> signatures) {
List<Method> methods = new ArrayList<>();
for(MethodMatcher.CallToSignature signature: signatures) {
Method method = getInterfaceMethod(e.getProject(), signature.getInstance(), signature.getMethod());
if(method != null) {
methods.add(method);
}
}
return methods;
}
public static Collection<MethodMatcher.CallToSignature> getFormBuilderInterface() {
Collection<MethodMatcher.CallToSignature> signatures = new ArrayList<>();
signatures.add(new MethodMatcher.CallToSignature("\\Symfony\\Component\\Form\\FormBuilderInterface", "add"));
signatures.add(new MethodMatcher.CallToSignature("\\Symfony\\Component\\Form\\FormBuilderInterface", "create"));
signatures.add(new MethodMatcher.CallToSignature("\\Symfony\\Component\\Form\\FormInterface", "add"));
signatures.add(new MethodMatcher.CallToSignature("\\Symfony\\Component\\Form\\FormInterface", "create"));
return signatures;
}
}