Skip to content

Commit e9b415f

Browse files
committed
Fix missing detection of dead code in closures
1 parent 9bde094 commit e9b415f

File tree

3 files changed

+13
-2
lines changed

3 files changed

+13
-2
lines changed

src/Analyser/NodeScopeResolver.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4690,7 +4690,7 @@ private function processClosureNode(
46904690
array_merge($statementResult->getImpurePoints(), $closureImpurePoints),
46914691
), $closureScope);
46924692

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

46964696
$count = 0;
@@ -4736,7 +4736,7 @@ private function processClosureNode(
47364736
array_merge($statementResult->getImpurePoints(), $closureImpurePoints),
47374737
), $closureScope);
47384738

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

47424742
/**
@@ -5180,6 +5180,7 @@ private function processArgs(
51805180
if ($callCallbackImmediately) {
51815181
$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()));
51825182
$impurePoints = array_merge($impurePoints, $closureResult->getImpurePoints());
5183+
$isAlwaysTerminating = $isAlwaysTerminating || $closureResult->isAlwaysTerminating();
51835184
}
51845185

51855186
$uses = [];

src/Analyser/ProcessClosureResult.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public function __construct(
1717
private array $throwPoints,
1818
private array $impurePoints,
1919
private array $invalidateExpressions,
20+
private bool $isAlwaysTerminating,
2021
)
2122
{
2223
}
@@ -50,4 +51,9 @@ public function getInvalidateExpressions(): array
5051
return $this->invalidateExpressions;
5152
}
5253

54+
public function isAlwaysTerminating(): bool
55+
{
56+
return $this->isAlwaysTerminating;
57+
}
58+
5359
}

tests/PHPStan/Analyser/ExpressionResultTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,10 @@ public static function dataIsAlwaysTerminating(): array
113113
'call_user_func(fn() => exit());',
114114
true,
115115
],
116+
[
117+
'call_user_func(function() { exit(); });',
118+
true,
119+
],
116120
[
117121
'var_dump(1+exit());',
118122
true,

0 commit comments

Comments
 (0)