|
| 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 | +} |
0 commit comments