Skip to content

Commit 523b7d4

Browse files
committed
fix method calls
1 parent e619c1a commit 523b7d4

File tree

3 files changed

+56
-4
lines changed

3 files changed

+56
-4
lines changed

src/Analyser/NodeScopeResolver.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2552,7 +2552,6 @@ 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();
25562555
if (
25572556
$nameType->isObject()->yes()
25582557
&& $nameType->isCallable()->yes()
@@ -2568,7 +2567,7 @@ static function (): void {
25682567
);
25692568
$throwPoints = array_merge($throwPoints, $invokeResult->getThrowPoints());
25702569
$impurePoints = array_merge($impurePoints, $invokeResult->getImpurePoints());
2571-
$isAlwaysTerminating = $isAlwaysTerminating || $invokeResult->isAlwaysTerminating();
2570+
$isAlwaysTerminating = $invokeResult->isAlwaysTerminating();
25722571
} elseif ($parametersAcceptor instanceof CallableParametersAcceptor) {
25732572
$callableThrowPoints = array_map(static fn (SimpleThrowPoint $throwPoint) => $throwPoint->isExplicit() ? ThrowPoint::createExplicit($scope, $throwPoint->getType(), $expr, $throwPoint->canContainAnyThrowable()) : ThrowPoint::createImplicit($scope, $expr), $parametersAcceptor->getThrowPoints());
25742573
if (!$this->implicitThrows) {
@@ -2815,6 +2814,7 @@ static function (): void {
28152814
$hasYield = $result->hasYield();
28162815
$throwPoints = $result->getThrowPoints();
28172816
$impurePoints = $result->getImpurePoints();
2817+
$isAlwaysTerminating = $result->isAlwaysTerminating();
28182818
$scope = $result->getScope();
28192819
if (isset($closureCallScope)) {
28202820
$scope = $scope->restoreOriginalScopeAfterClosureBind($originalScope);
@@ -2921,6 +2921,7 @@ static function (): void {
29212921
$hasYield = $hasYield || $result->hasYield();
29222922
$throwPoints = array_merge($throwPoints, $result->getThrowPoints());
29232923
$impurePoints = array_merge($impurePoints, $result->getImpurePoints());
2924+
$isAlwaysTerminating = $isAlwaysTerminating || $result->isAlwaysTerminating();
29242925
} elseif ($expr instanceof Expr\NullsafeMethodCall) {
29252926
$nonNullabilityResult = $this->ensureShallowNonNullability($scope, $scope, $expr->var);
29262927
$exprResult = $this->processExprNode($stmt, new MethodCall($expr->var, $expr->name, $expr->args, array_merge($expr->getAttributes(), ['virtualNullsafeMethodCall' => true])), $nonNullabilityResult->getScope(), $nodeCallback, $context);
@@ -3094,6 +3095,7 @@ static function (): void {
30943095
$hasYield = $hasYield || $result->hasYield();
30953096
$throwPoints = array_merge($throwPoints, $result->getThrowPoints());
30963097
$impurePoints = array_merge($impurePoints, $result->getImpurePoints());
3098+
$isAlwaysTerminating = $isAlwaysTerminating || $result->isAlwaysTerminating();
30973099
} elseif ($expr instanceof PropertyFetch) {
30983100
$scopeBeforeVar = $scope;
30993101
$result = $this->processExprNode($stmt, $expr->var, $scope, $nodeCallback, $context->enterDeep());

tests/PHPStan/Rules/DeadCode/UnreachableStatementRuleTest.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,23 @@ public function testBug13232a(): void
276276
],
277277
[
278278
'Unreachable statement - code above always terminates.',
279-
40,
279+
38,
280+
],
281+
[
282+
'Unreachable statement - code above always terminates.',
283+
44,
284+
],
285+
[
286+
'Unreachable statement - code above always terminates.',
287+
52,
288+
],
289+
[
290+
'Unreachable statement - code above always terminates.',
291+
61,
292+
],
293+
[
294+
'Unreachable statement - code above always terminates.',
295+
70,
280296
],
281297
]);
282298
}

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

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,19 @@ public function sayHe(): void
2828
$callable = function (): never {
2929
exit();
3030
};
31-
echo sprintf("Hello, %s no way", $callable);
31+
echo sprintf("Hello, %s no way", $callable());
32+
echo 'this will never happen';
33+
}
34+
35+
public function sayHe2(): void
36+
{
37+
$this->doFoo($this->neverReturnsMethod());
38+
echo 'this will never happen';
39+
}
40+
41+
public function sayHe3(): void
42+
{
43+
self::doStaticFoo($this->neverReturnsMethod());
3244
echo 'this will never happen';
3345
}
3446

@@ -40,9 +52,31 @@ public function sayHuu(): void
4052
echo 'this will never happen';
4153
}
4254

55+
public function sayClosure(): void
56+
{
57+
$callable = function (): never {
58+
exit();
59+
};
60+
$callable();
61+
echo 'this will never happen';
62+
}
63+
64+
public function sayIIFE(): void
65+
{
66+
(function (): never {
67+
exit();
68+
})();
69+
70+
echo 'this will never happen';
71+
}
72+
4373
function neverReturnsMethod(): never {
4474
exit();
4575
}
76+
77+
public function doFoo() {}
78+
79+
static public function doStaticFoo() {}
4680
}
4781
function neverReturns(): never {
4882
exit();

0 commit comments

Comments
 (0)