Skip to content

Commit 9c0d333

Browse files
committed
supporting type resolving in closures Haehnchen#38
1 parent 6efec71 commit 9c0d333

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

src/main/java/de/espend/idea/php/phpunit/utils/ChainVisitorUtil.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,18 @@ private static void visit(@Nullable PsiElement psiElement, @NotNull ChainProcess
5858
}
5959
}
6060

61+
/**
62+
* Find variables inside a function scope, ignoring closures
63+
*/
6164
@NotNull
6265
private static Collection<PhpPsiElement> resolveVariable(@NotNull Variable variable) {
6366
String name = variable.getName();
6467

65-
Function methodScope = PhpPsiUtil.getParentByCondition(variable, Function.INSTANCEOF);
68+
Function methodScope = PhpPsiUtil.getParentByCondition(
69+
variable,
70+
psiElement -> psiElement instanceof Function && !((Function) psiElement).isClosure()
71+
);
72+
6673
if(methodScope == null) {
6774
return Collections.emptyList();
6875
}

src/test/java/de/espend/idea/php/phpunit/tests/type/ProphecyTypeProviderTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,22 @@ public void testThatProphesizeForVariableIsResolved() {
3333
);
3434
}
3535

36+
public void testThatProphesizeForVariableIsResolvedForClosure() {
37+
assertPhpReferenceResolveTo(PhpFileType.INSTANCE, "<?php\n" +
38+
"class FooTest extends \\PHPUnit\\Framework\\TestCase\n" +
39+
" {\n" +
40+
" public function testFoobar()\n" +
41+
" {\n" +
42+
" $foo = $this->prophesize(Foo::class);\n" +
43+
" $closure = function() use ($class) {\n" +
44+
" $foo->getBar()->will<caret>Return();\n" +
45+
" };" +
46+
" }\n" +
47+
" }",
48+
PlatformPatterns.psiElement(Method.class).withName("willReturn")
49+
);
50+
}
51+
3652
public void testThatProphesizeForVariableInPropertyIsResolved() {
3753
assertPhpReferenceResolveTo(PhpFileType.INSTANCE, "<?php\n" +
3854
"class FooTest extends \\PHPUnit\\Framework\\TestCase\n" +

0 commit comments

Comments
 (0)