Skip to content

Commit

Permalink
Fix inferring closure return type after end statements change with mu…
Browse files Browse the repository at this point in the history
…ltiple ExecutionEndNode
  • Loading branch information
ondrejmirtes committed Jul 23, 2024
1 parent 4a0ffab commit 4463a3d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
20 changes: 14 additions & 6 deletions src/Analyser/MutatingScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -1375,12 +1375,12 @@ static function (Node $node, Scope $scope) use ($arrowScope, &$arrowFunctionImpu
$closureScope = $this->enterAnonymousFunctionWithoutReflection($node, $callableParameters);
$closureReturnStatements = [];
$closureYieldStatements = [];
$closureExecutionEnds = [];
$onlyNeverExecutionEnds = null;
$closureImpurePoints = [];
$invalidateExpressions = [];

try {
$closureStatementResult = $this->nodeScopeResolver->processStmtNodes($node, $node->stmts, $closureScope, static function (Node $node, Scope $scope) use ($closureScope, &$closureReturnStatements, &$closureYieldStatements, &$closureExecutionEnds, &$closureImpurePoints, &$invalidateExpressions): void {
$closureStatementResult = $this->nodeScopeResolver->processStmtNodes($node, $node->stmts, $closureScope, static function (Node $node, Scope $scope) use ($closureScope, &$closureReturnStatements, &$closureYieldStatements, &$onlyNeverExecutionEnds, &$closureImpurePoints, &$invalidateExpressions): void {
if ($scope->getAnonymousFunctionReflection() !== $closureScope->getAnonymousFunctionReflection()) {
return;
}
Expand All @@ -1405,16 +1405,24 @@ static function (Node $node, Scope $scope) use ($arrowScope, &$arrowFunctionImpu
if ($node->getStatementResult()->isAlwaysTerminating()) {
foreach ($node->getStatementResult()->getExitPoints() as $exitPoint) {
if ($exitPoint->getStatement() instanceof Node\Stmt\Return_) {
$onlyNeverExecutionEnds = false;
continue;
}

$closureExecutionEnds[] = $node;
if ($onlyNeverExecutionEnds === null) {
$onlyNeverExecutionEnds = true;
}

break;
}

if (count($node->getStatementResult()->getExitPoints()) === 0) {
$closureExecutionEnds[] = $node;
if ($onlyNeverExecutionEnds === null) {
$onlyNeverExecutionEnds = true;
}
}
} else {
$onlyNeverExecutionEnds = false;
}

return;
Expand Down Expand Up @@ -1449,13 +1457,13 @@ static function (Node $node, Scope $scope) use ($arrowScope, &$arrowFunctionImpu
}

if (count($returnTypes) === 0) {
if (count($closureExecutionEnds) > 0 && !$hasNull) {
if ($onlyNeverExecutionEnds === true && !$hasNull) {
$returnType = new NonAcceptingNeverType();
} else {
$returnType = new VoidType();
}
} else {
if (count($closureExecutionEnds) > 0) {
if ($onlyNeverExecutionEnds === true) {
$returnTypes[] = new NonAcceptingNeverType();
}
if ($hasNull) {
Expand Down
14 changes: 14 additions & 0 deletions tests/PHPStan/Rules/Missing/data/missing-return.php
Original file line number Diff line number Diff line change
Expand Up @@ -529,3 +529,17 @@ public function doFoo2(): int
}

}

class AnonymousFunctionOnlySometimesThrowsException
{

public function doFoo(): void
{
$cb = function (): void {
if (rand(0, 1)) {
throw new \Exception('bad luck');
}
};
}

}

0 comments on commit 4463a3d

Please sign in to comment.