Skip to content

Ternary operator inside arrow function #297

Closed
@ghost

Description

Consider the below MRE, which was OK for phpcs-variable-analysis v2.11.12, but receives a warning in v2.11.14 for the part of the code on the last line (between question mark and colon).

Warning: Variable $cls is undefined. (VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedVariable)

Note that PHP (8.0) has no issue with the code and it works as expected. As far as I can tell, it might be connected to PR #296.

class Test
{
    public function status(): bool
    {
        return true;
    }

    public static function hello(callable $callback): void
    {
        print('Hello');
    }
}

Test::hello(fn(Test $cls) => $cls->status() ? $cls : null);

This workaround can fix it for now.

class Test
{
    public function status(): bool
    {
        return true;
    }

    public static function hello(callable $callback): void
    {
        print('Hello');
    }
}

Test::hello(static function (Test $cls): ?Test {
    return $cls->status() ? $cls : null;
});

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions