Skip to content

[CodingStyle] Keep string callable global when shadowed in FunctionFirstClassCallableRector - #9057

Merged
TomasVotruba merged 1 commit into
mainfrom
fix-first-class-callable-global-function
Nov 24, 2026
Merged

TomasVotruba merged 1 commit into
mainfrom
fix-first-class-callable-global-function

Conversation

@TomasVotruba

@TomasVotruba TomasVotruba commented Nov 24, 2026 •

Copy link
Copy Markdown
Member

A string callable like 'trim' always resolves to the global function. A bare trim(...) resolves against the current namespace and use function imports first, so when a namespaced trim() exists or a use function Foo\trim; import is in the file, the rule switched the call to a different function.

namespace App;

function trim(string $value): string { return 'shadowed'; }

array_map('trim', $data);

Before: array_map(trim(...), $data) - calls App\trim().
After: array_map(\trim(...), $data) - still calls the global trim().

The fix emits the fully qualified name only when the bare name would resolve elsewhere - a <Namespace>\<name> function exists, or the file has a use function import (or alias) to that name. Otherwise the output stays bare as before. Added namespaced_function_shadow and use_function_import fixtures.

@TomasVotruba
TomasVotruba force-pushed the fix-first-class-callable-global-function branch from 832bec2 to 6c0ae79 Compare November 24, 2026 17:00
@TomasVotruba TomasVotruba changed the title [CodingStyle] Keep string callable global in FunctionFirstClassCallableRector [CodingStyle] Keep string callable global when shadowed in FunctionFirstClassCallableRector Nov 24, 2026
@TomasVotruba
TomasVotruba enabled auto-merge (squash) November 24, 2026 17:01
@TomasVotruba
TomasVotruba merged commit c7ad809 into main Nov 24, 2026
51 checks passed
@TomasVotruba
TomasVotruba deleted the fix-first-class-callable-global-function branch November 24, 2026 17:01
{
$namespaceName = ScopeFetcher::fetch($funcCall)->getNamespace();
if ($namespaceName !== null && $this->reflectionProvider->hasFunction(
new FullyQualified($namespaceName . '\\' . $functionName),

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nodeNameResolver->getName() should already resolve the correct name

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you PR this?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will try

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants