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

fix: Skip static calls which are made using the class name #6024

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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,18 @@ final class SkipPrivateStaticCallFromStatic
return static::reallyClear($input);
}

public static function foo(string $input)
{
return self::reallyFoo($input);
}

private static function reallyClear($input)
{
return $input . ' - clean';
}

private static function reallyFoo($input)
{
return $input . ' - foo';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\CodeQuality\Rector\ClassMethod\LocallyCalledStaticMethodToNonStaticRector\Fixture;

final class SkipPrivateStaticCallUsingClassNameFromStatic
{
public static function bar(string $input)
{
return SkipPrivateStaticCallUsingClassNameFromStatic::reallyBar($input);
}

private static function reallyBar($input)
{
return $input . ' - bar';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ private function refactorClassMethod(Class_ $class, ClassMethod $classMethod): ?
*/
private function isClassMethodCalledInAnotherStaticClassMethod(Class_ $class, ClassMethod $classMethod): bool
{
$currentClassNamespacedName = (string) $this->getName($class);
$currentClassMethodName = $this->getName($classMethod);

$isInsideStaticClassMethod = false;
Expand All @@ -163,14 +164,15 @@ private function isClassMethodCalledInAnotherStaticClassMethod(Class_ $class, Cl
}

$this->traverseNodesWithCallable($checkedClassMethod, function (Node $node) use (
$currentClassNamespacedName,
$currentClassMethodName,
&$isInsideStaticClassMethod
): ?int {
if (! $node instanceof StaticCall) {
return null;
}

if (! $this->isNames($node->class, ['self', 'static'])) {
if (! $this->isNames($node->class, ['self', 'static', $currentClassNamespacedName])) {
return null;
}

Expand Down