2019-10-13 05:59:52 +00:00
|
|
|
<?php
|
|
|
|
|
2021-05-09 20:15:43 +00:00
|
|
|
declare (strict_types=1);
|
2022-06-06 17:12:56 +00:00
|
|
|
namespace Rector\Renaming\Rector\MethodCall;
|
2017-10-06 11:57:21 +00:00
|
|
|
|
2022-11-19 11:50:17 +00:00
|
|
|
use PhpParser\BuilderHelpers;
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node;
|
2022-11-19 11:50:17 +00:00
|
|
|
use PhpParser\Node\Expr\ArrayDimFetch;
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node\Expr\MethodCall;
|
2024-01-07 16:56:38 +00:00
|
|
|
use PhpParser\Node\Expr\NullsafeMethodCall;
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node\Expr\StaticCall;
|
|
|
|
use PhpParser\Node\Identifier;
|
2023-06-10 09:11:14 +00:00
|
|
|
use PhpParser\Node\Stmt\Class_;
|
2023-09-09 22:49:40 +00:00
|
|
|
use PhpParser\Node\Stmt\ClassMethod;
|
2023-06-10 09:11:14 +00:00
|
|
|
use PhpParser\Node\Stmt\Interface_;
|
2022-06-06 17:12:56 +00:00
|
|
|
use PHPStan\Analyser\Scope;
|
|
|
|
use PHPStan\Reflection\ClassReflection;
|
|
|
|
use PHPStan\Reflection\ReflectionProvider;
|
2024-01-02 02:40:38 +00:00
|
|
|
use Rector\Contract\Rector\ConfigurableRectorInterface;
|
|
|
|
use Rector\NodeManipulator\ClassManipulator;
|
|
|
|
use Rector\Rector\AbstractScopeAwareRector;
|
|
|
|
use Rector\Reflection\ReflectionResolver;
|
2022-11-19 11:50:17 +00:00
|
|
|
use Rector\Renaming\Contract\MethodCallRenameInterface;
|
2022-06-06 17:12:56 +00:00
|
|
|
use Rector\Renaming\ValueObject\MethodCallRename;
|
2022-11-19 11:50:17 +00:00
|
|
|
use Rector\Renaming\ValueObject\MethodCallRenameWithArrayKey;
|
2022-06-07 09:18:30 +00:00
|
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
|
|
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
2024-04-01 16:51:34 +00:00
|
|
|
use RectorPrefix202404\Webmozart\Assert\Assert;
|
2019-09-03 09:11:45 +00:00
|
|
|
/**
|
2021-03-12 22:20:25 +00:00
|
|
|
* @see \Rector\Tests\Renaming\Rector\MethodCall\RenameMethodRector\RenameMethodRectorTest
|
2019-09-03 09:11:45 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
final class RenameMethodRector extends AbstractScopeAwareRector implements ConfigurableRectorInterface
|
2017-10-06 11:57:21 +00:00
|
|
|
{
|
2021-02-15 16:21:29 +00:00
|
|
|
/**
|
2023-06-11 23:01:39 +00:00
|
|
|
* @readonly
|
2024-01-02 02:40:38 +00:00
|
|
|
* @var \Rector\NodeManipulator\ClassManipulator
|
2021-02-15 16:21:29 +00:00
|
|
|
*/
|
|
|
|
private $classManipulator;
|
2022-05-11 00:39:05 +00:00
|
|
|
/**
|
2023-06-11 23:01:39 +00:00
|
|
|
* @readonly
|
2024-01-02 02:40:38 +00:00
|
|
|
* @var \Rector\Reflection\ReflectionResolver
|
2022-05-11 00:39:05 +00:00
|
|
|
*/
|
|
|
|
private $reflectionResolver;
|
|
|
|
/**
|
2023-06-11 23:01:39 +00:00
|
|
|
* @readonly
|
2022-05-11 00:39:05 +00:00
|
|
|
* @var \PHPStan\Reflection\ReflectionProvider
|
|
|
|
*/
|
|
|
|
private $reflectionProvider;
|
2023-06-08 22:00:17 +00:00
|
|
|
/**
|
|
|
|
* @var MethodCallRenameInterface[]
|
|
|
|
*/
|
|
|
|
private $methodCallRenames = [];
|
2022-12-23 17:10:25 +00:00
|
|
|
public function __construct(ClassManipulator $classManipulator, ReflectionResolver $reflectionResolver, ReflectionProvider $reflectionProvider)
|
2021-05-09 20:15:43 +00:00
|
|
|
{
|
2021-02-15 16:21:29 +00:00
|
|
|
$this->classManipulator = $classManipulator;
|
2022-05-11 00:39:05 +00:00
|
|
|
$this->reflectionResolver = $reflectionResolver;
|
|
|
|
$this->reflectionProvider = $reflectionProvider;
|
2021-02-15 16:21:29 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
public function getRuleDefinition() : RuleDefinition
|
2018-04-08 11:51:26 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +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();
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +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();
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2022-06-07 08:22:29 +00:00
|
|
|
, [new MethodCallRename('SomeExampleClass', 'oldMethod', 'newMethod')])]);
|
2018-04-08 11:51:26 +00:00
|
|
|
}
|
2018-08-14 22:12:41 +00:00
|
|
|
/**
|
2021-02-27 00:06:15 +00:00
|
|
|
* @return array<class-string<Node>>
|
2018-08-14 22:12:41 +00:00
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
public function getNodeTypes() : array
|
2017-10-06 11:57:21 +00:00
|
|
|
{
|
2024-01-07 16:56:38 +00:00
|
|
|
return [MethodCall::class, NullsafeMethodCall::class, StaticCall::class, Class_::class, Interface_::class];
|
2017-10-06 11:57:21 +00:00
|
|
|
}
|
|
|
|
/**
|
2024-01-07 16:56:38 +00:00
|
|
|
* @param MethodCall|NullsafeMethodCall|StaticCall|Class_|Interface_ $node
|
2017-10-06 11:57:21 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
public function refactorWithScope(Node $node, Scope $scope) : ?Node
|
2017-10-06 11:57:21 +00:00
|
|
|
{
|
2023-06-10 09:11:14 +00:00
|
|
|
if ($node instanceof Class_ || $node instanceof Interface_) {
|
|
|
|
return $this->refactorClass($node, $scope);
|
2018-08-14 22:12:41 +00:00
|
|
|
}
|
2023-06-10 09:11:14 +00:00
|
|
|
return $this->refactorMethodCallAndStaticCall($node);
|
2018-08-14 22:12:41 +00:00
|
|
|
}
|
2021-02-28 07:47:48 +00:00
|
|
|
/**
|
2021-11-28 17:01:20 +00:00
|
|
|
* @param mixed[] $configuration
|
2021-02-28 07:47:48 +00:00
|
|
|
*/
|
2021-12-10 10:22:23 +00:00
|
|
|
public function configure(array $configuration) : void
|
2020-07-29 13:55:33 +00:00
|
|
|
{
|
2022-11-19 11:50:17 +00:00
|
|
|
Assert::allIsAOf($configuration, MethodCallRenameInterface::class);
|
2022-02-18 01:46:17 +00:00
|
|
|
$this->methodCallRenames = $configuration;
|
2020-07-29 13:55:33 +00:00
|
|
|
}
|
2020-08-05 20:45:36 +00:00
|
|
|
/**
|
2024-01-07 16:56:38 +00:00
|
|
|
* @param \PhpParser\Node\Expr\MethodCall|\PhpParser\Node\Expr\NullsafeMethodCall|\PhpParser\Node\Expr\StaticCall $call
|
2020-08-05 20:45:36 +00:00
|
|
|
*/
|
2023-06-10 09:11:14 +00:00
|
|
|
private function shouldSkipClassMethod($call, MethodCallRenameInterface $methodCallRename) : bool
|
2020-08-05 20:45:36 +00:00
|
|
|
{
|
2023-06-10 09:11:14 +00:00
|
|
|
$classReflection = $this->reflectionResolver->resolveClassReflectionSourceObject($call);
|
|
|
|
if (!$classReflection instanceof ClassReflection) {
|
|
|
|
return \false;
|
2020-08-05 20:45:36 +00:00
|
|
|
}
|
2023-06-10 09:11:14 +00:00
|
|
|
$targetClass = $methodCallRename->getClass();
|
|
|
|
if (!$this->reflectionProvider->hasClass($targetClass)) {
|
2021-05-09 20:15:43 +00:00
|
|
|
return \false;
|
2020-07-13 21:55:06 +00:00
|
|
|
}
|
2023-06-10 09:11:14 +00:00
|
|
|
$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());
|
2020-07-13 21:55:06 +00:00
|
|
|
}
|
2022-05-25 21:43:02 +00:00
|
|
|
/**
|
2023-09-09 18:21:23 +00:00
|
|
|
* @param \PhpParser\Node\Stmt\Class_|\PhpParser\Node\Stmt\Interface_ $classOrInterface
|
2022-05-25 21:43:02 +00:00
|
|
|
*/
|
2023-09-09 18:21:23 +00:00
|
|
|
private function hasClassNewClassMethod($classOrInterface, MethodCallRenameInterface $methodCallRename) : bool
|
2023-06-10 09:11:14 +00:00
|
|
|
{
|
2023-09-09 18:21:23 +00:00
|
|
|
return (bool) $classOrInterface->getMethod($methodCallRename->getNewMethod());
|
2023-06-10 09:11:14 +00:00
|
|
|
}
|
|
|
|
private function shouldKeepForParentInterface(MethodCallRenameInterface $methodCallRename, ?ClassReflection $classReflection) : bool
|
2022-05-25 21:43:02 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$classReflection instanceof ClassReflection) {
|
2022-05-25 21:43:02 +00:00
|
|
|
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());
|
|
|
|
}
|
2023-06-10 09:11:14 +00:00
|
|
|
/**
|
|
|
|
* @param \PhpParser\Node\Stmt\Class_|\PhpParser\Node\Stmt\Interface_ $classOrInterface
|
|
|
|
* @return \PhpParser\Node\Stmt\Class_|\PhpParser\Node\Stmt\Interface_|null
|
|
|
|
*/
|
|
|
|
private function refactorClass($classOrInterface, Scope $scope)
|
|
|
|
{
|
|
|
|
if (!$scope->isInClass()) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
$classReflection = $scope->getClassReflection();
|
|
|
|
$hasChanged = \false;
|
|
|
|
foreach ($classOrInterface->getMethods() as $classMethod) {
|
2023-09-09 18:18:18 +00:00
|
|
|
$methodName = $this->getName($classMethod->name);
|
|
|
|
if ($methodName === null) {
|
|
|
|
continue;
|
|
|
|
}
|
2023-06-10 09:11:14 +00:00
|
|
|
foreach ($this->methodCallRenames as $methodCallRename) {
|
2023-09-09 18:18:18 +00:00
|
|
|
if ($this->shouldSkipRename($methodName, $classMethod, $methodCallRename, $classReflection, $classOrInterface)) {
|
2023-06-10 09:11:14 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$classMethod->name = new Identifier($methodCallRename->getNewMethod());
|
|
|
|
$hasChanged = \true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($hasChanged) {
|
|
|
|
return $classOrInterface;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2023-09-09 18:18:18 +00:00
|
|
|
/**
|
|
|
|
* @param \PhpParser\Node\Stmt\Class_|\PhpParser\Node\Stmt\Interface_ $classOrInterface
|
|
|
|
*/
|
2023-09-09 22:49:40 +00:00
|
|
|
private function shouldSkipRename(string $methodName, ClassMethod $classMethod, MethodCallRenameInterface $methodCallRename, ClassReflection $classReflection, $classOrInterface) : bool
|
2023-09-09 18:18:18 +00:00
|
|
|
{
|
|
|
|
if (!$this->nodeNameResolver->isStringName($methodName, $methodCallRename->getOldMethod())) {
|
|
|
|
return \true;
|
|
|
|
}
|
|
|
|
if (!$this->nodeTypeResolver->isMethodStaticCallOrClassMethodObjectType($classMethod, $methodCallRename->getObjectType())) {
|
|
|
|
return \true;
|
|
|
|
}
|
|
|
|
if ($this->shouldKeepForParentInterface($methodCallRename, $classReflection)) {
|
|
|
|
return \true;
|
|
|
|
}
|
2023-09-09 22:49:40 +00:00
|
|
|
return $this->hasClassNewClassMethod($classOrInterface, $methodCallRename);
|
2023-09-09 18:18:18 +00:00
|
|
|
}
|
2023-06-10 09:11:14 +00:00
|
|
|
/**
|
2024-01-07 16:56:38 +00:00
|
|
|
* @param \PhpParser\Node\Expr\StaticCall|\PhpParser\Node\Expr\MethodCall|\PhpParser\Node\Expr\NullsafeMethodCall $call
|
|
|
|
* @return \PhpParser\Node\Expr\ArrayDimFetch|null|\PhpParser\Node\Expr\MethodCall|\PhpParser\Node\Expr\StaticCall|\PhpParser\Node\Expr\NullsafeMethodCall
|
2023-06-10 09:11:14 +00:00
|
|
|
*/
|
|
|
|
private function refactorMethodCallAndStaticCall($call)
|
|
|
|
{
|
2023-09-09 18:18:18 +00:00
|
|
|
$callName = $this->getName($call->name);
|
|
|
|
if ($callName === null) {
|
|
|
|
return null;
|
|
|
|
}
|
2023-06-10 09:11:14 +00:00
|
|
|
foreach ($this->methodCallRenames as $methodCallRename) {
|
2023-09-09 18:18:18 +00:00
|
|
|
if (!$this->nodeNameResolver->isStringName($callName, $methodCallRename->getOldMethod())) {
|
2023-06-10 09:11:14 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!$this->nodeTypeResolver->isMethodStaticCallOrClassMethodObjectType($call, $methodCallRename->getObjectType())) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ($this->shouldSkipClassMethod($call, $methodCallRename)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$call->name = new Identifier($methodCallRename->getNewMethod());
|
|
|
|
if ($methodCallRename instanceof MethodCallRenameWithArrayKey) {
|
|
|
|
return new ArrayDimFetch($call, BuilderHelpers::normalizeValue($methodCallRename->getArrayKey()));
|
|
|
|
}
|
|
|
|
return $call;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2017-10-06 11:57:21 +00:00
|
|
|
}
|