rector/rules/Defluent/Reflection/MethodCallToClassMethodParser.php
Tomas Votruba cdc3b7adef Updated Rector to commit f451b0b8e1e6761ec7f50809745d44d01caba66d
f451b0b8e1 [PHP 8.0] Bump to promoted properties (#4)
2021-05-10 23:39:21 +00:00

53 lines
2.1 KiB
PHP

<?php
declare (strict_types=1);
namespace Rector\Defluent\Reflection;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Stmt\ClassMethod;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Type\TypeWithClassName;
use Rector\Core\Reflection\FunctionLikeReflectionParser;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\NodeTypeResolver\NodeTypeResolver;
final class MethodCallToClassMethodParser
{
/**
* @var \Rector\NodeTypeResolver\NodeTypeResolver
*/
private $nodeTypeResolver;
/**
* @var \Rector\NodeNameResolver\NodeNameResolver
*/
private $nodeNameResolver;
/**
* @var \PHPStan\Reflection\ReflectionProvider
*/
private $reflectionProvider;
/**
* @var \Rector\Core\Reflection\FunctionLikeReflectionParser
*/
private $functionLikeReflectionParser;
public function __construct(\Rector\NodeTypeResolver\NodeTypeResolver $nodeTypeResolver, \Rector\NodeNameResolver\NodeNameResolver $nodeNameResolver, \PHPStan\Reflection\ReflectionProvider $reflectionProvider, \Rector\Core\Reflection\FunctionLikeReflectionParser $functionLikeReflectionParser)
{
$this->nodeTypeResolver = $nodeTypeResolver;
$this->nodeNameResolver = $nodeNameResolver;
$this->reflectionProvider = $reflectionProvider;
$this->functionLikeReflectionParser = $functionLikeReflectionParser;
}
public function parseMethodCall(\PhpParser\Node\Expr\MethodCall $methodCall) : ?\PhpParser\Node\Stmt\ClassMethod
{
$callerStaticType = $this->nodeTypeResolver->getStaticType($methodCall->var);
if (!$callerStaticType instanceof \PHPStan\Type\TypeWithClassName) {
return null;
}
$callerClassReflection = $this->reflectionProvider->getClass($callerStaticType->getClassName());
$methodName = $this->nodeNameResolver->getName($methodCall->name);
if ($methodName === null) {
return null;
}
$methodReflection = $callerClassReflection->getNativeMethod($methodName);
return $this->functionLikeReflectionParser->parseMethodReflection($methodReflection);
}
}