public static boolean isFunctionReference( PsiElement psiElement, int wantIndex, String... funcName) { PsiElement variableContext = psiElement.getContext(); if (!(variableContext instanceof ParameterList)) { return false; } ParameterList parameterList = (ParameterList) variableContext; PsiElement context = parameterList.getContext(); if (!(context instanceof FunctionReference)) { return false; } FunctionReference methodReference = (FunctionReference) context; String name = methodReference.getName(); if (name == null || !Arrays.asList(funcName).contains(name)) { return false; } ParameterBag currentIndex = getCurrentParameterIndex(psiElement); if (currentIndex == null) { return false; } return !(wantIndex >= 0 && currentIndex.getIndex() != wantIndex); }
@Nullable public static String getReferenceSignature( FunctionReference functionReference, char trimKey, int equalParameterCount) { String refSignature = functionReference.getSignature(); if (StringUtil.isEmpty(refSignature)) { return null; } PsiElement[] parameters = functionReference.getParameters(); if (parameters.length != equalParameterCount) { return null; } PsiElement parameter = parameters[0]; // we already have a string value if ((parameter instanceof StringLiteralExpression)) { String param = ((StringLiteralExpression) parameter).getContents(); if (StringUtil.isNotEmpty(param)) { return refSignature + trimKey + param; } return null; } // whitelist here; we can also provide some more but think of performance // Service::NAME, $this->name and Entity::CLASS; if (parameter instanceof PhpReference && (parameter instanceof ClassConstantReference || parameter instanceof FieldReference)) { String signature = ((PhpReference) parameter).getSignature(); if (StringUtil.isNotEmpty(signature)) { return refSignature + trimKey + signature; } return null; } return null; }