Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Faster name resolving #4955

Merged
merged 4 commits into from
Sep 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/NodeNameResolver/NodeNameResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ public function isStringName(string $resolvedName, string $desiredName): bool
return $desiredName === $resolvedName;
}

return strtolower($resolvedName) === strtolower($desiredName);
return strcasecmp($resolvedName, $desiredName) === 0;
}

public function matchesStringName(string|Identifier $resolvedName, string $desiredNamePattern): bool
Expand Down
7 changes: 6 additions & 1 deletion rules/Arguments/Rector/ClassMethod/ArgumentAdderRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -318,8 +318,13 @@ private function processStaticCall(StaticCall $staticCall, int $position, Argume

private function refactorCall(StaticCall|MethodCall $call): void
{
$callName = $this->getName($call->name);
if ($callName === null) {
return;
}

foreach ($this->addedArguments as $addedArgument) {
if (! $this->isName($call->name, $addedArgument->getMethod())) {
if (! $this->nodeNameResolver->isStringName($callName, $addedArgument->getMethod())) {
continue;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,13 @@ public function refactor(Node $node): MethodCall | StaticCall | ClassMethod | Ne
return $this->refactorNew($node);
}

$nodeName = $this->getName($node->name);
if ($nodeName === null) {
return null;
}

foreach ($this->replaceArgumentDefaultValues as $replaceArgumentDefaultValue) {
if (! $this->isName($node->name, $replaceArgumentDefaultValue->getMethod())) {
if (! $this->nodeNameResolver->isStringName($nodeName, $replaceArgumentDefaultValue->getMethod())) {
continue;
}

Expand Down
59 changes: 42 additions & 17 deletions rules/Renaming/Rector/MethodCall/RenameMethodRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,23 +157,13 @@ private function refactorClass(Class_|Interface_ $classOrInterface, Scope $scope
$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;
}
$methodName = $this->getName($classMethod->name);
if ($methodName === null) {
continue;
}

if ($this->hasClassNewClassMethod($classOrInterface, $methodCallRename)) {
foreach ($this->methodCallRenames as $methodCallRename) {
if ($this->shouldSkipRename($methodName, $classMethod, $methodCallRename, $classReflection, $classOrInterface)) {
continue;
}

Expand All @@ -189,11 +179,46 @@ private function refactorClass(Class_|Interface_ $classOrInterface, Scope $scope
return null;
}

private function shouldSkipRename(
string $methodName,
Node\Stmt\ClassMethod $classMethod,
MethodCallRenameInterface $methodCallRename,
ClassReflection $classReflection,
Class_|Interface_ $classOrInterface
): bool
{
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;
}

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

return false;
}

private function refactorMethodCallAndStaticCall(
StaticCall|MethodCall $call
): ArrayDimFetch|null|MethodCall|StaticCall {
$callName = $this->getName($call->name);
if ($callName === null) {
return null;
}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,9 @@ public function refactor(Node $node): ?Node
continue;
}

$methodName = $this->getName($classMethod);
foreach ($this->addParamTypeDeclarations as $addParamTypeDeclaration) {
if (! $this->isName($classMethod, $addParamTypeDeclaration->getMethodName())) {
if (!$this->nodeNameResolver->isStringName($methodName, $addParamTypeDeclaration->getMethodName())) {
continue;
}

Expand Down