|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace PHPStan\Type\Symfony; |
| 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\Type\DynamicMethodReturnTypeExtension; |
| 10 | +use PHPStan\Type\GeneralizePrecision; |
| 11 | +use PHPStan\Type\Type; |
| 12 | + |
| 13 | +final class CacheInterfaceGetDynamicReturnTypeExtension implements DynamicMethodReturnTypeExtension |
| 14 | +{ |
| 15 | + |
| 16 | + public function getClass(): string |
| 17 | + { |
| 18 | + return 'Symfony\Contracts\Cache\CacheInterface'; |
| 19 | + } |
| 20 | + |
| 21 | + public function isMethodSupported(MethodReflection $methodReflection): bool |
| 22 | + { |
| 23 | + return $methodReflection->getName() === 'get'; |
| 24 | + } |
| 25 | + |
| 26 | + public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): ?Type |
| 27 | + { |
| 28 | + if (!isset($methodCall->getArgs()[1])) { |
| 29 | + return null; |
| 30 | + } |
| 31 | + |
| 32 | + $callbackReturnType = $scope->getType($methodCall->getArgs()[1]->value); |
| 33 | + if ($callbackReturnType->isCallable()->yes()) { |
| 34 | + $parametersAcceptor = ParametersAcceptorSelector::selectFromArgs( |
| 35 | + $scope, |
| 36 | + $methodCall->getArgs(), |
| 37 | + $callbackReturnType->getCallableParametersAcceptors($scope) |
| 38 | + ); |
| 39 | + $returnType = $parametersAcceptor->getReturnType(); |
| 40 | + |
| 41 | + // generalize template parameters |
| 42 | + return $returnType->generalize(GeneralizePrecision::templateArgument()); |
| 43 | + } |
| 44 | + |
| 45 | + return null; |
| 46 | + } |
| 47 | + |
| 48 | +} |
0 commit comments