rector/rules/renaming/src/Rector/MethodCall/RenameMethodRector.php

190 lines
5.7 KiB
PHP
Raw Normal View History

2019-10-13 05:59:52 +00:00
<?php
declare(strict_types=1);
2017-10-06 11:57:21 +00:00
2019-09-23 15:09:26 +00:00
namespace Rector\Renaming\Rector\MethodCall;
2017-10-06 11:57:21 +00:00
2018-12-11 17:22:54 +00:00
use PhpParser\BuilderHelpers;
2017-10-06 11:57:21 +00:00
use PhpParser\Node;
2018-12-11 17:22:54 +00:00
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;
2020-07-29 13:55:33 +00:00
use Rector\Core\Contract\Rector\ConfigurableRectorInterface;
use Rector\Core\NodeManipulator\ClassManipulator;
use Rector\Core\Rector\AbstractRector;
use Rector\NodeTypeResolver\Node\AttributeKey;
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 Webmozart\Assert\Assert;
2017-10-06 11:57:21 +00:00
2019-09-03 09:11:45 +00:00
/**
* @see \Rector\Renaming\Tests\Rector\MethodCall\RenameMethodRector\RenameMethodRectorTest
2019-09-03 09:11:45 +00:00
*/
2020-07-29 13:55:33 +00:00
final class RenameMethodRector extends AbstractRector implements ConfigurableRectorInterface
2017-10-06 11:57:21 +00:00
{
2020-07-29 13:55:33 +00:00
/**
* @var string
*/
public const METHOD_CALL_RENAMES = 'method_call_renames';
2020-07-29 13:55:33 +00:00
2017-10-06 11:57:21 +00:00
/**
* @var MethodCallRenameInterface[]
2017-10-06 11:57:21 +00:00
*/
private $methodCallRenames = [];
2017-10-06 11:57:21 +00:00
/**
* @var ClassManipulator
*/
private $classManipulator;
public function __construct(ClassManipulator $classManipulator)
{
$this->classManipulator = $classManipulator;
}
public function getRuleDefinition(): RuleDefinition
2018-04-08 11:51:26 +00:00
{
return new RuleDefinition('Turns method names to new ones.', [
2018-08-01 19:52:44 +00:00
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
2018-04-08 11:51:26 +00:00
,
<<<'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
2018-08-01 19:52:44 +00:00
,
[
self::METHOD_CALL_RENAMES => [
new MethodCallRename('SomeExampleClass', 'oldMethod', 'newMethod'),
2018-08-01 19:52:44 +00:00
],
]
),
2018-04-08 11:51:26 +00:00
]);
}
2018-08-14 22:12:41 +00:00
/**
* @return string[]
*/
public function getNodeTypes(): array
2017-10-06 11:57:21 +00:00
{
2018-12-11 17:22:54 +00:00
return [MethodCall::class, StaticCall::class, ClassMethod::class];
2017-10-06 11:57:21 +00:00
}
/**
2018-12-11 17:22:54 +00:00
* @param MethodCall|StaticCall|ClassMethod $node
2017-10-06 11:57:21 +00:00
*/
public function refactor(Node $node): ?Node
{
foreach ($this->methodCallRenames as $methodCallRename) {
$implementsInterface = $this->classManipulator->hasParentMethodOrInterface(
$methodCallRename->getOldClass(),
$methodCallRename->getOldMethod()
);
if ($implementsInterface) {
continue;
}
if (! $this->nodeTypeResolver->isMethodStaticCallOrClassMethodObjectType(
$node,
$methodCallRename->getOldClass()
)) {
2018-10-22 17:17:17 +00:00
continue;
}
2018-08-14 22:12:41 +00:00
if (! $this->isName($node->name, $methodCallRename->getOldMethod())) {
continue;
}
2018-08-14 22:12:41 +00:00
if ($this->skipClassMethod($node, $methodCallRename)) {
continue;
}
$node->name = new Identifier($methodCallRename->getNewMethod());
if ($methodCallRename instanceof MethodCallRenameWithArrayKey && ! $node instanceof ClassMethod) {
return new ArrayDimFetch($node, BuilderHelpers::normalizeValue($methodCallRename->getArrayKey()));
2018-10-22 17:17:17 +00:00
}
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
}
2020-07-29 13:55:33 +00:00
public function configure(array $configuration): void
{
$methodCallRenames = $configuration[self::METHOD_CALL_RENAMES] ?? [];
Assert::allIsInstanceOf($methodCallRenames, MethodCallRenameInterface::class);
$this->methodCallRenames = $methodCallRenames;
2020-07-29 13:55:33 +00:00
}
/**
* @param MethodCall|StaticCall|ClassMethod $node
*/
private function skipClassMethod(Node $node, MethodCallRenameInterface $methodCallRename): bool
{
if (! $node instanceof ClassMethod) {
return false;
}
if ($this->shouldSkipForAlreadyExistingClassMethod($node, $methodCallRename)) {
return true;
}
return $this->shouldSkipForExactClassMethodForClassMethodOrTargetInvokePrivate(
$node,
$methodCallRename->getOldClass(),
$methodCallRename->getNewMethod()
);
}
private function shouldSkipForAlreadyExistingClassMethod(
ClassMethod $classMethod,
MethodCallRenameInterface $methodCallRename
): bool {
$classLike = $classMethod->getAttribute(AttributeKey::CLASS_NODE);
if (! $classLike instanceof ClassLike) {
return false;
}
return (bool) $classLike->getMethod($methodCallRename->getNewMethod());
}
private function shouldSkipForExactClassMethodForClassMethodOrTargetInvokePrivate(
ClassMethod $classMethod,
string $type,
string $newMethodName
): bool {
$className = $classMethod->getAttribute(AttributeKey::CLASS_NAME);
$methodCalls = $this->nodeRepository->findMethodCallsOnClass($className);
$name = $this->getName($classMethod->name);
if (isset($methodCalls[$name])) {
return false;
}
$isExactClassMethodForClasssMethod = $classMethod->getAttribute(AttributeKey::CLASS_NAME) === $type;
if ($isExactClassMethodForClasssMethod) {
return true;
}
if ($classMethod->isPublic()) {
return false;
}
$newClassMethod = clone $classMethod;
$newClassMethod->name = new Identifier($newMethodName);
return $newClassMethod->isMagic();
}
2017-10-06 11:57:21 +00:00
}