Skip to content

Anonymous function with return type void returns void but should not return anything #3348

@thiagocordeiro

Description

@thiagocordeiro

Bug report

Arrow functions brings a few advantages beside implicit returns, one of them is to share scope so there is no need to use ($a, $b).

Code snippet that reproduces the problem

https://phpstan.org/r/44c31a2f-c40e-4b88-a295-08c593d44c62

class SideApiThingNotImportantToTheMainLogic
{
    private HttpClient $client;

    public function makeAmericaGreatAgain(): void
    {
        $this->safeCall(
            fn() => $this->client->call('make-america-great-again'),
        );
    }

    public function yesWeCan(): void
    {
        $this->safeCall(
            fn() => $this->client->call('yes-we-can'),
        );
    }

    private function safeCall(callable $fn): void
    {
        try {
            $fn();
        } catch (Exception $e) {
            // do nothing because we do not want to interrupt our main logic thing
        }
    }
}

Expected output

function with return type void can return void

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions