From 6c0ae792c58ed81dce820d5208803afa55f02126 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Tue, 24 Nov 2026 17:53:04 +0100 Subject: [PATCH] [CodingStyle] Keep string callable global when shadowed in FunctionFirstClassCallableRector --- .../namespaced_function_shadow.php.inc | 37 ++++++++++++++++ .../Fixture/use_function_import.php.inc | 31 +++++++++++++ .../FunctionFirstClassCallableRector.php | 44 ++++++++++++++++++- 3 files changed, 111 insertions(+), 1 deletion(-) create mode 100644 rules-tests/CodingStyle/Rector/FuncCall/FunctionFirstClassCallableRector/Fixture/namespaced_function_shadow.php.inc create mode 100644 rules-tests/CodingStyle/Rector/FuncCall/FunctionFirstClassCallableRector/Fixture/use_function_import.php.inc diff --git a/rules-tests/CodingStyle/Rector/FuncCall/FunctionFirstClassCallableRector/Fixture/namespaced_function_shadow.php.inc b/rules-tests/CodingStyle/Rector/FuncCall/FunctionFirstClassCallableRector/Fixture/namespaced_function_shadow.php.inc new file mode 100644 index 00000000000..49e845ea670 --- /dev/null +++ b/rules-tests/CodingStyle/Rector/FuncCall/FunctionFirstClassCallableRector/Fixture/namespaced_function_shadow.php.inc @@ -0,0 +1,37 @@ + +----- + diff --git a/rules-tests/CodingStyle/Rector/FuncCall/FunctionFirstClassCallableRector/Fixture/use_function_import.php.inc b/rules-tests/CodingStyle/Rector/FuncCall/FunctionFirstClassCallableRector/Fixture/use_function_import.php.inc new file mode 100644 index 00000000000..702571f12fc --- /dev/null +++ b/rules-tests/CodingStyle/Rector/FuncCall/FunctionFirstClassCallableRector/Fixture/use_function_import.php.inc @@ -0,0 +1,31 @@ + +----- + diff --git a/rules/CodingStyle/Rector/FuncCall/FunctionFirstClassCallableRector.php b/rules/CodingStyle/Rector/FuncCall/FunctionFirstClassCallableRector.php index d4f0d72b363..c8083e3315a 100644 --- a/rules/CodingStyle/Rector/FuncCall/FunctionFirstClassCallableRector.php +++ b/rules/CodingStyle/Rector/FuncCall/FunctionFirstClassCallableRector.php @@ -10,7 +10,11 @@ use PhpParser\Node\Name; use PhpParser\Node\Name\FullyQualified; use PhpParser\Node\Scalar\String_; +use PhpParser\Node\Stmt\Use_; use PhpParser\Node\VariadicPlaceholder; +use PHPStan\Reflection\ReflectionProvider; +use Rector\Naming\Naming\UseImportsResolver; +use Rector\PHPStan\ScopeFetcher; use Rector\Rector\AbstractRector; use Rector\RuleDoc\CodeSample\CodeSample; use Rector\RuleDoc\RuleDefinition; @@ -25,6 +29,12 @@ */ final class FunctionFirstClassCallableRector extends AbstractRector implements MinPhpVersionInterface { + public function __construct( + private readonly ReflectionProvider $reflectionProvider, + private readonly UseImportsResolver $useImportsResolver + ) { + } + public function getRuleDefinition(): RuleDefinition { // see RFC https://wiki.php.net/rfc/first_class_callable_syntax @@ -106,7 +116,10 @@ public function refactor(Node $node): ?FuncCall $node->args[$key] = new Arg( new FuncCall( - $isFullyQualified || str_contains($callableName, '\\') + $isFullyQualified || str_contains($callableName, '\\') || $this->isShadowedFunction( + $node, + $callableName + ) ? new FullyQualified($callableName) : new Name($callableName), [new VariadicPlaceholder()] @@ -119,6 +132,35 @@ public function refactor(Node $node): ?FuncCall return $hasChanged ? $node : null; } + /** + * A string callable always resolves to the global function, + * while a bare name resolves to a namespaced or imported function first + */ + private function isShadowedFunction(FuncCall $funcCall, string $functionName): bool + { + $namespaceName = ScopeFetcher::fetch($funcCall)->getNamespace(); + if ($namespaceName !== null && $this->reflectionProvider->hasFunction( + new FullyQualified($namespaceName . '\\' . $functionName), + null + )) { + return true; + } + + foreach ($this->useImportsResolver->resolve() as $use) { + foreach ($use->uses as $useItem) { + if ($use->type !== Use_::TYPE_FUNCTION && $useItem->type !== Use_::TYPE_FUNCTION) { + continue; + } + + if ($useItem->getAlias()->toLowerString() === strtolower($functionName)) { + return true; + } + } + } + + return false; + } + public function provideMinPhpVersion(): int { return PhpVersion::PHP_81;