package fr.adrienbrault.idea.symfony2plugin;

import com.intellij.patterns.PlatformPatterns;
import com.intellij.psi.*;
import com.intellij.util.ProcessingContext;
import com.jetbrains.php.lang.psi.elements.Method;
import com.jetbrains.php.lang.psi.elements.MethodReference;
import com.jetbrains.php.lang.psi.elements.ParameterList;
import com.jetbrains.php.lang.psi.elements.StringLiteralExpression;
import org.jetbrains.annotations.NotNull;

/**
 * @author Adrien Brault <adrien.brault@gmail.com>
 */
public class ServiceReferenceContributor extends PsiReferenceContributor {

    @Override
    public void registerReferenceProviders(PsiReferenceRegistrar psiReferenceRegistrar) {
        psiReferenceRegistrar.registerReferenceProvider(PlatformPatterns.psiElement(StringLiteralExpression.class), new PsiReferenceProvider() {
            @NotNull
            @Override
            public PsiReference[] getReferencesByElement(@NotNull PsiElement psiElement, @NotNull ProcessingContext processingContext) {
                ParameterList parameterList = (ParameterList) psiElement.getContext();
                if (!(parameterList instanceof ParameterList)) {
                    return new PsiReference[0];
                }

                MethodReference method = (MethodReference) parameterList.getContext();
                if (!(method instanceof MethodReference)) {
                    return new PsiReference[0];
                }

                if (!ContainerGetHelper.isContainerGetCall(method)) {
                    return new PsiReference[0];
                }

                return new PsiReference[]{ new ServiceReference((StringLiteralExpression) psiElement) };
            }
        });
    }
}

@@ -1021,114 +1021,37 @@
     
-ParameterList parameterList = (ParameterList) psiElement.getContext();%0A                if (!(parameterList
+if (!(psiElement.getContext()
  ins
@@ -1134,33 +1134,32 @@
               %7D%0A
-%0A
                 
@@ -1162,63 +1162,63 @@
     
-MethodReference method = (MethodReference) parameterLis
+ParameterList parameterList = (ParameterList) psiElemen
 t.ge
@@ -1229,16 +1229,17 @@
 text();%0A
+%0A
         
@@ -1252,22 +1252,42 @@
   if (!(
-method
+parameterList.getContext()
  instanc
@@ -1367,32 +1367,119 @@
                %7D
+%0A                MethodReference method = (MethodReference) parameterList.getContext();
 %0A%0A              

Fix possible ClassCastException