Skip to content

Commit 00d29ea

Browse files
authored
Merge branch refs/heads/1.11.x into 1.12.x
2 parents 35898a8 + 4463a3d commit 00d29ea

File tree

2 files changed

+28
-6
lines changed

2 files changed

+28
-6
lines changed

src/Analyser/MutatingScope.php

+14-6
Original file line numberDiff line numberDiff line change
@@ -1375,12 +1375,12 @@ static function (Node $node, Scope $scope) use ($arrowScope, &$arrowFunctionImpu
13751375
$closureScope = $this->enterAnonymousFunctionWithoutReflection($node, $callableParameters);
13761376
$closureReturnStatements = [];
13771377
$closureYieldStatements = [];
1378-
$closureExecutionEnds = [];
1378+
$onlyNeverExecutionEnds = null;
13791379
$closureImpurePoints = [];
13801380
$invalidateExpressions = [];
13811381

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

1411-
$closureExecutionEnds[] = $node;
1412+
if ($onlyNeverExecutionEnds === null) {
1413+
$onlyNeverExecutionEnds = true;
1414+
}
1415+
14121416
break;
14131417
}
14141418

14151419
if (count($node->getStatementResult()->getExitPoints()) === 0) {
1416-
$closureExecutionEnds[] = $node;
1420+
if ($onlyNeverExecutionEnds === null) {
1421+
$onlyNeverExecutionEnds = true;
1422+
}
14171423
}
1424+
} else {
1425+
$onlyNeverExecutionEnds = false;
14181426
}
14191427

14201428
return;
@@ -1449,13 +1457,13 @@ static function (Node $node, Scope $scope) use ($arrowScope, &$arrowFunctionImpu
14491457
}
14501458

14511459
if (count($returnTypes) === 0) {
1452-
if (count($closureExecutionEnds) > 0 && !$hasNull) {
1460+
if ($onlyNeverExecutionEnds === true && !$hasNull) {
14531461
$returnType = new NonAcceptingNeverType();
14541462
} else {
14551463
$returnType = new VoidType();
14561464
}
14571465
} else {
1458-
if (count($closureExecutionEnds) > 0) {
1466+
if ($onlyNeverExecutionEnds === true) {
14591467
$returnTypes[] = new NonAcceptingNeverType();
14601468
}
14611469
if ($hasNull) {

tests/PHPStan/Rules/Missing/data/missing-return.php

+14
Original file line numberDiff line numberDiff line change
@@ -529,3 +529,17 @@ public function doFoo2(): int
529529
}
530530

531531
}
532+
533+
class AnonymousFunctionOnlySometimesThrowsException
534+
{
535+
536+
public function doFoo(): void
537+
{
538+
$cb = function (): void {
539+
if (rand(0, 1)) {
540+
throw new \Exception('bad luck');
541+
}
542+
};
543+
}
544+
545+
}

0 commit comments

Comments
 (0)