Skip to content

Commit

Permalink
Fix LocallyCalledStaticMethodToNonStaticRector when static function i…
Browse files Browse the repository at this point in the history
…s called using the class name
  • Loading branch information
carlos-granados committed Sep 16, 2024
1 parent bbdeb0f commit de99118
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ class Fixture
public function run()
{
self::someStatic();
static::someStatic();
Fixture::someStatic();
\Rector\Tests\CodeQuality\Rector\ClassMethod\LocallyCalledStaticMethodToNonStaticRector\Fixture\Fixture::someStatic();
}

private static function someStatic()
Expand All @@ -25,6 +28,9 @@ class Fixture
public function run()
{
$this->someStatic();
$this->someStatic();
$this->someStatic();
$this->someStatic();
}

private function someStatic()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

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

final class SkipInStaticClosureUsingClassName
{
public function run()
{
return static function () {
return SkipInStaticClosureUsingClassName::method("string");
};
}

private static function method(?string $value)
{
return $value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -126,22 +126,25 @@ private function refactorClassMethod(Class_ $class, ClassMethod $classMethod): ?

// replace all the calls
$classMethodName = $this->getName($classMethod);
$className = $this->getName($class) ?? '';
$shouldSkip = false;

$this->traverseNodesWithCallable($class->getMethods(), function (Node $node) use (
&$shouldSkip,
$classMethodName
$classMethodName,
$className
): int|null {
if (($node instanceof Closure || $node instanceof ArrowFunction) && $node->static) {
$this->traverseNodesWithCallable($node->getStmts(), function (Node $subNode) use (
&$shouldSkip,
$classMethodName
$classMethodName,
$className
): ?int {
if (! $subNode instanceof StaticCall) {
return null;
}

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

Expand All @@ -168,13 +171,14 @@ private function refactorClassMethod(Class_ $class, ClassMethod $classMethod): ?
}

$this->traverseNodesWithCallable($class->getMethods(), function (Node $node) use (
$classMethodName
$classMethodName,
$className
): ?MethodCall {
if (! $node instanceof StaticCall) {
return null;
}

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

Expand Down

0 comments on commit de99118

Please sign in to comment.