Skip to content

Commit 7ac6383

Browse files
committed
Solve issues with arrow functions containing void expression
1 parent 143741c commit 7ac6383

File tree

4 files changed

+44
-2
lines changed

4 files changed

+44
-2
lines changed

src/Analyser/NodeScopeResolver.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1843,7 +1843,7 @@ static function () use ($scope, $expr): MutatingScope {
18431843

18441844
$arrowFunctionScope = $scope->enterArrowFunction($expr);
18451845
$nodeCallback(new InArrowFunctionNode($expr), $arrowFunctionScope);
1846-
$this->processExprNode($expr->expr, $arrowFunctionScope, $nodeCallback, $context);
1846+
$this->processExprNode($expr->expr, $arrowFunctionScope, $nodeCallback, ExpressionContext::createTopLevel());
18471847
$hasYield = false;
18481848

18491849
} elseif ($expr instanceof ErrorSuppress) {

src/Rules/Functions/ArrowFunctionReturnTypeRule.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use PHPStan\Node\InArrowFunctionNode;
88
use PHPStan\Rules\FunctionReturnTypeCheck;
99
use PHPStan\Type\ObjectType;
10+
use PHPStan\Type\VoidType;
1011

1112
/**
1213
* @implements \PHPStan\Rules\Rule<\PHPStan\Node\InArrowFunctionNode>
@@ -36,10 +37,16 @@ public function processNode(Node $node, Scope $scope): array
3637
$returnType = $scope->getAnonymousFunctionReturnType();
3738
$generatorType = new ObjectType(\Generator::class);
3839

40+
$originalNode = $node->getOriginalNode();
41+
$isVoidSuperType = (new VoidType())->isSuperTypeOf($returnType);
42+
if ($originalNode->returnType === null && $isVoidSuperType->yes()) {
43+
return [];
44+
}
45+
3946
return $this->returnTypeCheck->checkReturnType(
4047
$scope,
4148
$returnType,
42-
$node->getOriginalNode()->expr,
49+
$originalNode->expr,
4350
'Anonymous function should return %s but empty return statement found.',
4451
'Anonymous function with return type void returns %s but should not return anything.',
4552
'Anonymous function should return %s but returns %s.',

tests/PHPStan/Rules/Functions/data/arrow-functions-return-type.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,18 @@ public function doFoo(int $i)
1515
}
1616

1717
}
18+
19+
class Bar
20+
{
21+
22+
public function doFoo(): void
23+
{
24+
25+
}
26+
27+
public function doBar(): void
28+
{
29+
fn () => $this->doFoo();
30+
}
31+
32+
}

tests/PHPStan/Rules/Methods/data/arrow-function-bind.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,23 @@ public function fooMethod(): Foo
3838
}
3939

4040
}
41+
42+
class BazVoid
43+
{
44+
45+
public function doFoo(): void
46+
{
47+
48+
}
49+
50+
public function doBar(): void
51+
{
52+
$this->doBaz(fn () => $this->doFoo());
53+
}
54+
55+
public function doBaz(callable $cb): void
56+
{
57+
58+
}
59+
60+
}

0 commit comments

Comments
 (0)