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