|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace PHPStan\Analyser\ExprHandler\Virtual; |
| 4 | + |
| 5 | +use PhpParser\Node\Expr; |
| 6 | +use PhpParser\Node\Stmt; |
| 7 | +use PHPStan\Analyser\ExpressionContext; |
| 8 | +use PHPStan\Analyser\ExpressionResult; |
| 9 | +use PHPStan\Analyser\ExpressionResultStorage; |
| 10 | +use PHPStan\Analyser\ExprHandler; |
| 11 | +use PHPStan\Analyser\MutatingScope; |
| 12 | +use PHPStan\Analyser\NodeScopeResolver; |
| 13 | +use PHPStan\DependencyInjection\AutowiredService; |
| 14 | +use PHPStan\Node\Expr\AlwaysRememberedExpr; |
| 15 | +use PHPStan\Type\Type; |
| 16 | + |
| 17 | +/** |
| 18 | + * @implements ExprHandler<AlwaysRememberedExpr> |
| 19 | + */ |
| 20 | +#[AutowiredService] |
| 21 | +final class AlwaysRememberedExprHandler implements ExprHandler |
| 22 | +{ |
| 23 | + |
| 24 | + public function supports(Expr $expr): bool |
| 25 | + { |
| 26 | + return $expr instanceof AlwaysRememberedExpr; |
| 27 | + } |
| 28 | + |
| 29 | + public function processExpr( |
| 30 | + NodeScopeResolver $nodeScopeResolver, |
| 31 | + Stmt $stmt, |
| 32 | + Expr $expr, |
| 33 | + MutatingScope $scope, |
| 34 | + ExpressionResultStorage $storage, |
| 35 | + callable $nodeCallback, |
| 36 | + ExpressionContext $context, |
| 37 | + ): ExpressionResult |
| 38 | + { |
| 39 | + $innerExpr = $expr->getExpr(); |
| 40 | + $result = $nodeScopeResolver->processExprNode($stmt, $innerExpr, $scope, $storage, $nodeCallback, $context); |
| 41 | + $scope = $result->getScope(); |
| 42 | + |
| 43 | + return new ExpressionResult( |
| 44 | + $scope, |
| 45 | + hasYield: $result->hasYield(), |
| 46 | + isAlwaysTerminating: $result->isAlwaysTerminating(), |
| 47 | + throwPoints: $result->getThrowPoints(), |
| 48 | + impurePoints: $result->getImpurePoints(), |
| 49 | + truthyScopeCallback: static fn (): MutatingScope => $scope->filterByTruthyValue($innerExpr), |
| 50 | + falseyScopeCallback: static fn (): MutatingScope => $scope->filterByFalseyValue($innerExpr), |
| 51 | + ); |
| 52 | + } |
| 53 | + |
| 54 | + public function resolveType(MutatingScope $scope, Expr $expr): Type |
| 55 | + { |
| 56 | + return $scope->nativeTypesPromoted ? $expr->getNativeExprType() : $expr->getExprType(); |
| 57 | + } |
| 58 | + |
| 59 | +} |
0 commit comments