|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace PHPStan\Analyser\Generator\ExprHandler; |
| 4 | + |
| 5 | +use Generator; |
| 6 | +use PhpParser\Node\Expr; |
| 7 | +use PhpParser\Node\Expr\FuncCall; |
| 8 | +use PhpParser\Node\Name; |
| 9 | +use PHPStan\Analyser\Generator\ExprAnalysisRequest; |
| 10 | +use PHPStan\Analyser\Generator\ExprAnalysisResult; |
| 11 | +use PHPStan\Analyser\Generator\ExprHandler; |
| 12 | +use PHPStan\Analyser\Generator\GeneratorScope; |
| 13 | +use PHPStan\DependencyInjection\AutowiredService; |
| 14 | +use PHPStan\Reflection\ReflectionProvider; |
| 15 | +use PHPStan\ShouldNotHappenException; |
| 16 | + |
| 17 | +/** |
| 18 | + * @implements ExprHandler<FuncCall> |
| 19 | + */ |
| 20 | +#[AutowiredService] |
| 21 | +final class FuncCallHandler implements ExprHandler |
| 22 | +{ |
| 23 | + |
| 24 | + public function __construct(private ReflectionProvider $reflectionProvider) |
| 25 | + { |
| 26 | + } |
| 27 | + |
| 28 | + public function supports(Expr $expr): bool |
| 29 | + { |
| 30 | + return $expr instanceof FuncCall; |
| 31 | + } |
| 32 | + |
| 33 | + public function analyseExpr(Expr $expr, GeneratorScope $scope): Generator |
| 34 | + { |
| 35 | + if ($expr->name instanceof Expr) { |
| 36 | + $nameResult = yield new ExprAnalysisRequest($expr->name, $scope); |
| 37 | + $scope = $nameResult->scope; |
| 38 | + } |
| 39 | + |
| 40 | + $argTypes = []; |
| 41 | + |
| 42 | + foreach ($expr->getArgs() as $arg) { |
| 43 | + $argResult = yield new ExprAnalysisRequest($arg->value, $scope); |
| 44 | + $argTypes[] = $argResult->type; |
| 45 | + $scope = $argResult->scope; |
| 46 | + } |
| 47 | + |
| 48 | + if ($expr->name instanceof Name) { |
| 49 | + if ($this->reflectionProvider->hasFunction($expr->name, $scope)) { |
| 50 | + $function = $this->reflectionProvider->getFunction($expr->name, $scope); |
| 51 | + return new ExprAnalysisResult($function->getOnlyVariant()->getReturnType(), $scope); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + throw new ShouldNotHappenException('Not implemented'); |
| 56 | + } |
| 57 | + |
| 58 | +} |
0 commit comments