Skip to content

Commit 9ff7f20

Browse files
committed
our heart method "isCallTo" now supports class/method which are non unique because of multiResolve
1 parent d561533 commit 9ff7f20

File tree

1 file changed

+42
-7
lines changed

1 file changed

+42
-7
lines changed

src/fr/adrienbrault/idea/symfony2plugin/Symfony2InterfacesUtil.java

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
import com.intellij.openapi.project.Project;
44
import com.intellij.psi.PsiElement;
5+
import com.intellij.psi.PsiPolyVariantReference;
56
import com.intellij.psi.PsiReference;
7+
import com.intellij.psi.ResolveResult;
68
import com.jetbrains.php.PhpIndex;
79
import com.jetbrains.php.lang.psi.elements.Method;
810
import com.jetbrains.php.lang.psi.elements.MethodReference;
@@ -148,25 +150,58 @@ protected boolean isCallTo(PsiElement e, Method[] expectedMethods, int deepness)
148150
return false;
149151
}
150152

151-
PsiElement resolvedReference = psiReference.resolve();
152-
if (!(resolvedReference instanceof Method)) {
153+
Method method = getMultiResolvedMethod(psiReference);
154+
if (method == null) {
153155
return false;
154156
}
155157

156-
Method method = (Method) resolvedReference;
157158
PhpClass methodClass = method.getContainingClass();
159+
if(methodClass == null) {
160+
return false;
161+
}
158162

159-
for (Method expectedMethod : Arrays.asList(expectedMethods)) {
160-
if (null != expectedMethod
161-
&& expectedMethod.getName().equals(method.getName())
162-
&& isInstanceOf(methodClass, expectedMethod.getContainingClass())) {
163+
for (Method expectedMethod : expectedMethods) {
164+
165+
// @TODO: its stuff from beginning times :)
166+
if(expectedMethod == null) {
167+
continue;
168+
}
169+
170+
PhpClass containingClass = expectedMethod.getContainingClass();
171+
if (null != containingClass && expectedMethod.getName().equals(method.getName()) && isInstanceOf(methodClass, containingClass)) {
163172
return true;
164173
}
165174
}
166175

167176
return false;
168177
}
169178

179+
/**
180+
* Single resolve doesnt work if we have non unique class names in project context,
181+
* so try a multiResolve and use first matched method
182+
*/
183+
@Nullable
184+
protected static Method getMultiResolvedMethod(PsiReference psiReference) {
185+
186+
// class be unique in normal case, so try this first
187+
PsiElement resolvedReference = psiReference.resolve();
188+
if (resolvedReference instanceof Method) {
189+
return (Method) resolvedReference;
190+
}
191+
192+
// try multiResolve if class exists twice in project
193+
if(psiReference instanceof PsiPolyVariantReference) {
194+
for(ResolveResult resolveResult : ((PsiPolyVariantReference) psiReference).multiResolve(false)) {
195+
PsiElement element = resolveResult.getElement();
196+
if(element instanceof Method) {
197+
return (Method) element;
198+
}
199+
}
200+
}
201+
202+
return null;
203+
}
204+
170205
protected boolean isMatchingMethodName(MethodReference methodRef, Method[] expectedMethods) {
171206
for (Method expectedMethod : Arrays.asList(expectedMethods)) {
172207
if(expectedMethod != null && expectedMethod.getName().equals(methodRef.getName())) {

0 commit comments

Comments
 (0)