|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace PHPStan\Type\Doctrine\QueryBuilder\Expr; |
| 4 | + |
| 5 | +use PhpParser\Node\Expr\MethodCall; |
| 6 | +use PHPStan\Analyser\Scope; |
| 7 | +use PHPStan\Reflection\MethodReflection; |
| 8 | +use PHPStan\Reflection\ParametersAcceptorSelector; |
| 9 | +use PHPStan\Rules\Doctrine\ORM\DynamicQueryBuilderArgumentException; |
| 10 | +use PHPStan\Type\Doctrine\ArgumentsProcessor; |
| 11 | +use PHPStan\Type\DynamicMethodReturnTypeExtension; |
| 12 | +use PHPStan\Type\Type; |
| 13 | +use function get_class; |
| 14 | +use function is_object; |
| 15 | +use function method_exists; |
| 16 | + |
| 17 | +class BaseExpressionDynamicReturnTypeExtension implements DynamicMethodReturnTypeExtension |
| 18 | +{ |
| 19 | + |
| 20 | + /** @var ArgumentsProcessor */ |
| 21 | + private $argumentsProcessor; |
| 22 | + |
| 23 | + public function __construct( |
| 24 | + ArgumentsProcessor $argumentsProcessor |
| 25 | + ) |
| 26 | + { |
| 27 | + $this->argumentsProcessor = $argumentsProcessor; |
| 28 | + } |
| 29 | + |
| 30 | + public function getClass(): string |
| 31 | + { |
| 32 | + return 'Doctrine\ORM\Query\Expr\Base'; |
| 33 | + } |
| 34 | + |
| 35 | + public function isMethodSupported(MethodReflection $methodReflection): bool |
| 36 | + { |
| 37 | + return true; |
| 38 | + } |
| 39 | + |
| 40 | + public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): Type |
| 41 | + { |
| 42 | + $defaultReturnType = ParametersAcceptorSelector::selectFromArgs($scope, $methodCall->getArgs(), $methodReflection->getVariants())->getReturnType(); |
| 43 | + |
| 44 | + try { |
| 45 | + $args = $this->argumentsProcessor->processArgs($scope, $methodReflection->getName(), $methodCall->getArgs()); |
| 46 | + } catch (DynamicQueryBuilderArgumentException $e) { |
| 47 | + return $defaultReturnType; |
| 48 | + } |
| 49 | + |
| 50 | + $calledOnType = $scope->getType($methodCall->var); |
| 51 | + if (!$calledOnType instanceof ExprType) { |
| 52 | + return $defaultReturnType; |
| 53 | + } |
| 54 | + |
| 55 | + $expr = $calledOnType->getExprObject(); |
| 56 | + |
| 57 | + if (!method_exists($expr, $methodReflection->getName())) { |
| 58 | + return $defaultReturnType; |
| 59 | + } |
| 60 | + |
| 61 | + $exprValue = $expr->{$methodReflection->getName()}(...$args); |
| 62 | + if (is_object($exprValue)) { |
| 63 | + return new ExprType(get_class($exprValue), $exprValue); |
| 64 | + } |
| 65 | + |
| 66 | + return $scope->getTypeFromValue($exprValue); |
| 67 | + } |
| 68 | + |
| 69 | +} |
0 commit comments