rector/packages/NodeTypeResolver/MethodParameterTypeResolver.php
Tomas Votruba a5ec68bd9c Updated Rector to commit ec9c15adc729988276f3fe7b726609f913266f23
ec9c15adc7 Apply ParametersAcceptorSelectorVariantsWrapper::select() take 2 (#2718)
2022-07-28 14:06:21 +00:00

75 lines
2.7 KiB
PHP

<?php
declare (strict_types=1);
namespace Rector\NodeTypeResolver;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Stmt\ClassMethod;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Reflection\Native\NativeMethodReflection;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Type\Type;
use Rector\Core\Reflection\ReflectionResolver;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\NodeTypeResolver\PHPStan\ParametersAcceptorSelectorVariantsWrapper;
final class MethodParameterTypeResolver
{
/**
* @readonly
* @var \Rector\Core\Reflection\ReflectionResolver
*/
private $reflectionResolver;
public function __construct(ReflectionResolver $reflectionResolver)
{
$this->reflectionResolver = $reflectionResolver;
}
/**
* @return Type[]
*/
public function provideParameterTypesByStaticCall(StaticCall $staticCall) : array
{
$methodReflection = $this->reflectionResolver->resolveMethodReflectionFromStaticCall($staticCall);
if (!$methodReflection instanceof MethodReflection) {
return [];
}
return $this->provideParameterTypesFromMethodReflection($methodReflection, $staticCall);
}
/**
* @return Type[]
*/
public function provideParameterTypesByClassMethod(ClassMethod $classMethod) : array
{
$methodReflection = $this->reflectionResolver->resolveMethodReflectionFromClassMethod($classMethod);
if (!$methodReflection instanceof MethodReflection) {
return [];
}
return $this->provideParameterTypesFromMethodReflection($methodReflection, $classMethod);
}
/**
* @return Type[]
* @param \PhpParser\Node\Stmt\ClassMethod|\PhpParser\Node\Expr\StaticCall $node
*/
private function provideParameterTypesFromMethodReflection(MethodReflection $methodReflection, $node) : array
{
if ($methodReflection instanceof NativeMethodReflection) {
// method "getParameters()" does not exist there
return [];
}
$parameterTypes = [];
if ($node instanceof ClassMethod) {
$parametersAcceptor = ParametersAcceptorSelector::selectSingle($methodReflection->getVariants());
} else {
$scope = $node->getAttribute(AttributeKey::SCOPE);
if (!$scope instanceof Scope) {
return [];
}
$parametersAcceptor = ParametersAcceptorSelectorVariantsWrapper::select($methodReflection, $node, $scope);
}
foreach ($parametersAcceptor->getParameters() as $parameterReflection) {
$parameterTypes[] = $parameterReflection->getType();
}
return $parameterTypes;
}
}