Skip to content

Commit a4b453e

Browse files
committed
ExitHandler
1 parent 1bd36df commit a4b453e

2 files changed

Lines changed: 64 additions & 16 deletions

File tree

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Analyser\ExprHandler;
4+
5+
use PhpParser\Node\Expr;
6+
use PhpParser\Node\Expr\Exit_;
7+
use PhpParser\Node\Stmt;
8+
use PHPStan\Analyser\ExpressionContext;
9+
use PHPStan\Analyser\ExpressionResult;
10+
use PHPStan\Analyser\ExpressionResultStorage;
11+
use PHPStan\Analyser\ExprHandler;
12+
use PHPStan\Analyser\ImpurePoint;
13+
use PHPStan\Analyser\MutatingScope;
14+
use PHPStan\Analyser\NodeScopeResolver;
15+
use PHPStan\DependencyInjection\AutowiredService;
16+
17+
/**
18+
* @implements ExprHandler<Exit_>
19+
*/
20+
#[AutowiredService]
21+
final class ExitHandler implements ExprHandler
22+
{
23+
24+
public function __construct(
25+
private NodeScopeResolver $nodeScopeResolver,
26+
)
27+
{
28+
}
29+
30+
public function supports(Expr $expr): bool
31+
{
32+
return $expr instanceof Exit_;
33+
}
34+
35+
public function processExpr(Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context): ExpressionResult
36+
{
37+
$kind = $expr->getAttribute('kind', Exit_::KIND_EXIT);
38+
$identifier = $kind === Exit_::KIND_DIE ? 'die' : 'exit';
39+
$impurePoints = [
40+
new ImpurePoint($scope, $expr, $identifier, $identifier, true),
41+
];
42+
43+
$hasYield = false;
44+
$throwPoints = [];
45+
if ($expr->expr !== null) {
46+
$result = $this->nodeScopeResolver->processExprNode($stmt, $expr->expr, $scope, $storage, $nodeCallback, $context->enterDeep());
47+
$hasYield = $result->hasYield();
48+
$throwPoints = $result->getThrowPoints();
49+
$impurePoints = array_merge($impurePoints, $result->getImpurePoints());
50+
$scope = $result->getScope();
51+
}
52+
53+
return new ExpressionResult(
54+
$scope,
55+
hasYield: $hasYield,
56+
isAlwaysTerminating: true,
57+
throwPoints: $throwPoints,
58+
impurePoints: $impurePoints,
59+
truthyScopeCallback: static fn (): MutatingScope => $scope->filterByTruthyValue($expr),
60+
falseyScopeCallback: static fn (): MutatingScope => $scope->filterByFalseyValue($expr),
61+
);
62+
}
63+
64+
}

‎src/Analyser/NodeScopeResolver.php‎

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3613,22 +3613,6 @@ function (MutatingScope $scope) use ($stmt, $expr, $nodeCallback, $context, $sto
36133613
[],
36143614
[],
36153615
);
3616-
} elseif ($expr instanceof Exit_) {
3617-
$hasYield = false;
3618-
$throwPoints = [];
3619-
$kind = $expr->getAttribute('kind', Exit_::KIND_EXIT);
3620-
$identifier = $kind === Exit_::KIND_DIE ? 'die' : 'exit';
3621-
$impurePoints = [
3622-
new ImpurePoint($scope, $expr, $identifier, $identifier, true),
3623-
];
3624-
$isAlwaysTerminating = true;
3625-
if ($expr->expr !== null) {
3626-
$result = $this->processExprNode($stmt, $expr->expr, $scope, $storage, $nodeCallback, $context->enterDeep());
3627-
$hasYield = $result->hasYield();
3628-
$throwPoints = $result->getThrowPoints();
3629-
$impurePoints = array_merge($impurePoints, $result->getImpurePoints());
3630-
$scope = $result->getScope();
3631-
}
36323616
} elseif ($expr instanceof Node\Scalar\InterpolatedString) {
36333617
$hasYield = false;
36343618
$throwPoints = [];

0 commit comments

Comments
 (0)