diff --git a/packages/Skipper/FileSystem/FnMatchPathNormalizer.php b/packages/Skipper/FileSystem/FnMatchPathNormalizer.php index cbe69566653..88d78d2a314 100644 --- a/packages/Skipper/FileSystem/FnMatchPathNormalizer.php +++ b/packages/Skipper/FileSystem/FnMatchPathNormalizer.php @@ -4,9 +4,6 @@ namespace Rector\Skipper\FileSystem; -use Nette\Utils\Strings; -use Rector\Skipper\Enum\AsteriskMatch; - /** * @see \Rector\Tests\Skipper\FileSystem\FnMatchPathNormalizerTest */ diff --git a/rules/Renaming/Rector/MethodCall/RenameMethodRector.php b/rules/Renaming/Rector/MethodCall/RenameMethodRector.php index 586149e80cb..1a988b943d1 100644 --- a/rules/Renaming/Rector/MethodCall/RenameMethodRector.php +++ b/rules/Renaming/Rector/MethodCall/RenameMethodRector.php @@ -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; @@ -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); } /** @@ -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; } @@ -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; + } } diff --git a/src/FileSystem/FilesFinder.php b/src/FileSystem/FilesFinder.php index 65bfabe3df2..fe0cfac888a 100644 --- a/src/FileSystem/FilesFinder.php +++ b/src/FileSystem/FilesFinder.php @@ -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;