Skip to content

Commit

Permalink
[CodeQuality] Skip used by array callable on LocallyCalledStaticMetho…
Browse files Browse the repository at this point in the history
…dToNonStaticRector (#6473)

* [CodeQuality[ Skip used by array callable on LocallyCalledStaticMethodToNonStaticRector

* fix
  • Loading branch information
samsonasik authored Nov 22, 2024
1 parent c322032 commit c6ca719
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

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

class SkipUsedByArrayCallable
{
private static function bar(string $a, string $b): int
{
return $a <=> $b;
}

public static function foo(): void
{
$array = [];
usort($array, [self::class, 'bar']);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Rector\CodeQuality\Rector\ClassMethod;

use PhpParser\Node;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\ArrowFunction;
use PhpParser\Node\Expr\Closure;
use PhpParser\Node\Expr\MethodCall;
Expand All @@ -13,7 +14,10 @@
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\NodeVisitor;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\ClassReflection;
use Rector\NodeCollector\NodeAnalyzer\ArrayCallableMethodMatcher;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\Privatization\NodeManipulator\VisibilityManipulator;
use Rector\Privatization\VisibilityGuard\ClassMethodVisibilityGuard;
use Rector\Rector\AbstractRector;
Expand All @@ -29,7 +33,8 @@ final class LocallyCalledStaticMethodToNonStaticRector extends AbstractRector
public function __construct(
private readonly ClassMethodVisibilityGuard $classMethodVisibilityGuard,
private readonly VisibilityManipulator $visibilityManipulator,
private readonly ReflectionResolver $reflectionResolver
private readonly ReflectionResolver $reflectionResolver,
private readonly ArrayCallableMethodMatcher $arrayCallableMethodMatcher
) {
}

Expand Down Expand Up @@ -217,6 +222,18 @@ private function isClassMethodCalledInAnotherStaticClassMethod(Class_ $class, Cl
$currentClassMethodName,
&$isInsideStaticClassMethod
): ?int {
if ($node instanceof Array_) {
$scope = $node->getAttribute(AttributeKey::SCOPE);
if ($scope instanceof Scope && $this->arrayCallableMethodMatcher->match(
$node,
$scope,
$currentClassMethodName
)) {
$isInsideStaticClassMethod = true;
return NodeVisitor::STOP_TRAVERSAL;
}
}

if (! $node instanceof StaticCall) {
return null;
}
Expand Down

0 comments on commit c6ca719

Please sign in to comment.