Skip to content

Commit cf95fa0

Browse files
committed
use control flow for collecting method return statements
1 parent 02a2592 commit cf95fa0

File tree

3 files changed

+75
-8
lines changed

3 files changed

+75
-8
lines changed

src/main/java/fr/adrienbrault/idea/symfony2plugin/util/PhpElementsUtil.java

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@
1717
import com.jetbrains.php.PhpClassHierarchyUtils;
1818
import com.jetbrains.php.PhpIndex;
1919
import com.jetbrains.php.codeInsight.PhpCodeInsightUtil;
20+
import com.jetbrains.php.codeInsight.controlFlow.PhpControlFlowUtil;
21+
import com.jetbrains.php.codeInsight.controlFlow.PhpInstructionProcessor;
22+
import com.jetbrains.php.codeInsight.controlFlow.instructions.PhpCallInstruction;
23+
import com.jetbrains.php.codeInsight.controlFlow.instructions.PhpReturnInstruction;
2024
import com.jetbrains.php.completion.PhpLookupElement;
2125
import com.jetbrains.php.lang.PhpLangUtil;
2226
import com.jetbrains.php.lang.PhpLanguage;
@@ -634,26 +638,27 @@ static public String getMethodReturnAsString(@NotNull PhpClass phpClass, @NotNul
634638
* Find a string return value of a method context "function() { return 'foo'}"
635639
*/
636640
@NotNull
637-
static public Collection<String> getMethodReturnAsStrings(@NotNull PhpClass phpClass, @NotNull String methodName) {
638-
641+
public static Collection<String> getMethodReturnAsStrings(@NotNull PhpClass phpClass, @NotNull String methodName) {
639642
Method method = phpClass.findMethodByName(methodName);
640643
if(method == null) {
641644
return Collections.emptyList();
642645
}
643646

644-
final Set<String> values = new HashSet<>();
645-
method.acceptChildren(new PsiRecursiveElementWalkingVisitor() {
646-
@Override
647-
public void visitElement(PsiElement element) {
647+
Set<String> values = new HashSet<>();
648648

649+
PhpControlFlowUtil.processFlow(method.getControlFlow(), new PhpInstructionProcessor() {
650+
@Override
651+
public boolean processReturnInstruction(PhpReturnInstruction instruction) {
652+
PsiElement element = instruction.getArgument();
653+
649654
if(PhpElementsUtil.getMethodReturnPattern().accepts(element)) {
650655
String value = PhpElementsUtil.getStringValue(element);
651-
if(value != null && StringUtils.isNotBlank(value)) {
656+
if(StringUtils.isNotBlank(value)) {
652657
values.add(value);
653658
}
654659
}
655660

656-
super.visitElement(element);
661+
return super.processReturnInstruction(instruction);
657662
}
658663
});
659664

src/test/java/fr/adrienbrault/idea/symfony2plugin/tests/util/PhpElementsUtilTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package fr.adrienbrault.idea.symfony2plugin.tests.util;
22

3+
import com.intellij.openapi.vfs.VirtualFile;
34
import com.intellij.psi.PsiElement;
5+
import com.intellij.psi.PsiFile;
6+
import com.intellij.psi.util.PsiTreeUtil;
47
import com.intellij.util.containers.ContainerUtil;
58
import com.jetbrains.php.PhpIndex;
69
import com.jetbrains.php.lang.PhpFileType;
@@ -350,6 +353,14 @@ public void testGetMethodParameterListStringPattern() {
350353

351354
assertTrue(PhpElementsUtil.getMethodParameterListStringPattern().accepts(psiElement3));
352355
}
356+
public void testGetMethodWithFirstStringOrNamedArgumentPattern2() {
357+
PsiFile psiFile = myFixture.configureByFile("PhpElementsUtilReturn.php");
358+
PhpClass phpClass = PsiTreeUtil.collectElementsOfType(psiFile, PhpClass.class).iterator().next();
359+
360+
assertContainsElements(PhpElementsUtil.getMethodReturnAsStrings(phpClass, "foo1"), "foo1_1", "foo1_2", "foo1_3", "foo1_4_const", "Foo");
361+
assertContainsElements(PhpElementsUtil.getMethodReturnAsStrings(phpClass, "foo2"), "foo2_1", "foo2_2", "foo2_3", "foo2_4", "foo2_x");
362+
assertContainsElements(PhpElementsUtil.getMethodReturnAsStrings(phpClass, "foo3"), "foo3_1", "foo3_2", "foo3_3");
363+
}
353364

354365
public void testGetMethodWithFirstStringOrNamedArgumentPattern() {
355366
StringLiteralExpression psiElement = (StringLiteralExpression) myFixture.configureByText(PhpFileType.INSTANCE, "<?php" +
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
class Foo
4+
{
5+
const FOO = 'foo1_4_const';
6+
7+
public function foo1()
8+
{
9+
return 'foo1_1';
10+
11+
return 'foo1_2';
12+
13+
return 'foo1_3';
14+
15+
return self::FOO;
16+
17+
return Foo::class;
18+
}
19+
20+
public function foo2()
21+
{
22+
if (true) {
23+
return 'foo2_1';
24+
}
25+
26+
27+
if (true) {
28+
return 'foo2_2';
29+
if (true) {
30+
return 'foo2_3';
31+
if (true) {
32+
return 'foo2_4';
33+
}
34+
}
35+
}
36+
37+
return 'foo2_x';
38+
}
39+
40+
public function foo3()
41+
{
42+
switch (true) {
43+
case 0:
44+
return 'foo3_1';
45+
default:
46+
return 'foo3_2';
47+
}
48+
49+
return 'foo3_3';
50+
}
51+
}

0 commit comments

Comments
 (0)