diff --git a/src/fr/adrienbrault/idea/symfony2plugin/Symfony2InterfacesUtil.java b/src/fr/adrienbrault/idea/symfony2plugin/Symfony2InterfacesUtil.java index 661c1efcf..0945a17e0 100644 --- a/src/fr/adrienbrault/idea/symfony2plugin/Symfony2InterfacesUtil.java +++ b/src/fr/adrienbrault/idea/symfony2plugin/Symfony2InterfacesUtil.java @@ -2,7 +2,6 @@ import com.intellij.openapi.project.Project; import com.intellij.psi.PsiElement; -import com.intellij.psi.util.PsiTreeUtil; import com.jetbrains.php.PhpIndex; import com.jetbrains.php.lang.psi.elements.Method; import com.jetbrains.php.lang.psi.elements.MethodReference; @@ -59,15 +58,34 @@ protected boolean isCallTo(PsiElement e, Method[] expectedMethods, int deepness) return false; } - PhpClass methodClass = PsiTreeUtil.getParentOfType(e, PhpClass.class); - if(null == methodClass) { + // resolve is also called on invalid php code like "use " + // so double check the method name before resolve the method + if(!isMatchingMethodName(methodRef, expectedMethods)) { return false; } + PsiElement resolvedReference = methodRef.getReference().resolve(); + if (!(resolvedReference instanceof Method)) { + return false; + } + + Method method = (Method) resolvedReference; + PhpClass methodClass = method.getContainingClass(); + for (Method expectedMethod : Arrays.asList(expectedMethods)) { if (null != expectedMethod - && expectedMethod.getName().equals(methodRef.getName()) - && isInstanceOf(methodClass, expectedMethod.getContainingClass())) { + && expectedMethod.getName().equals(method.getName()) + && isInstanceOf(methodClass, expectedMethod.getContainingClass())) { + return true; + } + } + + return false; + } + + protected boolean isMatchingMethodName(MethodReference methodRef, Method[] expectedMethods) { + for (Method expectedMethod : Arrays.asList(expectedMethods)) { + if(expectedMethod != null && expectedMethod.getName().equals(methodRef.getName())) { return true; } } diff --git a/src/fr/adrienbrault/idea/symfony2plugin/dic/SymfonyContainerTypeProvider.java b/src/fr/adrienbrault/idea/symfony2plugin/dic/SymfonyContainerTypeProvider.java index fd0b69e47..c3adcda5a 100644 --- a/src/fr/adrienbrault/idea/symfony2plugin/dic/SymfonyContainerTypeProvider.java +++ b/src/fr/adrienbrault/idea/symfony2plugin/dic/SymfonyContainerTypeProvider.java @@ -1,7 +1,9 @@ package fr.adrienbrault.idea.symfony2plugin.dic; import com.intellij.openapi.project.DumbService; +import com.intellij.patterns.PlatformPatterns; import com.intellij.psi.PsiElement; +import com.jetbrains.php.lang.parser.PhpElementTypes; import com.jetbrains.php.lang.psi.elements.MethodReference; import com.jetbrains.php.lang.psi.resolve.types.PhpType; import com.jetbrains.php.lang.psi.resolve.types.PhpTypeProvider; @@ -26,6 +28,22 @@ public PhpType getType(PsiElement e) { return null; } + // filter out method calls without parameter + // $this->get('service_name') + if(!PlatformPatterns.psiElement(PhpElementTypes.METHOD_REFERENCE).withChild( + PlatformPatterns.psiElement(PhpElementTypes.PARAMETER_LIST).withFirstChild( + PlatformPatterns.psiElement(PhpElementTypes.STRING))).accepts(e)) { + + return null; + } + + // container calls are only on "get" methods + // cant we move it up to PlatformPatterns? withName condition dont looks working + String methodRefName = ((MethodReference) e).getName(); + if(methodRefName == null || !methodRefName.equals("get")) { + return null; + } + depth++; if (depth > 2) { // Try to avoid too much recursive things ...