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
2 changes: 1 addition & 1 deletion src/Analyser/ExprHandler/MethodCallHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Ex
if ($parametersAcceptor !== null) {
$normalizedExpr = ArgumentsNormalizer::reorderMethodArguments($parametersAcceptor, $expr) ?? $expr;
$returnType = $parametersAcceptor->getReturnType();
$isAlwaysTerminating = $returnType instanceof NeverType && $returnType->isExplicit();
$isAlwaysTerminating = $isAlwaysTerminating || ($returnType instanceof NeverType && $returnType->isExplicit());
}

$argsResult = $nodeScopeResolver->processArgs(
Expand Down
2 changes: 1 addition & 1 deletion src/Analyser/ExprHandler/StaticCallHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Ex
if ($parametersAcceptor !== null) {
$normalizedExpr = ArgumentsNormalizer::reorderStaticCallArguments($parametersAcceptor, $expr) ?? $expr;
$returnType = $parametersAcceptor->getReturnType();
$isAlwaysTerminating = $returnType instanceof NeverType && $returnType->isExplicit();
$isAlwaysTerminating = $isAlwaysTerminating || ($returnType instanceof NeverType && $returnType->isExplicit());
}
$argsResult = $nodeScopeResolver->processArgs($stmt, $methodReflection, null, $parametersAcceptor, $normalizedExpr, $scope, $storage, $nodeCallback, $context, $closureBindScope);
$scope = $argsResult->getScope();
Expand Down
24 changes: 24 additions & 0 deletions tests/PHPStan/Rules/DeadCode/UnreachableStatementRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -373,4 +373,28 @@ public function testBug13331(): void
$this->analyse([__DIR__ . '/data/bug-13331.php'], []);
}

#[RequiresPhp('>= 8.2')]
public function testBug14328(): void
{
$this->treatPhpDocTypesAsCertain = true;
$this->analyse([__DIR__ . '/data/bug-14328.php'], [
[
'Unreachable statement - code above always terminates.',
20,
],
[
'Unreachable statement - code above always terminates.',
26,
],
[
'Unreachable statement - code above always terminates.',
32,
],
[
'Unreachable statement - code above always terminates.',
38,
],
]);
}

}
39 changes: 39 additions & 0 deletions tests/PHPStan/Rules/DeadCode/data/bug-14328.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php // lint >= 8.2

declare(strict_types = 1);

namespace Bug14328;

class Foo {
public function returnThis(mixed $value): self {
return $this;
}

public static function returnSelf(mixed $value): self {
return new self();
}
}

function testMethodCallChainedWithMethodCall(): void {
$callback = fn (): never => throw new \Exception();
$x = (new Foo())->returnThis($callback())->returnThis('x');
$y = 'this will never run';
}

function testMethodCallChainedWithStaticCall(): void {
$callback = fn (): never => throw new \Exception();
$x = (new Foo())->returnThis($callback())::returnSelf('x');
$y = 'this will never run';
}

function testStaticCallChainedWithMethodCall(): void {
$callback = fn (): never => throw new \Exception();
$a = Foo::returnSelf($callback())->returnThis('x');
$b = 'this will never run either';
}

function testStaticCallChainedWithStaticCall(): void {
$callback = fn (): never => throw new \Exception();
$a = Foo::returnSelf($callback())::returnSelf('x');
$b = 'this will never run either';
}
Loading