Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/Analyser/NodeScopeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -4639,6 +4639,9 @@ private function processClosureNode(
throw new ShouldNotHappenException();
}

$returnType = $closureType->getReturnType();
$isAlwaysTerminating = ($returnType instanceof NeverType && $returnType->isExplicit());

$nodeCallback(new InClosureNode($closureType, $expr), $closureScope);

$executionEnds = [];
Expand Down Expand Up @@ -4690,7 +4693,7 @@ private function processClosureNode(
array_merge($statementResult->getImpurePoints(), $closureImpurePoints),
), $closureScope);

return new ProcessClosureResult($scope, $statementResult->getThrowPoints(), $statementResult->getImpurePoints(), $invalidateExpressions);
return new ProcessClosureResult($scope, $statementResult->getThrowPoints(), $statementResult->getImpurePoints(), $invalidateExpressions, $isAlwaysTerminating);
}

$count = 0;
Expand Down Expand Up @@ -4736,7 +4739,7 @@ private function processClosureNode(
array_merge($statementResult->getImpurePoints(), $closureImpurePoints),
), $closureScope);

return new ProcessClosureResult($scope->processClosureScope($closureResultScope, null, $byRefUses), $statementResult->getThrowPoints(), $statementResult->getImpurePoints(), $invalidateExpressions);
return new ProcessClosureResult($scope->processClosureScope($closureResultScope, null, $byRefUses), $statementResult->getThrowPoints(), $statementResult->getImpurePoints(), $invalidateExpressions, $isAlwaysTerminating);
}

/**
Expand Down Expand Up @@ -5180,6 +5183,7 @@ private function processArgs(
if ($callCallbackImmediately) {
$throwPoints = array_merge($throwPoints, array_map(static fn (ThrowPoint $throwPoint) => $throwPoint->isExplicit() ? ThrowPoint::createExplicit($scope, $throwPoint->getType(), $arg->value, $throwPoint->canContainAnyThrowable()) : ThrowPoint::createImplicit($scope, $arg->value), $closureResult->getThrowPoints()));
$impurePoints = array_merge($impurePoints, $closureResult->getImpurePoints());
$isAlwaysTerminating = $isAlwaysTerminating || $closureResult->isAlwaysTerminating();
}

$uses = [];
Expand Down
6 changes: 6 additions & 0 deletions src/Analyser/ProcessClosureResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public function __construct(
private array $throwPoints,
private array $impurePoints,
private array $invalidateExpressions,
private bool $isAlwaysTerminating,
)
{
}
Expand Down Expand Up @@ -50,4 +51,9 @@ public function getInvalidateExpressions(): array
return $this->invalidateExpressions;
}

public function isAlwaysTerminating(): bool
{
return $this->isAlwaysTerminating;
}

}
16 changes: 16 additions & 0 deletions tests/PHPStan/Analyser/ExpressionResultTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,22 @@ public static function dataIsAlwaysTerminating(): array
'call_user_func(fn() => exit());',
true,
],
[
'(function() { exit(); })();',
true,
],
[
'function () {};',
false,
],
[
'call_user_func(function() { exit(); });',
true,
],
[
'usort($arr, static function($a, $b):int { return $a <=> $b; });',
false,
],
[
'var_dump(1+exit());',
true,
Expand Down
Loading