|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace PHPStan\Type\Nette; |
| 4 | + |
| 5 | +use PhpParser\Node\Expr\MethodCall; |
| 6 | +use PHPStan\Analyser\Scope; |
| 7 | +use PHPStan\Reflection\MethodReflection; |
| 8 | +use PHPStan\Reflection\ParametersAcceptor; |
| 9 | +use PHPStan\Reflection\ParametersAcceptorSelector; |
| 10 | +use PHPStan\Type\DynamicMethodReturnTypeExtension; |
| 11 | +use PHPStan\Type\Type; |
| 12 | + |
| 13 | +final class CachingFallbacksDynamicReturnTypeExtension implements DynamicMethodReturnTypeExtension |
| 14 | +{ |
| 15 | + |
| 16 | + /** @var array<string, int> */ |
| 17 | + private $fallbackMethods = [ |
| 18 | + 'load' => 1, |
| 19 | + 'call' => 0, |
| 20 | + 'wrap' => 0, |
| 21 | + ]; |
| 22 | + |
| 23 | + public function getClass(): string |
| 24 | + { |
| 25 | + return 'Nette\Caching\Cache'; |
| 26 | + } |
| 27 | + |
| 28 | + public function isMethodSupported(MethodReflection $methodReflection): bool |
| 29 | + { |
| 30 | + $methodName = $methodReflection->getName(); |
| 31 | + |
| 32 | + return array_key_exists($methodName, $this->fallbackMethods); |
| 33 | + } |
| 34 | + |
| 35 | + public function getTypeFromMethodCall( |
| 36 | + MethodReflection $methodReflection, |
| 37 | + MethodCall $methodCall, |
| 38 | + Scope $scope |
| 39 | + ): Type |
| 40 | + { |
| 41 | + $fallbackParameterIndex = $this->fallbackMethods[$methodReflection->getName()]; |
| 42 | + |
| 43 | + if ($fallbackParameterIndex >= count($methodCall->args)) { |
| 44 | + return ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType(); |
| 45 | + } |
| 46 | + |
| 47 | + $fallbackParameterType = $scope->getType($methodCall->args[$fallbackParameterIndex]->value); |
| 48 | + if (!$fallbackParameterType->isCallable()->yes()) { |
| 49 | + return ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType(); |
| 50 | + } |
| 51 | + |
| 52 | + return ParametersAcceptorSelector::selectFromArgs($scope, $methodCall->args, $fallbackParameterType->getCallableParametersAcceptors($scope))->getReturnType(); |
| 53 | + } |
| 54 | + |
| 55 | +} |
0 commit comments