Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 197a46e

Browse files
Merge pull request #2111 from Haehnchen/feature/control-flow-return
use control flow for collecting method return statements
2 parents 02a2592 + 667bfdf commit 197a46e

File tree

3 files changed

+74
-8
lines changed

3 files changed

+74
-8
lines changed

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

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
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.PhpReturnInstruction;
2023
import com.jetbrains.php.completion.PhpLookupElement;
2124
import com.jetbrains.php.lang.PhpLangUtil;
2225
import com.jetbrains.php.lang.PhpLanguage;
@@ -634,26 +637,27 @@ static public String getMethodReturnAsString(@NotNull PhpClass phpClass, @NotNul
634637
* Find a string return value of a method context "function() { return 'foo'}"
635638
*/
636639
@NotNull
637-
static public Collection<String> getMethodReturnAsStrings(@NotNull PhpClass phpClass, @NotNull String methodName) {
638-
640+
public static Collection<String> getMethodReturnAsStrings(@NotNull PhpClass phpClass, @NotNull String methodName) {
639641
Method method = phpClass.findMethodByName(methodName);
640642
if(method == null) {
641643
return Collections.emptyList();
642644
}
643645

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

648+
PhpControlFlowUtil.processFlow(method.getControlFlow(), new PhpInstructionProcessor() {
649+
@Override
650+
public boolean processReturnInstruction(PhpReturnInstruction instruction) {
651+
PsiElement element = instruction.getArgument();
652+
649653
if(PhpElementsUtil.getMethodReturnPattern().accepts(element)) {
650654
String value = PhpElementsUtil.getStringValue(element);
651-
if(value != null && StringUtils.isNotBlank(value)) {
655+
if(StringUtils.isNotBlank(value)) {
652656
values.add(value);
653657
}
654658
}
655659

656-
super.visitElement(element);
660+
returnsuper.processReturnInstruction(instruction);
657661
}
658662
});
659663

‎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 testGetMethodReturnAsStrings() {
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)

AltStyle によって変換されたページ (->オリジナル) /