mirror of
https://github.com/rectorphp/rector.git
synced 2024-06-11 13:42:22 +00:00
3f89b13d0e
16f3c961ea
[CodeQuality] Add typed property support to IssetOnPropertyObjectToPropertyExistsRector (#3876)
65 lines
2.3 KiB
PHP
65 lines
2.3 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\Type\Type;
|
|
use Rector\Core\Reflection\ReflectionResolver;
|
|
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, Scope $scope) : array
|
|
{
|
|
$methodReflection = $this->reflectionResolver->resolveMethodReflectionFromStaticCall($staticCall);
|
|
if (!$methodReflection instanceof MethodReflection) {
|
|
return [];
|
|
}
|
|
return $this->provideParameterTypesFromMethodReflection($methodReflection, $staticCall, $scope);
|
|
}
|
|
/**
|
|
* @return Type[]
|
|
*/
|
|
public function provideParameterTypesByClassMethod(ClassMethod $classMethod, Scope $scope) : array
|
|
{
|
|
$methodReflection = $this->reflectionResolver->resolveMethodReflectionFromClassMethod($classMethod);
|
|
if (!$methodReflection instanceof MethodReflection) {
|
|
return [];
|
|
}
|
|
return $this->provideParameterTypesFromMethodReflection($methodReflection, $classMethod, $scope);
|
|
}
|
|
/**
|
|
* @return Type[]
|
|
* @param \PhpParser\Node\Stmt\ClassMethod|\PhpParser\Node\Expr\StaticCall $node
|
|
*/
|
|
private function provideParameterTypesFromMethodReflection(MethodReflection $methodReflection, $node, Scope $scope) : array
|
|
{
|
|
if ($methodReflection instanceof NativeMethodReflection) {
|
|
// method "getParameters()" does not exist there
|
|
return [];
|
|
}
|
|
$parameterTypes = [];
|
|
$parametersAcceptor = ParametersAcceptorSelectorVariantsWrapper::select($methodReflection, $node, $scope);
|
|
foreach ($parametersAcceptor->getParameters() as $parameterReflection) {
|
|
$parameterTypes[] = $parameterReflection->getType();
|
|
}
|
|
return $parameterTypes;
|
|
}
|
|
}
|