|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace PHPStan\Analyser\ExprHandler; |
| 4 | + |
| 5 | +use Closure; |
| 6 | +use PhpParser\Node\Expr; |
| 7 | +use PhpParser\Node\Expr\MethodCall; |
| 8 | +use PhpParser\Node\Identifier; |
| 9 | +use PhpParser\Node\Stmt; |
| 10 | +use PHPStan\Analyser\ExpressionContext; |
| 11 | +use PHPStan\Analyser\ExpressionResult; |
| 12 | +use PHPStan\Analyser\ExpressionResultStorage; |
| 13 | +use PHPStan\Analyser\ExprHandler; |
| 14 | +use PHPStan\Analyser\MutatingScope; |
| 15 | +use PHPStan\Analyser\NodeScopeResolver; |
| 16 | +use PHPStan\DependencyInjection\AutowiredService; |
| 17 | +use PHPStan\Reflection\InitializerExprTypeResolver; |
| 18 | +use PHPStan\ShouldNotHappenException; |
| 19 | +use PHPStan\Type\ObjectType; |
| 20 | +use PHPStan\Type\Type; |
| 21 | + |
| 22 | +/** |
| 23 | + * @implements ExprHandler<MethodCall> |
| 24 | + */ |
| 25 | +#[AutowiredService] |
| 26 | +final class FirstClassCallableMethodCallHandler implements ExprHandler |
| 27 | +{ |
| 28 | + |
| 29 | + public function __construct( |
| 30 | + private InitializerExprTypeResolver $initializerExprTypeResolver, |
| 31 | + ) |
| 32 | + { |
| 33 | + } |
| 34 | + |
| 35 | + public function supports(Expr $expr): bool |
| 36 | + { |
| 37 | + return $expr instanceof MethodCall && $expr->isFirstClassCallable(); |
| 38 | + } |
| 39 | + |
| 40 | + public function processExpr( |
| 41 | + NodeScopeResolver $nodeScopeResolver, |
| 42 | + Stmt $stmt, |
| 43 | + Expr $expr, |
| 44 | + MutatingScope $scope, |
| 45 | + ExpressionResultStorage $storage, |
| 46 | + callable $nodeCallback, |
| 47 | + ExpressionContext $context, |
| 48 | + ): ExpressionResult |
| 49 | + { |
| 50 | + // handled in NodeScopeResolver before ExprHandlers are called |
| 51 | + throw new ShouldNotHappenException(); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * @param MethodCall $expr |
| 56 | + */ |
| 57 | + public function resolveType(MutatingScope $scope, Expr $expr): Type |
| 58 | + { |
| 59 | + if (!$expr->name instanceof Identifier) { |
| 60 | + return new ObjectType(Closure::class); |
| 61 | + } |
| 62 | + |
| 63 | + $varType = $scope->getType($expr->var); |
| 64 | + $method = $scope->getMethodReflection($varType, $expr->name->toString()); |
| 65 | + if ($method === null) { |
| 66 | + return new ObjectType(Closure::class); |
| 67 | + } |
| 68 | + |
| 69 | + return $this->initializerExprTypeResolver->createFirstClassCallable( |
| 70 | + $method, |
| 71 | + $method->getVariants(), |
| 72 | + $scope->nativeTypesPromoted, |
| 73 | + ); |
| 74 | + } |
| 75 | + |
| 76 | +} |
0 commit comments