Skip to content

Commit

Permalink
Fix for Closure returns
Browse files Browse the repository at this point in the history
  • Loading branch information
herndlm committed Nov 30, 2023
1 parent 7dbe5a8 commit 42d4d9f
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/Analyser/MutatingScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -1231,7 +1231,9 @@ private function resolveType(string $exprString, Expr $node): Type
new VoidType(),
]);
} else {
$returnType = $arrowScope->getType($node->expr);
$expr = clone $node->expr;
$expr->setAttribute('keepVoid', true);
$returnType = $arrowScope->getType($expr);
if ($node->returnType !== null) {
$returnType = TypehintHelper::decideType($this->getFunctionType($node->returnType, false, false), $returnType);
}
Expand Down
10 changes: 10 additions & 0 deletions tests/PHPStan/Rules/Methods/ReturnTypeRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -974,4 +974,14 @@ public function testWrongListTip(): void
]);
}

public function testArrowFunctionReturningVoidClosure(): void
{
if (PHP_VERSION_ID < 70400) {
$this->markTestSkipped('Test requires PHP 7.4.');
}

$this->checkExplicitMixed = true;
$this->analyse([__DIR__ . '/data/arrow-function-returning-void-closure.php'], []);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace ArrowFunctionReturningVoidClosure;

class ArrowFunctionReturningVoidClosure
{
/**
* @return \Closure(): void
*/
public function getClosure(): \Closure
{
return fn () => $this->returnVoid();
}

public function returnVoid(): void
{

}
}

0 comments on commit 42d4d9f

Please sign in to comment.