rector/packages/NodeTypeResolver/MethodParameterTypeResolver.php
Abdul Malik Ikhsan fc10fce13d
[Rectify] [Php81] Enable Rectify on Readonly Property only (#1384)
* re-enable rectify and ecs

* [Rectify] [Php81] Enable Rectify on Readonly Property only

* comment

* [ci-review] Rector Rectify

* [ci-review] Rector Rectify

* [ci-review] Rector Rectify

* [ci-review] Rector Rectify

* [ci-review] Rector Rectify

Co-authored-by: GitHub Action <action@github.com>
2021-12-04 15:32:52 +03:00

68 lines
2.0 KiB
PHP

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