Skip to content

Commit 6e9a443

Browse files
committed
fix func args
1 parent c9e7f53 commit 6e9a443

File tree

3 files changed

+24
-5
lines changed

3 files changed

+24
-5
lines changed

src/Analyser/NodeScopeResolver.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2552,6 +2552,7 @@ function (MutatingScope $scope) use ($stmt, $expr, $nodeCallback, $context): Exp
25522552
$scope = $nameResult->getScope();
25532553
$throwPoints = $nameResult->getThrowPoints();
25542554
$impurePoints = $nameResult->getImpurePoints();
2555+
$isAlwaysTerminating = $nameResult->isAlwaysTerminating();
25552556
if (
25562557
$nameType->isObject()->yes()
25572558
&& $nameType->isCallable()->yes()
@@ -2567,6 +2568,7 @@ static function (): void {
25672568
);
25682569
$throwPoints = array_merge($throwPoints, $invokeResult->getThrowPoints());
25692570
$impurePoints = array_merge($impurePoints, $invokeResult->getImpurePoints());
2571+
$isAlwaysTerminating = $isAlwaysTerminating || $invokeResult->isAlwaysTerminating();
25702572
} elseif ($parametersAcceptor instanceof CallableParametersAcceptor) {
25712573
$callableThrowPoints = array_map(static fn (SimpleThrowPoint $throwPoint) => $throwPoint->isExplicit() ? ThrowPoint::createExplicit($scope, $throwPoint->getType(), $expr, $throwPoint->canContainAnyThrowable()) : ThrowPoint::createImplicit($scope, $expr), $parametersAcceptor->getThrowPoints());
25722574
if (!$this->implicitThrows) {
@@ -2602,13 +2604,14 @@ static function (): void {
26022604
if ($parametersAcceptor !== null) {
26032605
$expr = ArgumentsNormalizer::reorderFuncArguments($parametersAcceptor, $expr) ?? $expr;
26042606
$returnType = $parametersAcceptor->getReturnType();
2605-
$isAlwaysTerminating = $returnType instanceof NeverType && $returnType->isExplicit();
2607+
$isAlwaysTerminating = $isAlwaysTerminating || $returnType instanceof NeverType && $returnType->isExplicit();
26062608
}
26072609
$result = $this->processArgs($stmt, $functionReflection, null, $parametersAcceptor, $expr, $scope, $nodeCallback, $context);
26082610
$scope = $result->getScope();
26092611
$hasYield = $result->hasYield();
26102612
$throwPoints = array_merge($throwPoints, $result->getThrowPoints());
26112613
$impurePoints = array_merge($impurePoints, $result->getImpurePoints());
2614+
$isAlwaysTerminating = $isAlwaysTerminating || $result->isAlwaysTerminating();
26122615

26132616
if ($functionReflection !== null) {
26142617
$functionThrowPoint = $this->getFunctionThrowPoint($functionReflection, $parametersAcceptor, $expr, $scope);
@@ -5009,6 +5012,7 @@ private function processArgs(
50095012
$hasYield = false;
50105013
$throwPoints = [];
50115014
$impurePoints = [];
5015+
$isAlwaysTerminating = false;
50125016
foreach ($args as $i => $arg) {
50135017
$assignByReference = false;
50145018
$parameter = null;
@@ -5158,6 +5162,7 @@ private function processArgs(
51585162
$exprResult = $this->processExprNode($stmt, $arg->value, $scopeToPass, $nodeCallback, $context->enterDeep());
51595163
$throwPoints = array_merge($throwPoints, $exprResult->getThrowPoints());
51605164
$impurePoints = array_merge($impurePoints, $exprResult->getImpurePoints());
5165+
$isAlwaysTerminating = $isAlwaysTerminating || $exprResult->isAlwaysTerminating();
51615166
$scope = $exprResult->getScope();
51625167
$hasYield = $hasYield || $exprResult->hasYield();
51635168

@@ -5282,7 +5287,7 @@ static function (Node $node, Scope $scope) use ($nodeCallback): void {
52825287
}
52835288
}
52845289

5285-
return new ExpressionResult($scope, $hasYield, $throwPoints, $impurePoints);
5290+
return new ExpressionResult($scope, $hasYield, $throwPoints, $impurePoints, isAlwaysTerminating: $isAlwaysTerminating);
52865291
}
52875292

52885293
/**

tests/PHPStan/Rules/DeadCode/UnreachableStatementRuleTest.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,12 +248,16 @@ public function testBug13232a(): void
248248
$this->analyse([__DIR__ . '/data/bug-13232a.php'], [
249249
[
250250
'Unreachable statement - code above always terminates.',
251-
11,
251+
10,
252252
],
253253
[
254254
'Unreachable statement - code above always terminates.',
255255
17,
256256
],
257+
[
258+
'Unreachable statement - code above always terminates.',
259+
23,
260+
],
257261
]);
258262
}
259263

tests/PHPStan/Rules/DeadCode/data/bug-13232a.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44

55
final class HelloWorld
66
{
7+
public function sayHa(): void
8+
{
9+
echo sprintf("Hello, %s no way", $this->neverReturnsMethod());
10+
echo 'this will never happen';
11+
}
12+
713
public function sayHi(): void
814
{
915
echo 'Hello, ' . neverReturns()
@@ -17,7 +23,11 @@ public function sayHo(): void
1723
echo 'this will never happen';
1824
}
1925

20-
function neverReturnsMethod(): never {}
26+
function neverReturnsMethod(): never {
27+
exit();
28+
}
29+
}
30+
function neverReturns(): never {
31+
exit();
2132
}
22-
function neverReturns(): never {}
2333

0 commit comments

Comments
 (0)