Skip to content

[CodeQuality] Skip inner function referenced as string callable in InnerFunctionToPrivateMethodRector#8154

Merged
TomasVotruba merged 1 commit into
mainfrom
fix-inner-function-string-callable
Jul 8, 2026
Merged

[CodeQuality] Skip inner function referenced as string callable in InnerFunctionToPrivateMethodRector#8154
TomasVotruba merged 1 commit into
mainfrom
fix-inner-function-string-callable

Conversation

@TomasVotruba

Copy link
Copy Markdown
Member

Fixes rectorphp/rector#9801

InnerFunctionToPrivateMethodRector only rewrites FuncCall references (inner()) when converting an inner function to a private method. If the inner function is referenced as a string callable (e.g. usort($items, 'sortByOrder')), that reference is a String_ node and is left untouched — the function gets moved into a private method while the call site still points at a now non-existent global function. With RemoveUnusedPrivateMethodRector also enabled, the "unused" method is then deleted entirely.

This PR skips the transformation when the inner function name is referenced as a string within the method, keeping working code intact.

Before

class Demo
{
    public static function getOptions()
    {
        function sortByOrder(array $a, array $b): int
        {
            return strcmp((string) $a['label'], (string) $b['label']);
        }

        usort($options, 'sortByOrder');
        return $options;
    }
}

was rewritten to broken code — sortByOrder() moved to a private method, but usort($options, 'sortByOrder') still referencing the removed global function.

After

No change — the inner function is left as-is, because it is referenced by string callable and cannot be safely rewritten.

@TomasVotruba

Copy link
Copy Markdown
Member Author

LGTM 👍

@TomasVotruba TomasVotruba merged commit ca5155c into main Jul 8, 2026
65 checks passed
@TomasVotruba TomasVotruba deleted the fix-inner-function-string-callable branch July 8, 2026 11:03
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.

InnerFunctionToPrivateMethodRector produces broken code when the inner function is used as a string callable

1 participant