mirror of
https://github.com/rectorphp/rector.git
synced 2024-06-13 14:42:23 +00:00
fc10fce13d
* 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>
68 lines
2.0 KiB
PHP
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;
|
|
}
|
|
}
|