Skip to content

Fix missing detection of dead code in closures #4148

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

Draft
wants to merge 3 commits into
base: 2.1.x
Choose a base branch
from
Draft
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
5 changes: 3 additions & 2 deletions src/Analyser/NodeScopeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -4690,7 +4690,7 @@
array_merge($statementResult->getImpurePoints(), $closureImpurePoints),
), $closureScope);

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

$count = 0;
Expand Down Expand Up @@ -4736,7 +4736,7 @@
array_merge($statementResult->getImpurePoints(), $closureImpurePoints),
), $closureScope);

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

/**
Expand Down Expand Up @@ -5174,6 +5174,7 @@
if ($callCallbackImmediately) {
$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()));
$impurePoints = array_merge($impurePoints, $closureResult->getImpurePoints());
$isAlwaysTerminating = $isAlwaysTerminating || $closureResult->isAlwaysTerminating();
}

$uses = [];
Expand Down Expand Up @@ -6701,7 +6702,7 @@

return $param->var->name;
}, $node->getParams());
$resolvedPhpDoc = $this->phpDocInheritanceResolver->resolvePhpDocForMethod(

Check failure on line 6705 in src/Analyser/NodeScopeResolver.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.3)

Unreachable statement - code above always terminates.

Check failure on line 6705 in src/Analyser/NodeScopeResolver.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.4)

Unreachable statement - code above always terminates.

Check failure on line 6705 in src/Analyser/NodeScopeResolver.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.2)

Unreachable statement - code above always terminates.

Check failure on line 6705 in src/Analyser/NodeScopeResolver.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.2, ubuntu-latest)

Unreachable statement - code above always terminates.

Check failure on line 6705 in src/Analyser/NodeScopeResolver.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.3, ubuntu-latest)

Unreachable statement - code above always terminates.

Check failure on line 6705 in src/Analyser/NodeScopeResolver.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.4, ubuntu-latest)

Unreachable statement - code above always terminates.

Check failure on line 6705 in src/Analyser/NodeScopeResolver.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1, ubuntu-latest)

Unreachable statement - code above always terminates.

Check failure on line 6705 in src/Analyser/NodeScopeResolver.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.2, windows-latest)

Unreachable statement - code above always terminates.

Check failure on line 6705 in src/Analyser/NodeScopeResolver.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.4, windows-latest)

Unreachable statement - code above always terminates.

Check failure on line 6705 in src/Analyser/NodeScopeResolver.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.3, windows-latest)

Unreachable statement - code above always terminates.

Check failure on line 6705 in src/Analyser/NodeScopeResolver.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1, windows-latest)

Unreachable statement - code above always terminates.
$docComment,
$file,
$scope->getClassReflection(),
Expand All @@ -6723,7 +6724,7 @@
if (
!$param->var instanceof Variable
|| !is_string($param->var->name)
) {

Check failure on line 6727 in src/Analyser/NodeScopeResolver.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.4, ubuntu-latest)

Unreachable statement - code above always terminates.

Check failure on line 6727 in src/Analyser/NodeScopeResolver.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.4, windows-latest)

Unreachable statement - code above always terminates.
throw new ShouldNotHappenException();
}

Expand Down Expand Up @@ -6755,7 +6756,7 @@
}
}

if ($docComment !== null && $resolvedPhpDoc === null) {

Check failure on line 6759 in src/Analyser/NodeScopeResolver.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.3)

Strict comparison using === between null and null will always evaluate to true.

Check failure on line 6759 in src/Analyser/NodeScopeResolver.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.4)

Strict comparison using === between null and null will always evaluate to true.

Check failure on line 6759 in src/Analyser/NodeScopeResolver.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.2)

Strict comparison using === between null and null will always evaluate to true.

Check failure on line 6759 in src/Analyser/NodeScopeResolver.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.2, ubuntu-latest)

Strict comparison using === between null and null will always evaluate to true.

Check failure on line 6759 in src/Analyser/NodeScopeResolver.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.3, ubuntu-latest)

Strict comparison using === between null and null will always evaluate to true.

Check failure on line 6759 in src/Analyser/NodeScopeResolver.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.4, ubuntu-latest)

Strict comparison using === between null and null will always evaluate to true.

Check failure on line 6759 in src/Analyser/NodeScopeResolver.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1, ubuntu-latest)

Strict comparison using === between null and null will always evaluate to true.

Check failure on line 6759 in src/Analyser/NodeScopeResolver.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.2, windows-latest)

Strict comparison using === between null and null will always evaluate to true.

Check failure on line 6759 in src/Analyser/NodeScopeResolver.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.4, windows-latest)

Strict comparison using === between null and null will always evaluate to true.

Check failure on line 6759 in src/Analyser/NodeScopeResolver.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.3, windows-latest)

Strict comparison using === between null and null will always evaluate to true.

Check failure on line 6759 in src/Analyser/NodeScopeResolver.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1, windows-latest)

Strict comparison using === between null and null will always evaluate to true.
$resolvedPhpDoc = $this->fileTypeMapper->getResolvedPhpDoc(
$file,
$class,
Expand All @@ -6775,9 +6776,9 @@
}
$paramType = $paramTag->getType();
if ($scope->isInClass()) {
$paramType = $this->transformStaticType($scope->getClassReflection(), $paramType);

Check failure on line 6779 in src/Analyser/NodeScopeResolver.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.0, ubuntu-latest)

Unreachable statement - code above always terminates.

Check failure on line 6779 in src/Analyser/NodeScopeResolver.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.0, windows-latest)

Unreachable statement - code above always terminates.
}
$phpDocParameterTypes[$paramName] = $paramType;

Check failure on line 6781 in src/Analyser/NodeScopeResolver.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.4, ubuntu-latest)

Strict comparison using === between null and null will always evaluate to true.

Check failure on line 6781 in src/Analyser/NodeScopeResolver.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.4, windows-latest)

Strict comparison using === between null and null will always evaluate to true.
}
foreach ($resolvedPhpDoc->getParamClosureThisTags() as $paramName => $paramClosureThisTag) {
if (array_key_exists($paramName, $phpDocClosureThisTypeParameters)) {
Expand Down Expand Up @@ -6829,7 +6830,7 @@
$changedType = $changedType->getStaticObjectType();
}
return $traverse($changedType);
}

Check failure on line 6833 in src/Analyser/NodeScopeResolver.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.0, ubuntu-latest)

Strict comparison using === between null and null will always evaluate to true.

Check failure on line 6833 in src/Analyser/NodeScopeResolver.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.0, windows-latest)

Strict comparison using === between null and null will always evaluate to true.

return $traverse($type);
});
Expand Down
6 changes: 6 additions & 0 deletions src/Analyser/ProcessClosureResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public function __construct(
private array $throwPoints,
private array $impurePoints,
private array $invalidateExpressions,
private bool $isAlwaysTerminating,
)
{
}
Expand Down Expand Up @@ -50,4 +51,9 @@ public function getInvalidateExpressions(): array
return $this->invalidateExpressions;
}

public function isAlwaysTerminating(): bool
{
return $this->isAlwaysTerminating;
}

}
12 changes: 12 additions & 0 deletions tests/PHPStan/Analyser/ExpressionResultTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,18 @@ public static function dataIsAlwaysTerminating(): array
'exit() ?? $x;',
true,
],
[
'(function() { exit(); })();',
true,
],
[
'call_user_func(function() { exit(); });',
true,
],
[
'usort($arr, static function($a, $b):int { return $a <=> $b; });',
false,
],
[
'var_dump(1+exit());',
true,
Expand Down
Loading