Skip to content
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
@@ -0,0 +1,17 @@
<?php

namespace Rector\Symfony\Tests\Symfony61\Rector\Class_\MagicClosureTwigExtensionToNativeMethodsRector\Fixture;

use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;
use Rector\Symfony\Tests\Symfony61\Rector\Class_\MagicClosureTwigExtensionToNativeMethodsRector\Source\ExternalResolverService;

final class SkipExternalNonStaticMethod extends AbstractExtension
{
public function getFunctions(): array
{
return [
new TwigFunction('resolve', [ExternalResolverService::class, 'resolve']),
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace Rector\Symfony\Tests\Symfony61\Rector\Class_\MagicClosureTwigExtensionToNativeMethodsRector\Source;

final class ExternalResolverService
{
public function resolve(mixed $value): mixed
{
return $value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@

use PhpParser\Node;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\ClassConstFetch;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\VariadicPlaceholder;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Type\ObjectType;
use Rector\NodeCollector\NodeAnalyzer\ArrayCallableMethodMatcher;
use Rector\NodeCollector\ValueObject\ArrayCallable;
Expand All @@ -31,6 +33,7 @@ final class MagicClosureTwigExtensionToNativeMethodsRector extends AbstractRecto
{
public function __construct(
private readonly ArrayCallableMethodMatcher $arrayCallableMethodMatcher,
private readonly ReflectionProvider $reflectionProvider,
) {
}

Expand Down Expand Up @@ -147,6 +150,10 @@ private function refactorClassMethod(ClassMethod $classMethod, Scope $scope): bo
return null;
}

if ($this->isNonStaticExternalClassCallable($arrayCallable, $scope)) {
return null;
}

$hasChanged = true;

return new MethodCall($arrayCallable->getCallerExpr(), $arrayCallable->getMethod(), [
Expand All @@ -156,4 +163,25 @@ private function refactorClassMethod(ClassMethod $classMethod, Scope $scope): bo

return $hasChanged;
}

private function isNonStaticExternalClassCallable(ArrayCallable $arrayCallable, Scope $scope): bool
{
if (! $arrayCallable->getCallerExpr() instanceof ClassConstFetch) {
return false;
}

$className = $arrayCallable->getClass();
if (! $this->reflectionProvider->hasClass($className)) {
return false;
}

$classReflection = $this->reflectionProvider->getClass($className);
$methodName = $arrayCallable->getMethod();

if (! $classReflection->hasMethod($methodName)) {
return false;
}

return ! $classReflection->getMethod($methodName, $scope)->isStatic();
}
}
Loading