Skip to content

Commit f3d7657

Browse files
committed
VariableHandler
1 parent 8f9295c commit f3d7657

2 files changed

Lines changed: 65 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\Variable;
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\Analyser\Scope;
16+
use PHPStan\DependencyInjection\AutowiredService;
17+
use function in_array;
18+
19+
/**
20+
* @implements ExprHandler<Variable>
21+
*/
22+
#[AutowiredService]
23+
final class VariableHandler implements ExprHandler
24+
{
25+
26+
public function __construct(
27+
private NodeScopeResolver $nodeScopeResolver,
28+
)
29+
{
30+
}
31+
32+
public function supports(Expr $expr): bool
33+
{
34+
return $expr instanceof Variable;
35+
}
36+
37+
public function processExpr(Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context): ExpressionResult
38+
{
39+
$hasYield = false;
40+
$throwPoints = [];
41+
$impurePoints = [];
42+
$isAlwaysTerminating = false;
43+
if ($expr->name instanceof Expr) {
44+
$result = $this->nodeScopeResolver->processExprNode($stmt, $expr->name, $scope, $storage, $nodeCallback, $context->enterDeep());
45+
$hasYield = $result->hasYield();
46+
$throwPoints = $result->getThrowPoints();
47+
$impurePoints = $result->getImpurePoints();
48+
$isAlwaysTerminating = $result->isAlwaysTerminating();
49+
$scope = $result->getScope();
50+
} elseif (in_array($expr->name, Scope::SUPERGLOBAL_VARIABLES, true)) {
51+
$impurePoints[] = new ImpurePoint($scope, $expr, 'superglobal', 'access to superglobal variable', true);
52+
}
53+
return new ExpressionResult(
54+
$scope,
55+
$hasYield,
56+
$isAlwaysTerminating,
57+
$throwPoints,
58+
$impurePoints,
59+
static fn (): MutatingScope => $scope->filterByTruthyValue($expr),
60+
static fn (): MutatingScope => $scope->filterByFalseyValue($expr),
61+
);
62+
}
63+
64+
}

‎src/Analyser/NodeScopeResolver.php‎

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2665,22 +2665,7 @@ public function processExprNode(
26652665
return $exprHandler->processExpr($stmt, $expr, $scope, $storage, $nodeCallback, $context);
26662666
}
26672667

2668-
if ($expr instanceof Variable) {
2669-
$hasYield = false;
2670-
$throwPoints = [];
2671-
$impurePoints = [];
2672-
$isAlwaysTerminating = false;
2673-
if ($expr->name instanceof Expr) {
2674-
$result = $this->processExprNode($stmt, $expr->name, $scope, $storage, $nodeCallback, $context->enterDeep());
2675-
$hasYield = $result->hasYield();
2676-
$throwPoints = $result->getThrowPoints();
2677-
$impurePoints = $result->getImpurePoints();
2678-
$isAlwaysTerminating = $result->isAlwaysTerminating();
2679-
$scope = $result->getScope();
2680-
} elseif (in_array($expr->name, Scope::SUPERGLOBAL_VARIABLES, true)) {
2681-
$impurePoints[] = new ImpurePoint($scope, $expr, 'superglobal', 'access to superglobal variable', true);
2682-
}
2683-
} elseif ($expr instanceof Assign || $expr instanceof AssignRef) {
2668+
if ($expr instanceof Assign || $expr instanceof AssignRef) {
26842669
$result = $this->processAssignVar(
26852670
$scope,
26862671
$storage,

0 commit comments

Comments
 (0)