Skip to content

Incorrect behavior of FunctionLikeToFirstClassCallableRector #9461

@ajgarlag

Description

@ajgarlag

Bug Report

Subject Details
Rector version last dev-main
Installed as composer dependency

Minimal PHP Code Causing Issue

See https://getrector.com/demo/7d8588dd-1b3c-4f3b-8886-7608a24269ff

<?php
// Library code
final readonly class Renderer {
    public function __construct(private string $name, private object $object) {}
    public function render(callable $formatter): string
    {
        return $formatter($this->name, $this->object);
    }
}

// My code
$formatter = fn(string $label, object $object): string => ucfirst($label);
echo (new Renderer('name', new stdClass()))->render($formatter);

Responsible rules

  • FunctionLikeToFirstClassCallableRector

Expected Behavior

Rector should skip the change, because the code fails when the library code calls my callable with two arguments.

-$formatter = fn(string $label, object $object): string => ucfirst($label);
+$formatter = ucfirst(...);

See https://3v4l.org/10gCI

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions