rector/rules/Renaming/Rector/MethodCall/RenameMethodRector.php

163 lines
6.4 KiB
PHP
Raw Normal View History

2019-10-13 05:59:52 +00:00
<?php
declare (strict_types=1);
namespace Rector\Renaming\Rector\MethodCall;
2017-10-06 11:57:21 +00:00
use PhpParser\BuilderHelpers;
use PhpParser\Node;
use PhpParser\Node\Expr\ArrayDimFetch;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Identifier;
use PhpParser\Node\Stmt\ClassLike;
use PhpParser\Node\Stmt\ClassMethod;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\ReflectionProvider;
use Rector\Core\Contract\Rector\ConfigurableRectorInterface;
use Rector\Core\NodeManipulator\ClassManipulator;
use Rector\Core\Rector\AbstractScopeAwareRector;
use Rector\Core\Reflection\ReflectionResolver;
use Rector\Renaming\Contract\MethodCallRenameInterface;
use Rector\Renaming\ValueObject\MethodCallRename;
use Rector\Renaming\ValueObject\MethodCallRenameWithArrayKey;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
use RectorPrefix202306\Webmozart\Assert\Assert;
2019-09-03 09:11:45 +00:00
/**
* @see \Rector\Tests\Renaming\Rector\MethodCall\RenameMethodRector\RenameMethodRectorTest
2019-09-03 09:11:45 +00:00
*/
final class RenameMethodRector extends AbstractScopeAwareRector implements ConfigurableRectorInterface
2017-10-06 11:57:21 +00:00
{
/**
* @var MethodCallRenameInterface[]
2017-10-06 11:57:21 +00:00
*/
private $methodCallRenames = [];
/**
* @readonly
* @var \Rector\Core\NodeManipulator\ClassManipulator
*/
private $classManipulator;
/**
* @readonly
* @var \Rector\Core\Reflection\ReflectionResolver
*/
private $reflectionResolver;
/**
* @readonly
* @var \PHPStan\Reflection\ReflectionProvider
*/
private $reflectionProvider;
public function __construct(ClassManipulator $classManipulator, ReflectionResolver $reflectionResolver, ReflectionProvider $reflectionProvider)
{
$this->classManipulator = $classManipulator;
$this->reflectionResolver = $reflectionResolver;
$this->reflectionProvider = $reflectionProvider;
}
public function getRuleDefinition() : RuleDefinition
2018-04-08 11:51:26 +00:00
{
return new RuleDefinition('Turns method names to new ones.', [new ConfiguredCodeSample(<<<'CODE_SAMPLE'
2018-10-22 17:39:10 +00:00
$someObject = new SomeExampleClass;
2018-04-09 12:48:15 +00:00
$someObject->oldMethod();
CODE_SAMPLE
, <<<'CODE_SAMPLE'
2018-10-22 17:39:10 +00:00
$someObject = new SomeExampleClass;
2018-04-09 12:48:15 +00:00
$someObject->newMethod();
CODE_SAMPLE
, [new MethodCallRename('SomeExampleClass', 'oldMethod', 'newMethod')])]);
2018-04-08 11:51:26 +00:00
}
2018-08-14 22:12:41 +00:00
/**
* @return array<class-string<Node>>
2018-08-14 22:12:41 +00:00
*/
public function getNodeTypes() : array
2017-10-06 11:57:21 +00:00
{
return [MethodCall::class, StaticCall::class, ClassMethod::class];
2017-10-06 11:57:21 +00:00
}
/**
* @param MethodCall|StaticCall|ClassMethod $node
2017-10-06 11:57:21 +00:00
*/
public function refactorWithScope(Node $node, Scope $scope) : ?Node
2017-10-06 11:57:21 +00:00
{
$classReflection = $scope->getClassReflection();
foreach ($this->methodCallRenames as $methodCallRename) {
if (!$this->isName($node->name, $methodCallRename->getOldMethod())) {
continue;
}
if ($this->shouldKeepForParentInterface($methodCallRename, $node, $classReflection)) {
2018-10-22 17:17:17 +00:00
continue;
}
if (!$this->nodeTypeResolver->isMethodStaticCallOrClassMethodObjectType($node, $methodCallRename->getObjectType())) {
continue;
}
if ($this->shouldSkipClassMethod($node, $methodCallRename)) {
continue;
}
$node->name = new Identifier($methodCallRename->getNewMethod());
if ($methodCallRename instanceof MethodCallRenameWithArrayKey && !$node instanceof ClassMethod) {
return new ArrayDimFetch($node, BuilderHelpers::normalizeValue($methodCallRename->getArrayKey()));
}
return $node;
2018-08-14 22:12:41 +00:00
}
2018-12-11 17:22:54 +00:00
return null;
2018-08-14 22:12:41 +00:00
}
/**
* @param mixed[] $configuration
*/
public function configure(array $configuration) : void
2020-07-29 13:55:33 +00:00
{
Assert::allIsAOf($configuration, MethodCallRenameInterface::class);
$this->methodCallRenames = $configuration;
2020-07-29 13:55:33 +00:00
}
/**
* @param \PhpParser\Node\Expr\MethodCall|\PhpParser\Node\Expr\StaticCall|\PhpParser\Node\Stmt\ClassMethod $node
*/
private function shouldSkipClassMethod($node, MethodCallRenameInterface $methodCallRename) : bool
{
if (!$node instanceof ClassMethod) {
$classReflection = $this->reflectionResolver->resolveClassReflectionSourceObject($node);
if (!$classReflection instanceof ClassReflection) {
return \false;
}
$targetClass = $methodCallRename->getClass();
if (!$this->reflectionProvider->hasClass($targetClass)) {
return \false;
}
$targetClassReflection = $this->reflectionProvider->getClass($targetClass);
if ($classReflection->getName() === $targetClassReflection->getName()) {
return \false;
}
// different with configured ClassLike source? it is a child, which may has old and new exists
if (!$classReflection->hasMethod($methodCallRename->getOldMethod())) {
return \false;
}
return $classReflection->hasMethod($methodCallRename->getNewMethod());
}
return $this->shouldSkipForAlreadyExistingClassMethod($node, $methodCallRename);
}
private function shouldSkipForAlreadyExistingClassMethod(ClassMethod $classMethod, MethodCallRenameInterface $methodCallRename) : bool
{
$classLike = $this->betterNodeFinder->findParentType($classMethod, ClassLike::class);
if (!$classLike instanceof ClassLike) {
return \false;
}
return (bool) $classLike->getMethod($methodCallRename->getNewMethod());
}
/**
* @param \PhpParser\Node\Stmt\ClassMethod|\PhpParser\Node\Expr\StaticCall|\PhpParser\Node\Expr\MethodCall $node
*/
private function shouldKeepForParentInterface(MethodCallRenameInterface $methodCallRename, $node, ?ClassReflection $classReflection) : bool
{
if (!$node instanceof ClassMethod) {
return \false;
}
if (!$classReflection instanceof ClassReflection) {
return \false;
}
// interface can change current method, as parent contract is still valid
if (!$classReflection->isInterface()) {
return \false;
}
return $this->classManipulator->hasParentMethodOrInterface($methodCallRename->getObjectType(), $methodCallRename->getOldMethod(), $methodCallRename->getNewMethod());
}
2017-10-06 11:57:21 +00:00
}