|
| 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\Analyser\Scope; |
| 14 | +use PHPStan\Analyser\SpecifiedTypes; |
| 15 | +use PHPStan\Analyser\TypeSpecifier; |
| 16 | +use PHPStan\Analyser\TypeSpecifierContext; |
| 17 | +use PHPStan\DependencyInjection\AutowiredService; |
| 18 | +use PHPStan\Node\NullsafeFirstClassCallableNode; |
| 19 | +use PHPStan\Type\MixedType; |
| 20 | +use PHPStan\Type\Type; |
| 21 | +use function array_merge; |
| 22 | + |
| 23 | +/** |
| 24 | + * @implements ExprHandler<NullsafeFirstClassCallableNode> |
| 25 | + */ |
| 26 | +#[AutowiredService] |
| 27 | +final class NullsafeFirstClassCallableNodeHandler implements ExprHandler |
| 28 | +{ |
| 29 | + |
| 30 | + public function supports(Expr $expr): bool |
| 31 | + { |
| 32 | + return $expr instanceof NullsafeFirstClassCallableNode; |
| 33 | + } |
| 34 | + |
| 35 | + public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context): ExpressionResult |
| 36 | + { |
| 37 | + $varResult = $nodeScopeResolver->processExprNode($stmt, $expr->getVar(), $scope, $storage, $nodeCallback, ExpressionContext::createDeep()); |
| 38 | + $scope = $varResult->getScope(); |
| 39 | + $hasYield = $varResult->hasYield(); |
| 40 | + $throwPoints = $varResult->getThrowPoints(); |
| 41 | + $impurePoints = $varResult->getImpurePoints(); |
| 42 | + $isAlwaysTerminating = false; |
| 43 | + if ($expr->getName() instanceof Expr) { |
| 44 | + $nameResult = $nodeScopeResolver->processExprNode($stmt, $expr->getName(), $scope, $storage, $nodeCallback, ExpressionContext::createDeep()); |
| 45 | + $scope = $nameResult->getScope(); |
| 46 | + $hasYield = $hasYield || $nameResult->hasYield(); |
| 47 | + $throwPoints = array_merge($throwPoints, $nameResult->getThrowPoints()); |
| 48 | + $impurePoints = array_merge($impurePoints, $nameResult->getImpurePoints()); |
| 49 | + $isAlwaysTerminating = $nameResult->isAlwaysTerminating(); |
| 50 | + } |
| 51 | + |
| 52 | + return new ExpressionResult( |
| 53 | + $scope, |
| 54 | + hasYield: $hasYield, |
| 55 | + isAlwaysTerminating: $isAlwaysTerminating, |
| 56 | + throwPoints: $throwPoints, |
| 57 | + impurePoints: $impurePoints, |
| 58 | + ); |
| 59 | + } |
| 60 | + |
| 61 | + public function resolveType(MutatingScope $scope, Expr $expr): Type |
| 62 | + { |
| 63 | + // in practice the type of the first-class callable is resolved |
| 64 | + // by NullsafeMethodCallHandler / FirstClassCallableMethodCallHandler |
| 65 | + return new MixedType(); |
| 66 | + } |
| 67 | + |
| 68 | + public function specifyTypes(TypeSpecifier $typeSpecifier, Scope $scope, Expr $expr, TypeSpecifierContext $context): SpecifiedTypes |
| 69 | + { |
| 70 | + return $typeSpecifier->specifyDefaultTypes($scope, $expr, $context); |
| 71 | + } |
| 72 | + |
| 73 | +} |
0 commit comments