/** * Find a string return value of a method context "function() { return 'foo'}" First match wins */ @Nullable public static String getMethodReturnAsString( @NotNull PhpClass phpClass, @NotNull String methodName) { Method method = phpClass.findMethodByName(methodName); if (method == null) { return null; } final Set<String> values = new HashSet<String>(); method.acceptChildren( new PsiRecursiveElementWalkingVisitor() { @Override public void visitElement(PsiElement element) { if (PhpElementsUtil.getMethodReturnPattern().accepts(element)) { String value = PhpElementsUtil.getStringValue(element); if (value != null && StringUtils.isNotBlank(value)) { values.add(value); } } super.visitElement(element); } }); if (values.size() == 0) { return null; } // we support only first item return values.iterator().next(); }
private static void collectMethodElementsWithParents( final @NotNull Method method, final int depth, @NotNull final Collection<PsiElement> elements, @NotNull final Processor<PsiElement> processor) { method.acceptChildren( new PsiRecursiveElementWalkingVisitor() { @Override public void visitElement(PsiElement psiElement) { if (processor.process(psiElement)) { elements.add(psiElement); } if (psiElement instanceof MethodReference && ((MethodReference) psiElement).getReferenceType() == PhpModifier.State.PARENT && method.getName().equals(((MethodReference) psiElement).getName())) { PsiElement resolve = ((MethodReference) psiElement).resolve(); if (depth > 0 && resolve instanceof Method) { collectMethodElementsWithParents((Method) resolve, depth - 1, elements, processor); } } super.visitElement(psiElement); } }); }