Skip to content

Fix missing detection of dead code in expressions #4090

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 28 commits into from
Jul 21, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
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
8 changes: 4 additions & 4 deletions src/Analyser/NodeScopeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -3289,7 +3289,7 @@ static function (): void {
return new ExpressionResult(
$leftMergedWithRightScope,
$leftResult->hasYield() || $rightResult->hasYield(),
false,
$leftResult->isAlwaysTerminating() || $rightResult->isAlwaysTerminating(),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The right side might never be executed so this should only mention the left side.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$x && exit(); should be false

Copy link
Contributor Author

@staabm staabm Jul 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is false, see https://github.com/phpstan/phpstan-src/pull/4090/files#diff-fa85cdb8d6b73f343f042bbbe433ecc95119bf3dc5e76cc7291563d1c49cbd0dR87

I guess you was looking at an outdated version of the PR while commenting

array_merge($leftResult->getThrowPoints(), $rightResult->getThrowPoints()),
array_merge($leftResult->getImpurePoints(), $rightResult->getImpurePoints()),
static fn (): MutatingScope => $rightResult->getScope()->filterByTruthyValue($expr),
Expand All @@ -3310,7 +3310,7 @@ static function (): void {
return new ExpressionResult(
$leftMergedWithRightScope,
$leftResult->hasYield() || $rightResult->hasYield(),
false,
$leftResult->isAlwaysTerminating(),
array_merge($leftResult->getThrowPoints(), $rightResult->getThrowPoints()),
array_merge($leftResult->getImpurePoints(), $rightResult->getImpurePoints()),
static fn (): MutatingScope => $leftMergedWithRightScope->filterByTruthyValue($expr),
Expand All @@ -3335,7 +3335,7 @@ static function (): void {
$hasYield = $condResult->hasYield() || $rightResult->hasYield();
$throwPoints = array_merge($condResult->getThrowPoints(), $rightResult->getThrowPoints());
$impurePoints = array_merge($condResult->getImpurePoints(), $rightResult->getImpurePoints());
$isAlwaysTerminating = false;
$isAlwaysTerminating = $condResult->isAlwaysTerminating();
} elseif ($expr instanceof BinaryOp) {
$result = $this->processExprNode($stmt, $expr->left, $scope, $nodeCallback, $context->enterDeep());
$scope = $result->getScope();
Expand Down Expand Up @@ -3368,7 +3368,7 @@ static function (): void {
true,
);
$hasYield = $result->hasYield();
$isAlwaysTerminating = false;
$isAlwaysTerminating = $result->isAlwaysTerminating();
$scope = $result->getScope()->afterExtractCall();
} elseif ($expr instanceof Expr\Print_) {
$result = $this->processExprNode($stmt, $expr->expr, $scope, $nodeCallback, $context->enterDeep());
Expand Down
24 changes: 24 additions & 0 deletions tests/PHPStan/Analyser/ExpressionResultTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ public static function dataIsAlwaysTerminating(): array
'(string) $x;',
false,
],
[
'$x || exit();',
false,
],
[
'$x ?? exit();',
false,
],
[
'sprintf("hello %s", exit());',
true,
Expand Down Expand Up @@ -78,6 +86,22 @@ public static function dataIsAlwaysTerminating(): array
'@exit();',
true,
],
[
'$x && exit();',
true,
],
[
'exit() && $x;',
true,
],
[
'exit() || $x;',
true,
],
[
'exit() ?? $x;',
true,
],
];
}

Expand Down