Skip to content

Commit

Permalink
Remove parent node from RenameMethodRector (#4154)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Jun 10, 2023
1 parent 21a233d commit f8c6c5c
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 73 deletions.
3 changes: 0 additions & 3 deletions packages/Skipper/FileSystem/FnMatchPathNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@

namespace Rector\Skipper\FileSystem;

use Nette\Utils\Strings;
use Rector\Skipper\Enum\AsteriskMatch;

/**
* @see \Rector\Tests\Skipper\FileSystem\FnMatchPathNormalizerTest
*/
Expand Down
170 changes: 101 additions & 69 deletions rules/Renaming/Rector/MethodCall/RenameMethodRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
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 PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\Interface_;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\ReflectionProvider;
Expand Down Expand Up @@ -67,46 +67,19 @@ public function getRuleDefinition(): RuleDefinition
*/
public function getNodeTypes(): array
{
return [MethodCall::class, StaticCall::class, ClassMethod::class];
return [MethodCall::class, StaticCall::class, Class_::class, Interface_::class];
}

/**
* @param MethodCall|StaticCall|ClassMethod $node
* @param MethodCall|StaticCall|Class_|Interface_ $node
*/
public function refactorWithScope(Node $node, Scope $scope): ?Node
{
$classReflection = $scope->getClassReflection();

foreach ($this->methodCallRenames as $methodCallRename) {
if (! $this->isName($node->name, $methodCallRename->getOldMethod())) {
continue;
}

if ($this->shouldKeepForParentInterface($methodCallRename, $node, $classReflection)) {
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;
if ($node instanceof Class_ || $node instanceof Interface_) {
return $this->refactorClass($node, $scope);
}

return null;
return $this->refactorMethodCallAndStaticCall($node);
}

/**
Expand All @@ -120,58 +93,43 @@ public function configure(array $configuration): void
}

private function shouldSkipClassMethod(
MethodCall | StaticCall | ClassMethod $node,
MethodCall | StaticCall $call,
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;
}
$classReflection = $this->reflectionResolver->resolveClassReflectionSourceObject($call);
if (! $classReflection instanceof ClassReflection) {
return false;
}

$targetClassReflection = $this->reflectionProvider->getClass($targetClass);
if ($classReflection->getName() === $targetClassReflection->getName()) {
return false;
}
$targetClass = $methodCallRename->getClass();
if (! $this->reflectionProvider->hasClass($targetClass)) {
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;
}
$targetClassReflection = $this->reflectionProvider->getClass($targetClass);
if ($classReflection->getName() === $targetClassReflection->getName()) {
return false;
}

return $classReflection->hasMethod($methodCallRename->getNewMethod());
// different with configured ClassLike source? it is a child, which may has old and new exists
if (! $classReflection->hasMethod($methodCallRename->getOldMethod())) {
return false;
}

return $this->shouldSkipForAlreadyExistingClassMethod($node, $methodCallRename);
return $classReflection->hasMethod($methodCallRename->getNewMethod());
}

private function shouldSkipForAlreadyExistingClassMethod(
ClassMethod $classMethod,
private function hasClassNewClassMethod(
Class_|Interface_ $classOrInternace,
MethodCallRenameInterface $methodCallRename
): bool {
$classLike = $this->betterNodeFinder->findParentType($classMethod, ClassLike::class);
if (! $classLike instanceof ClassLike) {
return false;
}

return (bool) $classLike->getMethod($methodCallRename->getNewMethod());
return (bool) $classOrInternace->getMethod($methodCallRename->getNewMethod());
}

private function shouldKeepForParentInterface(
MethodCallRenameInterface $methodCallRename,
ClassMethod|StaticCall|MethodCall $node,
?ClassReflection $classReflection
): bool {
if (! $node instanceof ClassMethod) {
return false;
}

if (! $classReflection instanceof ClassReflection) {
return false;
}
Expand All @@ -187,4 +145,78 @@ private function shouldKeepForParentInterface(
$methodCallRename->getNewMethod()
);
}

private function refactorClass(Class_|Interface_ $classOrInterface, Scope $scope): Class_|Interface_|null
{
if (! $scope->isInClass()) {
return null;
}

$classReflection = $scope->getClassReflection();

$hasChanged = false;

foreach ($classOrInterface->getMethods() as $classMethod) {
foreach ($this->methodCallRenames as $methodCallRename) {
if (! $this->isName($classMethod->name, $methodCallRename->getOldMethod())) {
continue;
}

if (! $this->nodeTypeResolver->isMethodStaticCallOrClassMethodObjectType(
$classMethod,
$methodCallRename->getObjectType()
)) {
continue;
}

if ($this->shouldKeepForParentInterface($methodCallRename, $classReflection)) {
continue;
}

if ($this->hasClassNewClassMethod($classOrInterface, $methodCallRename)) {
continue;
}

$classMethod->name = new Identifier($methodCallRename->getNewMethod());
$hasChanged = true;
}
}

if ($hasChanged) {
return $classOrInterface;
}

return null;
}

private function refactorMethodCallAndStaticCall(
StaticCall|MethodCall $call
): ArrayDimFetch|null|MethodCall|StaticCall {
foreach ($this->methodCallRenames as $methodCallRename) {
if (! $this->isName($call->name, $methodCallRename->getOldMethod())) {
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;
}
}
1 change: 0 additions & 1 deletion src/FileSystem/FilesFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use Rector\Caching\UnchangedFilesFilter;
use Rector\Core\Util\StringUtils;
use Rector\Skipper\Enum\AsteriskMatch;
use Rector\Skipper\SkipCriteriaResolver\SkippedPathsResolver;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Finder\SplFileInfo;
Expand Down

0 comments on commit f8c6c5c

Please sign in to comment.